-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: dataset index calculation (#2512)
- Loading branch information
Showing
5 changed files
with
287 additions
and
227 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package dsindex | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// Index represents a sortable dataset index, e.g. 1 < 1_1 < 1_1_1 < 1_2 < 2 | ||
type Index struct { | ||
segments []int | ||
} | ||
|
||
// MustBump returns the next index that is greater than the current one, | ||
// but still less than the index provided in the parameter. Panics on error | ||
func (idx *Index) MustBump(previous *Index) *Index { | ||
res, err := idx.Bump(previous) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return res | ||
} | ||
|
||
// Bump returns the next index that is greater than the current one, | ||
// but still less than the index provided in the parameter. | ||
// | ||
// Bump doesn't increment the first, major segment of the index, it starts from the second segment instead. | ||
func (idx *Index) Bump(before *Index) (*Index, error) { | ||
if !idx.Less(before) { | ||
return nil, fmt.Errorf("%s is not before %s", idx, before) | ||
} | ||
var segment int // never increasing the major segment (index 0) | ||
for { | ||
segment++ | ||
res, err := idx.Increment(segment) | ||
if err != nil { | ||
return nil, err | ||
} | ||
pl := idx.Less(res) | ||
rl := res.Less(before) | ||
if pl && rl { | ||
return res, nil | ||
} | ||
} | ||
} | ||
|
||
// MustIncrement returns a new dataset index that is incremented by one in the specified segment, panics on error | ||
func (idx *Index) MustIncrement(segment int) *Index { | ||
res, err := idx.Increment(segment) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return res | ||
} | ||
|
||
// Increment returns a new dataset index that is incremented by one in the specified segment | ||
func (idx *Index) Increment(segment int) (*Index, error) { | ||
if segment < 0 || segment > idx.Length() { | ||
return nil, fmt.Errorf("cannot increment segment %d of %s", segment, idx) | ||
} | ||
var res *Index = &Index{} | ||
for i := 0; i < segment; i++ { | ||
toAppend := idx.segments[i] | ||
res.segments = append(res.segments, toAppend) | ||
} | ||
var lastSegment int | ||
if idx.Length() > segment { | ||
lastSegment = idx.segments[segment] | ||
} | ||
res.segments = append(res.segments, lastSegment+1) | ||
return res, nil | ||
} | ||
|
||
// Less returns true if this dataset index is Less than the other dataset index | ||
func (idx *Index) Less(other *Index) bool { | ||
for i, segment := range idx.segments { | ||
if i >= other.Length() { | ||
return false | ||
} | ||
if segment < other.segments[i] { | ||
return true | ||
} | ||
if segment > other.segments[i] { | ||
return false | ||
} | ||
} | ||
return other.Length() > idx.Length() | ||
} | ||
|
||
// Length returns the number of segments in the dataset index | ||
func (idx *Index) Length() int { | ||
return len(idx.segments) | ||
} | ||
|
||
// String returns a string representation of the dataset index | ||
func (idx *Index) String() string { | ||
var result []string | ||
for _, segment := range idx.segments { | ||
result = append(result, strconv.Itoa(segment)) | ||
} | ||
return strings.Join(result, "_") | ||
} | ||
|
||
// MustParse returns a dataset index from a string representation, panics on error | ||
func MustParse(value string) *Index { | ||
res, err := Parse(value) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return res | ||
} | ||
|
||
// Parse returns a dataset index from a string representation | ||
func Parse(value string) (*Index, error) { | ||
var result Index | ||
stringSegments := strings.Split(value, "_") | ||
for i, stringSegment := range stringSegments { | ||
segment, err := strconv.Atoi(stringSegment) | ||
if err != nil { | ||
return nil, fmt.Errorf("illegal value for segment %d: %s", i+1, value) | ||
} | ||
if i > 0 && segment <= 0 { | ||
return nil, fmt.Errorf("value for segment %d cannot be less than or equal to zero: %s", i+1, value) | ||
} | ||
result.segments = append(result.segments, segment) | ||
} | ||
return &result, nil | ||
} |
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,104 @@ | ||
package dsindex_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/rudderlabs/rudder-server/jobsdb/internal/dsindex" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_Index_Parse(t *testing.T) { | ||
t.Run("success scenarios", func(t *testing.T) { | ||
idx, err := dsindex.Parse("0") | ||
require.NoError(t, err) | ||
require.Equal(t, "0", idx.String()) | ||
require.Equal(t, 1, idx.Length()) | ||
|
||
idx, err = dsindex.Parse("0_1") | ||
require.NoError(t, err) | ||
require.Equal(t, "0_1", idx.String()) | ||
require.Equal(t, 2, idx.Length()) | ||
|
||
idx, err = dsindex.Parse("1_2_3") | ||
require.NoError(t, err) | ||
require.Equal(t, "1_2_3", idx.String()) | ||
require.Equal(t, 3, idx.Length()) | ||
}) | ||
|
||
t.Run("error scenarios", func(t *testing.T) { | ||
_, err := dsindex.Parse("") | ||
require.Error(t, err) | ||
|
||
_, err = dsindex.Parse("a") | ||
require.Error(t, err) | ||
|
||
_, err = dsindex.Parse("1a") | ||
require.Error(t, err) | ||
|
||
_, err = dsindex.Parse("1_a") | ||
require.Error(t, err) | ||
|
||
_, err = dsindex.Parse("1_0") // zero is allowed only as first segment | ||
require.Error(t, err) | ||
|
||
_, err = dsindex.Parse("1_-1") | ||
require.Error(t, err) | ||
|
||
require.Panics(t, func() { dsindex.MustParse("1_-1") }) | ||
}) | ||
} | ||
|
||
func Test_Index_Less(t *testing.T) { | ||
require.True(t, dsindex.MustParse("-10").Less(dsindex.MustParse("-9"))) | ||
require.True(t, dsindex.MustParse("0").Less(dsindex.MustParse("1"))) | ||
require.True(t, dsindex.MustParse("0_1").Less(dsindex.MustParse("1"))) | ||
require.True(t, dsindex.MustParse("0_1_2").Less(dsindex.MustParse("1"))) | ||
require.True(t, dsindex.MustParse("1_1").Less(dsindex.MustParse("1_2"))) | ||
require.True(t, dsindex.MustParse("1_1_1").Less(dsindex.MustParse("1_1_3"))) | ||
require.True(t, dsindex.MustParse("11").Less(dsindex.MustParse("11_1"))) | ||
|
||
require.False(t, dsindex.MustParse("1_1_1").Less(dsindex.MustParse("1_1"))) | ||
} | ||
|
||
func Test_Index_Increment(t *testing.T) { | ||
t.Run("success scenarios", func(t *testing.T) { | ||
require.Equal(t, "-9", dsindex.MustParse("-10").MustIncrement(0).String()) | ||
require.Equal(t, "0_1", dsindex.MustParse("0").MustIncrement(1).String()) | ||
require.Equal(t, "0_2", dsindex.MustParse("0_1").MustIncrement(1).String()) | ||
require.Equal(t, "1", dsindex.MustParse("0_1").MustIncrement(0).String()) | ||
require.Equal(t, "0_2", dsindex.MustParse("0_1_2").MustIncrement(1).String()) | ||
}) | ||
|
||
t.Run("error scenarios", func(t *testing.T) { | ||
_, err := dsindex.MustParse("0").Increment(-1) | ||
require.Error(t, err) | ||
|
||
_, err = dsindex.MustParse("0").Increment(2) | ||
require.Error(t, err) | ||
|
||
_, err = dsindex.MustParse("0").Increment(3) | ||
require.Error(t, err) | ||
|
||
require.Panics(t, func() { dsindex.MustParse("0").MustIncrement(3) }) | ||
}) | ||
} | ||
|
||
func Test_Index_Bump(t *testing.T) { | ||
t.Run("success scenarios", func(t *testing.T) { | ||
require.Equal(t, "0_1", dsindex.MustParse("0").MustBump(dsindex.MustParse("1")).String()) | ||
require.Equal(t, "0_1", dsindex.MustParse("0").MustBump(dsindex.MustParse("2")).String()) | ||
require.Equal(t, "0_1", dsindex.MustParse("0").MustBump(dsindex.MustParse("3")).String()) | ||
require.Equal(t, "1_2_2", dsindex.MustParse("1_2_1").MustBump(dsindex.MustParse("1_2_3")).String()) | ||
require.Equal(t, "-11_1", dsindex.MustParse("-11").MustBump(dsindex.MustParse("-10")).String()) | ||
}) | ||
|
||
t.Run("error scenarios", func(t *testing.T) { | ||
_, err := dsindex.MustParse("1_2_1").Bump(dsindex.MustParse("1_2_1")) | ||
require.Error(t, err, "bump should fail if index is not less than other") | ||
|
||
_, err = dsindex.MustParse("1_2").Bump(dsindex.MustParse("1_1")) | ||
require.Error(t, err, "bump should fail if index is not less than other") | ||
|
||
require.Panics(t, func() { dsindex.MustParse("1_2").MustBump(dsindex.MustParse("1_1")) }) | ||
}) | ||
} |
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
Oops, something went wrong.