github.com/beyonderyue/gochain@v2.2.26+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 "sync" 21 22 "github.com/gochain-io/gochain/common" 23 ) 24 25 /* 26 * This is a test memory database. Do not use for any production it does not get persisted 27 */ 28 type MemDatabase struct { 29 db map[string][]byte 30 lock sync.RWMutex 31 } 32 33 func NewMemDatabase() *MemDatabase { 34 return &MemDatabase{ 35 db: make(map[string][]byte), 36 } 37 } 38 39 func NewMemDatabaseWithCap(size int) (*MemDatabase, error) { 40 return &MemDatabase{ 41 db: make(map[string][]byte, size), 42 }, nil 43 } 44 45 func (db *MemDatabase) GlobalTable() common.Table { return db } 46 func (db *MemDatabase) BodyTable() common.Table { return db } 47 func (db *MemDatabase) HeaderTable() common.Table { return db } 48 func (db *MemDatabase) ReceiptTable() common.Table { return db } 49 50 func (db *MemDatabase) Put(key []byte, value []byte) error { 51 db.lock.Lock() 52 defer db.lock.Unlock() 53 54 db.db[string(key)] = common.CopyBytes(value) 55 return nil 56 } 57 58 func (db *MemDatabase) Has(key []byte) (bool, error) { 59 db.lock.RLock() 60 defer db.lock.RUnlock() 61 62 _, ok := db.db[string(key)] 63 return ok, nil 64 } 65 66 func (db *MemDatabase) Get(key []byte) ([]byte, error) { 67 db.lock.RLock() 68 defer db.lock.RUnlock() 69 70 if entry, ok := db.db[string(key)]; ok { 71 return common.CopyBytes(entry), nil 72 } 73 return nil, common.ErrNotFound 74 } 75 76 func (db *MemDatabase) Keys() [][]byte { 77 db.lock.RLock() 78 defer db.lock.RUnlock() 79 80 keys := [][]byte{} 81 for key := range db.db { 82 keys = append(keys, []byte(key)) 83 } 84 return keys 85 } 86 87 func (db *MemDatabase) Delete(key []byte) error { 88 db.lock.Lock() 89 defer db.lock.Unlock() 90 91 delete(db.db, string(key)) 92 return nil 93 } 94 95 func (db *MemDatabase) Close() error { return nil } 96 97 func (db *MemDatabase) NewBatch() common.Batch { 98 return &memBatch{db: db} 99 } 100 101 func (db *MemDatabase) Len() int { return len(db.db) } 102 103 type kv struct { 104 k, v []byte 105 del bool 106 } 107 108 type memBatch struct { 109 db *MemDatabase 110 writes []kv 111 size int 112 } 113 114 func (b *memBatch) Put(key, value []byte) error { 115 b.writes = append(b.writes, kv{common.CopyBytes(key), common.CopyBytes(value), false}) 116 b.size += len(value) 117 return nil 118 } 119 120 func (b *memBatch) Delete(key []byte) error { 121 b.writes = append(b.writes, kv{common.CopyBytes(key), nil, true}) 122 b.size += 1 123 return nil 124 } 125 126 func (b *memBatch) Write() error { 127 b.db.lock.Lock() 128 defer b.db.lock.Unlock() 129 130 for _, kv := range b.writes { 131 if kv.del { 132 delete(b.db.db, string(kv.k)) 133 continue 134 } 135 b.db.db[string(kv.k)] = kv.v 136 } 137 return nil 138 } 139 140 func (b *memBatch) ValueSize() int { 141 return b.size 142 } 143 144 func (b *memBatch) Reset() { 145 b.writes = b.writes[:0] 146 b.size = 0 147 }