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

Update Datastore Interface #36

Merged
merged 3 commits into from
Dec 3, 2019
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
9 changes: 0 additions & 9 deletions Makefile

This file was deleted.

27 changes: 17 additions & 10 deletions datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func NewDatastore(path string, opts *Options) (*Datastore, error) {
}

return &Datastore{
accessor: &accessor{ldb: db},
accessor: &accessor{ldb: db, syncWrites: true},
DB: db,
path: path,
}, nil
Expand All @@ -72,11 +72,16 @@ type levelDbOps interface {

// Datastore operations using either the DB or a transaction as the backend.
type accessor struct {
ldb levelDbOps
ldb levelDbOps
syncWrites bool
}

func (a *accessor) Put(key ds.Key, value []byte) (err error) {
return a.ldb.Put(key.Bytes(), value, nil)
return a.ldb.Put(key.Bytes(), value, &opt.WriteOptions{Sync: a.syncWrites})
}

func (a *accessor) Sync(prefix ds.Key) error {
return nil
}

func (a *accessor) Get(key ds.Key) (value []byte, err error) {
Expand All @@ -99,7 +104,7 @@ func (d *accessor) GetSize(key ds.Key) (size int, err error) {
}

func (a *accessor) Delete(key ds.Key) (err error) {
return a.ldb.Delete(key.Bytes(), nil)
return a.ldb.Delete(key.Bytes(), &opt.WriteOptions{Sync: a.syncWrites})
}

func (a *accessor) Query(q dsq.Query) (dsq.Results, error) {
Expand Down Expand Up @@ -180,14 +185,16 @@ func (d *Datastore) Close() (err error) {
}

type leveldbBatch struct {
b *leveldb.Batch
db *leveldb.DB
b *leveldb.Batch
db *leveldb.DB
syncWrites bool
}

func (d *Datastore) Batch() (ds.Batch, error) {
return &leveldbBatch{
b: new(leveldb.Batch),
db: d.DB,
b: new(leveldb.Batch),
db: d.DB,
syncWrites: d.syncWrites,
}, nil
}

Expand All @@ -197,7 +204,7 @@ func (b *leveldbBatch) Put(key ds.Key, value []byte) error {
}

func (b *leveldbBatch) Commit() error {
return b.db.Write(b.b, nil)
return b.db.Write(b.b, &opt.WriteOptions{Sync: b.syncWrites})
}

func (b *leveldbBatch) Delete(key ds.Key) error {
Expand All @@ -224,6 +231,6 @@ func (d *Datastore) NewTransaction(readOnly bool) (ds.Txn, error) {
if err != nil {
return nil, err
}
accessor := &accessor{tx}
accessor := &accessor{ldb: tx, syncWrites: false}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

false was chosen here because it's the default behavior and since the internal transaction totally ignores WriteOptions anyway.

return &transaction{accessor, tx}, nil
}
2 changes: 1 addition & 1 deletion ds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var testcases = map[string]string{
// d, close := newDS(t)
// defer close()
func newDS(t *testing.T) (*Datastore, func()) {
path, err := ioutil.TempDir("/tmp", "testing_leveldb_")
path, err := ioutil.TempDir("", "testing_leveldb_")
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/ipfs/go-ds-leveldb

require (
github.com/ipfs/go-datastore v0.2.0
github.com/ipfs/go-datastore v0.3.0
github.com/syndtr/goleveldb v1.0.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/ipfs/go-datastore v0.2.0 h1:5Wjw6YXzZmtqU1MSrlws64+oLmSqea7gEajTcJickh8=
github.com/ipfs/go-datastore v0.2.0/go.mod h1:w38XXW9kVFNp57Zj5knbKWM2T+KOZCGDRVNdgPHtbHw=
github.com/ipfs/go-datastore v0.3.0 h1:9au0tYi/+n7xeUnGHG6davnS8x9hWbOzP/388Vx3CMs=
github.com/ipfs/go-datastore v0.3.0/go.mod h1:w38XXW9kVFNp57Zj5knbKWM2T+KOZCGDRVNdgPHtbHw=
github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw=
github.com/jbenet/goprocess v0.0.0-20160826012719-b497e2f366b8 h1:bspPhN+oKYFk5fcGNuQzp6IGzYQSenLEgH3s6jkXrWw=
github.com/jbenet/goprocess v0.0.0-20160826012719-b497e2f366b8/go.mod h1:Ly/wlsjFq/qrU3Rar62tu1gASgGw6chQbSh/XgIIXCY=
Expand Down