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
f3978653
Commit
f3978653
authored
Nov 16, 2016
by
Kevin Atkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add RawKey function and make use of it.
Avoids some unnecessary creation of strings.
parent
7519431e
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
31 additions
and
7 deletions
+31
-7
key.go
key.go
+27
-2
keytransform/keytransform.go
keytransform/keytransform.go
+1
-1
namespace/namespace.go
namespace/namespace.go
+3
-4
No files found.
key.go
View file @
f3978653
...
...
@@ -40,6 +40,24 @@ func NewKey(s string) Key {
return
k
}
// RawKey creates a new Key without safety checking the input. Use with care.
func
RawKey
(
s
string
)
Key
{
// accept an empty string and fix it to avoid special cases
// elsewhere
if
len
(
s
)
==
0
{
return
Key
{
"/"
}
}
// perform a quick sanity check that the key is in the correct
// format, if it is not then it is a programmer error and it is
// okay to panic
if
len
(
s
)
==
0
||
s
[
0
]
!=
'/'
||
(
len
(
s
)
>
1
&&
s
[
len
(
s
)
-
1
]
==
'/'
)
{
panic
(
"invalid datastore key: "
+
s
)
}
return
Key
{
s
}
}
// KeyWithNamespaces constructs a key out of a namespace slice.
func
KeyWithNamespaces
(
ns
[]
string
)
Key
{
return
NewKey
(
strings
.
Join
(
ns
,
"/"
))
...
...
@@ -156,7 +174,7 @@ func (k Key) Path() Key {
func
(
k
Key
)
Parent
()
Key
{
n
:=
k
.
List
()
if
len
(
n
)
==
1
{
return
Ne
wKey
(
"/"
)
return
Ra
wKey
(
"/"
)
}
return
NewKey
(
strings
.
Join
(
n
[
:
len
(
n
)
-
1
],
"/"
))
}
...
...
@@ -165,7 +183,14 @@ func (k Key) Parent() Key {
// NewKey("/Comedy/MontyPython").Child(NewKey("Actor:JohnCleese"))
// NewKey("/Comedy/MontyPython/Actor:JohnCleese")
func
(
k
Key
)
Child
(
k2
Key
)
Key
{
return
NewKey
(
k
.
string
+
"/"
+
k2
.
string
)
switch
{
case
k
.
string
==
"/"
:
return
k2
case
k2
.
string
==
"/"
:
return
k
default
:
return
RawKey
(
k
.
string
+
k2
.
string
)
}
}
// ChildString returns the `child` Key of this Key -- string helper.
...
...
keytransform/keytransform.go
View file @
f3978653
...
...
@@ -67,7 +67,7 @@ func (d *ktds) Query(q dsq.Query) (dsq.Results, error) {
for
r
:=
range
qr
.
Next
()
{
if
r
.
Error
==
nil
{
r
.
Entry
.
Key
=
d
.
InvertKey
(
ds
.
Ne
wKey
(
r
.
Entry
.
Key
))
.
String
()
r
.
Entry
.
Key
=
d
.
InvertKey
(
ds
.
Ra
wKey
(
r
.
Entry
.
Key
))
.
String
()
}
ch
<-
r
}
...
...
namespace/namespace.go
View file @
f3978653
...
...
@@ -2,7 +2,6 @@ package namespace
import
(
"fmt"
"strings"
ds
"github.com/ipfs/go-datastore"
ktds
"github.com/ipfs/go-datastore/keytransform"
...
...
@@ -29,8 +28,8 @@ func PrefixTransform(prefix ds.Key) ktds.KeyTransform {
panic
(
"expected prefix not found"
)
}
s
:=
strings
.
TrimPrefix
(
k
.
String
()
,
prefix
.
String
())
return
ds
.
Ne
wKey
(
s
)
s
:=
k
.
String
()
[
len
(
prefix
.
String
())
:
]
return
ds
.
Ra
wKey
(
s
)
},
}
}
...
...
@@ -70,7 +69,7 @@ func (d *datastore) Query(q dsq.Query) (dsq.Results, error) {
continue
}
k
:=
ds
.
Ne
wKey
(
r
.
Entry
.
Key
)
k
:=
ds
.
Ra
wKey
(
r
.
Entry
.
Key
)
if
!
d
.
prefix
.
IsAncestorOf
(
k
)
{
continue
}
...
...
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