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-ds-leveldb
Commits
a706e049
Commit
a706e049
authored
Jan 09, 2015
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
leveldb query: limit + offset
parent
9978dc41
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
6 deletions
+37
-6
datastore.go
datastore.go
+18
-1
ds_test.go
ds_test.go
+19
-5
No files found.
datastore.go
View file @
a706e049
...
...
@@ -79,9 +79,21 @@ func (d *datastore) Query(q dsq.Query) (*dsq.Results, error) {
}
i
:=
d
.
DB
.
NewIterator
(
rnge
,
nil
)
// offset
if
q
.
Offset
>
0
{
for
j
:=
0
;
j
<
q
.
Offset
;
j
++
{
i
.
Next
()
}
}
var
es
[]
dsq
.
Entry
for
i
.
Next
()
{
// limit
if
q
.
Limit
>
0
&&
len
(
es
)
>=
q
.
Limit
{
break
}
k
:=
ds
.
NewKey
(
string
(
i
.
Key
()))
.
String
()
e
:=
dsq
.
Entry
{
Key
:
k
}
...
...
@@ -98,10 +110,15 @@ func (d *datastore) Query(q dsq.Query) (*dsq.Results, error) {
return
nil
,
err
}
// Now, apply remaining pieces.
q2
:=
q
q2
.
Offset
=
0
// already applied
q2
.
Limit
=
0
// already applied
// TODO: make this async with:
// qr := dsq.ResultsWithEntriesChan(q, ch)
qr
:=
dsq
.
ResultsWithEntries
(
q
,
es
)
qr
=
q
.
ApplyTo
(
qr
)
qr
=
q2
.
ApplyTo
(
qr
)
qr
.
Query
=
q
// set it back
return
qr
,
nil
}
...
...
ds_test.go
View file @
a706e049
...
...
@@ -59,21 +59,35 @@ func TestQuery(t *testing.T) {
t
.
Fatal
(
err
)
}
expect
:=
[]
string
{
expect
Matches
(
t
,
[]
string
{
"/a/b"
,
"/a/b/c"
,
"/a/b/d"
,
"/a/c"
,
"/a/d"
,
},
rs
.
AllEntries
())
// test offset and limit
rs
,
err
=
d
.
Query
(
dsq
.
Query
{
Prefix
:
"/a/"
,
Offset
:
2
,
Limit
:
2
})
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
re
:=
rs
.
AllEntries
()
if
len
(
re
)
!=
len
(
expect
)
{
t
.
Error
(
"not enough"
,
expect
,
re
)
expectMatches
(
t
,
[]
string
{
"/a/b/d"
,
"/a/c"
,
},
rs
.
AllEntries
())
}
func
expectMatches
(
t
*
testing
.
T
,
expect
[]
string
,
actual
[]
dsq
.
Entry
)
{
if
len
(
actual
)
!=
len
(
expect
)
{
t
.
Error
(
"not enough"
,
expect
,
actual
)
}
for
_
,
k
:=
range
expect
{
found
:=
false
for
_
,
e
:=
range
re
{
for
_
,
e
:=
range
actual
{
if
e
.
Key
==
k
{
found
=
true
}
...
...
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