github.com/Debrief-BC/go-debrief@v0.0.0-20200420203408-0c26ca968123/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/Debrief-BC/go-debrief/ethdb"
    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     ethdb.Database
    27  	prefix string
    28  }
    29  
    30  // NewTable returns a database object that prefixes all keys with a given string.
    31  func NewTable(db ethdb.Database, prefix string) ethdb.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  // HasAncient is a noop passthrough that just forwards the request to the underlying
    54  // database.
    55  func (t *table) HasAncient(kind string, number uint64) (bool, error) {
    56  	return t.db.HasAncient(kind, number)
    57  }
    58  
    59  // Ancient is a noop passthrough that just forwards the request to the underlying
    60  // database.
    61  func (t *table) Ancient(kind string, number uint64) ([]byte, error) {
    62  	return t.db.Ancient(kind, number)
    63  }
    64  
    65  // Ancients is a noop passthrough that just forwards the request to the underlying
    66  // database.
    67  func (t *table) Ancients() (uint64, error) {
    68  	return t.db.Ancients()
    69  }
    70  
    71  // AncientSize is a noop passthrough that just forwards the request to the underlying
    72  // database.
    73  func (t *table) AncientSize(kind string) (uint64, error) {
    74  	return t.db.AncientSize(kind)
    75  }
    76  
    77  // AppendAncient is a noop passthrough that just forwards the request to the underlying
    78  // database.
    79  func (t *table) AppendAncient(number uint64, hash, header, body, receipts, td []byte) error {
    80  	return t.db.AppendAncient(number, hash, header, body, receipts, td)
    81  }
    82  
    83  // TruncateAncients is a noop passthrough that just forwards the request to the underlying
    84  // database.
    85  func (t *table) TruncateAncients(items uint64) error {
    86  	return t.db.TruncateAncients(items)
    87  }
    88  
    89  // Sync is a noop passthrough that just forwards the request to the underlying
    90  // database.
    91  func (t *table) Sync() error {
    92  	return t.db.Sync()
    93  }
    94  
    95  // Put inserts the given value into the database at a prefixed version of the
    96  // provided key.
    97  func (t *table) Put(key []byte, value []byte) error {
    98  	return t.db.Put(append([]byte(t.prefix), key...), value)
    99  }
   100  
   101  // Delete removes the given prefixed key from the database.
   102  func (t *table) Delete(key []byte) error {
   103  	return t.db.Delete(append([]byte(t.prefix), key...))
   104  }
   105  
   106  // NewIterator creates a binary-alphabetical iterator over the entire keyspace
   107  // contained within the database.
   108  func (t *table) NewIterator() ethdb.Iterator {
   109  	return t.NewIteratorWithPrefix(nil)
   110  }
   111  
   112  // NewIteratorWithStart creates a binary-alphabetical iterator over a subset of
   113  // database content starting at a particular initial key (or after, if it does
   114  // not exist).
   115  func (t *table) NewIteratorWithStart(start []byte) ethdb.Iterator {
   116  	iter := t.db.NewIteratorWithStart(append([]byte(t.prefix), start...))
   117  	return &tableIterator{
   118  		iter:   iter,
   119  		prefix: t.prefix,
   120  	}
   121  }
   122  
   123  // NewIteratorWithPrefix creates a binary-alphabetical iterator over a subset
   124  // of database content with a particular key prefix.
   125  func (t *table) NewIteratorWithPrefix(prefix []byte) ethdb.Iterator {
   126  	iter := t.db.NewIteratorWithPrefix(append([]byte(t.prefix), prefix...))
   127  	return &tableIterator{
   128  		iter:   iter,
   129  		prefix: t.prefix,
   130  	}
   131  }
   132  
   133  // Stat returns a particular internal stat of the database.
   134  func (t *table) Stat(property string) (string, error) {
   135  	return t.db.Stat(property)
   136  }
   137  
   138  // Compact flattens the underlying data store for the given key range. In essence,
   139  // deleted and overwritten versions are discarded, and the data is rearranged to
   140  // reduce the cost of operations needed to access them.
   141  //
   142  // A nil start is treated as a key before all keys in the data store; a nil limit
   143  // is treated as a key after all keys in the data store. If both is nil then it
   144  // will compact entire data store.
   145  func (t *table) Compact(start []byte, limit []byte) error {
   146  	// If no start was specified, use the table prefix as the first value
   147  	if start == nil {
   148  		start = []byte(t.prefix)
   149  	}
   150  	// If no limit was specified, use the first element not matching the prefix
   151  	// as the limit
   152  	if limit == nil {
   153  		limit = []byte(t.prefix)
   154  		for i := len(limit) - 1; i >= 0; i-- {
   155  			// Bump the current character, stopping if it doesn't overflow
   156  			limit[i]++
   157  			if limit[i] > 0 {
   158  				break
   159  			}
   160  			// Character overflown, proceed to the next or nil if the last
   161  			if i == 0 {
   162  				limit = nil
   163  			}
   164  		}
   165  	}
   166  	// Range correctly calculated based on table prefix, delegate down
   167  	return t.db.Compact(start, limit)
   168  }
   169  
   170  // NewBatch creates a write-only database that buffers changes to its host db
   171  // until a final write is called, each operation prefixing all keys with the
   172  // pre-configured string.
   173  func (t *table) NewBatch() ethdb.Batch {
   174  	return &tableBatch{t.db.NewBatch(), t.prefix}
   175  }
   176  
   177  // tableBatch is a wrapper around a database batch that prefixes each key access
   178  // with a pre-configured string.
   179  type tableBatch struct {
   180  	batch  ethdb.Batch
   181  	prefix string
   182  }
   183  
   184  // Put inserts the given value into the batch for later committing.
   185  func (b *tableBatch) Put(key, value []byte) error {
   186  	return b.batch.Put(append([]byte(b.prefix), key...), value)
   187  }
   188  
   189  // Delete inserts the a key removal into the batch for later committing.
   190  func (b *tableBatch) Delete(key []byte) error {
   191  	return b.batch.Delete(append([]byte(b.prefix), key...))
   192  }
   193  
   194  // ValueSize retrieves the amount of data queued up for writing.
   195  func (b *tableBatch) ValueSize() int {
   196  	return b.batch.ValueSize()
   197  }
   198  
   199  // Write flushes any accumulated data to disk.
   200  func (b *tableBatch) Write() error {
   201  	return b.batch.Write()
   202  }
   203  
   204  // Reset resets the batch for reuse.
   205  func (b *tableBatch) Reset() {
   206  	b.batch.Reset()
   207  }
   208  
   209  // tableReplayer is a wrapper around a batch replayer which truncates
   210  // the added prefix.
   211  type tableReplayer struct {
   212  	w      ethdb.KeyValueWriter
   213  	prefix string
   214  }
   215  
   216  // Put implements the interface KeyValueWriter.
   217  func (r *tableReplayer) Put(key []byte, value []byte) error {
   218  	trimmed := key[len(r.prefix):]
   219  	return r.w.Put(trimmed, value)
   220  }
   221  
   222  // Delete implements the interface KeyValueWriter.
   223  func (r *tableReplayer) Delete(key []byte) error {
   224  	trimmed := key[len(r.prefix):]
   225  	return r.w.Delete(trimmed)
   226  }
   227  
   228  // Replay replays the batch contents.
   229  func (b *tableBatch) Replay(w ethdb.KeyValueWriter) error {
   230  	return b.batch.Replay(&tableReplayer{w: w, prefix: b.prefix})
   231  }
   232  
   233  // tableIterator is a wrapper around a database iterator that prefixes each key access
   234  // with a pre-configured string.
   235  type tableIterator struct {
   236  	iter   ethdb.Iterator
   237  	prefix string
   238  }
   239  
   240  // Next moves the iterator to the next key/value pair. It returns whether the
   241  // iterator is exhausted.
   242  func (iter *tableIterator) Next() bool {
   243  	return iter.iter.Next()
   244  }
   245  
   246  // Error returns any accumulated error. Exhausting all the key/value pairs
   247  // is not considered to be an error.
   248  func (iter *tableIterator) Error() error {
   249  	return iter.iter.Error()
   250  }
   251  
   252  // Key returns the key of the current key/value pair, or nil if done. The caller
   253  // should not modify the contents of the returned slice, and its contents may
   254  // change on the next call to Next.
   255  func (iter *tableIterator) Key() []byte {
   256  	key := iter.iter.Key()
   257  	if key == nil {
   258  		return nil
   259  	}
   260  	return key[len(iter.prefix):]
   261  }
   262  
   263  // Value returns the value of the current key/value pair, or nil if done. The
   264  // caller should not modify the contents of the returned slice, and its contents
   265  // may change on the next call to Next.
   266  func (iter *tableIterator) Value() []byte {
   267  	return iter.iter.Value()
   268  }
   269  
   270  // Release releases associated resources. Release should always succeed and can
   271  // be called multiple times without causing error.
   272  func (iter *tableIterator) Release() {
   273  	iter.iter.Release()
   274  }