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