table.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. package rawdb
  17. import (
  18. "github.com/ethereum/go-ethereum/ethdb"
  19. )
  20. // table is a wrapper around a database that prefixes each key access with a pre-
  21. // configured string.
  22. type table struct {
  23. db ethdb.Database
  24. prefix string
  25. }
  26. // NewTable returns a database object that prefixes all keys with a given string.
  27. func NewTable(db ethdb.Database, prefix string) ethdb.Database {
  28. return &table{
  29. db: db,
  30. prefix: prefix,
  31. }
  32. }
  33. // Close is a noop to implement the Database interface.
  34. func (t *table) Close() error {
  35. return nil
  36. }
  37. // Has retrieves if a prefixed version of a key is present in the database.
  38. func (t *table) Has(key []byte) (bool, error) {
  39. return t.db.Has(append([]byte(t.prefix), key...))
  40. }
  41. // Get retrieves the given prefixed key if it's present in the database.
  42. func (t *table) Get(key []byte) ([]byte, error) {
  43. return t.db.Get(append([]byte(t.prefix), key...))
  44. }
  45. // HasAncient is a noop passthrough that just forwards the request to the underlying
  46. // database.
  47. func (t *table) HasAncient(kind string, number uint64) (bool, error) {
  48. return t.db.HasAncient(kind, number)
  49. }
  50. // Ancient is a noop passthrough that just forwards the request to the underlying
  51. // database.
  52. func (t *table) Ancient(kind string, number uint64) ([]byte, error) {
  53. return t.db.Ancient(kind, number)
  54. }
  55. // AncientRange is a noop passthrough that just forwards the request to the underlying
  56. // database.
  57. func (t *table) AncientRange(kind string, start, count, maxBytes uint64) ([][]byte, error) {
  58. return t.db.AncientRange(kind, start, count, maxBytes)
  59. }
  60. // Ancients is a noop passthrough that just forwards the request to the underlying
  61. // database.
  62. func (t *table) Ancients() (uint64, error) {
  63. return t.db.Ancients()
  64. }
  65. // Tail is a noop passthrough that just forwards the request to the underlying
  66. // database.
  67. func (t *table) Tail() (uint64, error) {
  68. return t.db.Tail()
  69. }
  70. // AncientSize is a noop passthrough that just forwards the request to the underlying
  71. // database.
  72. func (t *table) AncientSize(kind string) (uint64, error) {
  73. return t.db.AncientSize(kind)
  74. }
  75. // ModifyAncients runs an ancient write operation on the underlying database.
  76. func (t *table) ModifyAncients(fn func(ethdb.AncientWriteOp) error) (int64, error) {
  77. return t.db.ModifyAncients(fn)
  78. }
  79. func (t *table) ReadAncients(fn func(reader ethdb.AncientReaderOp) error) (err error) {
  80. return t.db.ReadAncients(fn)
  81. }
  82. // TruncateHead is a noop passthrough that just forwards the request to the underlying
  83. // database.
  84. func (t *table) TruncateHead(items uint64) error {
  85. return t.db.TruncateHead(items)
  86. }
  87. // TruncateTail is a noop passthrough that just forwards the request to the underlying
  88. // database.
  89. func (t *table) TruncateTail(items uint64) error {
  90. return t.db.TruncateTail(items)
  91. }
  92. // Sync is a noop passthrough that just forwards the request to the underlying
  93. // database.
  94. func (t *table) Sync() error {
  95. return t.db.Sync()
  96. }
  97. // MigrateTable processes the entries in a given table in sequence
  98. // converting them to a new format if they're of an old format.
  99. func (t *table) MigrateTable(kind string, convert convertLegacyFn) error {
  100. return t.db.MigrateTable(kind, convert)
  101. }
  102. // AncientDatadir returns the ancient datadir of the underlying database.
  103. func (t *table) AncientDatadir() (string, error) {
  104. return t.db.AncientDatadir()
  105. }
  106. // Put inserts the given value into the database at a prefixed version of the
  107. // provided key.
  108. func (t *table) Put(key []byte, value []byte) error {
  109. return t.db.Put(append([]byte(t.prefix), key...), value)
  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. // NewIterator creates a binary-alphabetical iterator over a subset
  116. // of database content with a particular key prefix, starting at a particular
  117. // initial key (or after, if it does not exist).
  118. func (t *table) NewIterator(prefix []byte, start []byte) ethdb.Iterator {
  119. innerPrefix := append([]byte(t.prefix), prefix...)
  120. iter := t.db.NewIterator(innerPrefix, start)
  121. return &tableIterator{
  122. iter: iter,
  123. prefix: t.prefix,
  124. }
  125. }
  126. // Stat returns a particular internal stat of the database.
  127. func (t *table) Stat(property string) (string, error) {
  128. return t.db.Stat(property)
  129. }
  130. // Compact flattens the underlying data store for the given key range. In essence,
  131. // deleted and overwritten versions are discarded, and the data is rearranged to
  132. // reduce the cost of operations needed to access them.
  133. //
  134. // A nil start is treated as a key before all keys in the data store; a nil limit
  135. // is treated as a key after all keys in the data store. If both is nil then it
  136. // will compact entire data store.
  137. func (t *table) Compact(start []byte, limit []byte) error {
  138. // If no start was specified, use the table prefix as the first value
  139. if start == nil {
  140. start = []byte(t.prefix)
  141. } else {
  142. start = append([]byte(t.prefix), start...)
  143. }
  144. // If no limit was specified, use the first element not matching the prefix
  145. // as the limit
  146. if limit == nil {
  147. limit = []byte(t.prefix)
  148. for i := len(limit) - 1; i >= 0; i-- {
  149. // Bump the current character, stopping if it doesn't overflow
  150. limit[i]++
  151. if limit[i] > 0 {
  152. break
  153. }
  154. // Character overflown, proceed to the next or nil if the last
  155. if i == 0 {
  156. limit = nil
  157. }
  158. }
  159. } else {
  160. limit = append([]byte(t.prefix), limit...)
  161. }
  162. // Range correctly calculated based on table prefix, delegate down
  163. return t.db.Compact(start, limit)
  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. // NewBatchWithSize creates a write-only database batch with pre-allocated buffer.
  172. func (t *table) NewBatchWithSize(size int) ethdb.Batch {
  173. return &tableBatch{t.db.NewBatchWithSize(size), t.prefix}
  174. }
  175. // NewSnapshot creates a database snapshot based on the current state.
  176. // The created snapshot will not be affected by all following mutations
  177. // happened on the database.
  178. func (t *table) NewSnapshot() (ethdb.Snapshot, error) {
  179. return t.db.NewSnapshot()
  180. }
  181. // tableBatch is a wrapper around a database batch that prefixes each key access
  182. // with a pre-configured string.
  183. type tableBatch struct {
  184. batch ethdb.Batch
  185. prefix string
  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. // Delete inserts the a key removal into the batch for later committing.
  192. func (b *tableBatch) Delete(key []byte) error {
  193. return b.batch.Delete(append([]byte(b.prefix), key...))
  194. }
  195. // ValueSize retrieves the amount of data queued up for writing.
  196. func (b *tableBatch) ValueSize() int {
  197. return b.batch.ValueSize()
  198. }
  199. // Write flushes any accumulated data to disk.
  200. func (b *tableBatch) Write() error {
  201. return b.batch.Write()
  202. }
  203. // Reset resets the batch for reuse.
  204. func (b *tableBatch) Reset() {
  205. b.batch.Reset()
  206. }
  207. // tableReplayer is a wrapper around a batch replayer which truncates
  208. // the added prefix.
  209. type tableReplayer struct {
  210. w ethdb.KeyValueWriter
  211. prefix string
  212. }
  213. // Put implements the interface KeyValueWriter.
  214. func (r *tableReplayer) Put(key []byte, value []byte) error {
  215. trimmed := key[len(r.prefix):]
  216. return r.w.Put(trimmed, value)
  217. }
  218. // Delete implements the interface KeyValueWriter.
  219. func (r *tableReplayer) Delete(key []byte) error {
  220. trimmed := key[len(r.prefix):]
  221. return r.w.Delete(trimmed)
  222. }
  223. // Replay replays the batch contents.
  224. func (b *tableBatch) Replay(w ethdb.KeyValueWriter) error {
  225. return b.batch.Replay(&tableReplayer{w: w, prefix: b.prefix})
  226. }
  227. // tableIterator is a wrapper around a database iterator that prefixes each key access
  228. // with a pre-configured string.
  229. type tableIterator struct {
  230. iter ethdb.Iterator
  231. prefix string
  232. }
  233. // Next moves the iterator to the next key/value pair. It returns whether the
  234. // iterator is exhausted.
  235. func (iter *tableIterator) Next() bool {
  236. return iter.iter.Next()
  237. }
  238. // Error returns any accumulated error. Exhausting all the key/value pairs
  239. // is not considered to be an error.
  240. func (iter *tableIterator) Error() error {
  241. return iter.iter.Error()
  242. }
  243. // Key returns the key of the current key/value pair, or nil if done. The caller
  244. // should not modify the contents of the returned slice, and its contents may
  245. // change on the next call to Next.
  246. func (iter *tableIterator) Key() []byte {
  247. key := iter.iter.Key()
  248. if key == nil {
  249. return nil
  250. }
  251. return key[len(iter.prefix):]
  252. }
  253. // Value returns the value of the current key/value pair, or nil if done. The
  254. // caller should not modify the contents of the returned slice, and its contents
  255. // may change on the next call to Next.
  256. func (iter *tableIterator) Value() []byte {
  257. return iter.iter.Value()
  258. }
  259. // Release releases associated resources. Release should always succeed and can
  260. // be called multiple times without causing error.
  261. func (iter *tableIterator) Release() {
  262. iter.iter.Release()
  263. }