github.com/iotexproject/iotex-core@v1.14.1-rc1/action/protocol/kvstorefortrie.go (about) 1 // Copyright (c) 2019 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package protocol 7 8 import ( 9 "context" 10 11 "github.com/pkg/errors" 12 13 "github.com/iotexproject/iotex-core/db" 14 "github.com/iotexproject/iotex-core/db/trie" 15 "github.com/iotexproject/iotex-core/state" 16 ) 17 18 type ( 19 kvStoreForTrie struct { 20 nsOpt StateOption 21 sm StateManager 22 } 23 kvStoreForTrieWithStateReader struct { 24 nsOpt StateOption 25 sr StateReader 26 } 27 ) 28 29 // NewKVStoreForTrieWithStateManager creates a trie.KVStore with state manager 30 func NewKVStoreForTrieWithStateManager(ns string, sm StateManager) trie.KVStore { 31 return &kvStoreForTrie{nsOpt: NamespaceOption(ns), sm: sm} 32 } 33 34 func (kv *kvStoreForTrie) Start(context.Context) error { 35 return nil 36 } 37 38 func (kv *kvStoreForTrie) Stop(context.Context) error { 39 return nil 40 } 41 42 func (kv *kvStoreForTrie) Put(key []byte, value []byte) error { 43 var sb SerializableBytes 44 sb = make([]byte, len(value)) 45 copy(sb, value) 46 _, err := kv.sm.PutState(sb, KeyOption(key), kv.nsOpt) 47 48 return err 49 } 50 51 func (kv *kvStoreForTrie) Delete(key []byte) error { 52 _, err := kv.sm.DelState(KeyOption(key), kv.nsOpt) 53 if errors.Cause(err) == state.ErrStateNotExist { 54 return nil 55 } 56 return err 57 } 58 59 func (kv *kvStoreForTrie) Get(key []byte) ([]byte, error) { 60 var value SerializableBytes 61 _, err := kv.sm.State(&value, KeyOption(key), kv.nsOpt) 62 switch errors.Cause(err) { 63 case state.ErrStateNotExist: 64 return nil, errors.Wrapf(db.ErrNotExist, "failed to find key %x", key) 65 } 66 return value, err 67 } 68 69 // NewKVStoreForTrieWithStateReader creates a trie.KVStore with state reader 70 func NewKVStoreForTrieWithStateReader(ns string, sr StateReader) trie.KVStore { 71 return &kvStoreForTrieWithStateReader{nsOpt: NamespaceOption(ns), sr: sr} 72 } 73 74 func (kv *kvStoreForTrieWithStateReader) Start(context.Context) error { 75 return nil 76 } 77 78 func (kv *kvStoreForTrieWithStateReader) Stop(context.Context) error { 79 return nil 80 } 81 82 func (kv *kvStoreForTrieWithStateReader) Put(key []byte, value []byte) error { 83 return errors.New("not implemented") 84 } 85 86 func (kv *kvStoreForTrieWithStateReader) Delete(key []byte) error { 87 return errors.New("not implemented") 88 } 89 90 func (kv *kvStoreForTrieWithStateReader) Get(key []byte) ([]byte, error) { 91 var value SerializableBytes 92 _, err := kv.sr.State(&value, KeyOption(key), kv.nsOpt) 93 switch errors.Cause(err) { 94 case state.ErrStateNotExist: 95 return nil, errors.Wrapf(db.ErrNotExist, "failed to find key %x", key) 96 } 97 return value, err 98 }