mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-25 15:08:45 +08:00
use string datastore keys.
This commit is contained in:
@ -7,7 +7,7 @@ import (
|
|||||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||||
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"
|
||||||
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
|
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
|
||||||
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/op/go-logging"
|
logging "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/op/go-logging"
|
||||||
|
|
||||||
blocks "github.com/jbenet/go-ipfs/blocks"
|
blocks "github.com/jbenet/go-ipfs/blocks"
|
||||||
exchange "github.com/jbenet/go-ipfs/exchange"
|
exchange "github.com/jbenet/go-ipfs/exchange"
|
||||||
@ -37,11 +37,10 @@ func NewBlockService(d ds.Datastore, rem exchange.Interface) (*BlockService, err
|
|||||||
// AddBlock adds a particular block to the service, Putting it into the datastore.
|
// AddBlock adds a particular block to the service, Putting it into the datastore.
|
||||||
func (s *BlockService) AddBlock(b *blocks.Block) (u.Key, error) {
|
func (s *BlockService) AddBlock(b *blocks.Block) (u.Key, error) {
|
||||||
k := b.Key()
|
k := b.Key()
|
||||||
dsk := ds.NewKey(string(k))
|
|
||||||
log.Debug("storing [%s] in datastore", k.Pretty())
|
log.Debug("storing [%s] in datastore", k.Pretty())
|
||||||
// TODO(brian): define a block datastore with a Put method which accepts a
|
// TODO(brian): define a block datastore with a Put method which accepts a
|
||||||
// block parameter
|
// block parameter
|
||||||
err := s.Datastore.Put(dsk, b.Data)
|
err := s.Datastore.Put(k.DsKey(), b.Data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return k, err
|
return k, err
|
||||||
}
|
}
|
||||||
@ -56,8 +55,7 @@ func (s *BlockService) AddBlock(b *blocks.Block) (u.Key, error) {
|
|||||||
// Getting it from the datastore using the key (hash).
|
// Getting it from the datastore using the key (hash).
|
||||||
func (s *BlockService) GetBlock(k u.Key) (*blocks.Block, error) {
|
func (s *BlockService) GetBlock(k u.Key) (*blocks.Block, error) {
|
||||||
log.Debug("BlockService GetBlock: '%s'", k.Pretty())
|
log.Debug("BlockService GetBlock: '%s'", k.Pretty())
|
||||||
dsk := ds.NewKey(string(k))
|
datai, err := s.Datastore.Get(k.DsKey())
|
||||||
datai, err := s.Datastore.Get(dsk)
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
log.Debug("Blockservice: Got data in datastore.")
|
log.Debug("Blockservice: Got data in datastore.")
|
||||||
bdata, ok := datai.([]byte)
|
bdata, ok := datai.([]byte)
|
||||||
|
@ -27,7 +27,7 @@ type blockstore struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (bs *blockstore) Get(k u.Key) (*blocks.Block, error) {
|
func (bs *blockstore) Get(k u.Key) (*blocks.Block, error) {
|
||||||
maybeData, err := bs.datastore.Get(toDatastoreKey(k))
|
maybeData, err := bs.datastore.Get(k.DsKey())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -39,9 +39,5 @@ func (bs *blockstore) Get(k u.Key) (*blocks.Block, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (bs *blockstore) Put(block blocks.Block) error {
|
func (bs *blockstore) Put(block blocks.Block) error {
|
||||||
return bs.datastore.Put(toDatastoreKey(block.Key()), block.Data)
|
return bs.datastore.Put(block.Key().DsKey(), block.Data)
|
||||||
}
|
|
||||||
|
|
||||||
func toDatastoreKey(k u.Key) ds.Key {
|
|
||||||
return ds.NewKey(string(k))
|
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ func TestValueTypeMismatch(t *testing.T) {
|
|||||||
block := testutil.NewBlockOrFail(t, "some data")
|
block := testutil.NewBlockOrFail(t, "some data")
|
||||||
|
|
||||||
datastore := ds.NewMapDatastore()
|
datastore := ds.NewMapDatastore()
|
||||||
datastore.Put(toDatastoreKey(block.Key()), "data that isn't a block!")
|
datastore.Put(block.Key().DsKey(), "data that isn't a block!")
|
||||||
|
|
||||||
blockstore := NewBlockstore(datastore)
|
blockstore := NewBlockstore(datastore)
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ func (p *peerstore) Get(i ID) (*Peer, error) {
|
|||||||
p.RLock()
|
p.RLock()
|
||||||
defer p.RUnlock()
|
defer p.RUnlock()
|
||||||
|
|
||||||
k := ds.NewKey(string(i))
|
k := u.Key(i).DsKey()
|
||||||
val, err := p.peers.Get(k)
|
val, err := p.peers.Get(k)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -54,7 +54,7 @@ func (p *peerstore) Put(peer *Peer) error {
|
|||||||
p.Lock()
|
p.Lock()
|
||||||
defer p.Unlock()
|
defer p.Unlock()
|
||||||
|
|
||||||
k := ds.NewKey(string(peer.ID))
|
k := u.Key(peer.ID).DsKey()
|
||||||
return p.peers.Put(k, peer)
|
return p.peers.Put(k, peer)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,7 +62,7 @@ func (p *peerstore) Delete(i ID) error {
|
|||||||
p.Lock()
|
p.Lock()
|
||||||
defer p.Unlock()
|
defer p.Unlock()
|
||||||
|
|
||||||
k := ds.NewKey(string(i))
|
k := u.Key(i).DsKey()
|
||||||
return p.peers.Delete(k)
|
return p.peers.Delete(k)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,7 +84,7 @@ func (p *peerstore) All() (*Map, error) {
|
|||||||
|
|
||||||
pval, ok := val.(*Peer)
|
pval, ok := val.(*Peer)
|
||||||
if ok {
|
if ok {
|
||||||
(*ps)[u.Key(k.String())] = pval
|
(*ps)[u.Key(pval.ID)] = pval
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ps, nil
|
return ps, nil
|
||||||
|
@ -13,11 +13,11 @@ import (
|
|||||||
peer "github.com/jbenet/go-ipfs/peer"
|
peer "github.com/jbenet/go-ipfs/peer"
|
||||||
kb "github.com/jbenet/go-ipfs/routing/kbucket"
|
kb "github.com/jbenet/go-ipfs/routing/kbucket"
|
||||||
u "github.com/jbenet/go-ipfs/util"
|
u "github.com/jbenet/go-ipfs/util"
|
||||||
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/op/go-logging"
|
|
||||||
|
|
||||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||||
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"
|
||||||
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
|
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
|
||||||
|
logging "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/op/go-logging"
|
||||||
|
|
||||||
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
|
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
|
||||||
)
|
)
|
||||||
@ -328,7 +328,7 @@ func (dht *IpfsDHT) getFromPeerList(ctx context.Context, key u.Key,
|
|||||||
func (dht *IpfsDHT) getLocal(key u.Key) ([]byte, error) {
|
func (dht *IpfsDHT) getLocal(key u.Key) ([]byte, error) {
|
||||||
dht.dslock.Lock()
|
dht.dslock.Lock()
|
||||||
defer dht.dslock.Unlock()
|
defer dht.dslock.Unlock()
|
||||||
v, err := dht.datastore.Get(ds.NewKey(string(key)))
|
v, err := dht.datastore.Get(key.DsKey())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -341,7 +341,7 @@ func (dht *IpfsDHT) getLocal(key u.Key) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (dht *IpfsDHT) putLocal(key u.Key, value []byte) error {
|
func (dht *IpfsDHT) putLocal(key u.Key, value []byte) error {
|
||||||
return dht.datastore.Put(ds.NewKey(string(key)), value)
|
return dht.datastore.Put(key.DsKey(), value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update signals to all routingTables to Update their last-seen status
|
// Update signals to all routingTables to Update their last-seen status
|
||||||
@ -494,13 +494,19 @@ func (dht *IpfsDHT) ensureConnectedToPeer(pbp *Message_Peer) (*peer.Peer, error)
|
|||||||
return p, err
|
return p, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO: this should be smarter about which keys it selects.
|
||||||
func (dht *IpfsDHT) loadProvidableKeys() error {
|
func (dht *IpfsDHT) loadProvidableKeys() error {
|
||||||
kl, err := dht.datastore.KeyList()
|
kl, err := dht.datastore.KeyList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, k := range kl {
|
for _, dsk := range kl {
|
||||||
dht.providers.AddProvider(u.Key(k.Bytes()), dht.self)
|
k := u.KeyFromDsKey(dsk)
|
||||||
|
if len(k) == 0 {
|
||||||
|
log.Error("loadProvidableKeys error: %v", dsk)
|
||||||
|
}
|
||||||
|
|
||||||
|
dht.providers.AddProvider(k, dht.self)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,7 @@ func (dht *IpfsDHT) handleGetValue(p *peer.Peer, pmes *Message) (*Message, error
|
|||||||
|
|
||||||
// let's first check if we have the value locally.
|
// let's first check if we have the value locally.
|
||||||
u.DOut("[%s] handleGetValue looking into ds\n", dht.self.ID.Pretty())
|
u.DOut("[%s] handleGetValue looking into ds\n", dht.self.ID.Pretty())
|
||||||
dskey := ds.NewKey(pmes.GetKey())
|
dskey := u.Key(pmes.GetKey()).DsKey()
|
||||||
iVal, err := dht.datastore.Get(dskey)
|
iVal, err := dht.datastore.Get(dskey)
|
||||||
u.DOut("[%s] handleGetValue looking into ds GOT %v\n", dht.self.ID.Pretty(), iVal)
|
u.DOut("[%s] handleGetValue looking into ds GOT %v\n", dht.self.ID.Pretty(), iVal)
|
||||||
|
|
||||||
@ -96,7 +96,7 @@ func (dht *IpfsDHT) handleGetValue(p *peer.Peer, pmes *Message) (*Message, error
|
|||||||
func (dht *IpfsDHT) handlePutValue(p *peer.Peer, pmes *Message) (*Message, error) {
|
func (dht *IpfsDHT) handlePutValue(p *peer.Peer, pmes *Message) (*Message, error) {
|
||||||
dht.dslock.Lock()
|
dht.dslock.Lock()
|
||||||
defer dht.dslock.Unlock()
|
defer dht.dslock.Unlock()
|
||||||
dskey := ds.NewKey(pmes.GetKey())
|
dskey := u.Key(pmes.GetKey()).DsKey()
|
||||||
err := dht.datastore.Put(dskey, pmes.GetValue())
|
err := dht.datastore.Put(dskey, pmes.GetValue())
|
||||||
u.DOut("[%s] handlePutValue %v %v\n", dht.self.ID.Pretty(), dskey, pmes.GetValue())
|
u.DOut("[%s] handlePutValue %v %v\n", dht.self.ID.Pretty(), dskey, pmes.GetValue())
|
||||||
return pmes, err
|
return pmes, err
|
||||||
@ -137,7 +137,8 @@ func (dht *IpfsDHT) handleGetProviders(p *peer.Peer, pmes *Message) (*Message, e
|
|||||||
resp := newMessage(pmes.GetType(), pmes.GetKey(), pmes.GetClusterLevel())
|
resp := newMessage(pmes.GetType(), pmes.GetKey(), pmes.GetClusterLevel())
|
||||||
|
|
||||||
// check if we have this value, to add ourselves as provider.
|
// check if we have this value, to add ourselves as provider.
|
||||||
has, err := dht.datastore.Has(ds.NewKey(pmes.GetKey()))
|
dsk := u.Key(pmes.GetKey()).DsKey()
|
||||||
|
has, err := dht.datastore.Has(dsk)
|
||||||
if err != nil && err != ds.ErrNotFound {
|
if err != nil && err != ds.ErrNotFound {
|
||||||
u.PErr("unexpected datastore error: %v\n", err)
|
u.PErr("unexpected datastore error: %v\n", err)
|
||||||
has = false
|
has = false
|
||||||
|
@ -33,11 +33,11 @@ func (mr *MockRouter) SetRoutingServer(rs RoutingServer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (mr *MockRouter) PutValue(ctx context.Context, key u.Key, val []byte) error {
|
func (mr *MockRouter) PutValue(ctx context.Context, key u.Key, val []byte) error {
|
||||||
return mr.datastore.Put(ds.NewKey(string(key)), val)
|
return mr.datastore.Put(key.DsKey(), val)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mr *MockRouter) GetValue(ctx context.Context, key u.Key) ([]byte, error) {
|
func (mr *MockRouter) GetValue(ctx context.Context, key u.Key) ([]byte, error) {
|
||||||
v, err := mr.datastore.Get(ds.NewKey(string(key)))
|
v, err := mr.datastore.Get(key.DsKey())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
12
util/util.go
12
util/util.go
@ -41,6 +41,18 @@ func (k Key) Pretty() string {
|
|||||||
return b58.Encode([]byte(k))
|
return b58.Encode([]byte(k))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DsKey returns a Datastore key
|
||||||
|
func (k Key) DsKey() ds.Key {
|
||||||
|
return ds.NewKey(k.Pretty())
|
||||||
|
}
|
||||||
|
|
||||||
|
// KeyFromDsKey returns a Datastore key
|
||||||
|
func KeyFromDsKey(dsk ds.Key) Key {
|
||||||
|
l := dsk.List()
|
||||||
|
enc := l[len(l)-1]
|
||||||
|
return Key(b58.Decode(enc))
|
||||||
|
}
|
||||||
|
|
||||||
// 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
|
||||||
func Hash(data []byte) (mh.Multihash, error) {
|
func Hash(data []byte) (mh.Multihash, error) {
|
||||||
return mh.Sum(data, mh.SHA2_256, -1)
|
return mh.Sum(data, mh.SHA2_256, -1)
|
||||||
|
Reference in New Issue
Block a user