splitter: exposed MaxSegmentSize() for splitters

This commit is contained in:
Jarek Kowalski
2020-03-13 21:27:29 -07:00
committed by Julio López
parent 4564ed5ac1
commit b1020941dd
5 changed files with 17 additions and 0 deletions

View File

@@ -13,6 +13,7 @@
// It must return true if the object should be split after byte b is processed.
type Splitter interface {
ShouldSplit(b byte) bool
MaxSegmentSize() int
Reset()
Close()
}

View File

@@ -39,6 +39,10 @@ func (rs *buzhash32Splitter) ShouldSplit(b byte) bool {
return false
}
func (rs *buzhash32Splitter) MaxSegmentSize() int {
return rs.maxSize
}
func newBuzHash32SplitterFactory(avgSize int) Factory {
// avgSize must be a power of two, so 0b000001000...0000
// it just so happens that mask is avgSize-1 :)

View File

@@ -23,6 +23,10 @@ func (s *fixedSplitter) ShouldSplit(b byte) bool {
return false
}
func (s *fixedSplitter) MaxSegmentSize() int {
return s.chunkLength
}
// Fixed returns a factory that creates splitters with fixed chunk length.
func Fixed(length int) Factory {
return func() Splitter {

View File

@@ -39,6 +39,10 @@ func (rs *rabinKarp64Splitter) ShouldSplit(b byte) bool {
return false
}
func (rs *rabinKarp64Splitter) MaxSegmentSize() int {
return rs.maxSize
}
func newRabinKarp64SplitterFactory(avgSize int) Factory {
mask := uint64(avgSize - 1)
minSize, maxSize := avgSize/2, avgSize*2 //nolint:gomnd

View File

@@ -69,6 +69,10 @@ func TestSplitterStability(t *testing.T) {
minSplit := int(math.MaxInt32)
count := 0
if got, want := s.MaxSegmentSize(), tc.maxSplit; got != want {
t.Errorf("unexpected max segment size: %v, want %v", got, want)
}
for i, p := range rnd {
if !s.ShouldSplit(p) {
continue