Skip to content

Commit 28fa23c

Browse files
committed
fix: datastore now uses synchronous writes which was the expected behavior
1 parent 01d35bb commit 28fa23c

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

datastore.go

+13-10
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,12 @@ 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})
8081
}
8182

8283
func (a *accessor) Sync(prefix ds.Key) error {
@@ -103,7 +104,7 @@ func (d *accessor) GetSize(key ds.Key) (size int, err error) {
103104
}
104105

105106
func (a *accessor) Delete(key ds.Key) (err error) {
106-
return a.ldb.Delete(key.Bytes(), nil)
107+
return a.ldb.Delete(key.Bytes(), &opt.WriteOptions{Sync: a.syncWrites})
107108
}
108109

109110
func (a *accessor) Query(q dsq.Query) (dsq.Results, error) {
@@ -184,14 +185,16 @@ func (d *Datastore) Close() (err error) {
184185
}
185186

186187
type leveldbBatch struct {
187-
b *leveldb.Batch
188-
db *leveldb.DB
188+
b *leveldb.Batch
189+
db *leveldb.DB
190+
syncWrites bool
189191
}
190192

191193
func (d *Datastore) Batch() (ds.Batch, error) {
192194
return &leveldbBatch{
193-
b: new(leveldb.Batch),
194-
db: d.DB,
195+
b: new(leveldb.Batch),
196+
db: d.DB,
197+
syncWrites: d.syncWrites,
195198
}, nil
196199
}
197200

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

203206
func (b *leveldbBatch) Commit() error {
204-
return b.db.Write(b.b, nil)
207+
return b.db.Write(b.b, &opt.WriteOptions{Sync: b.syncWrites})
205208
}
206209

207210
func (b *leveldbBatch) Delete(key ds.Key) error {
@@ -228,6 +231,6 @@ func (d *Datastore) NewTransaction(readOnly bool) (ds.Txn, error) {
228231
if err != nil {
229232
return nil, err
230233
}
231-
accessor := &accessor{tx}
234+
accessor := &accessor{ldb: tx, syncWrites: false}
232235
return &transaction{accessor, tx}, nil
233236
}

0 commit comments

Comments
 (0)