mirror of
https://github.com/arnaucube/go-merkletree-iden3.git
synced 2026-02-07 03:26:46 +01:00
Upgrade linter rules
This commit is contained in:
@@ -7,41 +7,41 @@ import (
|
||||
"github.com/iden3/go-merkletree/db"
|
||||
)
|
||||
|
||||
// MemoryStorage implements the db.Storage interface
|
||||
type MemoryStorage struct {
|
||||
// Storage implements the db.Storage interface
|
||||
type Storage struct {
|
||||
prefix []byte
|
||||
kv db.KvMap
|
||||
}
|
||||
|
||||
// MemoryStorageTx implements the db.Tx interface
|
||||
type MemoryStorageTx struct {
|
||||
s *MemoryStorage
|
||||
// StorageTx implements the db.Tx interface
|
||||
type StorageTx struct {
|
||||
s *Storage
|
||||
kv db.KvMap
|
||||
}
|
||||
|
||||
// NewMemoryStorage returns a new MemoryStorage
|
||||
func NewMemoryStorage() *MemoryStorage {
|
||||
// NewMemoryStorage returns a new Storage
|
||||
func NewMemoryStorage() *Storage {
|
||||
kvmap := make(db.KvMap)
|
||||
return &MemoryStorage{[]byte{}, kvmap}
|
||||
return &Storage{[]byte{}, kvmap}
|
||||
}
|
||||
|
||||
// Info implements the method Info of the interface db.Storage
|
||||
func (m *MemoryStorage) Info() string {
|
||||
func (m *Storage) Info() string {
|
||||
return "in-memory"
|
||||
}
|
||||
|
||||
// WithPrefix implements the method WithPrefix of the interface db.Storage
|
||||
func (m *MemoryStorage) WithPrefix(prefix []byte) db.Storage {
|
||||
return &MemoryStorage{db.Concat(m.prefix, prefix), m.kv}
|
||||
func (m *Storage) WithPrefix(prefix []byte) db.Storage {
|
||||
return &Storage{db.Concat(m.prefix, prefix), m.kv}
|
||||
}
|
||||
|
||||
// NewTx implements the method NewTx of the interface db.Storage
|
||||
func (m *MemoryStorage) NewTx() (db.Tx, error) {
|
||||
return &MemoryStorageTx{m, make(db.KvMap)}, nil
|
||||
func (m *Storage) NewTx() (db.Tx, error) {
|
||||
return &StorageTx{m, make(db.KvMap)}, nil
|
||||
}
|
||||
|
||||
// Get retreives a value from a key in the db.Storage
|
||||
func (m *MemoryStorage) Get(key []byte) ([]byte, error) {
|
||||
func (m *Storage) Get(key []byte) ([]byte, error) {
|
||||
if v, ok := m.kv.Get(db.Concat(m.prefix, key[:])); ok {
|
||||
return v, nil
|
||||
}
|
||||
@@ -49,16 +49,19 @@ func (m *MemoryStorage) Get(key []byte) ([]byte, error) {
|
||||
}
|
||||
|
||||
// Iterate implements the method Iterate of the interface db.Storage
|
||||
func (m *MemoryStorage) Iterate(f func([]byte, []byte) (bool, error)) error {
|
||||
func (m *Storage) Iterate(f func([]byte, []byte) (bool, error)) error {
|
||||
kvs := make([]db.KV, 0)
|
||||
for _, v := range m.kv {
|
||||
if len(v.K) < len(m.prefix) || !bytes.Equal(v.K[:len(m.prefix)], m.prefix) {
|
||||
if len(v.K) < len(m.prefix) ||
|
||||
!bytes.Equal(v.K[:len(m.prefix)], m.prefix) {
|
||||
continue
|
||||
}
|
||||
localkey := v.K[len(m.prefix):]
|
||||
kvs = append(kvs, db.KV{K: localkey, V: v.V})
|
||||
}
|
||||
sort.SliceStable(kvs, func(i, j int) bool { return bytes.Compare(kvs[i].K, kvs[j].K) < 0 })
|
||||
sort.SliceStable(kvs, func(i, j int) bool {
|
||||
return bytes.Compare(kvs[i].K, kvs[j].K) < 0
|
||||
})
|
||||
|
||||
for _, kv := range kvs {
|
||||
if cont, err := f(kv.K, kv.V); err != nil {
|
||||
@@ -71,7 +74,7 @@ func (m *MemoryStorage) Iterate(f func([]byte, []byte) (bool, error)) error {
|
||||
}
|
||||
|
||||
// Get implements the method Get of the interface db.Tx
|
||||
func (tx *MemoryStorageTx) Get(key []byte) ([]byte, error) {
|
||||
func (tx *StorageTx) Get(key []byte) ([]byte, error) {
|
||||
if v, ok := tx.kv.Get(db.Concat(tx.s.prefix, key)); ok {
|
||||
return v, nil
|
||||
}
|
||||
@@ -83,13 +86,13 @@ func (tx *MemoryStorageTx) Get(key []byte) ([]byte, error) {
|
||||
}
|
||||
|
||||
// Put implements the method Put of the interface db.Tx
|
||||
func (tx *MemoryStorageTx) Put(k, v []byte) error {
|
||||
func (tx *StorageTx) Put(k, v []byte) error {
|
||||
tx.kv.Put(db.Concat(tx.s.prefix, k), v)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Commit implements the method Commit of the interface db.Tx
|
||||
func (tx *MemoryStorageTx) Commit() error {
|
||||
func (tx *StorageTx) Commit() error {
|
||||
for _, v := range tx.kv {
|
||||
tx.s.kv.Put(v.K, v.V)
|
||||
}
|
||||
@@ -98,8 +101,8 @@ func (tx *MemoryStorageTx) Commit() error {
|
||||
}
|
||||
|
||||
// Add implements the method Add of the interface db.Tx
|
||||
func (tx *MemoryStorageTx) Add(atx db.Tx) error {
|
||||
mstx := atx.(*MemoryStorageTx)
|
||||
func (tx *StorageTx) Add(atx db.Tx) error {
|
||||
mstx := atx.(*StorageTx)
|
||||
for _, v := range mstx.kv {
|
||||
tx.kv.Put(v.K, v.V)
|
||||
}
|
||||
@@ -107,16 +110,16 @@ func (tx *MemoryStorageTx) Add(atx db.Tx) error {
|
||||
}
|
||||
|
||||
// Close implements the method Close of the interface db.Tx
|
||||
func (tx *MemoryStorageTx) Close() {
|
||||
func (tx *StorageTx) Close() {
|
||||
tx.kv = nil
|
||||
}
|
||||
|
||||
// Close implements the method Close of the interface db.Storage
|
||||
func (m *MemoryStorage) Close() {
|
||||
func (m *Storage) Close() {
|
||||
}
|
||||
|
||||
// List implements the method List of the interface db.Storage
|
||||
func (m *MemoryStorage) List(limit int) ([]db.KV, error) {
|
||||
func (m *Storage) List(limit int) ([]db.KV, error) {
|
||||
ret := []db.KV{}
|
||||
err := m.Iterate(func(key []byte, value []byte) (bool, error) {
|
||||
ret = append(ret, db.KV{K: db.Clone(key), V: db.Clone(value)})
|
||||
|
||||
Reference in New Issue
Block a user