database.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 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. // Stater wraps the Stat method of a backing data store.
  34. type Stater 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. Batcher
  55. Iteratee
  56. Stater
  57. Compacter
  58. io.Closer
  59. }
  60. // AncientReader contains the methods required to read from immutable ancient data.
  61. type AncientReader interface {
  62. // HasAncient returns an indicator whether the specified data exists in the
  63. // ancient store.
  64. HasAncient(kind string, number uint64) (bool, error)
  65. // Ancient retrieves an ancient binary blob from the append-only immutable files.
  66. Ancient(kind string, number uint64) ([]byte, error)
  67. // Ancients returns the ancient store length
  68. Ancients() (uint64, error)
  69. }
  70. // AncientWriter contains the methods required to write to immutable ancient data.
  71. type AncientWriter interface {
  72. // AppendAncient injects all binary blobs belong to block at the end of the
  73. // append-only immutable table files.
  74. AppendAncient(number uint64, hash, header, body, receipt, td []byte) error
  75. // TruncateAncients discards all but the first n ancient data from the ancient store.
  76. TruncateAncients(n uint64) error
  77. // Sync flushes all in-memory ancient store data to disk.
  78. Sync() error
  79. }
  80. // Reader contains the methods required to read data from both key-value as well as
  81. // immutable ancient data.
  82. type Reader interface {
  83. KeyValueReader
  84. AncientReader
  85. }
  86. // Writer contains the methods required to write data to both key-value as well as
  87. // immutable ancient data.
  88. type Writer interface {
  89. KeyValueWriter
  90. AncientWriter
  91. }
  92. // AncientStore contains all the methods required to allow handling different
  93. // ancient data stores backing immutable chain data store.
  94. type AncientStore interface {
  95. AncientReader
  96. AncientWriter
  97. io.Closer
  98. }
  99. // Database contains all the methods required by the high level database to not
  100. // only access the key-value data store but also the chain freezer.
  101. type Database interface {
  102. Reader
  103. Writer
  104. Batcher
  105. Iteratee
  106. Stater
  107. Compacter
  108. io.Closer
  109. }