Skip to content

Commit

Permalink
add copy methods for cached stores
Browse files Browse the repository at this point in the history
  • Loading branch information
MalteHerrmann committed Oct 1, 2024
1 parent e7668ca commit bb49972
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
32 changes: 32 additions & 0 deletions store/cachekv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,38 @@ func (store *Store) CacheWrapWithTrace(w io.Writer, tc types.TraceContext) types
return NewStore(tracekv.NewStore(store, w, tc))
}

// Copy creates a deep copy of the Store object
func (store *Store) Copy() types.CacheKVStore {
store.mtx.Lock()
defer store.mtx.Unlock()

// Copy cache
cacheCopy := make(map[string]*cValue, len(store.cache))
for key, val := range store.cache {
newVal := *val // Create a copy of the cValue
cacheCopy[key] = &newVal
}

// Copy unsortedCache
unsortedCacheCopy := make(map[string]struct{}, len(store.unsortedCache))
for key := range store.unsortedCache {
unsortedCacheCopy[key] = struct{}{}
}

// Copy sortedCache
sortedCacheCopy := store.sortedCache.Copy()

// Create new Store with copied values
newStore := &Store{
cache: cacheCopy,
unsortedCache: unsortedCacheCopy,
sortedCache: sortedCacheCopy,
parent: store.parent,
}

return newStore
}

//----------------------------------------
// Iteration

Expand Down
26 changes: 26 additions & 0 deletions store/cachemulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,29 @@ func (cms Store) GetKVStore(key types.StoreKey) types.KVStore {
}
return store.(types.KVStore)
}

// Copy creates a deep copy of the Store object
func (cms Store) Copy() types.CacheMultiStore {
// Deep copy the db field
newDB := cms.db.Copy()

// Deep copy the cachekv stores map
newStores := make(map[types.StoreKey]types.CacheWrap, len(cms.stores))
for key, store := range cms.stores {
store, ok := store.(*cachekv.Store)
if ok {
newStores[key] = store.Copy()
}
}

// Create new Store with copied values
newStore := Store{
db: newDB,
stores: newStores,
keys: cms.keys,
traceWriter: cms.traceWriter,
traceContext: cms.traceContext,
}

return newStore
}
4 changes: 3 additions & 1 deletion store/types/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ type MultiStore interface {
// From MultiStore.CacheMultiStore()....
type CacheMultiStore interface {
MultiStore
Write() // Writes operations to underlying KVStore
Write() // Writes operations to underlying KVStore
Copy() CacheMultiStore // Returns a deep copy of the CacheMultiStore
}

// CommitMultiStore is an interface for a MultiStore without cache capabilities.
Expand Down Expand Up @@ -281,6 +282,7 @@ type CacheKVStore interface {

// Writes operations to underlying KVStore
Write()
Copy() CacheKVStore // Returns a deep copy of the CacheKVStore
}

// CommitKVStore is an interface for MultiStore.
Expand Down

0 comments on commit bb49972

Please sign in to comment.