mirror of
https://github.com/arnaucube/go-merkletree-iden3.git
synced 2026-02-07 11:36:47 +01:00
Upgrade linter rules
This commit is contained in:
@@ -11,20 +11,20 @@ import (
|
||||
"github.com/syndtr/goleveldb/leveldb/util"
|
||||
)
|
||||
|
||||
// LevelDbStorage implements the db.Storage interface
|
||||
type LevelDbStorage struct {
|
||||
// Storage implements the db.Storage interface
|
||||
type Storage struct {
|
||||
ldb *leveldb.DB
|
||||
prefix []byte
|
||||
}
|
||||
|
||||
// LevelDbStorageTx implements the db.Tx interface
|
||||
type LevelDbStorageTx struct {
|
||||
*LevelDbStorage
|
||||
// StorageTx implements the db.Tx interface
|
||||
type StorageTx struct {
|
||||
*Storage
|
||||
cache db.KvMap
|
||||
}
|
||||
|
||||
// NewLevelDbStorage returns a new LevelDbStorage
|
||||
func NewLevelDbStorage(path string, errorIfMissing bool) (*LevelDbStorage, error) {
|
||||
// NewLevelDbStorage returns a new Storage
|
||||
func NewLevelDbStorage(path string, errorIfMissing bool) (*Storage, error) {
|
||||
o := &opt.Options{
|
||||
ErrorIfMissing: errorIfMissing,
|
||||
}
|
||||
@@ -32,7 +32,7 @@ func NewLevelDbStorage(path string, errorIfMissing bool) (*LevelDbStorage, error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &LevelDbStorage{ldb, []byte{}}, nil
|
||||
return &Storage{ldb, []byte{}}, nil
|
||||
}
|
||||
|
||||
type storageInfo struct {
|
||||
@@ -41,7 +41,7 @@ type storageInfo struct {
|
||||
}
|
||||
|
||||
// Info implements the method Info of the interface db.Storage
|
||||
func (l *LevelDbStorage) Info() string {
|
||||
func (l *Storage) Info() string {
|
||||
snapshot, err := l.ldb.GetSnapshot()
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
@@ -72,17 +72,17 @@ func (l *LevelDbStorage) Info() string {
|
||||
}
|
||||
|
||||
// WithPrefix implements the method WithPrefix of the interface db.Storage
|
||||
func (l *LevelDbStorage) WithPrefix(prefix []byte) db.Storage {
|
||||
return &LevelDbStorage{l.ldb, db.Concat(l.prefix, prefix)}
|
||||
func (l *Storage) WithPrefix(prefix []byte) db.Storage {
|
||||
return &Storage{l.ldb, db.Concat(l.prefix, prefix)}
|
||||
}
|
||||
|
||||
// NewTx implements the method NewTx of the interface db.Storage
|
||||
func (l *LevelDbStorage) NewTx() (db.Tx, error) {
|
||||
return &LevelDbStorageTx{l, make(db.KvMap)}, nil
|
||||
func (l *Storage) NewTx() (db.Tx, error) {
|
||||
return &StorageTx{l, make(db.KvMap)}, nil
|
||||
}
|
||||
|
||||
// Get retreives a value from a key in the db.Storage
|
||||
func (l *LevelDbStorage) Get(key []byte) ([]byte, error) {
|
||||
func (l *Storage) Get(key []byte) ([]byte, error) {
|
||||
v, err := l.ldb.Get(db.Concat(l.prefix, key[:]), nil)
|
||||
if err == errors.ErrNotFound {
|
||||
return nil, db.ErrNotFound
|
||||
@@ -91,7 +91,7 @@ func (l *LevelDbStorage) Get(key []byte) ([]byte, error) {
|
||||
}
|
||||
|
||||
// Iterate implements the method Iterate of the interface db.Storage
|
||||
func (l *LevelDbStorage) Iterate(f func([]byte, []byte) (bool, error)) error {
|
||||
func (l *Storage) Iterate(f func([]byte, []byte) (bool, error)) error {
|
||||
// FIXME: Use the prefix!
|
||||
snapshot, err := l.ldb.GetSnapshot()
|
||||
if err != nil {
|
||||
@@ -112,7 +112,7 @@ func (l *LevelDbStorage) Iterate(f func([]byte, []byte) (bool, error)) error {
|
||||
}
|
||||
|
||||
// Get retreives a value from a key in the interface db.Tx
|
||||
func (tx *LevelDbStorageTx) Get(key []byte) ([]byte, error) {
|
||||
func (tx *StorageTx) Get(key []byte) ([]byte, error) {
|
||||
var err error
|
||||
|
||||
fullkey := db.Concat(tx.prefix, key)
|
||||
@@ -130,14 +130,14 @@ func (tx *LevelDbStorageTx) Get(key []byte) ([]byte, error) {
|
||||
}
|
||||
|
||||
// Put saves a key:value into the db.Storage
|
||||
func (tx *LevelDbStorageTx) Put(k, v []byte) error {
|
||||
func (tx *StorageTx) Put(k, v []byte) error {
|
||||
tx.cache.Put(db.Concat(tx.prefix, k[:]), v)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Add implements the method Add of the interface db.Tx
|
||||
func (tx *LevelDbStorageTx) Add(atx db.Tx) error {
|
||||
ldbtx := atx.(*LevelDbStorageTx)
|
||||
func (tx *StorageTx) Add(atx db.Tx) error {
|
||||
ldbtx := atx.(*StorageTx)
|
||||
for _, v := range ldbtx.cache {
|
||||
tx.cache.Put(v.K, v.V)
|
||||
}
|
||||
@@ -145,7 +145,7 @@ func (tx *LevelDbStorageTx) Add(atx db.Tx) error {
|
||||
}
|
||||
|
||||
// Commit implements the method Commit of the interface db.Tx
|
||||
func (tx *LevelDbStorageTx) Commit() error {
|
||||
func (tx *StorageTx) Commit() error {
|
||||
var batch leveldb.Batch
|
||||
for _, v := range tx.cache {
|
||||
batch.Put(v.K, v.V)
|
||||
@@ -156,12 +156,12 @@ func (tx *LevelDbStorageTx) Commit() error {
|
||||
}
|
||||
|
||||
// Close implements the method Close of the interface db.Tx
|
||||
func (tx *LevelDbStorageTx) Close() {
|
||||
func (tx *StorageTx) Close() {
|
||||
tx.cache = nil
|
||||
}
|
||||
|
||||
// Close implements the method Close of the interface db.Storage
|
||||
func (l *LevelDbStorage) Close() {
|
||||
func (l *Storage) Close() {
|
||||
if err := l.ldb.Close(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -169,12 +169,12 @@ func (l *LevelDbStorage) Close() {
|
||||
}
|
||||
|
||||
// LevelDB is an extra method that returns the *leveldb.DB
|
||||
func (l *LevelDbStorage) LevelDB() *leveldb.DB {
|
||||
func (l *Storage) LevelDB() *leveldb.DB {
|
||||
return l.ldb
|
||||
}
|
||||
|
||||
// List implements the method List of the interface db.Storage
|
||||
func (l *LevelDbStorage) List(limit int) ([]db.KV, error) {
|
||||
func (l *Storage) List(limit int) ([]db.KV, error) {
|
||||
ret := []db.KV{}
|
||||
err := l.Iterate(func(key []byte, value []byte) (bool, error) {
|
||||
ret = append(ret, db.KV{K: db.Clone(key), V: db.Clone(value)})
|
||||
|
||||
@@ -51,7 +51,7 @@ func TestLevelDbInterface(t *testing.T) {
|
||||
func TestMain(m *testing.M) {
|
||||
result := m.Run()
|
||||
for _, dir := range rmDirs {
|
||||
os.RemoveAll(dir)
|
||||
os.RemoveAll(dir) //nolint:errcheck,gosec
|
||||
}
|
||||
os.Exit(result)
|
||||
}
|
||||
|
||||
@@ -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)})
|
||||
|
||||
@@ -8,20 +8,20 @@ import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// PebbleStorage implements the db.Storage interface
|
||||
type PebbleStorage struct {
|
||||
// Storage implements the db.Storage interface
|
||||
type Storage struct {
|
||||
pdb *pebble.DB
|
||||
prefix []byte
|
||||
}
|
||||
|
||||
// PebbleStorageTx implements the db.Tx interface
|
||||
type PebbleStorageTx struct {
|
||||
*PebbleStorage
|
||||
// StorageTx implements the db.Tx interface
|
||||
type StorageTx struct {
|
||||
*Storage
|
||||
batch *pebble.Batch
|
||||
}
|
||||
|
||||
// NewPebbleStorage returns a new PebbleStorage
|
||||
func NewPebbleStorage(path string, errorIfMissing bool) (*PebbleStorage, error) {
|
||||
// NewPebbleStorage returns a new Storage
|
||||
func NewPebbleStorage(path string, errorIfMissing bool) (*Storage, error) {
|
||||
o := &pebble.Options{
|
||||
ErrorIfNotExists: errorIfMissing,
|
||||
}
|
||||
@@ -29,7 +29,7 @@ func NewPebbleStorage(path string, errorIfMissing bool) (*PebbleStorage, error)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &PebbleStorage{rdb, []byte{}}, nil
|
||||
return &Storage{rdb, []byte{}}, nil
|
||||
}
|
||||
|
||||
type storageInfo struct {
|
||||
@@ -38,7 +38,7 @@ type storageInfo struct {
|
||||
}
|
||||
|
||||
// Info implements the method Info of the interface db.Storage
|
||||
func (p *PebbleStorage) Info() string {
|
||||
func (p *Storage) Info() string {
|
||||
keycount := 0
|
||||
claimcount := 0
|
||||
err := p.Iterate(func(key []byte, value []byte) (bool, error) {
|
||||
@@ -63,25 +63,29 @@ func (p *PebbleStorage) Info() string {
|
||||
}
|
||||
|
||||
// WithPrefix implements the method WithPrefix of the interface db.Storage
|
||||
func (p *PebbleStorage) WithPrefix(prefix []byte) db.Storage {
|
||||
return &PebbleStorage{p.pdb, db.Concat(p.prefix, prefix)}
|
||||
func (p *Storage) WithPrefix(prefix []byte) db.Storage {
|
||||
return &Storage{p.pdb, db.Concat(p.prefix, prefix)}
|
||||
}
|
||||
|
||||
// NewTx implements the method NewTx of the interface db.Storage
|
||||
func (p *PebbleStorage) NewTx() (db.Tx, error) {
|
||||
return &PebbleStorageTx{p, p.pdb.NewIndexedBatch()}, nil
|
||||
func (p *Storage) NewTx() (db.Tx, error) {
|
||||
return &StorageTx{p, p.pdb.NewIndexedBatch()}, nil
|
||||
}
|
||||
|
||||
// Get retreives a value from a key in the db.Storage
|
||||
func (p *PebbleStorage) Get(key []byte) ([]byte, error) {
|
||||
func (p *Storage) Get(key []byte) ([]byte, error) {
|
||||
v, closer, err := p.pdb.Get(db.Concat(p.prefix, key[:]))
|
||||
if err == pebble.ErrNotFound {
|
||||
return nil, db.ErrNotFound
|
||||
}
|
||||
closer.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = closer.Close()
|
||||
return v, err
|
||||
}
|
||||
|
||||
//nolint:lll
|
||||
// https://github.com/cockroachdb/pebble/pull/923/files#diff-c2ade2f386c41794d5ebc57ee49b57a5fca8082e03255e5bff13977cbc061287R39
|
||||
func keyUpperBound(b []byte) []byte {
|
||||
end := make([]byte, len(b))
|
||||
@@ -102,7 +106,7 @@ func prefixIterOptions(prefix []byte) *pebble.IterOptions {
|
||||
}
|
||||
|
||||
// Iterate implements the method Iterate of the interface db.Storage
|
||||
func (p *PebbleStorage) Iterate(f func([]byte, []byte) (bool, error)) error {
|
||||
func (p *Storage) Iterate(f func([]byte, []byte) (bool, error)) (err error) {
|
||||
// NewIter already provides a point-in-time view of the current DB
|
||||
// state, but if is used for long term (is not the case), should use an
|
||||
// iterator over an snapshot:
|
||||
@@ -110,7 +114,13 @@ func (p *PebbleStorage) Iterate(f func([]byte, []byte) (bool, error)) error {
|
||||
// defer snapshot.Close()
|
||||
// iter := snapshot.NewIter(nil)
|
||||
iter := p.pdb.NewIter(prefixIterOptions(p.prefix))
|
||||
defer iter.Close()
|
||||
defer func() {
|
||||
err1 := iter.Close()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = err1
|
||||
}()
|
||||
|
||||
for iter.First(); iter.Valid(); iter.Next() {
|
||||
localKey := iter.Key()[len(p.prefix):]
|
||||
@@ -124,7 +134,7 @@ func (p *PebbleStorage) Iterate(f func([]byte, []byte) (bool, error)) error {
|
||||
}
|
||||
|
||||
// Get retreives a value from a key in the interface db.Tx
|
||||
func (tx *PebbleStorageTx) Get(key []byte) ([]byte, error) {
|
||||
func (tx *StorageTx) Get(key []byte) ([]byte, error) {
|
||||
var err error
|
||||
|
||||
fullkey := db.Concat(tx.prefix, key)
|
||||
@@ -133,34 +143,36 @@ func (tx *PebbleStorageTx) Get(key []byte) ([]byte, error) {
|
||||
if err == pebble.ErrNotFound {
|
||||
return nil, db.ErrNotFound
|
||||
}
|
||||
closer.Close()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = closer.Close()
|
||||
return v, err
|
||||
}
|
||||
|
||||
// Put saves a key:value into the db.Storage
|
||||
func (tx *PebbleStorageTx) Put(k, v []byte) error {
|
||||
func (tx *StorageTx) Put(k, v []byte) error {
|
||||
return tx.batch.Set(db.Concat(tx.prefix, k[:]), v, nil)
|
||||
}
|
||||
|
||||
// Add implements the method Add of the interface db.Tx
|
||||
func (tx *PebbleStorageTx) Add(atx db.Tx) error {
|
||||
patx := atx.(*PebbleStorageTx)
|
||||
func (tx *StorageTx) Add(atx db.Tx) error {
|
||||
patx := atx.(*StorageTx)
|
||||
return tx.batch.Apply(patx.batch, nil)
|
||||
}
|
||||
|
||||
// Commit implements the method Commit of the interface db.Tx
|
||||
func (tx *PebbleStorageTx) Commit() error {
|
||||
func (tx *StorageTx) Commit() error {
|
||||
return tx.batch.Commit(nil)
|
||||
}
|
||||
|
||||
// Close implements the method Close of the interface db.Tx
|
||||
func (tx *PebbleStorageTx) Close() {
|
||||
func (tx *StorageTx) Close() {
|
||||
_ = tx.batch.Close()
|
||||
}
|
||||
|
||||
// Close implements the method Close of the interface db.Storage
|
||||
func (p *PebbleStorage) Close() {
|
||||
func (p *Storage) Close() {
|
||||
if err := p.pdb.Close(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -168,12 +180,12 @@ func (p *PebbleStorage) Close() {
|
||||
}
|
||||
|
||||
// Pebble is an extra method that returns the *pebble.DB
|
||||
func (p *PebbleStorage) Pebble() *pebble.DB {
|
||||
func (p *Storage) Pebble() *pebble.DB {
|
||||
return p.pdb
|
||||
}
|
||||
|
||||
// List implements the method List of the interface db.Storage
|
||||
func (p *PebbleStorage) List(limit int) ([]db.KV, error) {
|
||||
func (p *Storage) List(limit int) ([]db.KV, error) {
|
||||
ret := []db.KV{}
|
||||
err := p.Iterate(func(key []byte, value []byte) (bool, error) {
|
||||
ret = append(ret, db.KV{K: db.Clone(key), V: db.Clone(value)})
|
||||
|
||||
@@ -51,7 +51,7 @@ func TestPebbleInterface(t *testing.T) {
|
||||
func TestMain(m *testing.M) {
|
||||
result := m.Run()
|
||||
for _, dir := range rmDirs {
|
||||
os.RemoveAll(dir)
|
||||
os.RemoveAll(dir) //nolint:errcheck,gosec
|
||||
}
|
||||
os.Exit(result)
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//nolint:gomnd
|
||||
//nolint:gomnd,golint
|
||||
package test
|
||||
|
||||
import (
|
||||
|
||||
Reference in New Issue
Block a user