Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some linters #26

Merged
merged 4 commits into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 44 additions & 11 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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 {
Expand Down
20 changes: 10 additions & 10 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,45 @@ import (
"strings"
)

type DBBackendType string
type BackendType string

// These are valid backend types.
const (
// GoLevelDBBackend represents goleveldb (github.com/syndtr/goleveldb - most
// 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
Expand All @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions go_level_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
57 changes: 28 additions & 29 deletions prefix_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -62,15 +62,15 @@ 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()

return pdb.db.Has(pdb.prefixed(key))
}

// 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()

Expand All @@ -79,31 +79,31 @@ 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()

pdb.db.SetSync(pdb.prefixed(key), value)
}

// Implements DB.
func (pdb *prefixDB) Delete(key []byte) {
func (pdb *PrefixDB) Delete(key []byte) {
pdb.mtx.Lock()
defer pdb.mtx.Unlock()

pdb.db.Delete(pdb.prefixed(key))
}

// Implements DB.
func (pdb *prefixDB) DeleteSync(key []byte) {
func (pdb *PrefixDB) DeleteSync(key []byte) {
pdb.mtx.Lock()
defer pdb.mtx.Unlock()

pdb.db.DeleteSync(pdb.prefixed(key))
}

// 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()

Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -158,36 +158,36 @@ 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()

pdb.db.Close()
}

// Implements DB.
func (pdb *prefixDB) Print() {
func (pdb *PrefixDB) Print() {
fmt.Printf("prefix: %X\n", pdb.prefix)

itr := pdb.Iterator(nil, nil)
Expand All @@ -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)
Expand All @@ -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...)
}

Expand Down Expand Up @@ -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,
}
}

Expand Down
4 changes: 2 additions & 2 deletions remotedb/grpcdb/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Loading