forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wrap v1 MultiStore in v2 interface; use in baseapp
- Loading branch information
Showing
9 changed files
with
205 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package db | ||
|
||
import ( | ||
"errors" | ||
|
||
dbm "github.com/cosmos/cosmos-sdk/db" | ||
|
||
tmdb "github.com/tendermint/tm-db" | ||
) | ||
|
||
// An adapter used to wrap objects supporting cosmos-sdk/db interface so that they support | ||
// the tm-db interface. | ||
// | ||
// This serves as a transitional step in introducing the new DB interface while maintaining | ||
// compatibility with existing code that expects the old interface. | ||
type tmdbAdapter struct { | ||
dbm.DBReadWriter | ||
db dbm.DBConnection | ||
} | ||
type tmdbBatchAdapter struct { | ||
*tmdbAdapter | ||
written bool | ||
} | ||
|
||
var ( | ||
_ tmdb.DB = (*tmdbAdapter)(nil) | ||
) | ||
|
||
// ConnectionAsTmdb returns a tmdb.DB which wraps a DBConnection. | ||
func ConnectionAsTmdb(db dbm.DBConnection) *tmdbAdapter { return &tmdbAdapter{db.ReadWriter(), db} } | ||
|
||
func (d *tmdbAdapter) Close() error { return d.db.Close() } | ||
|
||
func (d *tmdbAdapter) sync() error { | ||
err := d.DBReadWriter.Commit() | ||
if err != nil { | ||
return err | ||
} | ||
d.DBReadWriter = d.db.ReadWriter() | ||
return nil | ||
} | ||
func (d *tmdbAdapter) DeleteSync(k []byte) error { | ||
err := d.DBReadWriter.Delete(k) | ||
if err != nil { | ||
return err | ||
} | ||
return d.sync() | ||
} | ||
func (d *tmdbAdapter) SetSync(k, v []byte) error { | ||
err := d.DBReadWriter.Set(k, v) | ||
if err != nil { | ||
return err | ||
} | ||
return d.sync() | ||
} | ||
|
||
func (d *tmdbAdapter) Iterator(s, e []byte) (tmdb.Iterator, error) { | ||
it, err := d.DBReadWriter.Iterator(s, e) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return DBToStoreIterator(it), nil | ||
} | ||
func (d *tmdbAdapter) ReverseIterator(s, e []byte) (tmdb.Iterator, error) { | ||
it, err := d.DBReadWriter.ReverseIterator(s, e) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return DBToStoreIterator(it), nil | ||
} | ||
|
||
// NewBatch returns a tmdb.Batch which wraps a DBWriter. | ||
func (d *tmdbAdapter) NewBatch() tmdb.Batch { | ||
return &tmdbBatchAdapter{d, false} | ||
} | ||
func (d *tmdbAdapter) Print() error { return nil } | ||
func (d *tmdbAdapter) Stats() map[string]string { return nil } | ||
|
||
func (d *tmdbBatchAdapter) Set(k, v []byte) error { | ||
if d.written { | ||
return errors.New("Batch already written") | ||
} | ||
return d.tmdbAdapter.Set(k, v) | ||
} | ||
func (d *tmdbBatchAdapter) Delete(k []byte) error { | ||
if d.written { | ||
return errors.New("Batch already written") | ||
} | ||
return d.tmdbAdapter.Delete(k) | ||
} | ||
func (d *tmdbBatchAdapter) WriteSync() error { | ||
if d.written { | ||
return errors.New("Batch already written") | ||
} | ||
d.written = true | ||
return d.sync() | ||
} | ||
func (d *tmdbBatchAdapter) Write() error { | ||
if d.written { | ||
return errors.New("Batch already written") | ||
} | ||
return d.WriteSync() | ||
} | ||
func (d *tmdbBatchAdapter) Close() error { return nil } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package multi | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/cosmos/cosmos-sdk/db" | ||
dbutil "github.com/cosmos/cosmos-sdk/internal/db" | ||
"github.com/cosmos/cosmos-sdk/store/rootmulti" | ||
v1 "github.com/cosmos/cosmos-sdk/store/types" | ||
v2 "github.com/cosmos/cosmos-sdk/store/v2" | ||
) | ||
|
||
var ( | ||
_ v2.CommitMultiStore = store1as2{} | ||
_ v2.Queryable = store1as2{} | ||
_ v2.CacheMultiStore = cacheStore1as2{} | ||
) | ||
|
||
type store1as2 struct { | ||
*rootmulti.Store | ||
} | ||
|
||
type cacheStore1as2 struct { | ||
v1.CacheMultiStore | ||
} | ||
|
||
// NewV1MultiStoreAsV2 constructs a v1 CommitMultiStore from v2.StoreParams | ||
func NewV1MultiStoreAsV2(database db.DBConnection, opts StoreParams) (v2.CommitMultiStore, error) { | ||
cms := rootmulti.NewStore(dbutil.ConnectionAsTmdb(database)) | ||
for name, typ := range opts.StoreSchema { | ||
switch typ { | ||
case v2.StoreTypePersistent: | ||
typ = v1.StoreTypeIAVL | ||
} | ||
skey, err := opts.storeKey(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cms.MountStoreWithDB(skey, typ, nil) | ||
} | ||
cms.SetPruning(opts.Pruning) | ||
err := cms.SetInitialVersion(int64(opts.InitialVersion)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = cms.LoadLatestVersionAndUpgrade(opts.Upgrades) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for skey, ls := range opts.listeners { | ||
cms.AddListeners(skey, ls) | ||
} | ||
cms.SetTracer(opts.TraceWriter) | ||
cms.SetTracingContext(opts.TraceContext) | ||
return store1as2{cms}, nil | ||
} | ||
|
||
func WrapStoreAsV2CommitMultiStore(s *rootmulti.Store) v2.CommitMultiStore { | ||
return store1as2{s} | ||
} | ||
|
||
func WrapCacheStoreAsV2CacheMultiStore(cs v1.CacheMultiStore) v2.CacheMultiStore { | ||
return cacheStore1as2{cs} | ||
} | ||
|
||
// commit store | ||
|
||
func (s store1as2) CacheWrap() v2.CacheMultiStore { | ||
return cacheStore1as2{s.CacheMultiStore()} | ||
} | ||
func (s store1as2) GetVersion(ver int64) (v2.MultiStore, error) { | ||
ret, err := s.CacheMultiStoreWithVersion(ver) | ||
return cacheStore1as2{ret}, err | ||
} | ||
func (s store1as2) Close() error { return nil } | ||
func (s store1as2) SetInitialVersion(ver uint64) error { | ||
return s.Store.SetInitialVersion(int64(ver)) | ||
} | ||
|
||
func (s store1as2) SetTracer(w io.Writer) { s.SetTracer(w) } | ||
func (s store1as2) SetTracingContext(tc v2.TraceContext) { s.SetTracingContext(tc) } | ||
|
||
// cache store | ||
|
||
func (s cacheStore1as2) CacheWrap() v2.CacheMultiStore { | ||
return cacheStore1as2{s.CacheMultiStore.CacheMultiStore()} | ||
} | ||
|
||
func (s cacheStore1as2) SetTracer(w io.Writer) { s.SetTracer(w) } | ||
func (s cacheStore1as2) SetTracingContext(tc v2.TraceContext) { s.SetTracingContext(tc) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters