github.com/fff-chain/go-fff@v0.0.0-20220726032732-1c84420b8a99/ethdb/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 ethdb defines the interfaces for an Ethereum data store.
    18  package ethdb
    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  // Stater wraps the Stat method of a backing data store.
    41  type Stater 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  	Batcher
    64  	Iteratee
    65  	Stater
    66  	Compacter
    67  	io.Closer
    68  }
    69  
    70  // AncientReader contains the methods required to read from immutable ancient data.
    71  type AncientReader interface {
    72  	// HasAncient returns an indicator whether the specified data exists in the
    73  	// ancient store.
    74  	HasAncient(kind string, number uint64) (bool, error)
    75  
    76  	// Ancient retrieves an ancient binary blob from the append-only immutable files.
    77  	Ancient(kind string, number uint64) ([]byte, error)
    78  
    79  	// Ancients returns the ancient item numbers in the ancient store.
    80  	Ancients() (uint64, error)
    81  
    82  	// AncientSize returns the ancient size of the specified category.
    83  	AncientSize(kind string) (uint64, error)
    84  
    85  	// ItemAmountInAncient returns the actual length of current ancientDB.
    86  	ItemAmountInAncient() (uint64, error)
    87  
    88  	// AncientOffSet returns the offset of current ancientDB.
    89  	AncientOffSet() uint64
    90  }
    91  
    92  // AncientWriter contains the methods required to write to immutable ancient data.
    93  type AncientWriter interface {
    94  	// AppendAncient injects all binary blobs belong to block at the end of the
    95  	// append-only immutable table files.
    96  	AppendAncient(number uint64, hash, header, body, receipt, td []byte) error
    97  
    98  	// TruncateAncients discards all but the first n ancient data from the ancient store.
    99  	TruncateAncients(n uint64) error
   100  
   101  	// Sync flushes all in-memory ancient store data to disk.
   102  	Sync() error
   103  }
   104  
   105  // Reader contains the methods required to read data from both key-value as well as
   106  // immutable ancient data.
   107  type Reader interface {
   108  	KeyValueReader
   109  	AncientReader
   110  }
   111  
   112  // Writer contains the methods required to write data to both key-value as well as
   113  // immutable ancient data.
   114  type Writer interface {
   115  	KeyValueWriter
   116  	AncientWriter
   117  }
   118  
   119  // AncientStore contains all the methods required to allow handling different
   120  // ancient data stores backing immutable chain data store.
   121  type AncientStore interface {
   122  	AncientReader
   123  	AncientWriter
   124  	io.Closer
   125  }
   126  
   127  type DiffStore interface {
   128  	DiffStore() KeyValueStore
   129  	SetDiffStore(diff KeyValueStore)
   130  }
   131  
   132  // Database contains all the methods required by the high level database to not
   133  // only access the key-value data store but also the chain freezer.
   134  type Database interface {
   135  	Reader
   136  	Writer
   137  	DiffStore
   138  	Batcher
   139  	Iteratee
   140  	Stater
   141  	Compacter
   142  	io.Closer
   143  }