From cd4f1b7a2bb4a93e06f95df103640e9725c88914 Mon Sep 17 00:00:00 2001 From: Marko Date: Wed, 6 Nov 2019 17:57:29 +0000 Subject: [PATCH] Add some linters (#26) * Testing out ci Signed-off-by: Marko Baricevic * some linting * add changelog entry --- .golangci.yml | 55 +++++++++++++++++++++++++++++-------- CHANGELOG_PENDING.md | 2 ++ backend_test.go | 4 +-- common_test.go | 8 +++--- db.go | 20 +++++++------- go_level_db_test.go | 4 +-- prefix_db.go | 57 +++++++++++++++++++-------------------- remotedb/grpcdb/server.go | 4 +-- remotedb/remotedb_test.go | 2 +- 9 files changed, 95 insertions(+), 61 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 4b985b75f..82c6fef4f 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,13 +1,46 @@ -run: - deadline: 1m - linters: - enable-all: true - disable: - - scopelint - - gochecknoinits - - unconvert - - gochecknoglobals - - staticcheck + disable-all: true + enable: + - bodyclose + - deadcode + - depguard + - dogsled + - dupl + - errcheck + # - funlen + # - gochecknoglobals + # - gochecknoinits + - goconst + - gocritic + # - gocyclo + # - godox + - gofmt + - goimports - golint - - unparam + - gosec + - gosimple + - govet + - ineffassign + - interfacer + - lll + - misspell + - maligned + - nakedret + - prealloc + # - scopelint + - staticcheck + - structcheck + - stylecheck + - typecheck + - unconvert + # - unparam + - unused + - varcheck + # - whitespace + # - wsl + # - gocognit + +linters-settings: + maligned: + # print struct with more effective memory layout or not, false by default + suggest-new: true diff --git a/CHANGELOG_PENDING.md b/CHANGELOG_PENDING.md index d21030f1c..10c642bf4 100644 --- a/CHANGELOG_PENDING.md +++ b/CHANGELOG_PENDING.md @@ -6,4 +6,6 @@ Special thanks to external contributors on this release: ### BREAKING CHANGES +- [\#26](https://github.com/tendermint/tm-db/pull/26/files) Rename `DBBackendtype` to `BackendType` + ### IMPROVEMENTS diff --git a/backend_test.go b/backend_test.go index 8e4c376e3..9779a1ac0 100644 --- a/backend_test.go +++ b/backend_test.go @@ -18,7 +18,7 @@ func cleanupDBDir(dir, name string) { } } -func testBackendGetSetDelete(t *testing.T, backend DBBackendType) { +func testBackendGetSetDelete(t *testing.T, backend BackendType) { // Default dirname, err := ioutil.TempDir("", fmt.Sprintf("test_backend_%s_", backend)) require.Nil(t, err) @@ -163,7 +163,7 @@ func TestDBIterator(t *testing.T) { } } -func testDBIterator(t *testing.T, backend DBBackendType) { +func testDBIterator(t *testing.T, backend BackendType) { name := fmt.Sprintf("test_%x", randStr(12)) dir := os.TempDir() db := NewDB(name, backend, dir) diff --git a/common_test.go b/common_test.go index db4a18cee..4fe3b83b3 100644 --- a/common_test.go +++ b/common_test.go @@ -63,7 +63,7 @@ func checkValuePanics(t *testing.T, itr Iterator) { assert.Panics(t, func() { itr.Value() }, "checkValuePanics expected panic but didn't") } -func newTempDB(t *testing.T, backend DBBackendType) (db DB, dbDir string) { +func newTempDB(t *testing.T, backend BackendType) (db DB, dbDir string) { dirname, err := ioutil.TempDir("", "db_common_test") require.Nil(t, err) return NewDB("testdb", backend, dirname), dirname @@ -211,8 +211,8 @@ func benchmarkRandomReadsWrites(b *testing.B, db DB) { idx := int64(rand.Int()) % numItems // nolint:gosec testing file, so accepting weak random number generator internal[idx]++ val := internal[idx] - idxBytes := int642Bytes(int64(idx)) - valBytes := int642Bytes(int64(val)) + idxBytes := int642Bytes(idx) + valBytes := int642Bytes(val) //fmt.Printf("Set %X -> %X\n", idxBytes, valBytes) db.Set(idxBytes, valBytes) } @@ -221,7 +221,7 @@ func benchmarkRandomReadsWrites(b *testing.B, db DB) { { idx := int64(rand.Int()) % numItems // nolint:gosec testing file, so accepting weak random number generator valExp := internal[idx] - idxBytes := int642Bytes(int64(idx)) + idxBytes := int642Bytes(idx) valBytes := db.Get(idxBytes) //fmt.Printf("Get %X -> %X\n", idxBytes, valBytes) if valExp == 0 { diff --git a/db.go b/db.go index eff590eba..ca7d175fa 100644 --- a/db.go +++ b/db.go @@ -5,7 +5,7 @@ import ( "strings" ) -type DBBackendType string +type BackendType string // These are valid backend types. const ( @@ -13,37 +13,37 @@ const ( // popular implementation) // - pure go // - stable - GoLevelDBBackend DBBackendType = "goleveldb" + GoLevelDBBackend BackendType = "goleveldb" // CLevelDBBackend represents cleveldb (uses levigo wrapper) // - fast // - requires gcc // - use cleveldb build tag (go build -tags cleveldb) - CLevelDBBackend DBBackendType = "cleveldb" + CLevelDBBackend BackendType = "cleveldb" // MemDBBackend represents in-memoty key value store, which is mostly used // for testing. - MemDBBackend DBBackendType = "memdb" + MemDBBackend BackendType = "memdb" // FSDBBackend represents filesystem database // - EXPERIMENTAL // - slow - FSDBBackend DBBackendType = "fsdb" + FSDBBackend BackendType = "fsdb" // BoltDBBackend represents bolt (uses etcd's fork of bolt - // github.com/etcd-io/bbolt) // - EXPERIMENTAL // - may be faster is some use-cases (random reads - indexer) // - use boltdb build tag (go build -tags boltdb) - BoltDBBackend DBBackendType = "boltdb" + BoltDBBackend BackendType = "boltdb" // RocksDBBackend represents rocksdb (uses github.com/tecbot/gorocksdb) // - EXPERIMENTAL // - requires gcc // - use rocksdb build tag (go build -tags rocksdb) - RocksDBBackend DBBackendType = "rocksdb" + RocksDBBackend BackendType = "rocksdb" ) type dbCreator func(name string, dir string) (DB, error) -var backends = map[DBBackendType]dbCreator{} +var backends = map[BackendType]dbCreator{} -func registerDBCreator(backend DBBackendType, creator dbCreator, force bool) { +func registerDBCreator(backend BackendType, creator dbCreator, force bool) { _, ok := backends[backend] if !force && ok { return @@ -55,7 +55,7 @@ func registerDBCreator(backend DBBackendType, creator dbCreator, force bool) { // NOTE: function panics if: // - backend is unknown (not registered) // - creator function, provided during registration, returns error -func NewDB(name string, backend DBBackendType, dir string) DB { +func NewDB(name string, backend BackendType, dir string) DB { dbCreator, ok := backends[backend] if !ok { keys := make([]string, len(backends)) diff --git a/go_level_db_test.go b/go_level_db_test.go index 06f59489f..e1c879f10 100644 --- a/go_level_db_test.go +++ b/go_level_db_test.go @@ -21,11 +21,11 @@ func TestGoLevelDBNewGoLevelDB(t *testing.T) { // Test we can open the db twice for reading only ro1, err := NewGoLevelDBWithOpts(name, "", &opt.Options{ReadOnly: true}) - defer ro1.Close() require.Nil(t, err) + defer ro1.Close() ro2, err := NewGoLevelDBWithOpts(name, "", &opt.Options{ReadOnly: true}) - defer ro2.Close() require.Nil(t, err) + defer ro2.Close() } func BenchmarkGoLevelDBRandomReadsWrites(b *testing.B) { diff --git a/prefix_db.go b/prefix_db.go index 0dd06ef9d..ced82f922 100644 --- a/prefix_db.go +++ b/prefix_db.go @@ -32,27 +32,27 @@ func IteratePrefixStripped(db DB, prefix []byte) Iterator { //---------------------------------------- // prefixDB -type prefixDB struct { +type PrefixDB struct { mtx sync.Mutex prefix []byte db DB } // NewPrefixDB lets you namespace multiple DBs within a single DB. -func NewPrefixDB(db DB, prefix []byte) *prefixDB { - return &prefixDB{ +func NewPrefixDB(db DB, prefix []byte) *PrefixDB { + return &PrefixDB{ prefix: prefix, db: db, } } // Implements atomicSetDeleter. -func (pdb *prefixDB) Mutex() *sync.Mutex { +func (pdb *PrefixDB) Mutex() *sync.Mutex { return &(pdb.mtx) } // Implements DB. -func (pdb *prefixDB) Get(key []byte) []byte { +func (pdb *PrefixDB) Get(key []byte) []byte { pdb.mtx.Lock() defer pdb.mtx.Unlock() @@ -62,7 +62,7 @@ func (pdb *prefixDB) Get(key []byte) []byte { } // Implements DB. -func (pdb *prefixDB) Has(key []byte) bool { +func (pdb *PrefixDB) Has(key []byte) bool { pdb.mtx.Lock() defer pdb.mtx.Unlock() @@ -70,7 +70,7 @@ func (pdb *prefixDB) Has(key []byte) bool { } // Implements DB. -func (pdb *prefixDB) Set(key []byte, value []byte) { +func (pdb *PrefixDB) Set(key []byte, value []byte) { pdb.mtx.Lock() defer pdb.mtx.Unlock() @@ -79,7 +79,7 @@ func (pdb *prefixDB) Set(key []byte, value []byte) { } // Implements DB. -func (pdb *prefixDB) SetSync(key []byte, value []byte) { +func (pdb *PrefixDB) SetSync(key []byte, value []byte) { pdb.mtx.Lock() defer pdb.mtx.Unlock() @@ -87,7 +87,7 @@ func (pdb *prefixDB) SetSync(key []byte, value []byte) { } // Implements DB. -func (pdb *prefixDB) Delete(key []byte) { +func (pdb *PrefixDB) Delete(key []byte) { pdb.mtx.Lock() defer pdb.mtx.Unlock() @@ -95,7 +95,7 @@ func (pdb *prefixDB) Delete(key []byte) { } // Implements DB. -func (pdb *prefixDB) DeleteSync(key []byte) { +func (pdb *PrefixDB) DeleteSync(key []byte) { pdb.mtx.Lock() defer pdb.mtx.Unlock() @@ -103,7 +103,7 @@ func (pdb *prefixDB) DeleteSync(key []byte) { } // Implements DB. -func (pdb *prefixDB) Iterator(start, end []byte) Iterator { +func (pdb *PrefixDB) Iterator(start, end []byte) Iterator { pdb.mtx.Lock() defer pdb.mtx.Unlock() @@ -126,7 +126,7 @@ func (pdb *prefixDB) Iterator(start, end []byte) Iterator { } // Implements DB. -func (pdb *prefixDB) ReverseIterator(start, end []byte) Iterator { +func (pdb *PrefixDB) ReverseIterator(start, end []byte) Iterator { pdb.mtx.Lock() defer pdb.mtx.Unlock() @@ -149,7 +149,7 @@ func (pdb *prefixDB) ReverseIterator(start, end []byte) Iterator { // Implements DB. // Panics if the underlying DB is not an // atomicSetDeleter. -func (pdb *prefixDB) NewBatch() Batch { +func (pdb *PrefixDB) NewBatch() Batch { pdb.mtx.Lock() defer pdb.mtx.Unlock() @@ -158,28 +158,28 @@ func (pdb *prefixDB) NewBatch() Batch { /* NOTE: Uncomment to use memBatch instead of prefixBatch // Implements atomicSetDeleter. -func (pdb *prefixDB) SetNoLock(key []byte, value []byte) { +func (pdb *PrefixDB) SetNoLock(key []byte, value []byte) { pdb.db.(atomicSetDeleter).SetNoLock(pdb.prefixed(key), value) } // Implements atomicSetDeleter. -func (pdb *prefixDB) SetNoLockSync(key []byte, value []byte) { +func (pdb *PrefixDB) SetNoLockSync(key []byte, value []byte) { pdb.db.(atomicSetDeleter).SetNoLockSync(pdb.prefixed(key), value) } // Implements atomicSetDeleter. -func (pdb *prefixDB) DeleteNoLock(key []byte) { +func (pdb *PrefixDB) DeleteNoLock(key []byte) { pdb.db.(atomicSetDeleter).DeleteNoLock(pdb.prefixed(key)) } // Implements atomicSetDeleter. -func (pdb *prefixDB) DeleteNoLockSync(key []byte) { +func (pdb *PrefixDB) DeleteNoLockSync(key []byte) { pdb.db.(atomicSetDeleter).DeleteNoLockSync(pdb.prefixed(key)) } */ // Implements DB. -func (pdb *prefixDB) Close() { +func (pdb *PrefixDB) Close() { pdb.mtx.Lock() defer pdb.mtx.Unlock() @@ -187,7 +187,7 @@ func (pdb *prefixDB) Close() { } // Implements DB. -func (pdb *prefixDB) Print() { +func (pdb *PrefixDB) Print() { fmt.Printf("prefix: %X\n", pdb.prefix) itr := pdb.Iterator(nil, nil) @@ -200,7 +200,7 @@ func (pdb *prefixDB) Print() { } // Implements DB. -func (pdb *prefixDB) Stats() map[string]string { +func (pdb *PrefixDB) Stats() map[string]string { stats := make(map[string]string) stats["prefixdb.prefix.string"] = string(pdb.prefix) stats["prefixdb.prefix.hex"] = fmt.Sprintf("%X", pdb.prefix) @@ -211,7 +211,7 @@ func (pdb *prefixDB) Stats() map[string]string { return stats } -func (pdb *prefixDB) prefixed(key []byte) []byte { +func (pdb *PrefixDB) prefixed(key []byte) []byte { return append(cp(pdb.prefix), key...) } @@ -275,14 +275,13 @@ func newPrefixIterator(prefix, start, end []byte, source Iterator) *prefixIterat source: source, valid: false, } - } else { - return &prefixIterator{ - prefix: prefix, - start: start, - end: end, - source: source, - valid: true, - } + } + return &prefixIterator{ + prefix: prefix, + start: start, + end: end, + source: source, + valid: true, } } diff --git a/remotedb/grpcdb/server.go b/remotedb/grpcdb/server.go index 5f9f9733d..93c7e7e3e 100644 --- a/remotedb/grpcdb/server.go +++ b/remotedb/grpcdb/server.go @@ -58,12 +58,12 @@ var _ protodb.DBServer = (*server)(nil) // * fsdb // * memdB // * leveldb -// See https://godoc.org/github.com/tendermint/tendermint/libs/db#DBBackendType +// See https://godoc.org/github.com/tendermint/tendermint/libs/db#BackendType func (s *server) Init(ctx context.Context, in *protodb.Init) (*protodb.Entity, error) { s.mu.Lock() defer s.mu.Unlock() - s.db = db.NewDB(in.Name, db.DBBackendType(in.Type), in.Dir) + s.db = db.NewDB(in.Name, db.BackendType(in.Type), in.Dir) return &protodb.Entity{CreatedAt: time.Now().Unix()}, nil } diff --git a/remotedb/remotedb_test.go b/remotedb/remotedb_test.go index 9ee43a6ae..a0b2bc813 100644 --- a/remotedb/remotedb_test.go +++ b/remotedb/remotedb_test.go @@ -19,7 +19,7 @@ func TestRemoteDB(t *testing.T) { srv, err := grpcdb.NewServer(cert, key) require.Nil(t, err) defer srv.Stop() - go func() { + go func() { //nolint:staticcheck if err := srv.Serve(ln); err != nil { t.Fatalf("BindServer: %v", err) }