Skip to content

Commit

Permalink
Merge pull request #36 from The-K-R-O-K/with-pebble-db-option
Browse files Browse the repository at this point in the history
Add WithPebbleDB configuration option to Datastore implementation
  • Loading branch information
gammazero authored Aug 28, 2024
2 parents a1c4325 + b11ba40 commit d745b9d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
31 changes: 23 additions & 8 deletions datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/cockroachdb/pebble"
ds "github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/query"
log "github.com/ipfs/go-log/v2"
"github.com/ipfs/go-log/v2"
"github.com/jbenet/goprocess"
)

Expand All @@ -31,26 +31,41 @@ type Datastore struct {
var _ ds.Datastore = (*Datastore)(nil)
var _ ds.Batching = (*Datastore)(nil)

type DatastoreOption func(*Datastore)

// WithPebbleDB is used to configure the Datastore with a custom DB.
func WithPebbleDB(db *pebble.DB) DatastoreOption {
return func(ds *Datastore) {
ds.db = db
}
}

// NewDatastore creates a pebble-backed datastore.
// Users can provide pebble options or rely on Pebble's defaults.
func NewDatastore(path string, opts *pebble.Options) (*Datastore, error) {
func NewDatastore(path string, opts *pebble.Options, options ...DatastoreOption) (*Datastore, error) {
if opts == nil {
opts = &pebble.Options{}
opts.EnsureDefaults()
}
opts.Logger = logger

db, err := pebble.Open(path, opts)
if err != nil {
return nil, fmt.Errorf("failed to open pebble database: %w", err)
}

store := &Datastore{
db: db,
opts: opts,
closing: make(chan struct{}),
}

for _, opt := range options {
opt(store)
}

if store.db == nil {
db, err := pebble.Open(path, opts)
if err != nil {
return nil, fmt.Errorf("failed to open pebble database: %w", err)
}
store.db = db
}

return store, nil
}

Expand Down
36 changes: 36 additions & 0 deletions datastore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"testing"

"github.com/cockroachdb/pebble"
"github.com/ipfs/go-datastore"
dstest "github.com/ipfs/go-datastore/test"
)
Expand Down Expand Up @@ -36,10 +37,45 @@ func newDatastore(t *testing.T) (*Datastore, func()) {
}
}

func newDatastoreWithPebbleDB(t *testing.T) (*Datastore, func()) {
t.Helper()

path, err := os.MkdirTemp(os.TempDir(), "testing_pebble_with_db")
if err != nil {
t.Fatal(err)
}

db, err := pebble.Open(path, nil)
if err != nil {
t.Fatal(err)
}

d, err := NewDatastore(path, nil, WithPebbleDB(db))
if err != nil {
t.Fatal(err)
}

return d, func() {
_ = d.Close()
_ = os.RemoveAll(path)
}
}

func TestGet(t *testing.T) {
ds, cleanup := newDatastore(t)
defer cleanup()

testDatastore(t, ds)
}

func TestGetWithPebbleDB(t *testing.T) {
ds, cleanup := newDatastoreWithPebbleDB(t)
defer cleanup()

testDatastore(t, ds)
}

func testDatastore(t *testing.T, ds *Datastore) {
ctx := context.Background()
k := datastore.NewKey("a")
v := []byte("val")
Expand Down

0 comments on commit d745b9d

Please sign in to comment.