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.2 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) error {
  73. tx.kv.Put(db.Concat(tx.s.prefix, k), v)
  74. return nil
  75. }
  76. // Commit implements the method Commit of the interface db.Tx
  77. func (tx *MemoryStorageTx) Commit() error {
  78. for _, v := range tx.kv {
  79. tx.s.kv.Put(v.K, v.V)
  80. }
  81. tx.kv = nil
  82. return nil
  83. }
  84. // Add implements the method Add of the interface db.Tx
  85. func (tx *MemoryStorageTx) Add(atx db.Tx) error {
  86. mstx := atx.(*MemoryStorageTx)
  87. for _, v := range mstx.kv {
  88. tx.kv.Put(v.K, v.V)
  89. }
  90. return nil
  91. }
  92. // Close implements the method Close of the interface db.Tx
  93. func (tx *MemoryStorageTx) Close() {
  94. tx.kv = nil
  95. }
  96. // Close implements the method Close of the interface db.Storage
  97. func (m *MemoryStorage) Close() {
  98. }
  99. // List implements the method List of the interface db.Storage
  100. func (m *MemoryStorage) List(limit int) ([]db.KV, error) {
  101. ret := []db.KV{}
  102. err := m.Iterate(func(key []byte, value []byte) (bool, error) {
  103. ret = append(ret, db.KV{K: db.Clone(key), V: db.Clone(value)})
  104. if len(ret) == limit {
  105. return false, nil
  106. }
  107. return true, nil
  108. })
  109. return ret, err
  110. }