github.com/ethereum/go-ethereum@v1.16.1/ethdb/database.go (about) 1 // Copyright 2014 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 ethdb defines the interfaces for an Ethereum data store. 18 package ethdb 19 20 import ( 21 "bytes" 22 "errors" 23 "io" 24 ) 25 26 var ( 27 // MaximumKey is a special marker representing the largest possible key 28 // in the database. 29 // 30 // All prefixed database entries will be smaller than this marker. 31 // For trie nodes in hash mode, we use a 32-byte slice filled with 0xFF 32 // because there may be shared prefixes starting with multiple 0xFF bytes. 33 // Using 32 bytes ensures that only a hash collision could potentially 34 // match or exceed it. 35 MaximumKey = bytes.Repeat([]byte{0xff}, 32) 36 ) 37 38 // KeyValueReader wraps the Has and Get method of a backing data store. 39 type KeyValueReader interface { 40 // Has retrieves if a key is present in the key-value data store. 41 Has(key []byte) (bool, error) 42 43 // Get retrieves the given key if it's present in the key-value data store. 44 Get(key []byte) ([]byte, error) 45 } 46 47 // KeyValueWriter wraps the Put method of a backing data store. 48 type KeyValueWriter interface { 49 // Put inserts the given value into the key-value data store. 50 Put(key []byte, value []byte) error 51 52 // Delete removes the key from the key-value data store. 53 Delete(key []byte) error 54 } 55 56 var ErrTooManyKeys = errors.New("too many keys in deleted range") 57 58 // KeyValueRangeDeleter wraps the DeleteRange method of a backing data store. 59 type KeyValueRangeDeleter interface { 60 // DeleteRange deletes all of the keys (and values) in the range [start,end) 61 // (inclusive on start, exclusive on end). 62 // 63 // A nil start is treated as a key before all keys in the data store; a nil 64 // end is treated as a key after all keys in the data store. If both is nil 65 // then the entire data store will be purged. 66 // 67 // Some implementations of DeleteRange may return ErrTooManyKeys after 68 // partially deleting entries in the given range. 69 DeleteRange(start, end []byte) error 70 } 71 72 // KeyValueStater wraps the Stat method of a backing data store. 73 type KeyValueStater interface { 74 // Stat returns the statistic data of the database. 75 Stat() (string, error) 76 } 77 78 // KeyValueSyncer wraps the SyncKeyValue method of a backing data store. 79 type KeyValueSyncer interface { 80 // SyncKeyValue ensures that all pending writes are flushed to disk, 81 // guaranteeing data durability up to the point. 82 SyncKeyValue() error 83 } 84 85 // Compacter wraps the Compact method of a backing data store. 86 type Compacter interface { 87 // Compact flattens the underlying data store for the given key range. In essence, 88 // deleted and overwritten versions are discarded, and the data is rearranged to 89 // reduce the cost of operations needed to access them. 90 // 91 // A nil start is treated as a key before all keys in the data store; a nil limit 92 // is treated as a key after all keys in the data store. If both is nil then it 93 // will compact entire data store. 94 Compact(start []byte, limit []byte) error 95 } 96 97 // KeyValueStore contains all the methods required to allow handling different 98 // key-value data stores backing the high level database. 99 type KeyValueStore interface { 100 KeyValueReader 101 KeyValueWriter 102 KeyValueStater 103 KeyValueSyncer 104 KeyValueRangeDeleter 105 Batcher 106 Iteratee 107 Compacter 108 io.Closer 109 } 110 111 // AncientReaderOp contains the methods required to read from immutable ancient data. 112 type AncientReaderOp interface { 113 // Ancient retrieves an ancient binary blob from the append-only immutable files. 114 Ancient(kind string, number uint64) ([]byte, error) 115 116 // AncientRange retrieves multiple items in sequence, starting from the index 'start'. 117 // It will return 118 // - at most 'count' items, 119 // - if maxBytes is specified: at least 1 item (even if exceeding the maxByteSize), 120 // but will otherwise return as many items as fit into maxByteSize. 121 // - if maxBytes is not specified, 'count' items will be returned if they are present 122 AncientRange(kind string, start, count, maxBytes uint64) ([][]byte, error) 123 124 // Ancients returns the ancient item numbers in the ancient store. 125 Ancients() (uint64, error) 126 127 // Tail returns the number of first stored item in the ancient store. 128 // This number can also be interpreted as the total deleted items. 129 Tail() (uint64, error) 130 131 // AncientSize returns the ancient size of the specified category. 132 AncientSize(kind string) (uint64, error) 133 } 134 135 // AncientReader is the extended ancient reader interface including 'batched' or 'atomic' reading. 136 type AncientReader interface { 137 AncientReaderOp 138 139 // ReadAncients runs the given read operation while ensuring that no writes take place 140 // on the underlying ancient store. 141 ReadAncients(fn func(AncientReaderOp) error) (err error) 142 } 143 144 // AncientWriter contains the methods required to write to immutable ancient data. 145 type AncientWriter interface { 146 // ModifyAncients runs a write operation on the ancient store. 147 // If the function returns an error, any changes to the underlying store are reverted. 148 // The integer return value is the total size of the written data. 149 ModifyAncients(func(AncientWriteOp) error) (int64, error) 150 151 // SyncAncient flushes all in-memory ancient store data to disk. 152 SyncAncient() error 153 154 // TruncateHead discards all but the first n ancient data from the ancient store. 155 // After the truncation, the latest item can be accessed it item_n-1(start from 0). 156 TruncateHead(n uint64) (uint64, error) 157 158 // TruncateTail discards the first n ancient data from the ancient store. The already 159 // deleted items are ignored. After the truncation, the earliest item can be accessed 160 // is item_n(start from 0). The deleted items may not be removed from the ancient store 161 // immediately, but only when the accumulated deleted data reach the threshold then 162 // will be removed all together. 163 // 164 // Note that data marked as non-prunable will still be retained and remain accessible. 165 TruncateTail(n uint64) (uint64, error) 166 } 167 168 // AncientWriteOp is given to the function argument of ModifyAncients. 169 type AncientWriteOp interface { 170 // Append adds an RLP-encoded item. 171 Append(kind string, number uint64, item interface{}) error 172 173 // AppendRaw adds an item without RLP-encoding it. 174 AppendRaw(kind string, number uint64, item []byte) error 175 } 176 177 // AncientStater wraps the Stat method of a backing ancient store. 178 type AncientStater interface { 179 // AncientDatadir returns the path of the ancient store directory. 180 // 181 // If the ancient store is not activated, an error is returned. 182 // If an ephemeral ancient store is used, an empty path is returned. 183 // 184 // The path returned by AncientDatadir can be used as the root path 185 // of the ancient store to construct paths for other sub ancient stores. 186 AncientDatadir() (string, error) 187 } 188 189 // Reader contains the methods required to read data from both key-value as well as 190 // immutable ancient data. 191 type Reader interface { 192 KeyValueReader 193 AncientReader 194 } 195 196 // AncientStore contains all the methods required to allow handling different 197 // ancient data stores backing immutable data store. 198 type AncientStore interface { 199 AncientReader 200 AncientWriter 201 AncientStater 202 io.Closer 203 } 204 205 // ResettableAncientStore extends the AncientStore interface by adding a Reset method. 206 type ResettableAncientStore interface { 207 AncientStore 208 209 // Reset is designed to reset the entire ancient store to its default state. 210 Reset() error 211 } 212 213 // Database contains all the methods required by the high level database to not 214 // only access the key-value data store but also the ancient chain store. 215 type Database interface { 216 KeyValueStore 217 AncientStore 218 }