-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff3eeab
commit 21ff6ca
Showing
6 changed files
with
144 additions
and
0 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,29 @@ | ||
package scale | ||
|
||
import ( | ||
"github.com/tidwall/btree" | ||
"reflect" | ||
) | ||
|
||
// BTree is a wrapper around tidwall/btree.BTree that also stores the comparator function and the type of the items | ||
// stored in the BTree. This is needed during decoding because the BTree is a generic type, and we need to know the | ||
// type of the items stored in the BTree in order to decode them. | ||
type BTree struct { | ||
*btree.BTree | ||
Comparator func(a, b interface{}) bool | ||
ItemType reflect.Type | ||
} | ||
|
||
// NewBTree creates a new BTree with the given comparator function. | ||
func NewBTree[T any](comparator func(a, b any) bool) BTree { | ||
// There's no instantiation overhead of the actual type T because we're only creating a slice type and | ||
// getting the element type from it. | ||
var dummySlice []T | ||
elementType := reflect.TypeOf(dummySlice).Elem() | ||
|
||
return BTree{ | ||
BTree: btree.New(comparator), | ||
Comparator: comparator, | ||
ItemType: elementType, | ||
} | ||
} |
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,50 @@ | ||
package scale | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
type dummy struct { | ||
Field1 uint32 | ||
Field2 [32]byte | ||
} | ||
|
||
func TestBTree(t *testing.T) { | ||
comparator := func(a, b interface{}) bool { | ||
v1 := a.(dummy) | ||
v2 := b.(dummy) | ||
return v1.Field1 < v2.Field1 | ||
} | ||
|
||
// Create a BTree with 3 dummy items | ||
tree := NewBTree[dummy](comparator) | ||
tree.BTree.Set(dummy{Field1: 1}) | ||
tree.BTree.Set(dummy{Field1: 2}) | ||
tree.BTree.Set(dummy{Field1: 3}) | ||
|
||
encoded, err := Marshal(tree) | ||
require.NoError(t, err) | ||
|
||
// taken from the rust codec | ||
expectedEncoded := []byte{12, | ||
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
} | ||
require.Equal(t, expectedEncoded, encoded) | ||
|
||
// Output: | ||
expected := NewBTree[dummy](comparator) | ||
err = Unmarshal(encoded, &expected) | ||
require.NoError(t, err) | ||
|
||
// Check that the expected BTree has the same items as the original | ||
require.Equal(t, tree.BTree.Len(), expected.BTree.Len()) | ||
require.Equal(t, tree.ItemType, expected.ItemType) | ||
require.Equal(t, tree.BTree.Min(), expected.BTree.Min()) | ||
require.Equal(t, tree.BTree.Max(), expected.BTree.Max()) | ||
require.Equal(t, tree.BTree.Get(dummy{Field1: 1}), expected.BTree.Get(dummy{Field1: 1})) | ||
require.Equal(t, tree.BTree.Get(dummy{Field1: 2}), expected.BTree.Get(dummy{Field1: 2})) | ||
require.Equal(t, tree.BTree.Get(dummy{Field1: 3}), expected.BTree.Get(dummy{Field1: 3})) | ||
} |
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