Commit b31aad9b authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

Merge pull request #10 from maybebtc/r/ldb-closer

refactor(leveldb) add io.Closer to level interface
parents 7804ee2c 08d6bda4
package leveldb package leveldb
import ( import (
"io"
ds "github.com/jbenet/go-datastore" ds "github.com/jbenet/go-datastore"
"github.com/jbenet/go-datastore/Godeps/_workspace/src/github.com/syndtr/goleveldb/leveldb" "github.com/jbenet/go-datastore/Godeps/_workspace/src/github.com/syndtr/goleveldb/leveldb"
"github.com/jbenet/go-datastore/Godeps/_workspace/src/github.com/syndtr/goleveldb/leveldb/opt" "github.com/jbenet/go-datastore/Godeps/_workspace/src/github.com/syndtr/goleveldb/leveldb/opt"
) )
// Datastore uses a standard Go map for internal storage. type Datastore interface {
type Datastore struct { ds.ThreadSafeDatastore
io.Closer
}
type datastore struct {
DB *leveldb.DB DB *leveldb.DB
} }
type Options opt.Options type Options opt.Options
func NewDatastore(path string, opts *Options) (ds.ThreadSafeDatastore, error) { func NewDatastore(path string, opts *Options) (Datastore, error) {
var nopts opt.Options var nopts opt.Options
if opts != nil { if opts != nil {
nopts = opt.Options(*opts) nopts = opt.Options(*opts)
...@@ -23,7 +29,7 @@ func NewDatastore(path string, opts *Options) (ds.ThreadSafeDatastore, error) { ...@@ -23,7 +29,7 @@ func NewDatastore(path string, opts *Options) (ds.ThreadSafeDatastore, error) {
return nil, err return nil, err
} }
return &Datastore{ return &datastore{
DB: db, DB: db,
}, nil }, nil
} }
...@@ -32,7 +38,7 @@ func NewDatastore(path string, opts *Options) (ds.ThreadSafeDatastore, error) { ...@@ -32,7 +38,7 @@ func NewDatastore(path string, opts *Options) (ds.ThreadSafeDatastore, error) {
// //
// Note: using sync = false. // Note: using sync = false.
// see http://godoc.org/github.com/syndtr/goleveldb/leveldb/opt#WriteOptions // see http://godoc.org/github.com/syndtr/goleveldb/leveldb/opt#WriteOptions
func (d *Datastore) Put(key ds.Key, value interface{}) (err error) { func (d *datastore) Put(key ds.Key, value interface{}) (err error) {
val, ok := value.([]byte) val, ok := value.([]byte)
if !ok { if !ok {
return ds.ErrInvalidType return ds.ErrInvalidType
...@@ -40,7 +46,7 @@ func (d *Datastore) Put(key ds.Key, value interface{}) (err error) { ...@@ -40,7 +46,7 @@ func (d *Datastore) Put(key ds.Key, value interface{}) (err error) {
return d.DB.Put(key.Bytes(), val, nil) return d.DB.Put(key.Bytes(), val, nil)
} }
func (d *Datastore) Get(key ds.Key) (value interface{}, err error) { func (d *datastore) Get(key ds.Key) (value interface{}, err error) {
val, err := d.DB.Get(key.Bytes(), nil) val, err := d.DB.Get(key.Bytes(), nil)
if err != nil { if err != nil {
if err == leveldb.ErrNotFound { if err == leveldb.ErrNotFound {
...@@ -51,11 +57,11 @@ func (d *Datastore) Get(key ds.Key) (value interface{}, err error) { ...@@ -51,11 +57,11 @@ func (d *Datastore) Get(key ds.Key) (value interface{}, err error) {
return val, nil return val, nil
} }
func (d *Datastore) Has(key ds.Key) (exists bool, err error) { func (d *datastore) Has(key ds.Key) (exists bool, err error) {
return ds.GetBackedHas(d, key) return ds.GetBackedHas(d, key)
} }
func (d *Datastore) Delete(key ds.Key) (err error) { func (d *datastore) Delete(key ds.Key) (err error) {
err = d.DB.Delete(key.Bytes(), nil) err = d.DB.Delete(key.Bytes(), nil)
if err == leveldb.ErrNotFound { if err == leveldb.ErrNotFound {
return ds.ErrNotFound return ds.ErrNotFound
...@@ -63,7 +69,7 @@ func (d *Datastore) Delete(key ds.Key) (err error) { ...@@ -63,7 +69,7 @@ func (d *Datastore) Delete(key ds.Key) (err error) {
return err return err
} }
func (d *Datastore) KeyList() ([]ds.Key, error) { func (d *datastore) KeyList() ([]ds.Key, error) {
i := d.DB.NewIterator(nil, nil) i := d.DB.NewIterator(nil, nil)
var keys []ds.Key var keys []ds.Key
for i.Next() { for i.Next() {
...@@ -73,8 +79,8 @@ func (d *Datastore) KeyList() ([]ds.Key, error) { ...@@ -73,8 +79,8 @@ func (d *Datastore) KeyList() ([]ds.Key, error) {
} }
// LevelDB needs to be closed. // LevelDB needs to be closed.
func (d *Datastore) Close() (err error) { func (d *datastore) Close() (err error) {
return d.DB.Close() return d.DB.Close()
} }
func (d *Datastore) IsThreadSafe() {} func (d *datastore) IsThreadSafe() {}
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