Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Mar 29, 2024
1 parent 56752cb commit dfa2fc9
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions store/cachekv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func NewStore(parent types.KVStore) *Store {

// GStore wraps an in-memory cache around an underlying types.KVStore.
type GStore[V any] struct {
cache btree.BTree[V] // always ascending sorted
parent types.GKVStore[V]
writeSet btree.BTree[V] // always ascending sorted
parent types.GKVStore[V]

// isZero is a function that returns true if the value is considered "zero", for []byte and pointers the zero value
// is `nil`, zero value is not allowed to set to a key, and it's returned if the key is not found.
Expand All @@ -38,7 +38,7 @@ type GStore[V any] struct {
// NewStore creates a new Store object
func NewGStore[V any](parent types.GKVStore[V], isZero func(V) bool, valueLen func(V) int) *GStore[V] {
return &GStore[V]{
cache: btree.NewBTree[V](),
writeSet: btree.NewBTree[V](),
parent: parent,
isZero: isZero,
valueLen: valueLen,
Expand All @@ -54,28 +54,28 @@ func (store *GStore[V]) GetStoreType() types.StoreType {
// it only performs a shadowed copy so is very fast.
func (store *GStore[V]) Clone() types.BranchStore {
return &GStore[V]{
cache: store.cache.Copy(),
parent: store.parent,
writeSet: store.writeSet.Copy(),
parent: store.parent,
}
}

// swapCache swap out the internal cache store and leave the current store unusable.
func (store *GStore[V]) swapCache() btree.BTree[V] {
cache := store.cache
store.cache = btree.BTree[V]{}
cache := store.writeSet
store.writeSet = btree.BTree[V]{}
return cache
}

// Restore restores the store cache to a given snapshot, leaving the snapshot unusable.
func (store *GStore[V]) Restore(s types.BranchStore) {
store.cache = s.(*GStore[V]).swapCache()
store.writeSet = s.(*GStore[V]).swapCache()
}

// Get implements types.KVStore.
func (store *GStore[V]) Get(key []byte) V {
types.AssertValidKey(key)

value, found := store.cache.Get(key)
value, found := store.writeSet.Get(key)
if !found {
return store.parent.Get(key)
}
Expand All @@ -87,14 +87,14 @@ func (store *GStore[V]) Set(key []byte, value V) {
types.AssertValidKey(key)
types.AssertValidValueGeneric(value, store.isZero, store.valueLen)

store.cache.Set(key, value)
store.writeSet.Set(key, value)
}

// Has implements types.KVStore.
func (store *GStore[V]) Has(key []byte) bool {
types.AssertValidKey(key)

value, found := store.cache.Get(key)
value, found := store.writeSet.Get(key)
if !found {
return store.parent.Has(key)
}
Expand All @@ -104,20 +104,20 @@ func (store *GStore[V]) Has(key []byte) bool {
// Delete implements types.KVStore.
func (store *GStore[V]) Delete(key []byte) {
types.AssertValidKey(key)
store.cache.Set(key, store.zeroValue)
store.writeSet.Set(key, store.zeroValue)
}

// Implements Cachetypes.KVStore.
func (store *GStore[V]) Write() {
store.cache.Scan(func(key []byte, value V) bool {
store.writeSet.Scan(func(key []byte, value V) bool {
if store.isZero(value) {
store.parent.Delete(key)
} else {
store.parent.Set(key, value)
}
return true
})
store.cache.Clear()
store.writeSet.Clear()
}

// CacheWrap implements CacheWrapper.
Expand All @@ -139,7 +139,7 @@ func (store *GStore[V]) ReverseIterator(start, end []byte) types.GIterator[V] {
}

func (store *GStore[V]) iterator(start, end []byte, ascending bool) types.GIterator[V] {
isoSortedCache := store.cache.Copy()
isoSortedCache := store.writeSet.Copy()

var (
err error
Expand Down

0 comments on commit dfa2fc9

Please sign in to comment.