github.com/reapchain/go-reapchain@v0.2.15-0.20210609012950-9735c110c705/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/ethereum/go-ethereum/common" 24 "github.com/ethereum/go-ethereum/log" 25 ) 26 27 /* 28 * This is a test memory database. Do not use for any production it does not get persisted 29 */ 30 type MemDatabase struct { 31 db map[string][]byte 32 lock sync.RWMutex 33 } 34 35 func NewMemDatabase() (*MemDatabase, error) { 36 log.Info("NewMemDatabse create",) 37 return &MemDatabase{ 38 db: make(map[string][]byte), 39 }, nil 40 } 41 42 func (db *MemDatabase) Put(key []byte, value []byte) error { 43 db.lock.Lock() 44 defer db.lock.Unlock() 45 46 db.db[string(key)] = common.CopyBytes(value) 47 return nil 48 } 49 50 func (db *MemDatabase) Set(key []byte, value []byte) { 51 db.lock.Lock() 52 defer db.lock.Unlock() 53 54 db.Put(key, value) 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 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 /* 79 func (db *MemDatabase) GetKeys() []*common.Key { 80 data, _ := db.Get([]byte("KeyRing")) 81 82 return []*common.Key{common.NewKeyFromBytes(data)} 83 } 84 */ 85 86 func (db *MemDatabase) Delete(key []byte) error { 87 db.lock.Lock() 88 defer db.lock.Unlock() 89 90 delete(db.db, string(key)) 91 return nil 92 } 93 94 func (db *MemDatabase) Close() {} 95 96 func (db *MemDatabase) NewBatch() Batch { 97 return &memBatch{db: db} 98 } 99 100 type kv struct{ k, v []byte } 101 102 type memBatch struct { 103 db *MemDatabase 104 writes []kv 105 lock sync.RWMutex 106 } 107 108 func (b *memBatch) Put(key, value []byte) error { 109 b.lock.Lock() 110 defer b.lock.Unlock() 111 112 b.writes = append(b.writes, kv{common.CopyBytes(key), common.CopyBytes(value)}) 113 return nil 114 } 115 116 func (b *memBatch) Write() error { 117 b.lock.RLock() 118 defer b.lock.RUnlock() 119 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 }