@@ -53,7 +53,7 @@ func NewDatastore(path string, opts *Options) (*Datastore, error) {
53
53
}
54
54
55
55
return & Datastore {
56
- accessor : & accessor {ldb : db },
56
+ accessor : & accessor {ldb : db , syncWrites : true },
57
57
DB : db ,
58
58
path : path ,
59
59
}, nil
@@ -72,11 +72,12 @@ type levelDbOps interface {
72
72
73
73
// Datastore operations using either the DB or a transaction as the backend.
74
74
type accessor struct {
75
- ldb levelDbOps
75
+ ldb levelDbOps
76
+ syncWrites bool
76
77
}
77
78
78
79
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 } )
80
81
}
81
82
82
83
func (a * accessor ) Sync (prefix ds.Key ) error {
@@ -103,7 +104,7 @@ func (d *accessor) GetSize(key ds.Key) (size int, err error) {
103
104
}
104
105
105
106
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 } )
107
108
}
108
109
109
110
func (a * accessor ) Query (q dsq.Query ) (dsq.Results , error ) {
@@ -184,14 +185,16 @@ func (d *Datastore) Close() (err error) {
184
185
}
185
186
186
187
type leveldbBatch struct {
187
- b * leveldb.Batch
188
- db * leveldb.DB
188
+ b * leveldb.Batch
189
+ db * leveldb.DB
190
+ syncWrites bool
189
191
}
190
192
191
193
func (d * Datastore ) Batch () (ds.Batch , error ) {
192
194
return & leveldbBatch {
193
- b : new (leveldb.Batch ),
194
- db : d .DB ,
195
+ b : new (leveldb.Batch ),
196
+ db : d .DB ,
197
+ syncWrites : d .syncWrites ,
195
198
}, nil
196
199
}
197
200
@@ -201,7 +204,7 @@ func (b *leveldbBatch) Put(key ds.Key, value []byte) error {
201
204
}
202
205
203
206
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 } )
205
208
}
206
209
207
210
func (b * leveldbBatch ) Delete (key ds.Key ) error {
@@ -228,6 +231,6 @@ func (d *Datastore) NewTransaction(readOnly bool) (ds.Txn, error) {
228
231
if err != nil {
229
232
return nil , err
230
233
}
231
- accessor := & accessor {tx }
234
+ accessor := & accessor {ldb : tx , syncWrites : false }
232
235
return & transaction {accessor , tx }, nil
233
236
}
0 commit comments