From 1afab45cf590876879918cc3ccab4c1d827cc2f6 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Thu, 21 Dec 2017 20:05:41 -0800 Subject: [PATCH] Move store/types.go to types/store.go and alias --- store/memiterator.go | 16 ++++ store/types.go | 173 ++++----------------------------------- types/decorators.go | 8 +- types/decorators_test.go | 9 +- types/handler.go | 6 +- types/result.go | 2 +- types/store.go | 154 ++++++++++++++++++++++++++++++++++ 7 files changed, 192 insertions(+), 176 deletions(-) create mode 100644 types/store.go diff --git a/store/memiterator.go b/store/memiterator.go index 197148ed1..a05f3443e 100644 --- a/store/memiterator.go +++ b/store/memiterator.go @@ -1,6 +1,8 @@ package store import ( + "bytes" + cmn "github.com/tendermint/tmlibs/common" dbm "github.com/tendermint/tmlibs/db" ) @@ -62,3 +64,17 @@ func (mi *memIterator) Close() { mi.end = nil mi.items = nil } + +//---------------------------------------- +// Misc. + +// bytes.Compare but bounded on both sides by nil. +// both (k1, nil) and (nil, k2) return -1 +func keyCompare(k1, k2 []byte) int { + if k1 == nil && k2 == nil { + return 0 + } else if k1 == nil || k2 == nil { + return -1 + } + return bytes.Compare(k1, k2) +} diff --git a/store/types.go b/store/types.go index 02518453c..ec572e766 100644 --- a/store/types.go +++ b/store/types.go @@ -1,164 +1,19 @@ package store import ( - "bytes" - "fmt" - - "github.com/tendermint/tmlibs/db" + "github.com/cosmos/cosmos-sdk/types" ) -//---------------------------------------- -// MultiStore - -type MultiStore interface { - - // Last commit, or the zero CommitID. - // If not zero, CommitID.Version is NextVersion()-1. - LastCommitID() CommitID - - // Current version being worked on now, not yet committed. - // Should be greater than 0. - NextVersion() int64 - - // Cache wrap MultiStore. - // NOTE: Caller should probably not call .Write() on each, but - // call CacheMultiStore.Write(). - CacheMultiStore() CacheMultiStore - - // Convenience - GetStore(name string) interface{} - GetKVStore(name string) KVStore -} - -// From MultiStore.CacheMultiStore().... -type CacheMultiStore interface { - MultiStore - Write() // Writes operations to underlying KVStore -} - -// Substores of MultiStore must implement CommitStore. -type CommitStore interface { - Committer - CacheWrapper -} - -// A non-cache store that can commit (persist) and get a Merkle root. -type Committer interface { - Commit() CommitID -} - -// A non-cache MultiStore. -type CommitMultiStore interface { - CommitStore - MultiStore - - // Add a substore loader. - SetSubstoreLoader(name string, loader CommitStoreLoader) - - // Load the latest persisted version. - LoadLatestVersion() error - - // Load a specific persisted version. When you load an old version, or - // when the last commit attempt didn't complete, the next commit after - // loading must be idempotent (return the same commit id). Otherwise the - // behavior is undefined. - LoadVersion(ver int64) error -} - -// These must be added to the MultiStore before calling LoadVersion() or -// LoadLatest(). -type CommitStoreLoader func(id CommitID) (CommitStore, error) - -//---------------------------------------- -// KVStore - -// KVStore is a simple interface to get/set data -type KVStore interface { - - // Get returns nil iff key doesn't exist. Panics on nil key. - Get(key []byte) []byte - - // Has checks if a key exists. Panics on nil key. - Has(key []byte) bool - - // Set sets the key. Panics on nil key. - Set(key, value []byte) - - // Delete deletes the key. Panics on nil key. - Delete(key []byte) - - // Iterator over a domain of keys in ascending order. End is exclusive. - // Start must be less than end, or the Iterator is invalid. - // CONTRACT: No writes may happen within a domain while an iterator exists over it. - Iterator(start, end []byte) Iterator - - // Iterator over a domain of keys in descending order. End is exclusive. - // Start must be greater than end, or the Iterator is invalid. - // CONTRACT: No writes may happen within a domain while an iterator exists over it. - ReverseIterator(start, end []byte) Iterator -} - -// db.DB implements KVStore so we can CacheKVStore it. -var _ KVStore = db.DB(nil) - -// Alias iterator to db's Iterator for convenience. -type Iterator = db.Iterator - -// CacheKVStore cache-wraps a KVStore. After calling .Write() on the -// CacheKVStore, all previously created CacheKVStores on the object expire. -type CacheKVStore interface { - KVStore - Write() // Writes operations to underlying KVStore -} - -//---------------------------------------- -// CacheWrap - -/* - CacheWrap() makes the most appropriate cache-wrap. For example, - IAVLStore.CacheWrap() returns a CacheKVStore. - - CacheWrap() should not return a Committer, since Commit() on - cache-wraps make no sense. It can return KVStore, HeapStore, - SpaceStore, etc. -*/ -type CacheWrapper interface { - CacheWrap() CacheWrap -} - -type CacheWrap interface { - - // Write syncs with the underlying store. - Write() - - // CacheWrap recursively wraps again. - CacheWrap() CacheWrap -} - -//---------------------------------------- -// etc - -// CommitID contains the tree version number and its merkle root. -type CommitID struct { - Version int64 - Hash []byte -} - -func (cid CommitID) IsZero() bool { - return cid.Version == 0 && len(cid.Hash) == 0 -} - -func (cid CommitID) String() string { - return fmt.Sprintf("CommitID{%v:%X}", cid.Hash, cid.Version) -} - -// bytes.Compare but bounded on both sides by nil. -// both (k1, nil) and (nil, k2) return -1 -func keyCompare(k1, k2 []byte) int { - if k1 == nil && k2 == nil { - return 0 - } else if k1 == nil || k2 == nil { - return -1 - } - return bytes.Compare(k1, k2) -} +// Import cosmos-sdk/types/store.go for convenience. +type MultiStore = types.MultiStore +type CacheMultiStore = types.CacheMultiStore +type CommitStore = types.CommitStore +type Committer = types.Committer +type CommitMultiStore = types.CommitMultiStore +type CommitStoreLoader = types.CommitStoreLoader +type KVStore = types.KVStore +type Iterator = types.Iterator +type CacheKVStore = types.CacheKVStore +type CacheWrapper = types.CacheWrapper +type CacheWrap = types.CacheWrap +type CommitID = types.CommitID diff --git a/types/decorators.go b/types/decorators.go index c3b773648..06469a61e 100644 --- a/types/decorators.go +++ b/types/decorators.go @@ -1,15 +1,11 @@ package types -import ( - "github.com/cosmos/cosmos-sdk/store" -) - // A Decorator executes before/during/after a handler to enhance functionality. -type Decorator func(ctx Context, ms store.MultiStore, tx Tx, next Handler) Result +type Decorator func(ctx Context, ms MultiStore, tx Tx, next Handler) Result // Return a decorated handler func Decorate(dec Decorator, next Handler) Handler { - return func(ctx Context, ms store.MultiStore, tx Tx) Result { + return func(ctx Context, ms MultiStore, tx Tx) Result { return dec(ctx, ms, tx, next) } } diff --git a/types/decorators_test.go b/types/decorators_test.go index 9af091ac3..f04ee016e 100644 --- a/types/decorators_test.go +++ b/types/decorators_test.go @@ -3,26 +3,25 @@ package types import ( "testing" - "github.com/cosmos/cosmos-sdk/store" "github.com/stretchr/testify/assert" ) func TestDecorate(t *testing.T) { var calledDec1, calledDec2, calledHandler bool - dec1 := func(ctx Context, ms store.MultiStore, tx Tx, next Handler) Result { + dec1 := func(ctx Context, ms MultiStore, tx Tx, next Handler) Result { calledDec1 = true next(ctx, ms, tx) return Result{} } - dec2 := func(ctx Context, ms store.MultiStore, tx Tx, next Handler) Result { + dec2 := func(ctx Context, ms MultiStore, tx Tx, next Handler) Result { calledDec2 = true next(ctx, ms, tx) return Result{} } - handler := func(ctx Context, ms store.MultiStore, tx Tx) Result { + handler := func(ctx Context, ms MultiStore, tx Tx) Result { calledHandler = true return Result{} } @@ -30,7 +29,7 @@ func TestDecorate(t *testing.T) { decoratedHandler := ChainDecorators(dec1, dec2).WithHandler(handler) var ctx Context - var ms store.MultiStore + var ms MultiStore var tx Tx decoratedHandler(ctx, ms, tx) assert.True(t, calledDec1) diff --git a/types/handler.go b/types/handler.go index 1a4ea32a1..bf95d6323 100644 --- a/types/handler.go +++ b/types/handler.go @@ -1,9 +1,5 @@ package types -import ( - "github.com/cosmos/cosmos-sdk/store" -) - // Handler handles both ABCI DeliverTx and CheckTx requests. // Iff ABCI.CheckTx, ctx.IsCheckTx() returns true. -type Handler func(ctx Context, store store.MultiStore, tx Tx) Result +type Handler func(ctx Context, ms MultiStore, tx Tx) Result diff --git a/types/result.go b/types/result.go index b96187979..d56e5ceb1 100644 --- a/types/result.go +++ b/types/result.go @@ -1,8 +1,8 @@ package types import ( - cmn "github.com/tendemrint/tmlibs/common" abci "github.com/tendermint/abci/types" + cmn "github.com/tendermint/tmlibs/common" ) // Result is the union of ResponseDeliverTx and ResponseCheckTx. diff --git a/types/store.go b/types/store.go new file mode 100644 index 000000000..73a0e848a --- /dev/null +++ b/types/store.go @@ -0,0 +1,154 @@ +package types + +import ( + "fmt" + + dbm "github.com/tendermint/tmlibs/db" +) + +// NOTE: These are implemented in cosmos-sdk/store. + +//---------------------------------------- +// MultiStore + +type MultiStore interface { + + // Last commit, or the zero CommitID. + // If not zero, CommitID.Version is NextVersion()-1. + LastCommitID() CommitID + + // Current version being worked on now, not yet committed. + // Should be greater than 0. + NextVersion() int64 + + // Cache wrap MultiStore. + // NOTE: Caller should probably not call .Write() on each, but + // call CacheMultiStore.Write(). + CacheMultiStore() CacheMultiStore + + // Convenience + GetStore(name string) interface{} + GetKVStore(name string) KVStore +} + +// From MultiStore.CacheMultiStore().... +type CacheMultiStore interface { + MultiStore + Write() // Writes operations to underlying KVStore +} + +// Substores of MultiStore must implement CommitStore. +type CommitStore interface { + Committer + CacheWrapper +} + +// A non-cache store that can commit (persist) and get a Merkle root. +type Committer interface { + Commit() CommitID +} + +// A non-cache MultiStore. +type CommitMultiStore interface { + CommitStore + MultiStore + + // Add a substore loader. + SetSubstoreLoader(name string, loader CommitStoreLoader) + + // Load the latest persisted version. + LoadLatestVersion() error + + // Load a specific persisted version. When you load an old version, or + // when the last commit attempt didn't complete, the next commit after + // loading must be idempotent (return the same commit id). Otherwise the + // behavior is undefined. + LoadVersion(ver int64) error +} + +// These must be added to the MultiStore before calling LoadVersion() or +// LoadLatest(). +type CommitStoreLoader func(id CommitID) (CommitStore, error) + +//---------------------------------------- +// KVStore + +// KVStore is a simple interface to get/set data +type KVStore interface { + + // Get returns nil iff key doesn't exist. Panics on nil key. + Get(key []byte) []byte + + // Has checks if a key exists. Panics on nil key. + Has(key []byte) bool + + // Set sets the key. Panics on nil key. + Set(key, value []byte) + + // Delete deletes the key. Panics on nil key. + Delete(key []byte) + + // Iterator over a domain of keys in ascending order. End is exclusive. + // Start must be less than end, or the Iterator is invalid. + // CONTRACT: No writes may happen within a domain while an iterator exists over it. + Iterator(start, end []byte) Iterator + + // Iterator over a domain of keys in descending order. End is exclusive. + // Start must be greater than end, or the Iterator is invalid. + // CONTRACT: No writes may happen within a domain while an iterator exists over it. + ReverseIterator(start, end []byte) Iterator +} + +// dbm.DB implements KVStore so we can CacheKVStore it. +var _ KVStore = dbm.DB(nil) + +// Alias iterator to db's Iterator for convenience. +type Iterator = dbm.Iterator + +// CacheKVStore cache-wraps a KVStore. After calling .Write() on the +// CacheKVStore, all previously created CacheKVStores on the object expire. +type CacheKVStore interface { + KVStore + Write() // Writes operations to underlying KVStore +} + +//---------------------------------------- +// CacheWrap + +/* + CacheWrap() makes the most appropriate cache-wrap. For example, + IAVLStore.CacheWrap() returns a CacheKVStore. + + CacheWrap() should not return a Committer, since Commit() on + cache-wraps make no sense. It can return KVStore, HeapStore, + SpaceStore, etc. +*/ +type CacheWrapper interface { + CacheWrap() CacheWrap +} + +type CacheWrap interface { + + // Write syncs with the underlying store. + Write() + + // CacheWrap recursively wraps again. + CacheWrap() CacheWrap +} + +//---------------------------------------- +// etc + +// CommitID contains the tree version number and its merkle root. +type CommitID struct { + Version int64 + Hash []byte +} + +func (cid CommitID) IsZero() bool { + return cid.Version == 0 && len(cid.Hash) == 0 +} + +func (cid CommitID) String() string { + return fmt.Sprintf("CommitID{%v:%X}", cid.Hash, cid.Version) +}