Skip to content

Commit

Permalink
Fix cachekv DeleteAll (#476)
Browse files Browse the repository at this point in the history
## Describe your changes and provide context
Previously when getting keys from local cache the `start`/`end` boundary
was not respected

## Testing performed to validate your change
unit test that includes keys outside of the requested range
  • Loading branch information
codchen committed Mar 27, 2024
1 parent 57593a9 commit 20e0b67
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 5 additions & 0 deletions store/cachekv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,11 @@ func (store *Store) GetAllKeyStrsInRange(start, end []byte) (res []string) {
keyStrs[pk] = struct{}{}
}
store.cache.Range(func(key, value any) bool {
kbz := []byte(key.(string))
if bytes.Compare(kbz, start) < 0 || bytes.Compare(kbz, end) >= 0 {
// we don't want to break out of the iteration since cache isn't sorted
return true
}
cv := value.(*types.CValue)
if cv.Value() == nil {
delete(keyStrs, key.(string))
Expand Down
6 changes: 5 additions & 1 deletion store/cachekv/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,16 @@ func TestCacheKVStore(t *testing.T) {
mem.Set(keyFmt(1), valFmt(1))
mem.Set(keyFmt(3), valFmt(4))
st = cachekv.NewStore(mem, types.NewKVStoreKey("CacheKvTest"), types.DefaultCacheSizeLimit)
st.Set(keyFmt(0), valFmt(0))
st.Set(keyFmt(1), valFmt(2))
st.Set(keyFmt(2), valFmt(3))
require.Nil(t, st.DeleteAll(nil, nil))
st.Set(keyFmt(5), valFmt(6))
require.Nil(t, st.DeleteAll(keyFmt(1), keyFmt(5)))
require.Nil(t, st.Get(keyFmt(1)))
require.Nil(t, st.Get(keyFmt(2)))
require.Nil(t, st.Get(keyFmt(3)))
require.NotNil(t, st.Get(keyFmt(0)))
require.NotNil(t, st.Get(keyFmt(5)))
require.Equal(t, valFmt(1), mem.Get(keyFmt(1)))
require.Equal(t, valFmt(4), mem.Get(keyFmt(3)))
}
Expand Down

0 comments on commit 20e0b67

Please sign in to comment.