github.com/luckypickle/go-ethereum-vet@v1.14.2/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/luckypickle/go-ethereum-vet/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 {
    35  	return &MemDatabase{
    36  		db: make(map[string][]byte),
    37  	}
    38  }
    39  
    40  func NewMemDatabaseWithCap(size int) *MemDatabase {
    41  	return &MemDatabase{
    42  		db: make(map[string][]byte, size),
    43  	}
    44  }
    45  
    46  func (db *MemDatabase) Put(key []byte, value []byte) error {
    47  	db.lock.Lock()
    48  	defer db.lock.Unlock()
    49  
    50  	db.db[string(key)] = common.CopyBytes(value)
    51  	return nil
    52  }
    53  
    54  func (db *MemDatabase) Has(key []byte) (bool, error) {
    55  	db.lock.RLock()
    56  	defer db.lock.RUnlock()
    57  
    58  	_, ok := db.db[string(key)]
    59  	return ok, nil
    60  }
    61  
    62  func (db *MemDatabase) Get(key []byte) ([]byte, error) {
    63  	db.lock.RLock()
    64  	defer db.lock.RUnlock()
    65  
    66  	if entry, ok := db.db[string(key)]; ok {
    67  		return common.CopyBytes(entry), nil
    68  	}
    69  	return nil, errors.New("not found")
    70  }
    71  
    72  func (db *MemDatabase) Keys() [][]byte {
    73  	db.lock.RLock()
    74  	defer db.lock.RUnlock()
    75  
    76  	keys := [][]byte{}
    77  	for key := range db.db {
    78  		keys = append(keys, []byte(key))
    79  	}
    80  	return keys
    81  }
    82  
    83  func (db *MemDatabase) Delete(key []byte) error {
    84  	db.lock.Lock()
    85  	defer db.lock.Unlock()
    86  
    87  	delete(db.db, string(key))
    88  	return nil
    89  }
    90  
    91  func (db *MemDatabase) Close() {}
    92  
    93  func (db *MemDatabase) NewBatch() Batch {
    94  	return &memBatch{db: db}
    95  }
    96  
    97  func (db *MemDatabase) Len() int { return len(db.db) }
    98  
    99  type kv struct {
   100  	k, v []byte
   101  	del  bool
   102  }
   103  
   104  type memBatch struct {
   105  	db     *MemDatabase
   106  	writes []kv
   107  	size   int
   108  }
   109  
   110  func (b *memBatch) Put(key, value []byte) error {
   111  	b.writes = append(b.writes, kv{common.CopyBytes(key), common.CopyBytes(value), false})
   112  	b.size += len(value)
   113  	return nil
   114  }
   115  
   116  func (b *memBatch) Delete(key []byte) error {
   117  	b.writes = append(b.writes, kv{common.CopyBytes(key), nil, true})
   118  	b.size += 1
   119  	return nil
   120  }
   121  
   122  func (b *memBatch) Write() error {
   123  	b.db.lock.Lock()
   124  	defer b.db.lock.Unlock()
   125  
   126  	for _, kv := range b.writes {
   127  		if kv.del {
   128  			delete(b.db.db, string(kv.k))
   129  			continue
   130  		}
   131  		b.db.db[string(kv.k)] = kv.v
   132  	}
   133  	return nil
   134  }
   135  
   136  func (b *memBatch) ValueSize() int {
   137  	return b.size
   138  }
   139  
   140  func (b *memBatch) Reset() {
   141  	b.writes = b.writes[:0]
   142  	b.size = 0
   143  }