Skip to content

Commit

Permalink
storage: extend TestKeySchema_RandomKeys assertions
Browse files Browse the repository at this point in the history
Assert that seeking to each key finds the right key.

Epic: none
Release note: none
  • Loading branch information
jbowens committed Nov 13, 2024
1 parent 0e41957 commit 2244819
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions pkg/storage/pebble_key_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ func TestKeySchema_RandomKeys(t *testing.T) {
var it colblk.DataBlockIter
it.InitOnce(&keySchema, EngineKeyCompare, EngineKeySplit, nil)
require.NoError(t, it.Init(&dec, block.NoTransforms))
// Ensure that a scan across the block finds all the relevant keys.
var valBuf []byte
for k, kv := 0, it.First(); kv != nil; k, kv = k+1, it.Next() {
require.True(t, EngineKeyEqual(keys[k], kv.K.UserKey))
require.Zero(t, EngineKeyCompare(keys[k], kv.K.UserKey))
Expand All @@ -352,6 +354,42 @@ func TestKeySchema_RandomKeys(t *testing.T) {
t.Fatalf("key %q is longer than original key %q", kv.K.UserKey, keys[k])
}
checkEngineKey(kv.K.UserKey)

// We write keys[k] as the value too, so check that it's verbatim equal.
value, callerOwned, err := kv.V.Value(valBuf)
require.NoError(t, err)
require.Equal(t, keys[k], value)
if callerOwned {
valBuf = value
}
}
// Ensure that seeking to each key finds the key.
for i := range keys {
kv := it.SeekGE(keys[i], 0)
require.True(t, EngineKeyEqual(keys[i], kv.K.UserKey))
require.Zero(t, EngineKeyCompare(keys[i], kv.K.UserKey))
}
// Ensure seeking to just the prefix of each key finds a key with the same
// prefix.
for i := range keys {
si := EngineKeySplit(keys[i])
kv := it.SeekGE(keys[i][:si], 0)
require.True(t, EngineKeyEqual(keys[i][:si], pebble.Split(EngineKeySplit).Prefix(kv.K.UserKey)))
}
// Ensure seeking to the key but in random order finds the key.
for _, i := range rng.Perm(len(keys)) {
kv := it.SeekGE(keys[i], 0)
require.True(t, EngineKeyEqual(keys[i], kv.K.UserKey))
require.Zero(t, EngineKeyCompare(keys[i], kv.K.UserKey))

// We write keys[k] as the value too, so check that it's verbatim equal.
value, callerOwned, err := kv.V.Value(valBuf)
require.NoError(t, err)
require.Equal(t, keys[i], value)
if callerOwned {
valBuf = value
}
}

require.NoError(t, it.Close())
}

0 comments on commit 2244819

Please sign in to comment.