Skip to content

Commit

Permalink
Use custom Separator function
Browse files Browse the repository at this point in the history
  • Loading branch information
asdine committed Jul 14, 2022
1 parent c9f6a47 commit a35719a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion internal/database/pebble/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var DefaultComparer = &pebble.Comparer{
Equal: encoding.Equal,
AbbreviatedKey: encoding.AbbreviatedKey,
FormatKey: pebble.DefaultComparer.FormatKey,
Separator: pebble.DefaultComparer.Separator,
Separator: encoding.Separator,
Successor: encoding.Successor,
// This name is part of the C++ Level-DB implementation's default file
// format, and should not be changed.
Expand Down
25 changes: 24 additions & 1 deletion internal/encoding/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,5 +353,28 @@ func abbreviatedValue(key []byte) uint64 {
}

return 0
// panic(fmt.Sprintf("unsupported value type: %d, %v", key[0], key))
}

func Separator(dst, a, b []byte) (foo []byte) {
var n, cmp int
var idx int
var aa []byte = a

for {
if n == len(a) {
return a
}

a = a[n:]
b = b[n:]
idx += n

cmp, n = compareNextValue(a, b)
if cmp != 0 {
idx += n
dst = append(dst, aa[:idx]...)
dst = append(dst, 0xFF)
return dst
}
}
}
23 changes: 23 additions & 0 deletions internal/encoding/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,26 @@ func TestAbbreviatedKey(t *testing.T) {
})
}
}

func TestSeparator(t *testing.T) {
tests := []struct {
k1, k2 string
}{
{`[1, 1]`, `[1, 2]`},
{`[1, 1]`, `[1, 3]`},
}

for _, test := range tests {
t.Run(fmt.Sprintf("Separator(%v, %v)", test.k1, test.k2), func(t *testing.T) {
v1, err := testutil.ParseExpr(t, test.k1).Eval(&environment.Environment{})
require.NoError(t, err)
v2, err := testutil.ParseExpr(t, test.k2).Eval(&environment.Environment{})
require.NoError(t, err)
k1 := mustNewKey(t, v1.V().(*document.ValueBuffer).Values...)
k2 := mustNewKey(t, v2.V().(*document.ValueBuffer).Values...)
sep := encoding.Separator(nil, k1, k2)
require.LessOrEqual(t, encoding.Compare(k1, sep), 0)
require.Less(t, encoding.Compare(sep, k2), 0)
})
}
}

0 comments on commit a35719a

Please sign in to comment.