Skip to content

Commit

Permalink
base: verify all mandatory Comparer fields are set
Browse files Browse the repository at this point in the history
  • Loading branch information
RaduBerinde committed Feb 26, 2024
1 parent 306ea71 commit 403ee52
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
13 changes: 9 additions & 4 deletions ingest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1897,16 +1897,21 @@ func TestIngestExternal(t *testing.T) {

func TestIngestMemtableOverlaps(t *testing.T) {
comparers := []Comparer{
{Name: "default", Compare: DefaultComparer.Compare, FormatKey: DefaultComparer.FormatKey},
{
Name: "reverse",
Compare: func(a, b []byte) int { return DefaultComparer.Compare(b, a) },
FormatKey: DefaultComparer.FormatKey,
Name: "default",
Compare: DefaultComparer.Compare,
},
{
Name: "reverse",
Compare: func(a, b []byte) int { return DefaultComparer.Compare(b, a) },
},
}
m := make(map[string]*Comparer)
for i := range comparers {
c := &comparers[i]
c.AbbreviatedKey = func(key []byte) uint64 { panic("unimplemented") }
c.Successor = func(dst, a []byte) []byte { panic("unimplemented") }
c.Separator = func(dst, a, b []byte) []byte { panic("unimplemented") }
m[c.Name] = c
}

Expand Down
3 changes: 3 additions & 0 deletions internal/base/comparer.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ func (c *Comparer) EnsureDefaults() *Comparer {
if c == nil {
return DefaultComparer
}
if c.Compare == nil || c.AbbreviatedKey == nil || c.Separator == nil || c.Successor == nil || c.Name == "" {
panic("invalid Comparer: mandatory field not set")
}
if c.Equal != nil && c.FormatKey != nil {
return c
}
Expand Down
5 changes: 4 additions & 1 deletion open_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,11 @@ func TestOpenOptionsCheck(t *testing.T) {
require.NoError(t, err)
require.NoError(t, d.Close())

fooCmp := *base.DefaultComparer
fooCmp.Name = "foo"

opts = &Options{
Comparer: &Comparer{Name: "foo"},
Comparer: &fooCmp,
FS: mem,
}
_, err = Open("", opts)
Expand Down

0 comments on commit 403ee52

Please sign in to comment.