Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ds.NewTransaction() now returns an error parameter #36

Merged
merged 3 commits into from
Sep 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}