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