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