Skip to content

Commit

Permalink
requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
okdas committed Aug 21, 2024
1 parent 449cd8c commit 730030f
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 8 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ benchmark_smst_ops: ## runs the benchmarks test different operations on the SMS
benchmark_proof_sizes: ## runs the benchmarks test the proof sizes for different sized tries
go test -tags=benchmark -v ./benchmarks -run ProofSizes

.PHONY: benchmark_pebble
benchmark_pebble: ## TODO: Add the pebble benchmarks
echo "TODO: Implement pebble benchmarks"

###########################
### Release Helpers ###
###########################
Expand Down Expand Up @@ -124,4 +128,4 @@ tag_minor_release: ## Tag a new minor release (e.g. v1.0.0 -> v1.1.0)
@echo "New minor release version tagged: $(NEW_TAG)"
@echo "Run the following commands to push the new tag:"
@echo " git push origin $(NEW_TAG)"
@echo "And draft a new release at https://github.com/pokt-network/smt/releases/new"
@echo "And draft a new release at https://github.com/pokt-network/smt/releases/new"
17 changes: 11 additions & 6 deletions kvstore/badger/kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,12 @@ func (store *badgerKVStore) GetAll(prefix []byte, descending bool) (keys, values
return keys, values, nil
}

// Exists checks whether the key exists in the store
// Exists checks whether the key exists in the store without retrieving the full value.
// This avoids unnecessary memory allocations and copying that would occur with a full Get.
func (store *badgerKVStore) Exists(key []byte) (bool, error) {
var exists bool
err := store.db.View(func(tx *badgerv4.Txn) error {
item, err := tx.Get(key)
if err == badgerv4.ErrKeyNotFound {
return ErrBadgerUnableToGetValue
}
if err != nil {
return err
}
Expand Down Expand Up @@ -196,8 +194,15 @@ func (store *badgerKVStore) Len() (int, error) {
return count, nil
}

// PrefixEndBytes returns the end byteslice for a noninclusive range
// that would include all byte slices for which the input is the prefix
// prefixEndBytes returns the end byteslice for a noninclusive range
// that would include all byte slices for which the input is the prefix.
// It's used in reverse iteration to set the upper bound of the key range.
//
// Example:
// If prefix is []byte("user:1"), prefixEndBytes returns []byte("user:2").
// This ensures that in reverse iteration:
// - Keys like "user:1", "user:1:profile", "user:10" are included.
// - But "user:2", "user:2:profile" are not included.
func prefixEndBytes(prefix []byte) []byte {
if len(prefix) == 0 {
return nil
Expand Down
14 changes: 13 additions & 1 deletion kvstore/pebble/kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,19 @@ func (store *pebbleKVStore) Len() (int, error) {
return count, nil
}

// prefixEndBytes returns the end key for prefix scans.
// prefixEndBytes returns the end byte slice for a noninclusive range
// that would include all byte slices for which the input is the prefix.
// It's used in reverse iteration to set the upper bound of the key range.
//
// Example:
// If prefix is []byte("user:1"), prefixEndBytes returns []byte("user:2").
// This ensures that in reverse iteration:
// - Keys like "user:1", "user:1:profile", "user:10" are included.
// - But "user:2", "user:2:profile" are not included.
//
// See `TestPebble_KVStore_GetAllWithPrefixEnd` for more examples.
//
// Note: This function assumes the prefix is composed of standard ASCII characters.
func prefixEndBytes(prefix []byte) []byte {
if len(prefix) == 0 {
return nil
Expand Down
63 changes: 63 additions & 0 deletions kvstore/pebble/kvstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,69 @@ func TestPebble_KVStore_Len(t *testing.T) {
}
}

func TestPebble_KVStore_GetAllWithPrefixEnd(t *testing.T) {
store, err := pebble.NewKVStore("")
require.NoError(t, err)
require.NotNil(t, store)

keys := [][]byte{
[]byte("user:1"),
[]byte("user:10"),
[]byte("user:100"),
[]byte("user:2"),
[]byte("user:20"),
}
values := [][]byte{
[]byte("value1"),
[]byte("value10"),
[]byte("value100"),
[]byte("value2"),
[]byte("value20"),
}

for i := 0; i < len(keys); i++ {
err := store.Set(keys[i], values[i])
require.NoError(t, err)
}

t.Run("GetAll with prefix user:1", func(t *testing.T) {
expectedKeys := [][]byte{
[]byte("user:1"),
[]byte("user:10"),
[]byte("user:100"),
}
expectedValues := [][]byte{
[]byte("value1"),
[]byte("value10"),
[]byte("value100"),
}

allKeys, allValues, err := store.GetAll([]byte("user:1"), false)
require.NoError(t, err)
require.Equal(t, expectedKeys, allKeys)
require.Equal(t, expectedValues, allValues)
})

t.Run("GetAll with prefix user:2", func(t *testing.T) {
expectedKeys := [][]byte{
[]byte("user:2"),
[]byte("user:20"),
}
expectedValues := [][]byte{
[]byte("value2"),
[]byte("value20"),
}

allKeys, allValues, err := store.GetAll([]byte("user:2"), false)
require.NoError(t, err)
require.Equal(t, expectedKeys, allKeys)
require.Equal(t, expectedValues, allValues)
})

err = store.Stop()
require.NoError(t, err)
}

func setupStore(t *testing.T, store pebble.PebbleKVStore) {
t.Helper()
err := store.Set([]byte("foo"), []byte("bar"))
Expand Down

0 comments on commit 730030f

Please sign in to comment.