github.com/Finschia/finschia-sdk@v0.48.1/types/store.go (about) 1 package types 2 3 import ( 4 fmt "fmt" 5 "sort" 6 "strings" 7 8 "github.com/Finschia/finschia-sdk/store/types" 9 "github.com/Finschia/finschia-sdk/types/kv" 10 ) 11 12 type ( 13 PruningOptions = types.PruningOptions 14 ) 15 16 type ( 17 Store = types.Store 18 Committer = types.Committer 19 CommitStore = types.CommitStore 20 Queryable = types.Queryable 21 MultiStore = types.MultiStore 22 CacheMultiStore = types.CacheMultiStore 23 CommitMultiStore = types.CommitMultiStore 24 MultiStorePersistentCache = types.MultiStorePersistentCache 25 KVStore = types.KVStore 26 Iterator = types.Iterator 27 ) 28 29 // StoreDecoderRegistry defines each of the modules store decoders. Used for ImportExport 30 // simulation. 31 type StoreDecoderRegistry map[string]func(kvA, kvB kv.Pair) string 32 33 // Iterator over all the keys with a certain prefix in ascending order 34 func KVStorePrefixIterator(kvs KVStore, prefix []byte) Iterator { 35 return types.KVStorePrefixIterator(kvs, prefix) 36 } 37 38 // Iterator over all the keys with a certain prefix in descending order. 39 func KVStoreReversePrefixIterator(kvs KVStore, prefix []byte) Iterator { 40 return types.KVStoreReversePrefixIterator(kvs, prefix) 41 } 42 43 // KVStorePrefixIteratorPaginated returns iterator over items in the selected page. 44 // Items iterated and skipped in ascending order. 45 func KVStorePrefixIteratorPaginated(kvs KVStore, prefix []byte, page, limit uint) Iterator { 46 return types.KVStorePrefixIteratorPaginated(kvs, prefix, page, limit) 47 } 48 49 // KVStoreReversePrefixIteratorPaginated returns iterator over items in the selected page. 50 // Items iterated and skipped in descending order. 51 func KVStoreReversePrefixIteratorPaginated(kvs KVStore, prefix []byte, page, limit uint) Iterator { 52 return types.KVStoreReversePrefixIteratorPaginated(kvs, prefix, page, limit) 53 } 54 55 // DiffKVStores compares two KVstores and returns all the key/value pairs 56 // that differ from one another. It also skips value comparison for a set of provided prefixes 57 func DiffKVStores(a KVStore, b KVStore, prefixesToSkip [][]byte) (kvAs, kvBs []kv.Pair) { 58 return types.DiffKVStores(a, b, prefixesToSkip) 59 } 60 61 type ( 62 CacheKVStore = types.CacheKVStore 63 CommitKVStore = types.CommitKVStore 64 CacheWrap = types.CacheWrap 65 CacheWrapper = types.CacheWrapper 66 CommitID = types.CommitID 67 ) 68 69 type StoreType = types.StoreType 70 71 const ( 72 StoreTypeMulti = types.StoreTypeMulti 73 StoreTypeDB = types.StoreTypeDB 74 StoreTypeIAVL = types.StoreTypeIAVL 75 StoreTypeTransient = types.StoreTypeTransient 76 StoreTypeMemory = types.StoreTypeMemory 77 ) 78 79 type ( 80 StoreKey = types.StoreKey 81 CapabilityKey = types.CapabilityKey 82 KVStoreKey = types.KVStoreKey 83 TransientStoreKey = types.TransientStoreKey 84 MemoryStoreKey = types.MemoryStoreKey 85 ) 86 87 // assertNoCommonPrefix will panic if there are two keys: k1 and k2 in keys, such that 88 // k1 is a prefix of k2 89 func assertNoPrefix(keys []string) { 90 sorted := make([]string, len(keys)) 91 copy(sorted, keys) 92 sort.Strings(sorted) 93 for i := 1; i < len(sorted); i++ { 94 if strings.HasPrefix(sorted[i], sorted[i-1]) { 95 panic(fmt.Sprint("Potential key collision between KVStores:", sorted[i], " - ", sorted[i-1])) 96 } 97 } 98 } 99 100 // NewKVStoreKey returns a new pointer to a KVStoreKey. 101 func NewKVStoreKey(name string) *KVStoreKey { 102 return types.NewKVStoreKey(name) 103 } 104 105 // NewKVStoreKeys returns a map of new pointers to KVStoreKey's. 106 // The function will panic if there is a potential conflict in names (see `assertNoPrefix` 107 // function for more details). 108 func NewKVStoreKeys(names ...string) map[string]*KVStoreKey { 109 assertNoPrefix(names) 110 keys := make(map[string]*KVStoreKey, len(names)) 111 for _, n := range names { 112 keys[n] = NewKVStoreKey(n) 113 } 114 115 return keys 116 } 117 118 // Constructs new TransientStoreKey 119 // Must return a pointer according to the ocap principle 120 func NewTransientStoreKey(name string) *types.TransientStoreKey { 121 return types.NewTransientStoreKey(name) 122 } 123 124 // NewTransientStoreKeys constructs a new map of TransientStoreKey's 125 // Must return pointers according to the ocap principle 126 // The function will panic if there is a potential conflict in names (see `assertNoPrefix` 127 // function for more details). 128 func NewTransientStoreKeys(names ...string) map[string]*TransientStoreKey { 129 assertNoPrefix(names) 130 keys := make(map[string]*TransientStoreKey) 131 for _, n := range names { 132 keys[n] = NewTransientStoreKey(n) 133 } 134 135 return keys 136 } 137 138 // NewMemoryStoreKeys constructs a new map matching store key names to their 139 // respective MemoryStoreKey references. 140 // The function will panic if there is a potential conflict in names (see `assertNoPrefix` 141 // function for more details). 142 func NewMemoryStoreKeys(names ...string) map[string]*MemoryStoreKey { 143 assertNoPrefix(names) 144 keys := make(map[string]*MemoryStoreKey) 145 for _, n := range names { 146 keys[n] = types.NewMemoryStoreKey(n) 147 } 148 149 return keys 150 } 151 152 // PrefixEndBytes returns the []byte that would end a 153 // range query for all []byte with a certain prefix 154 // Deals with last byte of prefix being FF without overflowing 155 func PrefixEndBytes(prefix []byte) []byte { 156 return types.PrefixEndBytes(prefix) 157 } 158 159 // InclusiveEndBytes returns the []byte that would end a 160 // range query such that the input would be included 161 func InclusiveEndBytes(inclusiveBytes []byte) (exclusiveBytes []byte) { 162 return types.InclusiveEndBytes(inclusiveBytes) 163 } 164 165 //---------------------------------------- 166 167 // key-value result for iterator queries 168 type KVPair = types.KVPair 169 170 //---------------------------------------- 171 172 // TraceContext contains TraceKVStore context data. It will be written with 173 // every trace operation. 174 type TraceContext = types.TraceContext 175 176 // -------------------------------------- 177 178 type ( 179 Gas = types.Gas 180 GasMeter = types.GasMeter 181 GasConfig = types.GasConfig 182 ) 183 184 func NewGasMeter(limit Gas) GasMeter { 185 return types.NewGasMeter(limit) 186 } 187 188 type ( 189 ErrorOutOfGas = types.ErrorOutOfGas 190 ErrorGasOverflow = types.ErrorGasOverflow 191 ) 192 193 func NewInfiniteGasMeter() GasMeter { 194 return types.NewInfiniteGasMeter() 195 }