Unverified Commit f885e89a authored by Adin Schmahmann's avatar Adin Schmahmann Committed by GitHub

Merge pull request #36 from ipfs/feat/ds-update

update datastore Interface
parents 9ad1663b d7c8228c
export IPFS_API ?= v04x.ipfs.io
gx:
go get -u github.com/whyrusleeping/gx
go get -u github.com/whyrusleeping/gx-go
deps: gx
gx --verbose install --global
gx-go rewrite
......@@ -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
......@@ -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) {
......@@ -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) {
......@@ -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
}
......@@ -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 {
......@@ -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
}
......@@ -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)
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment