Skip to content

Commit

Permalink
refactor: Make rootstore require Batching and TxnDatastore (sourcenet…
Browse files Browse the repository at this point in the history
…work#940)

Relevant issue(s)
Resolves sourcenetwork#683

Description
This PR change the requirement for our supported datastore to implement both ds.Batching and ds.TxnDatastore. This means that the map datastore is no longer supported.
  • Loading branch information
fredcarle committed Dec 19, 2022
1 parent 73a2fc4 commit daa1319
Show file tree
Hide file tree
Showing 24 changed files with 147 additions and 196 deletions.
4 changes: 2 additions & 2 deletions cli/serverdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
"os"
"os/signal"

ds "github.com/ipfs/go-datastore"
"github.com/spf13/cobra"

ds "github.com/sourcenetwork/defradb/datastore"
badgerds "github.com/sourcenetwork/defradb/datastore/badger/v3"
"github.com/sourcenetwork/defradb/db"
"github.com/sourcenetwork/defradb/errors"
Expand All @@ -36,7 +36,7 @@ var serverDumpCmd = &cobra.Command{
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt)

var rootstore ds.Batching
var rootstore ds.RootStore
var err error
if datastore == badgerDatastoreName {
info, err := os.Stat(cfg.Datastore.Badger.Path)
Expand Down
4 changes: 2 additions & 2 deletions cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"strings"

badger "github.com/dgraph-io/badger/v3"
ds "github.com/ipfs/go-datastore"
ma "github.com/multiformats/go-multiaddr"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -30,6 +29,7 @@ import (
httpapi "github.com/sourcenetwork/defradb/api/http"
"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/config"
ds "github.com/sourcenetwork/defradb/datastore"
badgerds "github.com/sourcenetwork/defradb/datastore/badger/v3"
"github.com/sourcenetwork/defradb/db"
"github.com/sourcenetwork/defradb/errors"
Expand Down Expand Up @@ -203,7 +203,7 @@ func (di *defraInstance) close(ctx context.Context) {
func start(ctx context.Context) (*defraInstance, error) {
log.FeedbackInfo(ctx, "Starting DefraDB service...")

var rootstore ds.Batching
var rootstore ds.RootStore

var err error
if cfg.Datastore.Store == badgerDatastoreName {
Expand Down
3 changes: 1 addition & 2 deletions client/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package client
import (
"context"

ds "github.com/ipfs/go-datastore"
blockstore "github.com/ipfs/go-ipfs-blockstore"

"github.com/sourcenetwork/defradb/datastore"
Expand All @@ -28,7 +27,7 @@ type DB interface {
GetCollectionBySchemaID(context.Context, string) (Collection, error)
GetAllCollections(ctx context.Context) ([]Collection, error)

Root() ds.Batching
Root() datastore.RootStore
Blockstore() blockstore.Blockstore

NewTxn(context.Context, bool) (datastore.Txn, error)
Expand Down
12 changes: 11 additions & 1 deletion datastore/memory/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,15 @@ import "github.com/sourcenetwork/defradb/errors"
var (
ErrReadOnlyTxn = errors.New("read only transaction")
ErrTxnDiscarded = errors.New("transaction discarded")
ErrTxnConflict = errors.New("transaction conflict")
ErrTxnConflict = txnConflictError{errors.New("transaction conflict")}
)

type txnConflictError struct {
error
}

// custom error formatting that is non idiomatic but matches the
// Badger transaction conflict message
func (e txnConflictError) Error() string {
return "Transaction Conflict. Please retry"
}
2 changes: 1 addition & 1 deletion datastore/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (d *Datastore) Put(ctx context.Context, key ds.Key, value []byte) (err erro

// Query implements ds.Query
func (d *Datastore) Query(ctx context.Context, q dsq.Query) (dsq.Results, error) {
re := make([]dsq.Entry, 0, d.values.Len())
re := make([]dsq.Entry, 0, d.values.Height())
iter := d.values.Iter()
for iter.Next() {
// fast forward to last inserted version
Expand Down
22 changes: 10 additions & 12 deletions datastore/memory/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (t *basicTxn) Query(ctx context.Context, q dsq.Query) (dsq.Results, error)
return nil, ErrTxnDiscarded
}
// best effort allocation
re := make([]dsq.Entry, 0, t.ds.values.Len()+t.ops.Len())
re := make([]dsq.Entry, 0, t.ds.values.Height()+t.ops.Height())
iter := t.ds.values.Iter()
iterOps := t.ops.Iter()
iterOpsHasValue := iterOps.Next()
Expand Down Expand Up @@ -208,20 +208,18 @@ func (t *basicTxn) Commit(ctx context.Context) error {
if t.discarded {
return ErrTxnDiscarded
}
if t.readOnly {
return ErrReadOnlyTxn
}
defer t.Discard(ctx)

c := commit{
tx: t,
err: make(chan error),
if !t.readOnly {
c := commit{
tx: t,
err: make(chan error),
}
t.ds.commit <- c
return <-c.err
}
t.ds.commit <- c
e := <-c.err

t.Discard(ctx)

return e
return nil
}

func (t *basicTxn) checkForConflicts(ctx context.Context) error {
Expand Down
Loading

0 comments on commit daa1319

Please sign in to comment.