github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/ethdb/memory_database.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 ethdb
    18  
    19  import (
    20  	"errors"
    21  	"sync"
    22  
    23  	"github.com/ethereumproject/go-ethereum/common"
    24  )
    25  
    26  /*
    27   * This is a test memory database. Do not use for any production it does not get persisted
    28   */
    29  type MemDatabase struct {
    30  	db   map[string][]byte
    31  	lock sync.RWMutex
    32  }
    33  
    34  func NewMemDatabase() (*MemDatabase, error) {
    35  	return &MemDatabase{
    36  		db: make(map[string][]byte),
    37  	}, nil
    38  }
    39  
    40  func (db *MemDatabase) Put(key []byte, value []byte) error {
    41  	db.lock.Lock()
    42  	defer db.lock.Unlock()
    43  
    44  	db.db[string(key)] = common.CopyBytes(value)
    45  	return nil
    46  }
    47  
    48  func (db *MemDatabase) Set(key []byte, value []byte) {
    49  	db.lock.Lock()
    50  	defer db.lock.Unlock()
    51  
    52  	db.Put(key, value)
    53  }
    54  
    55  func (db *MemDatabase) Get(key []byte) ([]byte, error) {
    56  	db.lock.RLock()
    57  	defer db.lock.RUnlock()
    58  
    59  	if entry, ok := db.db[string(key)]; ok {
    60  		return entry, nil
    61  	}
    62  	return nil, errors.New("not found")
    63  }
    64  
    65  func (db *MemDatabase) Has(key []byte) (bool, error) {
    66  	db.lock.RLock()
    67  	defer db.lock.RUnlock()
    68  
    69  	_, ok := db.db[string(key)]
    70  	return ok, nil
    71  }
    72  
    73  func (db *MemDatabase) Keys() [][]byte {
    74  	db.lock.RLock()
    75  	defer db.lock.RUnlock()
    76  
    77  	keys := [][]byte{}
    78  	for key := range db.db {
    79  		keys = append(keys, []byte(key))
    80  	}
    81  	return keys
    82  }
    83  
    84  /*
    85  func (db *MemDatabase) GetKeys() []*common.Key {
    86  	data, _ := db.Get([]byte("KeyRing"))
    87  
    88  	return []*common.Key{common.NewKeyFromBytes(data)}
    89  }
    90  */
    91  
    92  func (db *MemDatabase) Delete(key []byte) error {
    93  	db.lock.Lock()
    94  	defer db.lock.Unlock()
    95  
    96  	delete(db.db, string(key))
    97  	return nil
    98  }
    99  
   100  func (db *MemDatabase) Close() {}
   101  
   102  func (db *MemDatabase) NewBatch() Batch {
   103  	return &memBatch{db: db}
   104  }
   105  
   106  type kv struct{ k, v []byte }
   107  type memBatch struct {
   108  	db     *MemDatabase
   109  	writes []kv
   110  	size   int
   111  }
   112  
   113  func (b *memBatch) Put(key, value []byte) error {
   114  	b.writes = append(b.writes, kv{common.CopyBytes(key), common.CopyBytes(value)})
   115  	b.size += len(value)
   116  	return nil
   117  }
   118  
   119  func (b *memBatch) Write() error {
   120  	b.db.lock.Lock()
   121  	defer b.db.lock.Unlock()
   122  
   123  	for _, kv := range b.writes {
   124  		b.db.db[string(kv.k)] = kv.v
   125  	}
   126  	return nil
   127  }
   128  
   129  func (b *memBatch) ValueSize() int {
   130  	return b.size
   131  }