github.com/amazechain/amc@v0.1.3/internal/amcdb/memdb/memdb.go (about) 1 // Copyright 2022 The AmazeChain Authors 2 // This file is part of the AmazeChain library. 3 // 4 // The AmazeChain 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 AmazeChain 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 AmazeChain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package memdb 18 19 import ( 20 "fmt" 21 "github.com/amazechain/amc/common/db" 22 "sync" 23 ) 24 25 type MemoryDB struct { 26 db map[string][]byte 27 lock sync.RWMutex 28 } 29 30 func (m *MemoryDB) Snapshot() (db.ISnapshot, error) { 31 //TODO implement me 32 panic("implement me") 33 } 34 35 func NewMemDB() db.IDatabase { 36 return &MemoryDB{ 37 db: make(map[string][]byte), 38 } 39 } 40 41 func (m *MemoryDB) OpenReader(dbName string) (db.IDatabaseReader, error) { 42 return m, nil 43 } 44 45 func (m *MemoryDB) OpenWriter(dbName string) (db.IDatabaseWriter, error) { 46 return nil, nil 47 } 48 49 func (m *MemoryDB) Open(dbName string) (db.IDatabaseWriterReader, error) { 50 return nil, nil 51 } 52 53 func (m *MemoryDB) Close() error { 54 return nil 55 } 56 57 func (m *MemoryDB) Get(key []byte) ([]byte, error) { 58 m.lock.RLock() 59 defer m.lock.RUnlock() 60 if _, ok := m.db[string(key)]; ok { 61 v := m.db[string(key)] 62 return v, nil 63 } 64 65 return nil, fmt.Errorf("invalid key") 66 } 67 68 func (m *MemoryDB) Gets(key []byte, count uint) ([][]byte, [][]byte, error) { 69 return nil, nil, nil 70 } 71 72 func (m *MemoryDB) GetIterator(key []byte) (db.IIterator, error) { 73 return nil, nil 74 } 75 76 /* 77 type IDBWriter interface { 78 Put(key []byte, value []byte) (err error) 79 Puts(keys [][]byte, values [][]byte) (err error) 80 Delete(key []byte) (err error) 81 Drop() (err error) 82 } 83 */ 84 85 func (m *MemoryDB) Put(key []byte, value []byte) error { 86 m.lock.Lock() 87 defer m.lock.Unlock() 88 if _, ok := m.db[string(key)]; ok { 89 return fmt.Errorf("already add") 90 } 91 92 m.db[string(key)] = value 93 94 return nil 95 } 96 97 func (m *MemoryDB) Puts(keys [][]byte, values [][]byte) error { 98 for i := 0; i < len(keys); i++ { 99 if err := m.Put(keys[i], values[i]); err != nil { 100 return err 101 } 102 } 103 104 return nil 105 } 106 107 func (m *MemoryDB) Delete(key []byte) error { 108 m.lock.Lock() 109 defer m.lock.Unlock() 110 111 if _, ok := m.db[string(key)]; ok { 112 delete(m.db, string(key)) 113 return nil 114 } 115 116 return fmt.Errorf("invalid key") 117 } 118 119 func (m *MemoryDB) Drop() error { 120 return nil 121 }