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.

129 lines
3.1 KiB

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