-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move chunker type parsing into its own file in chunk
License: MIT Signed-off-by: Jeromy <[email protected]>
- Loading branch information
1 parent
3f33df1
commit 52aacb6
Showing
3 changed files
with
77 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]'") | ||
} | ||
} |