github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/intdb/memorydb/memorydb.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 memorydb implements the key-value database layer based on memory maps.
    18  package memorydb
    19  
    20  import (
    21  	"errors"
    22  	"sort"
    23  	"strings"
    24  	"sync"
    25  
    26  	"github.com/intfoundation/intchain/common"
    27  	"github.com/intfoundation/intchain/intdb"
    28  )
    29  
    30  var (
    31  	// errMemorydbClosed is returned if a memory database was already closed at the
    32  	// invocation of a data access operation.
    33  	errMemorydbClosed = errors.New("database closed")
    34  
    35  	// errMemorydbNotFound is returned if a key is requested that is not found in
    36  	// the provided memory database.
    37  	errMemorydbNotFound = errors.New("not found")
    38  )
    39  
    40  // Database is an ephemeral key-value store. Apart from basic data storage
    41  // functionality it also supports batch writes and iterating over the keyspace in
    42  // binary-alphabetical order.
    43  type Database struct {
    44  	db   map[string][]byte
    45  	lock sync.RWMutex
    46  }
    47  
    48  // New returns a wrapped map with all the required database interface methods
    49  // implemented.
    50  func New() *Database {
    51  	return &Database{
    52  		db: make(map[string][]byte),
    53  	}
    54  }
    55  
    56  // NewWithCap returns a wrapped map pre-allocated to the provided capcity with
    57  // all the required database interface methods implemented.
    58  func NewWithCap(size int) *Database {
    59  	return &Database{
    60  		db: make(map[string][]byte, size),
    61  	}
    62  }
    63  
    64  // Close deallocates the internal map and ensures any consecutive data access op
    65  // failes with an error.
    66  func (db *Database) Close() error {
    67  	db.lock.Lock()
    68  	defer db.lock.Unlock()
    69  
    70  	db.db = nil
    71  	return nil
    72  }
    73  
    74  // Has retrieves if a key is present in the key-value store.
    75  func (db *Database) Has(key []byte) (bool, error) {
    76  	db.lock.RLock()
    77  	defer db.lock.RUnlock()
    78  
    79  	if db.db == nil {
    80  		return false, errMemorydbClosed
    81  	}
    82  	_, ok := db.db[string(key)]
    83  	return ok, nil
    84  }
    85  
    86  // Get retrieves the given key if it's present in the key-value store.
    87  func (db *Database) Get(key []byte) ([]byte, error) {
    88  	db.lock.RLock()
    89  	defer db.lock.RUnlock()
    90  
    91  	if db.db == nil {
    92  		return nil, errMemorydbClosed
    93  	}
    94  	if entry, ok := db.db[string(key)]; ok {
    95  		return common.CopyBytes(entry), nil
    96  	}
    97  	return nil, errMemorydbNotFound
    98  }
    99  
   100  // Put inserts the given value into the key-value store.
   101  func (db *Database) Put(key []byte, value []byte) error {
   102  	db.lock.Lock()
   103  	defer db.lock.Unlock()
   104  
   105  	if db.db == nil {
   106  		return errMemorydbClosed
   107  	}
   108  	db.db[string(key)] = common.CopyBytes(value)
   109  	return nil
   110  }
   111  
   112  // Delete removes the key from the key-value store.
   113  func (db *Database) Delete(key []byte) error {
   114  	db.lock.Lock()
   115  	defer db.lock.Unlock()
   116  
   117  	if db.db == nil {
   118  		return errMemorydbClosed
   119  	}
   120  	delete(db.db, string(key))
   121  	return nil
   122  }
   123  
   124  // NewBatch creates a write-only key-value store that buffers changes to its host
   125  // database until a final write is called.
   126  func (db *Database) NewBatch() intdb.Batch {
   127  	return &batch{
   128  		db: db,
   129  	}
   130  }
   131  
   132  // NewIterator creates a binary-alphabetical iterator over the entire keyspace
   133  // contained within the memory database.
   134  func (db *Database) NewIterator() intdb.Iterator {
   135  	return db.NewIteratorWithPrefix(nil)
   136  }
   137  
   138  // NewIteratorWithPrefix creates a binary-alphabetical iterator over a subset
   139  // of database content with a particular key prefix.
   140  func (db *Database) NewIteratorWithPrefix(prefix []byte) intdb.Iterator {
   141  	db.lock.RLock()
   142  	defer db.lock.RUnlock()
   143  
   144  	var (
   145  		pr     = string(prefix)
   146  		keys   = make([]string, 0, len(db.db))
   147  		values = make([][]byte, 0, len(db.db))
   148  	)
   149  	// Collect the keys from the memory database corresponding to the given prefix
   150  	for key := range db.db {
   151  		if strings.HasPrefix(key, pr) {
   152  			keys = append(keys, key)
   153  		}
   154  	}
   155  	// Sort the items and retrieve the associated values
   156  	sort.Strings(keys)
   157  	for _, key := range keys {
   158  		values = append(values, db.db[key])
   159  	}
   160  	return &iterator{
   161  		keys:   keys,
   162  		values: values,
   163  	}
   164  }
   165  
   166  // Stat returns a particular internal stat of the database.
   167  func (db *Database) Stat(property string) (string, error) {
   168  	return "", errors.New("unknown property")
   169  }
   170  
   171  // Compact is not supported on a memory database.
   172  func (db *Database) Compact(start []byte, limit []byte) error {
   173  	return errors.New("unsupported operation")
   174  }
   175  
   176  // Len returns the number of entries currently present in the memory database.
   177  //
   178  // Note, this method is only used for testing (i.e. not public in general) and
   179  // does not have explicit checks for closed-ness to allow simpler testing code.
   180  func (db *Database) Len() int {
   181  	db.lock.RLock()
   182  	defer db.lock.RUnlock()
   183  
   184  	return len(db.db)
   185  }
   186  
   187  // keyvalue is a key-value tuple tagged with a deletion field to allow creating
   188  // memory-database write batches.
   189  type keyvalue struct {
   190  	key    []byte
   191  	value  []byte
   192  	delete bool
   193  }
   194  
   195  // batch is a write-only memory batch that commits changes to its host
   196  // database when Write is called. A batch cannot be used concurrently.
   197  type batch struct {
   198  	db     *Database
   199  	writes []keyvalue
   200  	size   int
   201  }
   202  
   203  // Put inserts the given value into the batch for later committing.
   204  func (b *batch) Put(key, value []byte) error {
   205  	b.writes = append(b.writes, keyvalue{common.CopyBytes(key), common.CopyBytes(value), false})
   206  	b.size += len(value)
   207  	return nil
   208  }
   209  
   210  // Delete inserts the a key removal into the batch for later committing.
   211  func (b *batch) Delete(key []byte) error {
   212  	b.writes = append(b.writes, keyvalue{common.CopyBytes(key), nil, true})
   213  	b.size += 1
   214  	return nil
   215  }
   216  
   217  // ValueSize retrieves the amount of data queued up for writing.
   218  func (b *batch) ValueSize() int {
   219  	return b.size
   220  }
   221  
   222  // Write flushes any accumulated data to the memory database.
   223  func (b *batch) Write() error {
   224  	b.db.lock.Lock()
   225  	defer b.db.lock.Unlock()
   226  
   227  	for _, keyvalue := range b.writes {
   228  		if keyvalue.delete {
   229  			delete(b.db.db, string(keyvalue.key))
   230  			continue
   231  		}
   232  		b.db.db[string(keyvalue.key)] = keyvalue.value
   233  	}
   234  	return nil
   235  }
   236  
   237  // Reset resets the batch for reuse.
   238  func (b *batch) Reset() {
   239  	b.writes = b.writes[:0]
   240  	b.size = 0
   241  }
   242  
   243  // Replay replays the batch contents.
   244  func (b *batch) Replay(w intdb.Writer) error {
   245  	for _, keyvalue := range b.writes {
   246  		if keyvalue.delete {
   247  			if err := w.Delete(keyvalue.key); err != nil {
   248  				return err
   249  			}
   250  			continue
   251  		}
   252  		if err := w.Put(keyvalue.key, keyvalue.value); err != nil {
   253  			return err
   254  		}
   255  	}
   256  	return nil
   257  }
   258  
   259  // iterator can walk over the (potentially partial) keyspace of a memory key
   260  // value store. Internally it is a deep copy of the entire iterated state,
   261  // sorted by keys.
   262  type iterator struct {
   263  	inited bool
   264  	keys   []string
   265  	values [][]byte
   266  }
   267  
   268  // Next moves the iterator to the next key/value pair. It returns whether the
   269  // iterator is exhausted.
   270  func (it *iterator) Next() bool {
   271  	// If the iterator was not yet initialized, do it now
   272  	if !it.inited {
   273  		it.inited = true
   274  		return len(it.keys) > 0
   275  	}
   276  	// Iterator already initialize, advance it
   277  	if len(it.keys) > 0 {
   278  		it.keys = it.keys[1:]
   279  		it.values = it.values[1:]
   280  	}
   281  	return len(it.keys) > 0
   282  }
   283  
   284  // Error returns any accumulated error. Exhausting all the key/value pairs
   285  // is not considered to be an error. A memory iterator cannot encounter errors.
   286  func (it *iterator) Error() error {
   287  	return nil
   288  }
   289  
   290  // Key returns the key of the current key/value pair, or nil if done. The caller
   291  // should not modify the contents of the returned slice, and its contents may
   292  // change on the next call to Next.
   293  func (it *iterator) Key() []byte {
   294  	if len(it.keys) > 0 {
   295  		return []byte(it.keys[0])
   296  	}
   297  	return nil
   298  }
   299  
   300  // Value returns the value of the current key/value pair, or nil if done. The
   301  // caller should not modify the contents of the returned slice, and its contents
   302  // may change on the next call to Next.
   303  func (it *iterator) Value() []byte {
   304  	if len(it.values) > 0 {
   305  		return it.values[0]
   306  	}
   307  	return nil
   308  }
   309  
   310  // Release releases associated resources. Release should always succeed and can
   311  // be called multiple times without causing error.
   312  func (it *iterator) Release() {
   313  	it.keys, it.values = nil, nil
   314  }