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

feat!: add goleveldb and remove pebbledb build flag #202

Merged
merged 5 commits into from
Sep 20, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Add `goleveldb` build flag.
([\#202](https://github.com/cometbft/cometbft-db/pull/202))
137 changes: 80 additions & 57 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,66 @@ run:
timeout: 10m

linters:
disable-all: true
enable:
- copyloopvar
- errcheck
- gci
- goconst
- gocritic
- gofumpt
- gosec
- gosimple
- govet
- ineffassign
- misspell
- nakedret
- staticcheck
- thelper
- typecheck
- stylecheck
- revive
- typecheck
- tenv
- unconvert
# Prefer unparam over revive's unused param. It is more thorough in its checking.
- unparam
- unused
- misspell
enable-all: true
disable:
- containedctx
- contextcheck
- cyclop
- dupword
- errorlint
- errname
- err113
- exhaustive
- exhaustruct
- execinquery
- forbidigo
- forcetypeassert
- funlen
- gochecknoglobals
- gochecknoinits
- gocognit
- gocyclo
- godox
- gomnd
- interfacebloat
- intrange
- ireturn
- lll
- maintidx
- mnd
- nestif
- nilnil
- nlreturn
- nonamedreturns
- predeclared
- tagliatelle
- testifylint
- varnamelen
- wrapcheck
- wsl

issues:
exclude-rules:
- text: 'differs only by capitalization to method'
linters:
- revive
- text: 'Use of weak random number generator'
- path: _test\.go
linters:
- gocritic
- gofmt
- goimport
- gosec
- linters:
- staticcheck
text: "SA1019:" # silence errors on usage of deprecated funcs

- noctx
- paralleltest
- testpackage
- tparallel
max-issues-per-linter: 10000
max-same-issues: 10000

linters-settings:
errcheck:
check-blank: true
dogsled:
max-blank-identifiers: 3
goconst:
ignore-tests: true
misspell:
locale: US
gci:
sections:
- standard # Standard section: captures all standard packages.
Expand All @@ -56,14 +71,27 @@ linters-settings:
- dot # dot imports
- prefix(github.com/cometbft/cometbft-db)
custom-order: true
depguard:
rules:
main:
files:
- $all
- "!$test"
allow:
- $gostd
- github.com/cockroachdb/pebble
- github.com/google/btree
test:
files:
- "$test"
allow:
- $gostd
- github.com/stretchr/testify

revive:
enable-all-rules: true
# Do NOT whine about the following, full explanation found in:
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#description-of-available-rules
rules:
- name: use-any
disabled: true
- name: if-return
- name: comment-spacings # temporarily disabled
disabled: true
- name: max-public-structs
disabled: true
Expand All @@ -73,6 +101,8 @@ linters-settings:
disabled: true
- name: cyclomatic
disabled: true
- name: deep-exit
disabled: true
- name: file-header
disabled: true
- name: function-length
Expand All @@ -87,29 +117,22 @@ linters-settings:
disabled: true
- name: empty-lines
disabled: true
- name: banned-characters
disabled: true
- name: deep-exit
disabled: true
- name: confusing-results
disabled: true
- name: unused-parameter
- name: import-shadowing
disabled: true
- name: modifies-value-receiver
disabled: true
- name: early-return
disabled: true
- name: confusing-naming
disabled: true
- name: defer
disabled: true
# Disabled in favour of unparam.
- name: unused-parameter
- name: unchecked-type-assertion
disabled: true
- name: unhandled-error
disabled: false
disabled: true
arguments:
- 'fmt.Printf'
- 'fmt.Print'
- 'fmt.Println'
- 'myFunction'
- "fmt.Printf"
- "fmt.Print"
- "fmt.Println"
gosec:
excludes:
- G115
12 changes: 1 addition & 11 deletions backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/stretchr/testify/require"
)

// Register a test backend for PrefixDB as well, with some unrelated junk data
// Register a test backend for PrefixDB as well, with some unrelated junk data.
func init() {
//nolint: errcheck, revive // probably should check errors?
registerDBCreator("prefixdb", func(name, dir string) (DB, error) {
Expand Down Expand Up @@ -166,16 +166,6 @@ func TestBackendsGetSetDelete(t *testing.T) {
}
}

func TestGoLevelDBBackend(t *testing.T) {
name := fmt.Sprintf("test_%x", randStr(12))
db, err := NewDB(name, GoLevelDBBackend, "")
require.NoError(t, err)
defer cleanupDBDir("", name)

_, ok := db.(*GoLevelDB)
assert.True(t, ok)
}

func TestDBIterator(t *testing.T) {
for dbType := range backends {
t.Run(string(dbType), func(t *testing.T) {
Expand Down
9 changes: 4 additions & 5 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func benchmarkRangeScans(b *testing.B, db DB, dbSize int64) {
b.StartTimer()

for i := 0; i < b.N; i++ {
start := rand.Int63n(dbSize - rangeSize) //nolint:gosec
start := rand.Int63n(dbSize - rangeSize)
end := start + rangeSize
iter, err := db.Iterator(int642Bytes(start), int642Bytes(end))
require.NoError(b, err)
Expand Down Expand Up @@ -136,7 +136,7 @@ func benchmarkRandomReadsWrites(b *testing.B, db DB) {
for i := 0; i < b.N; i++ {
// Write something
{
idx := rand.Int63n(numItems) //nolint:gosec
idx := rand.Int63n(numItems)
internal[idx]++
val := internal[idx]
idxBytes := int642Bytes(idx)
Expand All @@ -150,7 +150,7 @@ func benchmarkRandomReadsWrites(b *testing.B, db DB) {

// Read something
{
idx := rand.Int63n(numItems) //nolint:gosec
idx := rand.Int63n(numItems)
valExp := internal[idx]
idxBytes := int642Bytes(idx)
valBytes, err := db.Get(idxBytes)
Expand All @@ -175,7 +175,6 @@ func benchmarkRandomReadsWrites(b *testing.B, db DB) {
}
}
}

}
}

Expand All @@ -186,5 +185,5 @@ func int642Bytes(i int64) []byte {
}

func bytes2Int64(buf []byte) int64 {
return int64(binary.BigEndian.Uint64(buf)) //nolint:gosec
return int64(binary.BigEndian.Uint64(buf))
}
14 changes: 7 additions & 7 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ type BackendType string
const (
// GoLevelDBBackend represents goleveldb (github.com/syndtr/goleveldb - most
// popular implementation)
// - UNMAINTANED
// - pure go
// - stable
// - unmaintaned
// - use goleveldb build tag (go build -tags goleveldb)
GoLevelDBBackend BackendType = "goleveldb"
// CLevelDBBackend represents cleveldb (uses levigo wrapper)
// - DEPRECATED
// - fast
// - requires gcc
// - use cleveldb build tag (go build -tags cleveldb)
Expand All @@ -25,22 +27,20 @@ const (
MemDBBackend BackendType = "memdb"
// 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)
// - DEPRECATED
// - pure go
// - use boltdb build tag (go build -tags boltdb)
BoltDBBackend BackendType = "boltdb"
// RocksDBBackend represents rocksdb (uses github.com/tecbot/gorocksdb)
// - EXPERIMENTAL
// - requires gcc
// - use rocksdb build tag (go build -tags rocksdb)
RocksDBBackend BackendType = "rocksdb"
// BadgerDBBackend represents badger (uses github.com/dgraph-io/badger)
// - EXPERIMENTAL
// - pure go
// - use badgerdb build tag (go build -tags badgerdb)
BadgerDBBackend BackendType = "badgerdb"
// PebbleDBDBBackend represents pebble (uses github.com/cockroachdb/pebble)
// - EXPERIMENTAL
// - use pebbledb build tag (go build -tags pebbledb)
// - pure go
PebbleDBBackend BackendType = "pebbledb"
)

Expand Down
3 changes: 3 additions & 0 deletions goleveldb.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build goleveldb
// +build goleveldb

package db

import (
Expand Down
3 changes: 3 additions & 0 deletions goleveldb_batch.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build goleveldb
// +build goleveldb

package db

import (
Expand Down
3 changes: 3 additions & 0 deletions goleveldb_iterator.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build goleveldb
// +build goleveldb

package db

import (
Expand Down
14 changes: 14 additions & 0 deletions goleveldb_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//go:build goleveldb
// +build goleveldb

package db

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/syndtr/goleveldb/leveldb/opt"
)
Expand Down Expand Up @@ -43,3 +47,13 @@ func BenchmarkGoLevelDBRandomReadsWrites(b *testing.B) {

benchmarkRandomReadsWrites(b, db)
}

func TestGoLevelDBBackend(t *testing.T) {
name := fmt.Sprintf("test_%x", randStr(12))
db, err := NewDB(name, GoLevelDBBackend, "")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noted that the GoLevelDBBackend const will always be included because it's part of db.go which doesn't have the goleveldb flag. It's not harmful because unused const are safe in Go, but in the future if we have more build flags, we could add something like

//go:build goleveldb
// +build goleveldb

package db

const (
	GoLevelDBBackend BackendType = "goleveldb"
)

require.NoError(t, err)
defer cleanupDBDir("", name)

_, ok := db.(*GoLevelDB)
assert.True(t, ok)
}
9 changes: 5 additions & 4 deletions memdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package db
import (
"bytes"
"fmt"
"strconv"
"sync"

"github.com/google/btree"
Expand All @@ -14,12 +15,12 @@ const (
)

func init() {
registerDBCreator(MemDBBackend, func(name, dir string) (DB, error) {
registerDBCreator(MemDBBackend, func(_, _ string) (DB, error) {
return NewMemDB(), nil
})
}

// item is a btree.Item with byte slices as keys and values
// item is a btree.Item with byte slices as keys and values.
type item struct {
key []byte
value []byte
Expand Down Expand Up @@ -166,7 +167,7 @@ func (db *MemDB) Stats() map[string]string {

stats := make(map[string]string)
stats["database.type"] = "memDB"
stats["database.size"] = fmt.Sprintf("%d", db.btree.Len())
stats["database.size"] = strconv.Itoa(db.btree.Len())
return stats
}

Expand Down Expand Up @@ -209,7 +210,7 @@ func (db *MemDB) ReverseIteratorNoMtx(start, end []byte) (Iterator, error) {
return newMemDBIteratorMtxChoice(db, start, end, true, false), nil
}

func (*MemDB) Compact(start, end []byte) error {
func (*MemDB) Compact(_, _ []byte) error {
// No Compaction is supported for memDB and there is no point in supporting compaction for a memory DB
return nil
}
Loading
Loading