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