Commit 4d7b87df authored by Jeromy's avatar Jeromy

Merge branch 'ipns' of github.com:jbenet/go-ipfs into ipns

parents 1aafd285 31cc605c
......@@ -64,7 +64,7 @@
},
{
"ImportPath": "github.com/jbenet/datastore.go",
"Rev": "e7d6f7cb9e3c207a04c5397c449d10a6f9d403a0"
"Rev": "60ebc56447b5a8264cfed3ae3ff48deb984d7cf1"
},
{
"ImportPath": "github.com/jbenet/go-base58",
......
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
}
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
}
......@@ -5,9 +5,12 @@ import (
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
fsds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go/fs"
ktds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go/keytransform"
lds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go/leveldb"
syncds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go/sync"
config "github.com/jbenet/go-ipfs/config"
u "github.com/jbenet/go-ipfs/util"
)
func makeDatastore(cfg config.Datastore) (ds.ThreadSafeDatastore, error) {
......@@ -28,7 +31,8 @@ func makeDatastore(cfg config.Datastore) (ds.ThreadSafeDatastore, error) {
if err != nil {
return nil, err
}
return syncds.MutexWrap(d), nil
ktd := ktds.WrapDatastore(d, u.DsKeyB58Encode)
return syncds.MutexWrap(ktd), nil
}
return nil, fmt.Errorf("Unknown datastore type: %s", cfg.Type)
......
......@@ -13,7 +13,7 @@ It has these top-level messages:
*/
package unixfs
import proto "code.google.com/p/goprotobuf/proto"
import proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
import math "math"
// Reference imports to suppress errors if they are not otherwise used.
......
......@@ -55,14 +55,34 @@ func (k Key) Pretty() string {
// DsKey returns a Datastore key
func (k Key) DsKey() ds.Key {
return ds.NewKey(k.Pretty())
return ds.NewKey(string(k))
}
// KeyFromDsKey returns a Datastore key
func KeyFromDsKey(dsk ds.Key) Key {
l := dsk.List()
enc := l[len(l)-1]
return Key(b58.Decode(enc))
return Key(dsk.BaseNamespace())
}
// DsKeyB58Encode returns a B58 encoded Datastore key
// TODO: this is hacky because it encodes every path component. some
// path components may be proper strings already...
func DsKeyB58Encode(dsk ds.Key) ds.Key {
k := ds.NewKey("/")
for _, n := range dsk.Namespaces() {
k = k.Child(b58.Encode([]byte(n)))
}
return k
}
// DsKeyB58Decode returns a b58 decoded Datastore key
// TODO: this is hacky because it encodes every path component. some
// path components may be proper strings already...
func DsKeyB58Decode(dsk ds.Key) ds.Key {
k := ds.NewKey("/")
for _, n := range dsk.Namespaces() {
k = k.Child(string(b58.Decode(n)))
}
return k
}
// Hash is the global IPFS hash function. uses multihash SHA2_256, 256 bits
......@@ -134,13 +154,14 @@ func SetupLogging() {
logging.SetBackend(backend)
logging.SetFormatter(logging.MustStringFormatter(LogFormat))
/*
if Debug {
logging.SetLevel(logging.DEBUG, "")
} else {
logging.SetLevel(logging.ERROR, "")
}
*/
// just uncomment Debug = True right here for all logging.
// but please don't commit that.
// Debug = True
if Debug {
logging.SetLevel(logging.DEBUG, "")
} else {
logging.SetLevel(logging.ERROR, "")
}
for n, log := range loggers {
logging.SetLevel(logging.ERROR, n)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment