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-dms3
Commits
9ebf3ae6
Commit
9ebf3ae6
authored
Oct 08, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
keytransform ds
parent
093c8fb0
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
149 additions
and
1 deletion
+149
-1
Godeps/Godeps.json
Godeps/Godeps.json
+1
-1
Godeps/_workspace/src/github.com/jbenet/datastore.go/keytransform/keytransform.go
...thub.com/jbenet/datastore.go/keytransform/keytransform.go
+88
-0
Godeps/_workspace/src/github.com/jbenet/datastore.go/keytransform/keytransform_test.go
...com/jbenet/datastore.go/keytransform/keytransform_test.go
+60
-0
No files found.
Godeps/Godeps.json
View file @
9ebf3ae6
...
...
@@ -64,7 +64,7 @@
},
{
"ImportPath"
:
"github.com/jbenet/datastore.go"
,
"Rev"
:
"
e7d6f7cb9e3c207a04c5397c449d10a6f9d403a0
"
"Rev"
:
"
60ebc56447b5a8264cfed3ae3ff48deb984d7cf1
"
},
{
"ImportPath"
:
"github.com/jbenet/go-base58"
,
...
...
Godeps/_workspace/src/github.com/jbenet/datastore.go/keytransform/keytransform.go
0 → 100644
View file @
9ebf3ae6
package
keytransform
import
ds
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
// KeyTransform is a function that transforms one key into another.
type
KeyTransform
func
(
ds
.
Key
)
ds
.
Key
// Datastore is a keytransform.Datastore
type
Datastore
interface
{
ds
.
Shim
// Transform runs the transformation function
Transform
(
ds
.
Key
)
ds
.
Key
// TransformFunc returns the KeyTransform function
TransformFunc
()
KeyTransform
}
// ktds keeps a KeyTransform function
type
ktds
struct
{
child
ds
.
Datastore
xform
KeyTransform
}
// WrapDatastore wraps a given datastore with a KeyTransform function.
// The resulting wrapped datastore will use the transform on all Datastore
// operations.
func
WrapDatastore
(
child
ds
.
Datastore
,
f
KeyTransform
)
Datastore
{
if
f
==
nil
{
panic
(
"f (KeyTransform) is nil"
)
}
if
child
==
nil
{
panic
(
"child (ds.Datastore) is nil"
)
}
return
&
ktds
{
child
,
f
}
}
// TransformFunc returns the KeyTransform function
func
(
d
*
ktds
)
TransformFunc
()
KeyTransform
{
return
d
.
xform
}
// Transform runs the KeyTransform function
func
(
d
*
ktds
)
Transform
(
k
ds
.
Key
)
ds
.
Key
{
return
d
.
xform
(
k
)
}
// Children implements ds.Shim
func
(
d
*
ktds
)
Children
()
[]
ds
.
Datastore
{
return
[]
ds
.
Datastore
{
d
.
child
}
}
// Put stores the given value, transforming the key first.
func
(
d
*
ktds
)
Put
(
key
ds
.
Key
,
value
interface
{})
(
err
error
)
{
return
d
.
child
.
Put
(
d
.
Transform
(
key
),
value
)
}
// Get returns the value for given key, transforming the key first.
func
(
d
*
ktds
)
Get
(
key
ds
.
Key
)
(
value
interface
{},
err
error
)
{
return
d
.
child
.
Get
(
d
.
Transform
(
key
))
}
// Has returns whether the datastore has a value for a given key, transforming
// the key first.
func
(
d
*
ktds
)
Has
(
key
ds
.
Key
)
(
exists
bool
,
err
error
)
{
return
d
.
child
.
Has
(
d
.
Transform
(
key
))
}
// Delete removes the value for given key
func
(
d
*
ktds
)
Delete
(
key
ds
.
Key
)
(
err
error
)
{
return
d
.
child
.
Delete
(
d
.
Transform
(
key
))
}
// KeyList returns a list of all keys in the datastore, transforming keys out.
func
(
d
*
ktds
)
KeyList
()
([]
ds
.
Key
,
error
)
{
keys
,
err
:=
d
.
child
.
KeyList
()
if
err
!=
nil
{
return
nil
,
err
}
for
i
,
k
:=
range
keys
{
keys
[
i
]
=
d
.
Transform
(
k
)
}
return
keys
,
nil
}
Godeps/_workspace/src/github.com/jbenet/datastore.go/keytransform/keytransform_test.go
0 → 100644
View file @
9ebf3ae6
package
keytransform_test
import
(
"bytes"
"testing"
ds
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
kt
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go/keytransform"
.
"launchpad.net/gocheck"
)
// Hook up gocheck into the "go test" runner.
func
Test
(
t
*
testing
.
T
)
{
TestingT
(
t
)
}
type
DSSuite
struct
{
dir
string
ds
ds
.
Datastore
}
var
_
=
Suite
(
&
DSSuite
{})
func
(
ks
*
DSSuite
)
TestBasic
(
c
*
C
)
{
mpds
:=
ds
.
NewMapDatastore
()
ktds
:=
kt
.
WrapDatastore
(
mpds
,
func
(
k
ds
.
Key
)
ds
.
Key
{
return
k
.
Reverse
()
})
keys
:=
strsToKeys
([]
string
{
"foo"
,
"foo/bar"
,
"foo/bar/baz"
,
"foo/barb"
,
"foo/bar/bazb"
,
"foo/bar/baz/barb"
,
})
for
_
,
k
:=
range
keys
{
err
:=
ktds
.
Put
(
k
,
[]
byte
(
k
.
String
()))
c
.
Check
(
err
,
Equals
,
nil
)
}
for
_
,
k
:=
range
keys
{
v1
,
err
:=
ktds
.
Get
(
k
)
c
.
Check
(
err
,
Equals
,
nil
)
c
.
Check
(
bytes
.
Equal
(
v1
.
([]
byte
),
[]
byte
(
k
.
String
())),
Equals
,
true
)
v2
,
err
:=
mpds
.
Get
(
k
.
Reverse
())
c
.
Check
(
err
,
Equals
,
nil
)
c
.
Check
(
bytes
.
Equal
(
v2
.
([]
byte
),
[]
byte
(
k
.
String
())),
Equals
,
true
)
}
}
func
strsToKeys
(
strs
[]
string
)
[]
ds
.
Key
{
keys
:=
make
([]
ds
.
Key
,
len
(
strs
))
for
i
,
s
:=
range
strs
{
keys
[
i
]
=
ds
.
NewKey
(
s
)
}
return
keys
}
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