You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
4.0 KiB

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