Skip to content

Commit

Permalink
Merge pull request #36 from ipfs/feat/ds-update
Browse files Browse the repository at this point in the history
update datastore Interface
  • Loading branch information
aschmahmann committed Dec 3, 2019
2 parents 9ad1663 + d7c8228 commit f885e89
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 23 deletions.
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}
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

0 comments on commit f885e89

Please sign in to comment.