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
af06d328
Commit
af06d328
authored
Apr 04, 2019
by
Steven Allen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test ordered queries
parent
b503c740
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
4 deletions
+54
-4
datastore.go
datastore.go
+8
-4
ds_test.go
ds_test.go
+46
-0
No files found.
datastore.go
View file @
af06d328
...
...
@@ -114,22 +114,26 @@ func (a *accessor) Delete(key ds.Key) (err error) {
func
(
a
*
accessor
)
Query
(
q
dsq
.
Query
)
(
dsq
.
Results
,
error
)
{
var
rnge
*
util
.
Range
// make a copy of the query for the fallback naive query implementation.
// don't modify the original so res.Query() returns the correct results.
qNaive
:=
q
if
q
.
Prefix
!=
""
{
rnge
=
util
.
BytesPrefix
([]
byte
(
q
.
Prefix
))
q
.
Prefix
=
""
q
Naive
.
Prefix
=
""
}
i
:=
a
.
ldb
.
NewIterator
(
rnge
,
nil
)
next
:=
i
.
Next
if
len
(
q
.
Orders
)
>
0
{
switch
q
.
Orders
[
0
]
.
(
type
)
{
case
dsq
.
OrderByKey
,
*
dsq
.
OrderByKey
:
q
.
Orders
=
nil
q
Naive
.
Orders
=
nil
case
dsq
.
OrderByKeyDescending
,
*
dsq
.
OrderByKeyDescending
:
next
=
func
()
bool
{
next
=
i
.
Prev
return
i
.
Last
()
}
q
.
Orders
=
nil
q
Naive
.
Orders
=
nil
default
:
}
}
...
...
@@ -153,7 +157,7 @@ func (a *accessor) Query(q dsq.Query) (dsq.Results, error) {
return
nil
},
})
return
dsq
.
NaiveQueryApply
(
q
,
r
),
nil
return
dsq
.
NaiveQueryApply
(
q
Naive
,
r
),
nil
}
// DiskUsage returns the current disk size used by this levelDB.
...
...
ds_test.go
View file @
af06d328
...
...
@@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"os"
"sort"
"testing"
ds
"github.com/ipfs/go-datastore"
...
...
@@ -101,6 +102,33 @@ func testQuery(t *testing.T, d *Datastore) {
"/a/b/d"
,
"/a/c"
,
},
rs
)
// test order
rs
,
err
=
d
.
Query
(
dsq
.
Query
{
Orders
:
[]
dsq
.
Order
{
dsq
.
OrderByKey
{}}})
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
keys
:=
make
([]
string
,
0
,
len
(
testcases
))
for
k
:=
range
testcases
{
keys
=
append
(
keys
,
k
)
}
sort
.
Strings
(
keys
)
expectOrderedMatches
(
t
,
keys
,
rs
)
rs
,
err
=
d
.
Query
(
dsq
.
Query
{
Orders
:
[]
dsq
.
Order
{
dsq
.
OrderByKeyDescending
{}}})
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
// reverse
for
i
,
j
:=
0
,
len
(
keys
)
-
1
;
i
<
j
;
i
,
j
=
i
+
1
,
j
-
1
{
keys
[
i
],
keys
[
j
]
=
keys
[
j
],
keys
[
i
]
}
expectOrderedMatches
(
t
,
keys
,
rs
)
}
func
TestQuery
(
t
*
testing
.
T
)
{
...
...
@@ -125,6 +153,7 @@ func TestQueryRespectsProcessMem(t *testing.T) {
}
func
expectMatches
(
t
*
testing
.
T
,
expect
[]
string
,
actualR
dsq
.
Results
)
{
t
.
Helper
()
actual
,
err
:=
actualR
.
Rest
()
if
err
!=
nil
{
t
.
Error
(
err
)
...
...
@@ -146,6 +175,23 @@ func expectMatches(t *testing.T, expect []string, actualR dsq.Results) {
}
}
func
expectOrderedMatches
(
t
*
testing
.
T
,
expect
[]
string
,
actualR
dsq
.
Results
)
{
t
.
Helper
()
actual
,
err
:=
actualR
.
Rest
()
if
err
!=
nil
{
t
.
Error
(
err
)
}
if
len
(
actual
)
!=
len
(
expect
)
{
t
.
Error
(
"not enough"
,
expect
,
actual
)
}
for
i
:=
range
expect
{
if
expect
[
i
]
!=
actual
[
i
]
.
Key
{
t
.
Errorf
(
"expected %q, got %q"
,
expect
[
i
],
actual
[
i
]
.
Key
)
}
}
}
func
testBatching
(
t
*
testing
.
T
,
d
*
Datastore
)
{
b
,
err
:=
d
.
Batch
()
if
err
!=
nil
{
...
...
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