Skip to content

Commit

Permalink
move chunker type parsing into its own file in chunk
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jeromy <[email protected]>
  • Loading branch information
whyrusleeping committed Aug 8, 2015
1 parent 3f33df1 commit 52aacb6
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 51 deletions.
51 changes: 1 addition & 50 deletions core/commands/add.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package commands

import (
"errors"
"fmt"
"io"
"path"
"strconv"
"strings"

"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/cheggaaa/pb"

Expand Down Expand Up @@ -275,55 +272,9 @@ type adder struct {
chunker string
}

func getChunker(r io.Reader, chunker string) (chunk.Splitter, error) {
switch {
case chunker == "":
return chunk.NewSizeSplitter(r, chunk.DefaultBlockSize), nil
case strings.HasPrefix(chunker, "size-"):
sizeStr := strings.Split(chunker, "-")[1]
size, err := strconv.Atoi(sizeStr)
if err != nil {
return nil, err
}

return chunk.NewSizeSplitter(r, int64(size)), nil
case strings.HasPrefix(chunker, "rabin"):
parts := strings.Split(chunker, "-")
switch len(parts) {
case 1:
return chunk.NewRabin(r, uint64(chunk.DefaultBlockSize)), nil
case 2:
size, err := strconv.Atoi(parts[1])
if err != nil {
return nil, err
}
return chunk.NewRabin(r, uint64(size)), nil
case 4:
min, err := strconv.Atoi(parts[1])
if err != nil {
return nil, err
}
avg, err := strconv.Atoi(parts[2])
if err != nil {
return nil, err
}
max, err := strconv.Atoi(parts[3])
if err != nil {
return nil, err
}

return chunk.NewRabinMinMax(r, uint64(min), uint64(avg), uint64(max)), nil
default:
return nil, errors.New("incorrect format (expected 'rabin' 'rabin-[avg]' or 'rabin-[min]-[avg]-[max]'")
}
default:
return nil, fmt.Errorf("unrecognized chunker option: %s", chunker)
}
}

// Perform the actual add & pin locally, outputting results to reader
func add(n *core.IpfsNode, reader io.Reader, useTrickle bool, chunker string) (*dag.Node, error) {
chnk, err := getChunker(reader, chunker)
chnk, err := chunk.FromString(reader, chunker)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion fuse/readonly/ipfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func randObj(t *testing.T, nd *core.IpfsNode, size int64) (*dag.Node, []byte) {
buf := make([]byte, size)
u.NewTimeSeededRand().Read(buf)
read := bytes.NewReader(buf)
obj, err := importer.BuildTrickleDagFromReader(read, nd.DAG, chunk.DefaultSplitter, nil)
obj, err := importer.BuildTrickleDagFromReader(nd.DAG, chunk.DefaultSplitter(read), nil)
if err != nil {
t.Fatal(err)
}
Expand Down
75 changes: 75 additions & 0 deletions importer/chunk/parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package chunk

import (
"errors"
"fmt"
"io"
"strconv"
"strings"
)

func FromString(r io.Reader, chunker string) (Splitter, error) {
switch {
case chunker == "" || chunker == "default":
return NewSizeSplitter(r, DefaultBlockSize), nil

case strings.HasPrefix(chunker, "size-"):
sizeStr := strings.Split(chunker, "-")[1]
size, err := strconv.Atoi(sizeStr)
if err != nil {
return nil, err
}
return NewSizeSplitter(r, int64(size)), nil

case strings.HasPrefix(chunker, "rabin"):
return parseRabinString(r, chunker)

default:
return nil, fmt.Errorf("unrecognized chunker option: %s", chunker)
}
}

func parseRabinString(r io.Reader, chunker string) (Splitter, error) {
parts := strings.Split(chunker, "-")
switch len(parts) {
case 1:
return NewRabin(r, uint64(DefaultBlockSize)), nil
case 2:
size, err := strconv.Atoi(parts[1])
if err != nil {
return nil, err
}
return NewRabin(r, uint64(size)), nil
case 4:
sub := strings.Split(parts[1], ":")
if len(sub) > 1 && sub[0] != "min" {
return nil, errors.New("first label must be min")
}
min, err := strconv.Atoi(sub[len(sub)-1])
if err != nil {
return nil, err
}

sub = strings.Split(parts[1], ":")
if len(sub) > 1 && sub[0] != "avg" {
return nil, errors.New("second label must be avg")
}
avg, err := strconv.Atoi(sub[len(sub)-1])
if err != nil {
return nil, err
}

sub = strings.Split(parts[1], ":")
if len(sub) > 1 && sub[0] != "max" {
return nil, errors.New("final label must be max")
}
max, err := strconv.Atoi(parts[3])
if err != nil {
return nil, err
}

return NewRabinMinMax(r, uint64(min), uint64(avg), uint64(max)), nil
default:
return nil, errors.New("incorrect format (expected 'rabin' 'rabin-[avg]' or 'rabin-[min]-[avg]-[max]'")
}
}

0 comments on commit 52aacb6

Please sign in to comment.