Skip to content

Commit

Permalink
Merge pull request #24 from ChainSafe/eclesio/upgrade-to-badger-v3
Browse files Browse the repository at this point in the history
feat: upgrade project to use `badger/v4`
  • Loading branch information
EclesioMeloJunior authored May 24, 2023
2 parents 82cdba6 + ea23c57 commit 413097f
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 49 deletions.
7 changes: 4 additions & 3 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import (
"sync"

log "github.com/ChainSafe/log15"
"github.com/dgraph-io/badger/v2"
"github.com/dgraph-io/badger/v2/options"
"github.com/dgraph-io/badger/v4"
"github.com/dgraph-io/badger/v4/options"
"github.com/dgraph-io/badger/v4/pb"
)

// BadgerDB contains directory path to data and db instance
Expand Down Expand Up @@ -184,7 +185,7 @@ func (db *BadgerDB) ClearAll() error {
}

// Subscribe to watch for changes for the given prefixes
func (db *BadgerDB) Subscribe(ctx context.Context, cb func(kv *KVList) error, prefixes []byte) error {
func (db *BadgerDB) Subscribe(ctx context.Context, cb func(kv *KVList) error, prefixes []pb.Match) error {
return db.db.Subscribe(ctx, cb, prefixes)
}

Expand Down
7 changes: 5 additions & 2 deletions database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import (
"testing"
"time"

"github.com/dgraph-io/badger/v2"
"github.com/dgraph-io/badger/v4"
"github.com/dgraph-io/badger/v4/pb"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -382,7 +383,9 @@ func newBadgerCompressed(t *testing.T, compress bool, filePath string) Database
}

func ExampleDB_Subscribe() {
prefix := []byte{'a'}
prefix := []pb.Match{
{Prefix: []byte{'a'}},
}

// This key should be printed, since it matches the prefix.
aKey := []byte("a-key")
Expand Down
2 changes: 1 addition & 1 deletion errors.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package chaindb

import (
"github.com/dgraph-io/badger/v2"
"github.com/dgraph-io/badger/v4"
)

// ErrKeyNotFound is returned if there is a database get for a key that does not exist
Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.19

require (
github.com/ChainSafe/log15 v1.0.0
github.com/dgraph-io/badger/v2 v2.2007.4
github.com/dgraph-io/badger/v4 v4.1.0
github.com/stretchr/testify v1.8.2
)

Expand All @@ -16,14 +16,19 @@ require (
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.0.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/flatbuffers v23.1.21+incompatible // indirect
github.com/klauspost/compress v1.16.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
Expand Down
146 changes: 107 additions & 39 deletions go.sum

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package chaindb
import (
"context"
"io"

"github.com/dgraph-io/badger/v4/pb"
)

// Database wraps all database operations. All methods are safe for concurrent use.
Expand All @@ -30,7 +32,7 @@ type Database interface {
NewBatch() Batch
Path() string
NewIterator() Iterator
Subscribe(ctx context.Context, cb func(kv *KVList) error, prefixes []byte) error
Subscribe(ctx context.Context, cb func(kv *KVList) error, prefixes []pb.Match) error
ClearAll() error
}

Expand Down
5 changes: 3 additions & 2 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import (
"fmt"

log "github.com/ChainSafe/log15"
"github.com/dgraph-io/badger/v2"
"github.com/dgraph-io/badger/v4"
"github.com/dgraph-io/badger/v4/pb"
)

type table struct {
Expand Down Expand Up @@ -217,6 +218,6 @@ func (tb *tableBatch) Del(k []byte) error {
return tb.batch.Del(append([]byte(tb.prefix), k...))
}

func (dt *table) Subscribe(ctx context.Context, cb func(kv *KVList) error, prefixes []byte) error {
func (dt *table) Subscribe(ctx context.Context, cb func(kv *KVList) error, prefixes []pb.Match) error {
return dt.db.Subscribe(ctx, cb, prefixes)
}

0 comments on commit 413097f

Please sign in to comment.