Skip to content

Commit f885e89

Browse files
authored
Merge pull request #36 from ipfs/feat/ds-update
update datastore Interface
2 parents 9ad1663 + d7c8228 commit f885e89

File tree

5 files changed

+21
-23
lines changed

5 files changed

+21
-23
lines changed

Makefile

Lines changed: 0 additions & 9 deletions
This file was deleted.

datastore.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func NewDatastore(path string, opts *Options) (*Datastore, error) {
5353
}
5454

5555
return &Datastore{
56-
accessor: &accessor{ldb: db},
56+
accessor: &accessor{ldb: db, syncWrites: true},
5757
DB: db,
5858
path: path,
5959
}, nil
@@ -72,11 +72,16 @@ type levelDbOps interface {
7272

7373
// Datastore operations using either the DB or a transaction as the backend.
7474
type accessor struct {
75-
ldb levelDbOps
75+
ldb levelDbOps
76+
syncWrites bool
7677
}
7778

7879
func (a *accessor) Put(key ds.Key, value []byte) (err error) {
79-
return a.ldb.Put(key.Bytes(), value, nil)
80+
return a.ldb.Put(key.Bytes(), value, &opt.WriteOptions{Sync: a.syncWrites})
81+
}
82+
83+
func (a *accessor) Sync(prefix ds.Key) error {
84+
return nil
8085
}
8186

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

101106
func (a *accessor) Delete(key ds.Key) (err error) {
102-
return a.ldb.Delete(key.Bytes(), nil)
107+
return a.ldb.Delete(key.Bytes(), &opt.WriteOptions{Sync: a.syncWrites})
103108
}
104109

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

182187
type leveldbBatch struct {
183-
b *leveldb.Batch
184-
db *leveldb.DB
188+
b *leveldb.Batch
189+
db *leveldb.DB
190+
syncWrites bool
185191
}
186192

187193
func (d *Datastore) Batch() (ds.Batch, error) {
188194
return &leveldbBatch{
189-
b: new(leveldb.Batch),
190-
db: d.DB,
195+
b: new(leveldb.Batch),
196+
db: d.DB,
197+
syncWrites: d.syncWrites,
191198
}, nil
192199
}
193200

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

199206
func (b *leveldbBatch) Commit() error {
200-
return b.db.Write(b.b, nil)
207+
return b.db.Write(b.b, &opt.WriteOptions{Sync: b.syncWrites})
201208
}
202209

203210
func (b *leveldbBatch) Delete(key ds.Key) error {
@@ -224,6 +231,6 @@ func (d *Datastore) NewTransaction(readOnly bool) (ds.Txn, error) {
224231
if err != nil {
225232
return nil, err
226233
}
227-
accessor := &accessor{tx}
234+
accessor := &accessor{ldb: tx, syncWrites: false}
228235
return &transaction{accessor, tx}, nil
229236
}

ds_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var testcases = map[string]string{
3030
// d, close := newDS(t)
3131
// defer close()
3232
func newDS(t *testing.T) (*Datastore, func()) {
33-
path, err := ioutil.TempDir("/tmp", "testing_leveldb_")
33+
path, err := ioutil.TempDir("", "testing_leveldb_")
3434
if err != nil {
3535
t.Fatal(err)
3636
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module github.com/ipfs/go-ds-leveldb
22

33
require (
4-
github.com/ipfs/go-datastore v0.2.0
4+
github.com/ipfs/go-datastore v0.3.0
55
github.com/syndtr/goleveldb v1.0.0
66
)
77

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
1010
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
1111
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
1212
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
13-
github.com/ipfs/go-datastore v0.2.0 h1:5Wjw6YXzZmtqU1MSrlws64+oLmSqea7gEajTcJickh8=
14-
github.com/ipfs/go-datastore v0.2.0/go.mod h1:w38XXW9kVFNp57Zj5knbKWM2T+KOZCGDRVNdgPHtbHw=
13+
github.com/ipfs/go-datastore v0.3.0 h1:9au0tYi/+n7xeUnGHG6davnS8x9hWbOzP/388Vx3CMs=
14+
github.com/ipfs/go-datastore v0.3.0/go.mod h1:w38XXW9kVFNp57Zj5knbKWM2T+KOZCGDRVNdgPHtbHw=
1515
github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw=
1616
github.com/jbenet/goprocess v0.0.0-20160826012719-b497e2f366b8 h1:bspPhN+oKYFk5fcGNuQzp6IGzYQSenLEgH3s6jkXrWw=
1717
github.com/jbenet/goprocess v0.0.0-20160826012719-b497e2f366b8/go.mod h1:Ly/wlsjFq/qrU3Rar62tu1gASgGw6chQbSh/XgIIXCY=

0 commit comments

Comments
 (0)