table.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. // Put inserts the given value into the database at a prefixed version of the
  46. // provided key.
  47. func (t *table) Put(key []byte, value []byte) error {
  48. return t.db.Put(append([]byte(t.prefix), key...), value)
  49. }
  50. // Delete removes the given prefixed key from the database.
  51. func (t *table) Delete(key []byte) error {
  52. return t.db.Delete(append([]byte(t.prefix), key...))
  53. }
  54. // NewIterator creates a binary-alphabetical iterator over the entire keyspace
  55. // contained within the database.
  56. func (t *table) NewIterator() ethdb.Iterator {
  57. return t.NewIteratorWithPrefix(nil)
  58. }
  59. // NewIteratorWithPrefix creates a binary-alphabetical iterator over a subset
  60. // of database content with a particular key prefix.
  61. func (t *table) NewIteratorWithPrefix(prefix []byte) ethdb.Iterator {
  62. return t.db.NewIteratorWithPrefix(append([]byte(t.prefix), prefix...))
  63. }
  64. // Stat returns a particular internal stat of the database.
  65. func (t *table) Stat(property string) (string, error) {
  66. return t.db.Stat(property)
  67. }
  68. // Compact flattens the underlying data store for the given key range. In essence,
  69. // deleted and overwritten versions are discarded, and the data is rearranged to
  70. // reduce the cost of operations needed to access them.
  71. //
  72. // A nil start is treated as a key before all keys in the data store; a nil limit
  73. // is treated as a key after all keys in the data store. If both is nil then it
  74. // will compact entire data store.
  75. func (t *table) Compact(start []byte, limit []byte) error {
  76. // If no start was specified, use the table prefix as the first value
  77. if start == nil {
  78. start = []byte(t.prefix)
  79. }
  80. // If no limit was specified, use the first element not matching the prefix
  81. // as the limit
  82. if limit == nil {
  83. limit = []byte(t.prefix)
  84. for i := len(limit) - 1; i >= 0; i-- {
  85. // Bump the current character, stopping if it doesn't overflow
  86. limit[i]++
  87. if limit[i] > 0 {
  88. break
  89. }
  90. // Character overflown, proceed to the next or nil if the last
  91. if i == 0 {
  92. limit = nil
  93. }
  94. }
  95. }
  96. // Range correctly calculated based on table prefix, delegate down
  97. return t.db.Compact(start, limit)
  98. }
  99. // NewBatch creates a write-only database that buffers changes to its host db
  100. // until a final write is called, each operation prefixing all keys with the
  101. // pre-configured string.
  102. func (t *table) NewBatch() ethdb.Batch {
  103. return &tableBatch{t.db.NewBatch(), t.prefix}
  104. }
  105. // tableBatch is a wrapper around a database batch that prefixes each key access
  106. // with a pre-configured string.
  107. type tableBatch struct {
  108. batch ethdb.Batch
  109. prefix string
  110. }
  111. // Put inserts the given value into the batch for later committing.
  112. func (b *tableBatch) Put(key, value []byte) error {
  113. return b.batch.Put(append([]byte(b.prefix), key...), value)
  114. }
  115. // Delete inserts the a key removal into the batch for later committing.
  116. func (b *tableBatch) Delete(key []byte) error {
  117. return b.batch.Delete(append([]byte(b.prefix), key...))
  118. }
  119. // ValueSize retrieves the amount of data queued up for writing.
  120. func (b *tableBatch) ValueSize() int {
  121. return b.batch.ValueSize()
  122. }
  123. // Write flushes any accumulated data to disk.
  124. func (b *tableBatch) Write() error {
  125. return b.batch.Write()
  126. }
  127. // Reset resets the batch for reuse.
  128. func (b *tableBatch) Reset() {
  129. b.batch.Reset()
  130. }
  131. // Replay replays the batch contents.
  132. func (b *tableBatch) Replay(w ethdb.Writer) error {
  133. return b.batch.Replay(w)
  134. }