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
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
.
ThreadSafe
Datastore
,
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
&
D
atastore
{
return
&
d
atastore
{
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
*
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
)
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
*
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
)
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
*
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
)
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
)
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
*
D
atastore
)
KeyList
()
([]
ds
.
Key
,
error
)
{
func
(
d
*
d
atastore
)
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
*
D
atastore
)
Close
()
(
err
error
)
{
func
(
d
*
d
atastore
)
Close
()
(
err
error
)
{
return
d
.
DB
.
Close
()
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