github.com/fff-chain/go-fff@v0.0.0-20220726032732-1c84420b8a99/core/rawdb/table.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package rawdb 18 19 import ( 20 "github.com/fff-chain/go-fff/ethdb" 21 ) 22 23 // table is a wrapper around a database that prefixes each key access with a pre- 24 // configured string. 25 type table struct { 26 db ethdb.Database 27 prefix string 28 } 29 30 // NewTable returns a database object that prefixes all keys with a given string. 31 func NewTable(db ethdb.Database, prefix string) ethdb.Database { 32 return &table{ 33 db: db, 34 prefix: prefix, 35 } 36 } 37 38 // Close is a noop to implement the Database interface. 39 func (t *table) Close() error { 40 return nil 41 } 42 43 // Has retrieves if a prefixed version of a key is present in the database. 44 func (t *table) Has(key []byte) (bool, error) { 45 return t.db.Has(append([]byte(t.prefix), key...)) 46 } 47 48 // Get retrieves the given prefixed key if it's present in the database. 49 func (t *table) Get(key []byte) ([]byte, error) { 50 return t.db.Get(append([]byte(t.prefix), key...)) 51 } 52 53 // HasAncient is a noop passthrough that just forwards the request to the underlying 54 // database. 55 func (t *table) HasAncient(kind string, number uint64) (bool, error) { 56 return t.db.HasAncient(kind, number) 57 } 58 59 // Ancient is a noop passthrough that just forwards the request to the underlying 60 // database. 61 func (t *table) Ancient(kind string, number uint64) ([]byte, error) { 62 return t.db.Ancient(kind, number) 63 } 64 65 // Ancients is a noop passthrough that just forwards the request to the underlying 66 // database. 67 func (t *table) Ancients() (uint64, error) { 68 return t.db.Ancients() 69 } 70 71 // ItemAmountInAncient returns the actual length of current ancientDB. 72 func (t *table) ItemAmountInAncient() (uint64, error) { 73 return t.db.ItemAmountInAncient() 74 } 75 76 // AncientOffSet returns the offset of current ancientDB. 77 func (t *table) AncientOffSet() uint64 { 78 return t.db.AncientOffSet() 79 } 80 81 // AncientSize is a noop passthrough that just forwards the request to the underlying 82 // database. 83 func (t *table) AncientSize(kind string) (uint64, error) { 84 return t.db.AncientSize(kind) 85 } 86 87 // AppendAncient is a noop passthrough that just forwards the request to the underlying 88 // database. 89 func (t *table) AppendAncient(number uint64, hash, header, body, receipts, td []byte) error { 90 return t.db.AppendAncient(number, hash, header, body, receipts, td) 91 } 92 93 // TruncateAncients is a noop passthrough that just forwards the request to the underlying 94 // database. 95 func (t *table) TruncateAncients(items uint64) error { 96 return t.db.TruncateAncients(items) 97 } 98 99 // Sync is a noop passthrough that just forwards the request to the underlying 100 // database. 101 func (t *table) Sync() error { 102 return t.db.Sync() 103 } 104 105 // Put inserts the given value into the database at a prefixed version of the 106 // provided key. 107 func (t *table) Put(key []byte, value []byte) error { 108 return t.db.Put(append([]byte(t.prefix), key...), value) 109 } 110 111 // Delete removes the given prefixed key from the database. 112 func (t *table) Delete(key []byte) error { 113 return t.db.Delete(append([]byte(t.prefix), key...)) 114 } 115 116 // NewIterator creates a binary-alphabetical iterator over a subset 117 // of database content with a particular key prefix, starting at a particular 118 // initial key (or after, if it does not exist). 119 func (t *table) NewIterator(prefix []byte, start []byte) ethdb.Iterator { 120 innerPrefix := append([]byte(t.prefix), prefix...) 121 iter := t.db.NewIterator(innerPrefix, start) 122 return &tableIterator{ 123 iter: iter, 124 prefix: t.prefix, 125 } 126 } 127 128 // Stat returns a particular internal stat of the database. 129 func (t *table) Stat(property string) (string, error) { 130 return t.db.Stat(property) 131 } 132 133 // Compact flattens the underlying data store for the given key range. In essence, 134 // deleted and overwritten versions are discarded, and the data is rearranged to 135 // reduce the cost of operations needed to access them. 136 // 137 // A nil start is treated as a key before all keys in the data store; a nil limit 138 // is treated as a key after all keys in the data store. If both is nil then it 139 // will compact entire data store. 140 func (t *table) Compact(start []byte, limit []byte) error { 141 // If no start was specified, use the table prefix as the first value 142 if start == nil { 143 start = []byte(t.prefix) 144 } 145 // If no limit was specified, use the first element not matching the prefix 146 // as the limit 147 if limit == nil { 148 limit = []byte(t.prefix) 149 for i := len(limit) - 1; i >= 0; i-- { 150 // Bump the current character, stopping if it doesn't overflow 151 limit[i]++ 152 if limit[i] > 0 { 153 break 154 } 155 // Character overflown, proceed to the next or nil if the last 156 if i == 0 { 157 limit = nil 158 } 159 } 160 } 161 // Range correctly calculated based on table prefix, delegate down 162 return t.db.Compact(start, limit) 163 } 164 165 // NewBatch creates a write-only database that buffers changes to its host db 166 // until a final write is called, each operation prefixing all keys with the 167 // pre-configured string. 168 func (t *table) NewBatch() ethdb.Batch { 169 return &tableBatch{t.db.NewBatch(), t.prefix} 170 } 171 172 func (t *table) DiffStore() ethdb.KeyValueStore { 173 return nil 174 } 175 176 func (t *table) SetDiffStore(diff ethdb.KeyValueStore) { 177 panic("not implement") 178 } 179 180 // tableBatch is a wrapper around a database batch that prefixes each key access 181 // with a pre-configured string. 182 type tableBatch struct { 183 batch ethdb.Batch 184 prefix string 185 } 186 187 // Put inserts the given value into the batch for later committing. 188 func (b *tableBatch) Put(key, value []byte) error { 189 return b.batch.Put(append([]byte(b.prefix), key...), value) 190 } 191 192 // Delete inserts the a key removal into the batch for later committing. 193 func (b *tableBatch) Delete(key []byte) error { 194 return b.batch.Delete(append([]byte(b.prefix), key...)) 195 } 196 197 // ValueSize retrieves the amount of data queued up for writing. 198 func (b *tableBatch) ValueSize() int { 199 return b.batch.ValueSize() 200 } 201 202 // Write flushes any accumulated data to disk. 203 func (b *tableBatch) Write() error { 204 return b.batch.Write() 205 } 206 207 // Reset resets the batch for reuse. 208 func (b *tableBatch) Reset() { 209 b.batch.Reset() 210 } 211 212 // tableReplayer is a wrapper around a batch replayer which truncates 213 // the added prefix. 214 type tableReplayer struct { 215 w ethdb.KeyValueWriter 216 prefix string 217 } 218 219 // Put implements the interface KeyValueWriter. 220 func (r *tableReplayer) Put(key []byte, value []byte) error { 221 trimmed := key[len(r.prefix):] 222 return r.w.Put(trimmed, value) 223 } 224 225 // Delete implements the interface KeyValueWriter. 226 func (r *tableReplayer) Delete(key []byte) error { 227 trimmed := key[len(r.prefix):] 228 return r.w.Delete(trimmed) 229 } 230 231 // Replay replays the batch contents. 232 func (b *tableBatch) Replay(w ethdb.KeyValueWriter) error { 233 return b.batch.Replay(&tableReplayer{w: w, prefix: b.prefix}) 234 } 235 236 // tableIterator is a wrapper around a database iterator that prefixes each key access 237 // with a pre-configured string. 238 type tableIterator struct { 239 iter ethdb.Iterator 240 prefix string 241 } 242 243 // Next moves the iterator to the next key/value pair. It returns whether the 244 // iterator is exhausted. 245 func (iter *tableIterator) Next() bool { 246 return iter.iter.Next() 247 } 248 249 // Error returns any accumulated error. Exhausting all the key/value pairs 250 // is not considered to be an error. 251 func (iter *tableIterator) Error() error { 252 return iter.iter.Error() 253 } 254 255 // Key returns the key of the current key/value pair, or nil if done. The caller 256 // should not modify the contents of the returned slice, and its contents may 257 // change on the next call to Next. 258 func (iter *tableIterator) Key() []byte { 259 key := iter.iter.Key() 260 if key == nil { 261 return nil 262 } 263 return key[len(iter.prefix):] 264 } 265 266 // Value returns the value of the current key/value pair, or nil if done. The 267 // caller should not modify the contents of the returned slice, and its contents 268 // may change on the next call to Next. 269 func (iter *tableIterator) Value() []byte { 270 return iter.iter.Value() 271 } 272 273 // Release releases associated resources. Release should always succeed and can 274 // be called multiple times without causing error. 275 func (iter *tableIterator) Release() { 276 iter.iter.Release() 277 }