Skip to content

Commit

Permalink
Merge pull request #36 from raulk/txn-err
Browse files Browse the repository at this point in the history
ds.NewTransaction() now returns an error parameter
  • Loading branch information
raulk authored Sep 27, 2018
2 parents 10321f7 + 61f565c commit aa6f265
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .gx/lastpubver
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.8.0: QmaUU1Hu8pbR2NiTJ1NHVBcNTQR499AJVMkkAoRqhdxbgz
1.9.0: Qmd1Zxnuj9puWCPS7DV1YVw1RM478NqyZmf1X5nsgjp6fQ
11 changes: 8 additions & 3 deletions datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ var DefaultOptions = Options{
Options: badger.DefaultOptions,
}

var _ ds.Datastore = (*Datastore)(nil)
var _ ds.TxnDatastore = (*Datastore)(nil)
var _ ds.TTLDatastore = (*Datastore)(nil)

// NewDatastore creates a new badger datastore.
//
// DO NOT set the Dir and/or ValuePath fields of opt, they will be set for you.
Expand Down Expand Up @@ -81,8 +85,8 @@ func NewDatastore(path string, options *Options) (*Datastore, error) {
// NewTransaction starts a new transaction. The resulting transaction object
// can be mutated without incurring changes to the underlying Datastore until
// the transaction is Committed.
func (d *Datastore) NewTransaction(readOnly bool) ds.Txn {
return &txn{d.DB.NewTransaction(!readOnly), false}
func (d *Datastore) NewTransaction(readOnly bool) (ds.Txn, error) {
return &txn{d.DB.NewTransaction(!readOnly), false}, nil
}

// newImplicitTransaction creates a transaction marked as 'implicit'.
Expand Down Expand Up @@ -179,7 +183,8 @@ func (d *Datastore) Close() error {
func (d *Datastore) IsThreadSafe() {}

func (d *Datastore) Batch() (ds.Batch, error) {
return d.NewTransaction(false), nil
tx, _ := d.NewTransaction(false)
return tx, nil
}

func (d *Datastore) CollectGarbage() error {
Expand Down
36 changes: 28 additions & 8 deletions ds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,10 @@ func TestTxnDiscard(t *testing.T) {
t.Fatal(err)
}

txn := d.NewTransaction(false)
txn, err := d.NewTransaction(false)
if err != nil {
t.Fatal(err)
}
key := ds.NewKey("/test/thing")
if err := txn.Put(key, []byte{1, 2, 3}); err != nil {
t.Fatal(err)
Expand All @@ -553,7 +556,10 @@ func TestTxnCommit(t *testing.T) {
t.Fatal(err)
}

txn := d.NewTransaction(false)
txn, err := d.NewTransaction(false)
if err != nil {
t.Fatal(err)
}
key := ds.NewKey("/test/thing")
if err := txn.Put(key, []byte{1, 2, 3}); err != nil {
t.Fatal(err)
Expand All @@ -577,8 +583,10 @@ func TestTxnBatch(t *testing.T) {
t.Fatal(err)
}

txn := d.NewTransaction(false)

txn, err := d.NewTransaction(false)
if err != nil {
t.Fatal(err)
}
data := make(map[ds.Key][]byte)
for i := 0; i < 10; i++ {
key := ds.NewKey(fmt.Sprintf("/test/%d", i))
Expand Down Expand Up @@ -624,7 +632,10 @@ func TestTTL(t *testing.T) {
t.Fatal(err)
}

txn := d.NewTransaction(false)
txn, err := d.NewTransaction(false)
if err != nil {
t.Fatal(err)
}

data := make(map[ds.Key][]byte)
for i := 0; i < 10; i++ {
Expand All @@ -649,7 +660,10 @@ func TestTTL(t *testing.T) {
t.Fatal(err)
}

txn = d.NewTransaction(true)
txn, err = d.NewTransaction(true)
if err != nil {
t.Fatal(err)
}
for key := range data {
_, err := txn.Get(key)
if err != nil {
Expand Down Expand Up @@ -679,7 +693,10 @@ func TestExpirations(t *testing.T) {
d, done := newDS(t)
defer done()

txn := d.NewTransaction(false)
txn, err := d.NewTransaction(false)
if err != nil {
t.Fatal(err)
}
ttltxn := txn.(ds.TTLDatastore)
defer txn.Discard()

Expand All @@ -702,7 +719,10 @@ func TestExpirations(t *testing.T) {
}

// Second transaction to retrieve expirations.
txn = d.NewTransaction(true)
txn, err = d.NewTransaction(true)
if err != nil {
t.Fatal(err)
}
ttltxn = txn.(ds.TTLDatastore)
defer txn.Discard()

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
},
{
"author": "jbenet",
"hash": "QmUyz7JTJzgegC6tiJrfby3mPhzcdswVtG4x58TQ6pq8jV",
"hash": "QmbQshXLNcCPRUGZv4sBGxnZNAHREA6MKeomkwihNXPZWP",
"name": "go-datastore",
"version": "3.2.0"
"version": "3.3.0"
},
{
"author": "dgraph-io",
Expand All @@ -37,6 +37,6 @@
"license": "",
"name": "go-ds-badger",
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
"version": "1.8.0"
"version": "1.9.0"
}

0 comments on commit aa6f265

Please sign in to comment.