github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/core/rawdb/table.go (about)

     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  
    17  package rawdb
    18  
    19  import (
    20  	"github.com/intfoundation/intchain/intdb"
    21  )
    22  
    23  // table is a wrapper around a database that prefixes each key access with a pre-
    24  // configured string.
    25  type table struct {
    26  	db     intdb.Database
    27  	prefix string
    28  }
    29  
    30  // NewTable returns a database object that prefixes all keys with a given string.
    31  func NewTable(db intdb.Database, prefix string) intdb.Database {
    32  	return &table{
    33  		db:     db,
    34  		prefix: prefix,
    35  	}
    36  }
    37  
    38  // Close is a noop to implement the Database interface.
    39  func (t *table) Close() error {
    40  	return nil
    41  }
    42  
    43  // Has retrieves if a prefixed version of a key is present in the database.
    44  func (t *table) Has(key []byte) (bool, error) {
    45  	return t.db.Has(append([]byte(t.prefix), key...))
    46  }
    47  
    48  // Get retrieves the given prefixed key if it's present in the database.
    49  func (t *table) Get(key []byte) ([]byte, error) {
    50  	return t.db.Get(append([]byte(t.prefix), key...))
    51  }
    52  
    53  // Put inserts the given value into the database at a prefixed version of the
    54  // provided key.
    55  func (t *table) Put(key []byte, value []byte) error {
    56  	return t.db.Put(append([]byte(t.prefix), key...), value)
    57  }
    58  
    59  // Delete removes the given prefixed key from the database.
    60  func (t *table) Delete(key []byte) error {
    61  	return t.db.Delete(append([]byte(t.prefix), key...))
    62  }
    63  
    64  // NewIterator creates a binary-alphabetical iterator over the entire keyspace
    65  // contained within the database.
    66  func (t *table) NewIterator() intdb.Iterator {
    67  	return t.NewIteratorWithPrefix(nil)
    68  }
    69  
    70  // NewIteratorWithPrefix creates a binary-alphabetical iterator over a subset
    71  // of database content with a particular key prefix.
    72  func (t *table) NewIteratorWithPrefix(prefix []byte) intdb.Iterator {
    73  	return t.db.NewIteratorWithPrefix(append([]byte(t.prefix), prefix...))
    74  }
    75  
    76  // Stat returns a particular internal stat of the database.
    77  func (t *table) Stat(property string) (string, error) {
    78  	return t.db.Stat(property)
    79  }
    80  
    81  // Compact flattens the underlying data store for the given key range. In essence,
    82  // deleted and overwritten versions are discarded, and the data is rearranged to
    83  // reduce the cost of operations needed to access them.
    84  //
    85  // A nil start is treated as a key before all keys in the data store; a nil limit
    86  // is treated as a key after all keys in the data store. If both is nil then it
    87  // will compact entire data store.
    88  func (t *table) Compact(start []byte, limit []byte) error {
    89  	// If no start was specified, use the table prefix as the first value
    90  	if start == nil {
    91  		start = []byte(t.prefix)
    92  	}
    93  	// If no limit was specified, use the first element not matching the prefix
    94  	// as the limit
    95  	if limit == nil {
    96  		limit = []byte(t.prefix)
    97  		for i := len(limit) - 1; i >= 0; i-- {
    98  			// Bump the current character, stopping if it doesn't overflow
    99  			limit[i]++
   100  			if limit[i] > 0 {
   101  				break
   102  			}
   103  			// Character overflown, proceed to the next or nil if the last
   104  			if i == 0 {
   105  				limit = nil
   106  			}
   107  		}
   108  	}
   109  	// Range correctly calculated based on table prefix, delegate down
   110  	return t.db.Compact(start, limit)
   111  }
   112  
   113  // NewBatch creates a write-only database that buffers changes to its host db
   114  // until a final write is called, each operation prefixing all keys with the
   115  // pre-configured string.
   116  func (t *table) NewBatch() intdb.Batch {
   117  	return &tableBatch{t.db.NewBatch(), t.prefix}
   118  }
   119  
   120  // tableBatch is a wrapper around a database batch that prefixes each key access
   121  // with a pre-configured string.
   122  type tableBatch struct {
   123  	batch  intdb.Batch
   124  	prefix string
   125  }
   126  
   127  // Put inserts the given value into the batch for later committing.
   128  func (b *tableBatch) Put(key, value []byte) error {
   129  	return b.batch.Put(append([]byte(b.prefix), key...), value)
   130  }
   131  
   132  // Delete inserts the a key removal into the batch for later committing.
   133  func (b *tableBatch) Delete(key []byte) error {
   134  	return b.batch.Delete(append([]byte(b.prefix), key...))
   135  }
   136  
   137  // ValueSize retrieves the amount of data queued up for writing.
   138  func (b *tableBatch) ValueSize() int {
   139  	return b.batch.ValueSize()
   140  }
   141  
   142  // Write flushes any accumulated data to disk.
   143  func (b *tableBatch) Write() error {
   144  	return b.batch.Write()
   145  }
   146  
   147  // Reset resets the batch for reuse.
   148  func (b *tableBatch) Reset() {
   149  	b.batch.Reset()
   150  }
   151  
   152  // Replay replays the batch contents.
   153  func (b *tableBatch) Replay(w intdb.Writer) error {
   154  	return b.batch.Replay(w)
   155  }