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
67d8e74a
Commit
67d8e74a
authored
Jul 26, 2016
by
Jeromy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implement batching for leveldb
parent
9b48c4ee
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
68 additions
and
9 deletions
+68
-9
flatfs/flatfs.go
flatfs/flatfs.go
+4
-4
leveldb/datastore.go
leveldb/datastore.go
+31
-5
leveldb/ds_test.go
leveldb/ds_test.go
+33
-0
No files found.
flatfs/flatfs.go
View file @
67d8e74a
...
...
@@ -99,7 +99,7 @@ func (fs *Datastore) makePrefixDirNoSync(dir string) error {
return
nil
}
var
putMaxRetries
=
3
var
putMaxRetries
=
6
func
(
fs
*
Datastore
)
Put
(
key
datastore
.
Key
,
value
interface
{})
error
{
val
,
ok
:=
value
.
([]
byte
)
...
...
@@ -111,15 +111,15 @@ func (fs *Datastore) Put(key datastore.Key, value interface{}) error {
for
i
:=
0
;
i
<
putMaxRetries
;
i
++
{
err
=
fs
.
doPut
(
key
,
val
)
if
err
==
nil
{
re
turn
nil
b
re
ak
}
if
!
strings
.
Contains
(
err
.
Error
(),
"too many open files"
)
{
re
turn
err
b
re
ak
}
log
.
Errorf
(
"too many open files, retrying in %dms"
,
100
*
i
)
time
.
Sleep
(
time
.
Millisecond
*
100
*
time
.
Duration
(
i
))
time
.
Sleep
(
time
.
Millisecond
*
100
*
time
.
Duration
(
i
+
1
))
}
return
err
}
...
...
leveldb/datastore.go
View file @
67d8e74a
...
...
@@ -147,14 +147,40 @@ func (d *datastore) runQuery(worker goprocess.Process, qrb *dsq.ResultBuilder) {
}
}
func
(
d
*
datastore
)
Batch
()
(
ds
.
Batch
,
error
)
{
// TODO: implement batch on leveldb
return
nil
,
ds
.
ErrBatchUnsupported
}
// LevelDB needs to be closed.
func
(
d
*
datastore
)
Close
()
(
err
error
)
{
return
d
.
DB
.
Close
()
}
func
(
d
*
datastore
)
IsThreadSafe
()
{}
type
leveldbBatch
struct
{
b
*
leveldb
.
Batch
db
*
leveldb
.
DB
}
func
(
d
*
datastore
)
Batch
()
(
ds
.
Batch
,
error
)
{
return
&
leveldbBatch
{
b
:
new
(
leveldb
.
Batch
),
db
:
d
.
DB
,
},
nil
}
func
(
b
*
leveldbBatch
)
Put
(
key
ds
.
Key
,
value
interface
{})
error
{
val
,
ok
:=
value
.
([]
byte
)
if
!
ok
{
return
ds
.
ErrInvalidType
}
b
.
b
.
Put
(
key
.
Bytes
(),
val
)
return
nil
}
func
(
b
*
leveldbBatch
)
Commit
()
error
{
return
b
.
db
.
Write
(
b
.
b
,
nil
)
}
func
(
b
*
leveldbBatch
)
Delete
(
key
ds
.
Key
)
error
{
b
.
b
.
Delete
(
key
.
Bytes
())
return
nil
}
leveldb/ds_test.go
View file @
67d8e74a
...
...
@@ -122,3 +122,36 @@ func expectMatches(t *testing.T, expect []string, actualR dsq.Results) {
}
}
}
func
TestBatching
(
t
*
testing
.
T
)
{
d
,
done
:=
newDS
(
t
)
defer
done
()
b
,
err
:=
d
.
Batch
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
for
k
,
v
:=
range
testcases
{
err
:=
b
.
Put
(
ds
.
NewKey
(
k
),
[]
byte
(
v
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
}
err
=
b
.
Commit
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
for
k
,
v
:=
range
testcases
{
val
,
err
:=
d
.
Get
(
ds
.
NewKey
(
k
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
v
!=
string
(
val
.
([]
byte
))
{
t
.
Fatal
(
"got wrong data!"
)
}
}
}
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