github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/ethdb/memory_database.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 package ethdb 13 14 import ( 15 "errors" 16 "sync" 17 18 "github.com/Sberex/go-sberex/common" 19 ) 20 21 /* 22 * This is a test memory database. Do not use for any production it does not get persisted 23 */ 24 type MemDatabase struct { 25 db map[string][]byte 26 lock sync.RWMutex 27 } 28 29 func NewMemDatabase() (*MemDatabase, error) { 30 return &MemDatabase{ 31 db: make(map[string][]byte), 32 }, nil 33 } 34 35 func NewMemDatabaseWithCap(size int) (*MemDatabase, error) { 36 return &MemDatabase{ 37 db: make(map[string][]byte, size), 38 }, nil 39 } 40 41 func (db *MemDatabase) Put(key []byte, value []byte) error { 42 db.lock.Lock() 43 defer db.lock.Unlock() 44 45 db.db[string(key)] = common.CopyBytes(value) 46 return nil 47 } 48 49 func (db *MemDatabase) Has(key []byte) (bool, error) { 50 db.lock.RLock() 51 defer db.lock.RUnlock() 52 53 _, ok := db.db[string(key)] 54 return ok, nil 55 } 56 57 func (db *MemDatabase) Get(key []byte) ([]byte, error) { 58 db.lock.RLock() 59 defer db.lock.RUnlock() 60 61 if entry, ok := db.db[string(key)]; ok { 62 return common.CopyBytes(entry), nil 63 } 64 return nil, errors.New("not found") 65 } 66 67 func (db *MemDatabase) Keys() [][]byte { 68 db.lock.RLock() 69 defer db.lock.RUnlock() 70 71 keys := [][]byte{} 72 for key := range db.db { 73 keys = append(keys, []byte(key)) 74 } 75 return keys 76 } 77 78 func (db *MemDatabase) Delete(key []byte) error { 79 db.lock.Lock() 80 defer db.lock.Unlock() 81 82 delete(db.db, string(key)) 83 return nil 84 } 85 86 func (db *MemDatabase) Close() {} 87 88 func (db *MemDatabase) NewBatch() Batch { 89 return &memBatch{db: db} 90 } 91 92 func (db *MemDatabase) Len() int { return len(db.db) } 93 94 type kv struct{ k, v []byte } 95 96 type memBatch struct { 97 db *MemDatabase 98 writes []kv 99 size int 100 } 101 102 func (b *memBatch) Put(key, value []byte) error { 103 b.writes = append(b.writes, kv{common.CopyBytes(key), common.CopyBytes(value)}) 104 b.size += len(value) 105 return nil 106 } 107 108 func (b *memBatch) Write() error { 109 b.db.lock.Lock() 110 defer b.db.lock.Unlock() 111 112 for _, kv := range b.writes { 113 b.db.db[string(kv.k)] = kv.v 114 } 115 return nil 116 } 117 118 func (b *memBatch) ValueSize() int { 119 return b.size 120 } 121 122 func (b *memBatch) Reset() { 123 b.writes = b.writes[:0] 124 b.size = 0 125 }