Skip to content
This repository has been archived by the owner on Apr 24, 2023. It is now read-only.

feat: Name & WriteLowPri methods #35

Merged
merged 12 commits into from
Jan 21, 2022
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- uses: actions/checkout@v2
- name: test & coverage report creation
run: |
go test ./... -mod=readonly -timeout 8m -race -coverprofile=coverage.txt -covermode=atomic -tags=memdb,goleveldb,cleveldb,boltdb,rocksdb,badgerdb -v
CGO_LDFLAGS=-lrocksdb go test ./... -mod=readonly -timeout 8m -race -coverprofile=coverage.txt -covermode=atomic -tags=memdb,goleveldb,cleveldb,boltdb,rocksdb,badgerdb -v
- uses: codecov/codecov-action@v1
with:
file: ./coverage.txt
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* (cleveldb/rocksdb) [\#3](https://github.com/line/tm-db/pull/3) Make path for cleveldb, rocksdb
* (prefix) [\#10](https://github.com/line/tm-db/pull/10) Prefix iterator (#10)
* (api) [\#15](https://github.com/line/tm-db/pull/15) Add AvailableDBBackends function (#15)
* (rdb) [\#34](https://github.com/line/tm-db/pull/34) Name & WriteLowPri methods (#34)

### Improvements
* (global) [\#1](https://github.com/line/tm-db/pull/1) Revise module path
Expand Down
4 changes: 4 additions & 0 deletions badgerdb/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ func (b *badgerDBBatch) WriteSync() error {
return withSync(b.db, b.Write())
}

func (b *badgerDBBatch) WriteLowPri() error {
return b.Write()
}

func (b *badgerDBBatch) Close() error {
select {
case <-b.firstFlush: // a Flush after Cancel panics too
Expand Down
10 changes: 8 additions & 2 deletions badgerdb/db.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package badgerdb

import (
"path"
"path/filepath"

"github.com/dgraph-io/badger/v2"
Expand All @@ -9,7 +10,8 @@ import (
)

type BadgerDB struct {
db *badger.DB
name string
db *badger.DB
}

var _ tmdb.DB = (*BadgerDB)(nil)
Expand Down Expand Up @@ -38,7 +40,11 @@ func NewDBWithOptions(opts badger.Options) (*BadgerDB, error) {
if err != nil {
return nil, err
}
return &BadgerDB{db: db}, nil
return &BadgerDB{name: path.Base(opts.Dir), db: db}, nil
}

func (b *BadgerDB) Name() string {
return b.name
}

func (b *BadgerDB) Get(key []byte) ([]byte, error) {
Expand Down
5 changes: 5 additions & 0 deletions boltdb/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ func (b *boltDBBatch) WriteSync() error {
return b.Write()
}

// WriteLowPri implements Batch.
func (b *boltDBBatch) WriteLowPri() error {
return b.Write()
}

// Close implements Batch.
func (b *boltDBBatch) Close() error {
b.ops = nil
Expand Down
9 changes: 7 additions & 2 deletions boltdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ var bucket = []byte("tm")
// A single bucket ([]byte("tm")) is used per a database instance. This could
// lead to performance issues when/if there will be lots of keys.
type BoltDB struct {
db *bbolt.DB
name string
db *bbolt.DB
}

var _ tmdb.DB = (*BoltDB)(nil)
Expand Down Expand Up @@ -53,7 +54,11 @@ func NewDBWithOpts(name string, dir string, opts *bbolt.Options) (tmdb.DB, error
return nil, err
}

return &BoltDB{db: db}, nil
return &BoltDB{name: name, db: db}, nil
}

func (bdb *BoltDB) Name() string {
return bdb.name
}

// Get implements DB.
Expand Down
5 changes: 5 additions & 0 deletions cleveldb/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ func (b *cLevelDBBatch) WriteSync() error {
return nil
}

// WriteLowPri imelements Batch.
func (b *cLevelDBBatch) WriteLowPri() error {
return b.Write()
}

// Close implements Batch.
func (b *cLevelDBBatch) Close() error {
if b.batch != nil {
Expand Down
6 changes: 6 additions & 0 deletions cleveldb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

// CLevelDB uses the C LevelDB database via a Go wrapper.
type CLevelDB struct {
name string
db *levigo.DB
ro *levigo.ReadOptions
wo *levigo.WriteOptions
Expand Down Expand Up @@ -42,6 +43,7 @@ func NewDB(name string, dir string) (*CLevelDB, error) {
woSync := levigo.NewWriteOptions()
woSync.SetSync(true)
database := &CLevelDB{
name: dbPath,
db: db,
ro: ro,
wo: wo,
Expand All @@ -50,6 +52,10 @@ func NewDB(name string, dir string) (*CLevelDB, error) {
return database, nil
}

func (db *CLevelDB) Name() string {
return db.name
}

// Get implements DB.
func (db *CLevelDB) Get(key []byte) ([]byte, error) {
if len(key) == 0 {
Expand Down
11 changes: 11 additions & 0 deletions contrib/get_cleveldb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
set -e

PWD=$(pwd)
iproudhon marked this conversation as resolved.
Show resolved Hide resolved
version="1.23"
leveldb="leveldb"
archive="${version}.tar.gz"

rm -rf ${leveldb} ${leveldb}-${archive}
wget -O ${leveldb}-${archive} https://github.com/google/leveldb/archive/${archive}
tar -zxvf ${leveldb}-${archive}
mv ${leveldb}-${version} ${leveldb}
12 changes: 12 additions & 0 deletions contrib/get_rocksdb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
set -e

PWD=$(pwd)
iproudhon marked this conversation as resolved.
Show resolved Hide resolved
version="6.20.3"
rocksdb="rocksdb"
rocksdb_dir="rocksdb.build"
archive="v${version}.tar.gz"

rm -rf ${rocksdb_dir} ${rocksdb}-${archive}
wget -O ${rocksdb}-${archive} https://github.com/facebook/rocksdb/archive/${archive}
tar -zxvf ${rocksdb}-${archive}
mv ${rocksdb}-${version} ${rocksdb_dir}
5 changes: 5 additions & 0 deletions goleveldb/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func (b *goLevelDBBatch) WriteSync() error {
return b.write(true)
}

// WriteLowPri implements Batch.
func (b *goLevelDBBatch) WriteLowPri() error {
return b.Write()
}

func (b *goLevelDBBatch) write(sync bool) error {
if b.batch == nil {
return tmdb.ErrBatchClosed
Expand Down
10 changes: 8 additions & 2 deletions goleveldb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
)

type GoLevelDB struct {
db *leveldb.DB
name string
db *leveldb.DB
}

var _ tmdb.DB = (*GoLevelDB)(nil)
Expand All @@ -28,11 +29,16 @@ func NewDBWithOpts(name string, dir string, o *opt.Options) (*GoLevelDB, error)
return nil, err
}
database := &GoLevelDB{
db: db,
name: name,
db: db,
}
return database, nil
}

func (db *GoLevelDB) Name() string {
return db.name
}

// Get implements DB.
func (db *GoLevelDB) Get(key []byte) ([]byte, error) {
if len(key) == 0 {
Expand Down
35 changes: 33 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ GOTOOLS = github.com/golangci/golangci-lint/cmd/golangci-lint
PACKAGES=$(shell go list ./...)
INCLUDE = -I=. -I=${GOPATH}/src -I=${GOPATH}/src/github.com/gogo/protobuf/protobuf

CLEVELDB_DIR=$(shell pwd)/leveldb
ROCKSDB_DIR=$(shell pwd)/rocksdb.build
CGO_CFLAGS=-I$(CLEVELDB_DIR)/include -I$(ROCKSDB_DIR)/include
CGO_LDFLAGS=-L$(CLEVELDB_DIR)/build -L$(ROCKSDB_DIR) -lleveldb -lrocksdb -lm -lstdc++ $(shell awk '/PLATFORM_LDFLAGS/ {sub("PLATFORM_LDFLAGS=", ""); print}' < $(ROCKSDB_DIR)/make_config.mk)

export GO111MODULE = on

all: lint test

### go tests
## By default this will only test memdb & goleveldb
test:
test: cleveldb rocksdb.build
@echo "--> Running go test"
@go test $(PACKAGES) -tags memdb,goleveldb -v
@CGO_CFLAGS="$(CGO_CFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" \
go test $(PACKAGES) -tags memdb,goleveldb -v

test-memdb:
@echo "--> Running go test"
Expand All @@ -28,6 +34,10 @@ test-rocksdb:
@echo "--> Running go test"
@go test ./rocksdb/... -tags rocksdb -v

test-rdb:
@echo "--> Running go test"
@go test ./rdb/... -tags rocksdb -v

test-boltdb:
@echo "--> Running go test"
@go test ./boltdb/... -tags boltdb -v
Expand Down Expand Up @@ -101,6 +111,27 @@ format:
tools:
go get -v $(GOTOOLS)

.PHONY: cleveldb rocksdb
cleveldb:
@if [ ! -e $(CLEVELDB_DIR) ]; then \
sh contrib/get_cleveldb.sh; \
fi
@if [ ! -e $(CLEVELDB_DIR)/libcleveldb.a ]; then \
cd $(CLEVELDB_DIR); \
mkdir build; \
cd build; \
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DLEVELDB_BUILD_TESTS=OFF -DLEVELDB_BUILD_BENCHMARKS=OFF ..; \
make; \
iproudhon marked this conversation as resolved.
Show resolved Hide resolved
fi

rocksdb.build:
@if [ ! -e $(ROCKSDB_DIR) ]; then \
sh ./contrib/get_rocksdb.sh; \
fi
@if [ ! -e $(ROCKSDB_DIR)/librocksdb.a ]; then \
cd $(ROCKSDB_DIR) && make -j4 static_lib; \
fi

# generates certificates for TLS testing in remotedb
gen_certs: clean_certs
certstrap init --common-name "tendermint.com" --passphrase ""
Expand Down
5 changes: 5 additions & 0 deletions memdb/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ func (b *memDBBatch) WriteSync() error {
return b.Write()
}

// WriteLowPri implements Batch.
func (b *memDBBatch) WriteLowPri() error {
return b.Write()
}

// Close implements Batch.
func (b *memDBBatch) Close() error {
b.ops = nil
Expand Down
4 changes: 4 additions & 0 deletions memdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func NewDB() *MemDB {
return database
}

func (db *MemDB) Name() string {
return "nameless-memdb"
}

// Get implements DB.
func (db *MemDB) Get(key []byte) ([]byte, error) {
if len(key) == 0 {
Expand Down
2 changes: 2 additions & 0 deletions metadb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const (
// - EXPERIMENTAL
// - use badgerdb build tag (go build -tags badgerdb)
BadgerDBBackend BackendType = "badgerdb"
// Alternative rocksdb
RDBBackend BackendType = "stonesdb"
)

type dbCreator func(name string, dir string) (tmdb.DB, error)
Expand Down
14 changes: 14 additions & 0 deletions metadb/db_rdb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// +build rocksdb

package metadb

import (
tmdb "github.com/line/tm-db/v2"
"github.com/line/tm-db/v2/rdb"
)

func rdbCreator(name, dir string) (tmdb.DB, error) {
return rdb.NewDB(name, dir)
}

func init() { registerDBCreator(RDBBackend, rdbCreator, true) }
7 changes: 5 additions & 2 deletions metadb/db_rocksdb.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
//go:build rocksdb
// +build rocksdb

package metadb

import (
tmdb "github.com/line/tm-db/v2"
"github.com/line/tm-db/v2/rocksdb"
"github.com/line/tm-db/v2/rdb"
_ "github.com/line/tm-db/v2/rocksdb"
)

func rocksDBCreator(name, dir string) (tmdb.DB, error) {
return rocksdb.NewDB(name, dir)
// TODO: use rdb instead of rocksdb for now as gorocksdb doesn't have low priority write option
return rdb.NewDB(name, dir)
}

func init() { registerDBCreator(RocksDBBackend, rocksDBCreator, true) }
5 changes: 5 additions & 0 deletions prefixdb/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ func (pb prefixDBBatch) WriteSync() error {
return pb.source.WriteSync()
}

// WriteLowPri implements Batch.
func (pb prefixDBBatch) WriteLowPri() error {
return pb.source.WriteLowPri()
}

// Close implements Batch.
func (pb prefixDBBatch) Close() error {
return pb.source.Close()
Expand Down
4 changes: 4 additions & 0 deletions prefixdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func NewDB(db tmdb.DB, prefix []byte) *PrefixDB {
}
}

func (pdb *PrefixDB) Name() string {
return fmt.Sprintf("%s->%s", pdb.db.Name(), pdb.prefix)
}

// Get implements DB.
func (pdb *PrefixDB) Get(key []byte) ([]byte, error) {
if len(key) == 0 {
Expand Down
Loading