From f00136ab46aca53526cac2d2aad6a47c19c4ef94 Mon Sep 17 00:00:00 2001 From: UlyanaAndrukhiv Date: Tue, 23 Jul 2024 13:01:00 +0300 Subject: [PATCH 1/2] Added datastore option WithPebbleDB --- datastore.go | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/datastore.go b/datastore.go index 69e8ddf..db2f407 100644 --- a/datastore.go +++ b/datastore.go @@ -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" ) @@ -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 } From b11ba40ed8c6f7195733d3b821865113dbc0dea9 Mon Sep 17 00:00:00 2001 From: UlyanaAndrukhiv Date: Tue, 23 Jul 2024 13:01:49 +0300 Subject: [PATCH 2/2] Added test --- datastore_test.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/datastore_test.go b/datastore_test.go index 41cb07c..71cdf6f 100644 --- a/datastore_test.go +++ b/datastore_test.go @@ -6,6 +6,7 @@ import ( "os" "testing" + "github.com/cockroachdb/pebble" "github.com/ipfs/go-datastore" dstest "github.com/ipfs/go-datastore/test" ) @@ -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")