1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-29 09:34:03 +08:00

use encoded (pretty) keys only on fs ds

This commit is contained in:
Juan Batiz-Benet
2014-10-08 14:49:02 -07:00
parent 0ffc20384e
commit dc6fdd39c5
2 changed files with 29 additions and 5 deletions

View File

@ -5,9 +5,12 @@ import (
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go" 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" 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" 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" syncds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go/sync"
config "github.com/jbenet/go-ipfs/config" config "github.com/jbenet/go-ipfs/config"
u "github.com/jbenet/go-ipfs/util"
) )
func makeDatastore(cfg config.Datastore) (ds.ThreadSafeDatastore, error) { func makeDatastore(cfg config.Datastore) (ds.ThreadSafeDatastore, error) {
@ -28,7 +31,8 @@ func makeDatastore(cfg config.Datastore) (ds.ThreadSafeDatastore, error) {
if err != nil { if err != nil {
return nil, err 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) return nil, fmt.Errorf("Unknown datastore type: %s", cfg.Type)

View File

@ -55,14 +55,34 @@ func (k Key) Pretty() string {
// DsKey returns a Datastore key // DsKey returns a Datastore key
func (k Key) DsKey() ds.Key { func (k Key) DsKey() ds.Key {
return ds.NewKey(k.Pretty()) return ds.NewKey(string(k))
} }
// KeyFromDsKey returns a Datastore key // KeyFromDsKey returns a Datastore key
func KeyFromDsKey(dsk ds.Key) Key { func KeyFromDsKey(dsk ds.Key) Key {
l := dsk.List() return Key(dsk.BaseNamespace())
enc := l[len(l)-1] }
return Key(b58.Decode(enc))
// 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 // Hash is the global IPFS hash function. uses multihash SHA2_256, 256 bits