github.com/MetalBlockchain/subnet-evm@v0.4.9/core/rawdb/table.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2018 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package rawdb
    28  
    29  import (
    30  	"github.com/MetalBlockchain/subnet-evm/ethdb"
    31  )
    32  
    33  // table is a wrapper around a database that prefixes each key access with a pre-
    34  // configured string.
    35  type table struct {
    36  	db     ethdb.Database
    37  	prefix string
    38  }
    39  
    40  // NewTable returns a database object that prefixes all keys with a given string.
    41  func NewTable(db ethdb.Database, prefix string) ethdb.Database {
    42  	return &table{
    43  		db:     db,
    44  		prefix: prefix,
    45  	}
    46  }
    47  
    48  // Close is a noop to implement the Database interface.
    49  func (t *table) Close() error {
    50  	return nil
    51  }
    52  
    53  // Has retrieves if a prefixed version of a key is present in the database.
    54  func (t *table) Has(key []byte) (bool, error) {
    55  	return t.db.Has(append([]byte(t.prefix), key...))
    56  }
    57  
    58  // Get retrieves the given prefixed key if it's present in the database.
    59  func (t *table) Get(key []byte) ([]byte, error) {
    60  	return t.db.Get(append([]byte(t.prefix), key...))
    61  }
    62  
    63  // Put inserts the given value into the database at a prefixed version of the
    64  // provided key.
    65  func (t *table) Put(key []byte, value []byte) error {
    66  	return t.db.Put(append([]byte(t.prefix), key...), value)
    67  }
    68  
    69  // Delete removes the given prefixed key from the database.
    70  func (t *table) Delete(key []byte) error {
    71  	return t.db.Delete(append([]byte(t.prefix), key...))
    72  }
    73  
    74  // NewIterator creates a binary-alphabetical iterator over a subset
    75  // of database content with a particular key prefix, starting at a particular
    76  // initial key (or after, if it does not exist).
    77  func (t *table) NewIterator(prefix []byte, start []byte) ethdb.Iterator {
    78  	innerPrefix := append([]byte(t.prefix), prefix...)
    79  	iter := t.db.NewIterator(innerPrefix, start)
    80  	return &tableIterator{
    81  		iter:   iter,
    82  		prefix: t.prefix,
    83  	}
    84  }
    85  
    86  // Stat returns a particular internal stat of the database.
    87  func (t *table) Stat(property string) (string, error) {
    88  	return t.db.Stat(property)
    89  }
    90  
    91  // Compact flattens the underlying data store for the given key range. In essence,
    92  // deleted and overwritten versions are discarded, and the data is rearranged to
    93  // reduce the cost of operations needed to access them.
    94  //
    95  // A nil start is treated as a key before all keys in the data store; a nil limit
    96  // is treated as a key after all keys in the data store. If both is nil then it
    97  // will compact entire data store.
    98  func (t *table) Compact(start []byte, limit []byte) error {
    99  	// If no start was specified, use the table prefix as the first value
   100  	if start == nil {
   101  		start = []byte(t.prefix)
   102  	} else {
   103  		start = append([]byte(t.prefix), start...)
   104  	}
   105  	// If no limit was specified, use the first element not matching the prefix
   106  	// as the limit
   107  	if limit == nil {
   108  		limit = []byte(t.prefix)
   109  		for i := len(limit) - 1; i >= 0; i-- {
   110  			// Bump the current character, stopping if it doesn't overflow
   111  			limit[i]++
   112  			if limit[i] > 0 {
   113  				break
   114  			}
   115  			// Character overflown, proceed to the next or nil if the last
   116  			if i == 0 {
   117  				limit = nil
   118  			}
   119  		}
   120  	} else {
   121  		limit = append([]byte(t.prefix), limit...)
   122  	}
   123  	// Range correctly calculated based on table prefix, delegate down
   124  	return t.db.Compact(start, limit)
   125  }
   126  
   127  // NewBatch creates a write-only database that buffers changes to its host db
   128  // until a final write is called, each operation prefixing all keys with the
   129  // pre-configured string.
   130  func (t *table) NewBatch() ethdb.Batch {
   131  	return &tableBatch{t.db.NewBatch(), t.prefix}
   132  }
   133  
   134  // NewBatchWithSize creates a write-only database batch with pre-allocated buffer.
   135  func (t *table) NewBatchWithSize(size int) ethdb.Batch {
   136  	return &tableBatch{t.db.NewBatchWithSize(size), t.prefix}
   137  }
   138  
   139  // tableBatch is a wrapper around a database batch that prefixes each key access
   140  // with a pre-configured string.
   141  type tableBatch struct {
   142  	batch  ethdb.Batch
   143  	prefix string
   144  }
   145  
   146  // Put inserts the given value into the batch for later committing.
   147  func (b *tableBatch) Put(key, value []byte) error {
   148  	return b.batch.Put(append([]byte(b.prefix), key...), value)
   149  }
   150  
   151  // Delete inserts the a key removal into the batch for later committing.
   152  func (b *tableBatch) Delete(key []byte) error {
   153  	return b.batch.Delete(append([]byte(b.prefix), key...))
   154  }
   155  
   156  // ValueSize retrieves the amount of data queued up for writing.
   157  func (b *tableBatch) ValueSize() int {
   158  	return b.batch.ValueSize()
   159  }
   160  
   161  // Write flushes any accumulated data to disk.
   162  func (b *tableBatch) Write() error {
   163  	return b.batch.Write()
   164  }
   165  
   166  // Reset resets the batch for reuse.
   167  func (b *tableBatch) Reset() {
   168  	b.batch.Reset()
   169  }
   170  
   171  // tableReplayer is a wrapper around a batch replayer which truncates
   172  // the added prefix.
   173  type tableReplayer struct {
   174  	w      ethdb.KeyValueWriter
   175  	prefix string
   176  }
   177  
   178  // Put implements the interface KeyValueWriter.
   179  func (r *tableReplayer) Put(key []byte, value []byte) error {
   180  	trimmed := key[len(r.prefix):]
   181  	return r.w.Put(trimmed, value)
   182  }
   183  
   184  // Delete implements the interface KeyValueWriter.
   185  func (r *tableReplayer) Delete(key []byte) error {
   186  	trimmed := key[len(r.prefix):]
   187  	return r.w.Delete(trimmed)
   188  }
   189  
   190  // Replay replays the batch contents.
   191  func (b *tableBatch) Replay(w ethdb.KeyValueWriter) error {
   192  	return b.batch.Replay(&tableReplayer{w: w, prefix: b.prefix})
   193  }
   194  
   195  // tableIterator is a wrapper around a database iterator that prefixes each key access
   196  // with a pre-configured string.
   197  type tableIterator struct {
   198  	iter   ethdb.Iterator
   199  	prefix string
   200  }
   201  
   202  // Next moves the iterator to the next key/value pair. It returns whether the
   203  // iterator is exhausted.
   204  func (iter *tableIterator) Next() bool {
   205  	return iter.iter.Next()
   206  }
   207  
   208  // Error returns any accumulated error. Exhausting all the key/value pairs
   209  // is not considered to be an error.
   210  func (iter *tableIterator) Error() error {
   211  	return iter.iter.Error()
   212  }
   213  
   214  // Key returns the key of the current key/value pair, or nil if done. The caller
   215  // should not modify the contents of the returned slice, and its contents may
   216  // change on the next call to Next.
   217  func (iter *tableIterator) Key() []byte {
   218  	key := iter.iter.Key()
   219  	if key == nil {
   220  		return nil
   221  	}
   222  	return key[len(iter.prefix):]
   223  }
   224  
   225  // Value returns the value of the current key/value pair, or nil if done. The
   226  // caller should not modify the contents of the returned slice, and its contents
   227  // may change on the next call to Next.
   228  func (iter *tableIterator) Value() []byte {
   229  	return iter.iter.Value()
   230  }
   231  
   232  // Release releases associated resources. Release should always succeed and can
   233  // be called multiple times without causing error.
   234  func (iter *tableIterator) Release() {
   235  	iter.iter.Release()
   236  }