database.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. // Package ethdb defines the interfaces for an Ethereum data store.
  17. package ethdb
  18. import "io"
  19. // KeyValueReader wraps the Has and Get method of a backing data store.
  20. type KeyValueReader interface {
  21. // Has retrieves if a key is present in the key-value data store.
  22. Has(key []byte) (bool, error)
  23. // Get retrieves the given key if it's present in the key-value data store.
  24. Get(key []byte) ([]byte, error)
  25. }
  26. // KeyValueWriter wraps the Put method of a backing data store.
  27. type KeyValueWriter interface {
  28. // Put inserts the given value into the key-value data store.
  29. Put(key []byte, value []byte) error
  30. // Delete removes the key from the key-value data store.
  31. Delete(key []byte) error
  32. }
  33. // KeyValueStater wraps the Stat method of a backing data store.
  34. type KeyValueStater interface {
  35. // Stat returns a particular internal stat of the database.
  36. Stat(property string) (string, error)
  37. }
  38. // Compacter wraps the Compact method of a backing data store.
  39. type Compacter interface {
  40. // Compact flattens the underlying data store for the given key range. In essence,
  41. // deleted and overwritten versions are discarded, and the data is rearranged to
  42. // reduce the cost of operations needed to access them.
  43. //
  44. // A nil start is treated as a key before all keys in the data store; a nil limit
  45. // is treated as a key after all keys in the data store. If both is nil then it
  46. // will compact entire data store.
  47. Compact(start []byte, limit []byte) error
  48. }
  49. // KeyValueStore contains all the methods required to allow handling different
  50. // key-value data stores backing the high level database.
  51. type KeyValueStore interface {
  52. KeyValueReader
  53. KeyValueWriter
  54. KeyValueStater
  55. Batcher
  56. Iteratee
  57. Compacter
  58. Snapshotter
  59. io.Closer
  60. }
  61. // AncientReaderOp contains the methods required to read from immutable ancient data.
  62. type AncientReaderOp interface {
  63. // HasAncient returns an indicator whether the specified data exists in the
  64. // ancient store.
  65. HasAncient(kind string, number uint64) (bool, error)
  66. // Ancient retrieves an ancient binary blob from the append-only immutable files.
  67. Ancient(kind string, number uint64) ([]byte, error)
  68. // AncientRange retrieves multiple items in sequence, starting from the index 'start'.
  69. // It will return
  70. // - at most 'count' items,
  71. // - at least 1 item (even if exceeding the maxBytes), but will otherwise
  72. // return as many items as fit into maxBytes.
  73. AncientRange(kind string, start, count, maxBytes uint64) ([][]byte, error)
  74. // Ancients returns the ancient item numbers in the ancient store.
  75. Ancients() (uint64, error)
  76. // Tail returns the number of first stored item in the freezer.
  77. // This number can also be interpreted as the total deleted item numbers.
  78. Tail() (uint64, error)
  79. // AncientSize returns the ancient size of the specified category.
  80. AncientSize(kind string) (uint64, error)
  81. }
  82. // AncientReader is the extended ancient reader interface including 'batched' or 'atomic' reading.
  83. type AncientReader interface {
  84. AncientReaderOp
  85. // ReadAncients runs the given read operation while ensuring that no writes take place
  86. // on the underlying freezer.
  87. ReadAncients(fn func(AncientReaderOp) error) (err error)
  88. }
  89. // AncientWriter contains the methods required to write to immutable ancient data.
  90. type AncientWriter interface {
  91. // ModifyAncients runs a write operation on the ancient store.
  92. // If the function returns an error, any changes to the underlying store are reverted.
  93. // The integer return value is the total size of the written data.
  94. ModifyAncients(func(AncientWriteOp) error) (int64, error)
  95. // TruncateHead discards all but the first n ancient data from the ancient store.
  96. // After the truncation, the latest item can be accessed it item_n-1(start from 0).
  97. TruncateHead(n uint64) error
  98. // TruncateTail discards the first n ancient data from the ancient store. The already
  99. // deleted items are ignored. After the truncation, the earliest item can be accessed
  100. // is item_n(start from 0). The deleted items may not be removed from the ancient store
  101. // immediately, but only when the accumulated deleted data reach the threshold then
  102. // will be removed all together.
  103. TruncateTail(n uint64) error
  104. // Sync flushes all in-memory ancient store data to disk.
  105. Sync() error
  106. // MigrateTable processes and migrates entries of a given table to a new format.
  107. // The second argument is a function that takes a raw entry and returns it
  108. // in the newest format.
  109. MigrateTable(string, func([]byte) ([]byte, error)) error
  110. }
  111. // AncientWriteOp is given to the function argument of ModifyAncients.
  112. type AncientWriteOp interface {
  113. // Append adds an RLP-encoded item.
  114. Append(kind string, number uint64, item interface{}) error
  115. // AppendRaw adds an item without RLP-encoding it.
  116. AppendRaw(kind string, number uint64, item []byte) error
  117. }
  118. // AncientStater wraps the Stat method of a backing data store.
  119. type AncientStater interface {
  120. // AncientDatadir returns the path of root ancient directory. Empty string
  121. // will be returned if ancient store is not enabled at all. The returned
  122. // path can be used to construct the path of other freezers.
  123. AncientDatadir() (string, error)
  124. }
  125. // Reader contains the methods required to read data from both key-value as well as
  126. // immutable ancient data.
  127. type Reader interface {
  128. KeyValueReader
  129. AncientReader
  130. }
  131. // Writer contains the methods required to write data to both key-value as well as
  132. // immutable ancient data.
  133. type Writer interface {
  134. KeyValueWriter
  135. AncientWriter
  136. }
  137. // Stater contains the methods required to retrieve states from both key-value as well as
  138. // immutable ancient data.
  139. type Stater interface {
  140. KeyValueStater
  141. AncientStater
  142. }
  143. // AncientStore contains all the methods required to allow handling different
  144. // ancient data stores backing immutable chain data store.
  145. type AncientStore interface {
  146. AncientReader
  147. AncientWriter
  148. io.Closer
  149. }
  150. // Database contains all the methods required by the high level database to not
  151. // only access the key-value data store but also the chain freezer.
  152. type Database interface {
  153. Reader
  154. Writer
  155. Batcher
  156. Iteratee
  157. Stater
  158. Compacter
  159. Snapshotter
  160. io.Closer
  161. }