Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-datastore
Commits
b31aad9b
Commit
b31aad9b
authored
Nov 17, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #10 from maybebtc/r/ldb-closer
refactor(leveldb) add io.Closer to level interface
parents
7804ee2c
08d6bda4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
11 deletions
+17
-11
leveldb/datastore.go
leveldb/datastore.go
+17
-11
No files found.
leveldb/datastore.go
View file @
b31aad9b
package
leveldb
import
(
"io"
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/opt"
)
// Datastore uses a standard Go map for internal storage.
type
Datastore
struct
{
type
Datastore
interface
{
ds
.
ThreadSafeDatastore
io
.
Closer
}
type
datastore
struct
{
DB
*
leveldb
.
DB
}
type
Options
opt
.
Options
func
NewDatastore
(
path
string
,
opts
*
Options
)
(
ds
.
ThreadSafe
Datastore
,
error
)
{
func
NewDatastore
(
path
string
,
opts
*
Options
)
(
Datastore
,
error
)
{
var
nopts
opt
.
Options
if
opts
!=
nil
{
nopts
=
opt
.
Options
(
*
opts
)
...
...
@@ -23,7 +29,7 @@ func NewDatastore(path string, opts *Options) (ds.ThreadSafeDatastore, error) {
return
nil
,
err
}
return
&
D
atastore
{
return
&
d
atastore
{
DB
:
db
,
},
nil
}
...
...
@@ -32,7 +38,7 @@ func NewDatastore(path string, opts *Options) (ds.ThreadSafeDatastore, error) {
//
// Note: using sync = false.
// see http://godoc.org/github.com/syndtr/goleveldb/leveldb/opt#WriteOptions
func
(
d
*
D
atastore
)
Put
(
key
ds
.
Key
,
value
interface
{})
(
err
error
)
{
func
(
d
*
d
atastore
)
Put
(
key
ds
.
Key
,
value
interface
{})
(
err
error
)
{
val
,
ok
:=
value
.
([]
byte
)
if
!
ok
{
return
ds
.
ErrInvalidType
...
...
@@ -40,7 +46,7 @@ func (d *Datastore) Put(key ds.Key, value interface{}) (err error) {
return
d
.
DB
.
Put
(
key
.
Bytes
(),
val
,
nil
)
}
func
(
d
*
D
atastore
)
Get
(
key
ds
.
Key
)
(
value
interface
{},
err
error
)
{
func
(
d
*
d
atastore
)
Get
(
key
ds
.
Key
)
(
value
interface
{},
err
error
)
{
val
,
err
:=
d
.
DB
.
Get
(
key
.
Bytes
(),
nil
)
if
err
!=
nil
{
if
err
==
leveldb
.
ErrNotFound
{
...
...
@@ -51,11 +57,11 @@ func (d *Datastore) Get(key ds.Key) (value interface{}, err error) {
return
val
,
nil
}
func
(
d
*
D
atastore
)
Has
(
key
ds
.
Key
)
(
exists
bool
,
err
error
)
{
func
(
d
*
d
atastore
)
Has
(
key
ds
.
Key
)
(
exists
bool
,
err
error
)
{
return
ds
.
GetBackedHas
(
d
,
key
)
}
func
(
d
*
D
atastore
)
Delete
(
key
ds
.
Key
)
(
err
error
)
{
func
(
d
*
d
atastore
)
Delete
(
key
ds
.
Key
)
(
err
error
)
{
err
=
d
.
DB
.
Delete
(
key
.
Bytes
(),
nil
)
if
err
==
leveldb
.
ErrNotFound
{
return
ds
.
ErrNotFound
...
...
@@ -63,7 +69,7 @@ func (d *Datastore) Delete(key ds.Key) (err error) {
return
err
}
func
(
d
*
D
atastore
)
KeyList
()
([]
ds
.
Key
,
error
)
{
func
(
d
*
d
atastore
)
KeyList
()
([]
ds
.
Key
,
error
)
{
i
:=
d
.
DB
.
NewIterator
(
nil
,
nil
)
var
keys
[]
ds
.
Key
for
i
.
Next
()
{
...
...
@@ -73,8 +79,8 @@ func (d *Datastore) KeyList() ([]ds.Key, error) {
}
// LevelDB needs to be closed.
func
(
d
*
D
atastore
)
Close
()
(
err
error
)
{
func
(
d
*
d
atastore
)
Close
()
(
err
error
)
{
return
d
.
DB
.
Close
()
}
func
(
d
*
D
atastore
)
IsThreadSafe
()
{}
func
(
d
*
d
atastore
)
IsThreadSafe
()
{}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment