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
7c05cd07
Commit
7c05cd07
authored
Jul 03, 2017
by
Łukasz Magiera
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve nsds.Query
parent
b5f64d84
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
62 additions
and
1 deletion
+62
-1
namespace/namespace.go
namespace/namespace.go
+3
-1
namespace/namespace_test.go
namespace/namespace_test.go
+59
-0
No files found.
namespace/namespace.go
View file @
7c05cd07
...
...
@@ -55,7 +55,9 @@ type datastore struct {
}
// Query implements Query, inverting keys on the way back out.
// This function assumes that child datastore.Query returns ordered results
func
(
d
*
datastore
)
Query
(
q
dsq
.
Query
)
(
dsq
.
Results
,
error
)
{
q
.
Prefix
=
d
.
prefix
.
Child
(
ds
.
NewKey
(
q
.
Prefix
))
.
String
()
qr
,
err
:=
d
.
raw
.
Query
(
q
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -73,7 +75,7 @@ func (d *datastore) Query(q dsq.Query) (dsq.Results, error) {
}
k
:=
ds
.
RawKey
(
r
.
Entry
.
Key
)
if
!
d
.
prefix
.
IsAncestorOf
(
k
)
{
continu
e
return
dsq
.
Result
{},
fals
e
}
r
.
Entry
.
Key
=
d
.
Datastore
.
InvertKey
(
k
)
.
String
()
...
...
namespace/namespace_test.go
View file @
7c05cd07
...
...
@@ -78,6 +78,65 @@ func (ks *DSSuite) testBasic(c *C, prefix string) {
}
}
func
(
ks
*
DSSuite
)
TestQuery
(
c
*
C
)
{
mpds
:=
ds
.
NewMapDatastore
()
nsds
:=
ns
.
Wrap
(
mpds
,
ds
.
NewKey
(
"/foo"
))
keys
:=
strsToKeys
([]
string
{
"abc/foo"
,
"bar/foo"
,
"foo/bar"
,
"foo/bar/baz"
,
"foo/baz/abc"
,
"xyz/foo"
,
})
for
_
,
k
:=
range
keys
{
err
:=
mpds
.
Put
(
k
,
[]
byte
(
k
.
String
()))
c
.
Check
(
err
,
Equals
,
nil
)
}
qres
,
err
:=
nsds
.
Query
(
dsq
.
Query
{})
c
.
Check
(
err
,
Equals
,
nil
)
expect
:=
[]
dsq
.
Entry
{
{
Key
:
"/bar"
,
Value
:
[]
byte
(
"/foo/bar"
)},
{
Key
:
"/bar/baz"
,
Value
:
[]
byte
(
"/foo/bar/baz"
)},
{
Key
:
"/baz/abc"
,
Value
:
[]
byte
(
"/foo/baz/abc"
)},
}
results
,
err
:=
qres
.
Rest
()
c
.
Check
(
err
,
Equals
,
nil
)
for
i
,
ent
:=
range
results
{
c
.
Check
(
ent
.
Key
,
Equals
,
expect
[
i
]
.
Key
)
entval
,
_
:=
ent
.
Value
.
([]
byte
)
expval
,
_
:=
expect
[
i
]
.
Value
.
([]
byte
)
c
.
Check
(
string
(
entval
),
Equals
,
string
(
expval
))
}
err
=
qres
.
Close
()
c
.
Check
(
err
,
Equals
,
nil
)
qres
,
err
=
nsds
.
Query
(
dsq
.
Query
{
Prefix
:
"bar"
})
c
.
Check
(
err
,
Equals
,
nil
)
expect
=
[]
dsq
.
Entry
{
{
Key
:
"/bar"
,
Value
:
[]
byte
(
"/foo/bar"
)},
{
Key
:
"/bar/baz"
,
Value
:
[]
byte
(
"/foo/bar/baz"
)},
}
results
,
err
=
qres
.
Rest
()
c
.
Check
(
err
,
Equals
,
nil
)
for
i
,
ent
:=
range
results
{
c
.
Check
(
ent
.
Key
,
Equals
,
expect
[
i
]
.
Key
)
entval
,
_
:=
ent
.
Value
.
([]
byte
)
expval
,
_
:=
expect
[
i
]
.
Value
.
([]
byte
)
c
.
Check
(
string
(
entval
),
Equals
,
string
(
expval
))
}
}
func
strsToKeys
(
strs
[]
string
)
[]
ds
.
Key
{
keys
:=
make
([]
ds
.
Key
,
len
(
strs
))
for
i
,
s
:=
range
strs
{
...
...
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