go-merkletree-iden3/db/memory/memory.go
db/memory/memory.goRaw
1package memory
2
3import (
4 "bytes"
5 "sort"
6
7 "github.com/iden3/go-merkletree/db"
8)
9
10// MemoryStorage implements the db.Storage interface
11type MemoryStorage struct {
12 prefix []byte
13 kv db.KvMap
14}
15
16// MemoryStorageTx implements the db.Tx interface
17type MemoryStorageTx struct {
18 s *MemoryStorage
19 kv db.KvMap
20}
21
22// NewMemoryStorage returns a new MemoryStorage
23func NewMemoryStorage() *MemoryStorage {
24 kvmap := make(db.KvMap)
25 return &MemoryStorage{[]byte{}, kvmap}
26}
27
28// Info implements the method Info of the interface db.Storage
29func (l *MemoryStorage) Info() string {
30 return "in-memory"
31}
32
33// WithPrefix implements the method WithPrefix of the interface db.Storage
34func (m *MemoryStorage) WithPrefix(prefix []byte) db.Storage {
35 return &MemoryStorage{db.Concat(m.prefix, prefix), m.kv}
36}
37
38// NewTx implements the method NewTx of the interface db.Storage
39func (m *MemoryStorage) NewTx() (db.Tx, error) {
40 return &MemoryStorageTx{m, make(db.KvMap)}, nil
41}
42
43// Get retreives a value from a key in the db.Storage
44func (l *MemoryStorage) Get(key []byte) ([]byte, error) {
45 if v, ok := l.kv.Get(db.Concat(l.prefix, key[:])); ok {
46 return v, nil
47 }
48 return nil, db.ErrNotFound
49}
50
51// Iterate implements the method Iterate of the interface db.Storage
52func (l *MemoryStorage) Iterate(f func([]byte, []byte) (bool, error)) error {
53 kvs := make([]db.KV, 0)
54 for _, v := range l.kv {
55 if len(v.K) < len(l.prefix) || !bytes.Equal(v.K[:len(l.prefix)], l.prefix) {
56 continue
57 }
58 localkey := v.K[len(l.prefix):]
59 kvs = append(kvs, db.KV{K: localkey, V: v.V})
60
61 }
62 sort.SliceStable(kvs, func(i, j int) bool { return bytes.Compare(kvs[i].K, kvs[j].K) < 0 })
63
64 for _, kv := range kvs {
65 if cont, err := f(kv.K, kv.V); err != nil {
66 return err
67 } else if !cont {
68 break
69 }
70 }
71 return nil
72}
73
74// Get implements the method Get of the interface db.Tx
75func (tx *MemoryStorageTx) Get(key []byte) ([]byte, error) {
76
77 if v, ok := tx.kv.Get(db.Concat(tx.s.prefix, key)); ok {
78 return v, nil
79 }
80 if v, ok := tx.s.kv.Get(db.Concat(tx.s.prefix, key)); ok {
81 return v, nil
82 }
83
84 return nil, db.ErrNotFound
85}
86
87// Put implements the method Put of the interface db.Tx
88func (tx *MemoryStorageTx) Put(k, v []byte) {
89 tx.kv.Put(db.Concat(tx.s.prefix, k), v)
90}
91
92// Commit implements the method Commit of the interface db.Tx
93func (tx *MemoryStorageTx) Commit() error {
94 for _, v := range tx.kv {
95 tx.s.kv.Put(v.K, v.V)
96 }
97 tx.kv = nil
98 return nil
99}
100
101// Add implements the method Add of the interface db.Tx
102func (tx *MemoryStorageTx) Add(atx db.Tx) {
103 mstx := atx.(*MemoryStorageTx)
104 for _, v := range mstx.kv {
105 tx.kv.Put(v.K, v.V)
106 }
107}
108
109// Close implements the method Close of the interface db.Tx
110func (tx *MemoryStorageTx) Close() {
111 tx.kv = nil
112}
113
114// Close implements the method Close of the interface db.Storage
115func (m *MemoryStorage) Close() {
116}
117
118// List implements the method List of the interface db.Storage
119func (l *MemoryStorage) List(limit int) ([]db.KV, error) {
120 ret := []db.KV{}
121 err := l.Iterate(func(key []byte, value []byte) (bool, error) {
122 ret = append(ret, db.KV{K: db.Clone(key), V: db.Clone(value)})
123 if len(ret) == limit {
124 return false, nil
125 }
126 return true, nil
127 })
128 return ret, err
129}