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-unixfs
Commits
612be596
Commit
612be596
authored
10 years ago
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use string datastore keys.
parent
2ce9415c
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
39 additions
and
26 deletions
+39
-26
blockservice/blockservice.go
blockservice/blockservice.go
+3
-5
blockstore/blockstore.go
blockstore/blockstore.go
+2
-6
blockstore/blockstore_test.go
blockstore/blockstore_test.go
+1
-1
peer/peerstore.go
peer/peerstore.go
+4
-4
routing/dht/dht.go
routing/dht/dht.go
+11
-5
routing/dht/handlers.go
routing/dht/handlers.go
+4
-3
routing/mock/routing.go
routing/mock/routing.go
+2
-2
util/util.go
util/util.go
+12
-0
No files found.
blockservice/blockservice.go
View file @
612be596
...
...
@@ -7,7 +7,7 @@ import (
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"
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"
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.
func
(
s
*
BlockService
)
AddBlock
(
b
*
blocks
.
Block
)
(
u
.
Key
,
error
)
{
k
:=
b
.
Key
()
dsk
:=
ds
.
NewKey
(
string
(
k
))
log
.
Debug
(
"storing [%s] in datastore"
,
k
.
Pretty
())
// TODO(brian): define a block datastore with a Put method which accepts a
// block parameter
err
:=
s
.
Datastore
.
Put
(
dsk
,
b
.
Data
)
err
:=
s
.
Datastore
.
Put
(
k
.
DsKey
()
,
b
.
Data
)
if
err
!=
nil
{
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).
func
(
s
*
BlockService
)
GetBlock
(
k
u
.
Key
)
(
*
blocks
.
Block
,
error
)
{
log
.
Debug
(
"BlockService GetBlock: '%s'"
,
k
.
Pretty
())
dsk
:=
ds
.
NewKey
(
string
(
k
))
datai
,
err
:=
s
.
Datastore
.
Get
(
dsk
)
datai
,
err
:=
s
.
Datastore
.
Get
(
k
.
DsKey
())
if
err
==
nil
{
log
.
Debug
(
"Blockservice: Got data in datastore."
)
bdata
,
ok
:=
datai
.
([]
byte
)
...
...
This diff is collapsed.
Click to expand it.
blockstore/blockstore.go
View file @
612be596
...
...
@@ -27,7 +27,7 @@ type blockstore struct {
}
func
(
bs
*
blockstore
)
Get
(
k
u
.
Key
)
(
*
blocks
.
Block
,
error
)
{
maybeData
,
err
:=
bs
.
datastore
.
Get
(
toDatastore
Key
(
k
))
maybeData
,
err
:=
bs
.
datastore
.
Get
(
k
.
Ds
Key
())
if
err
!=
nil
{
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
{
return
bs
.
datastore
.
Put
(
toDatastoreKey
(
block
.
Key
()),
block
.
Data
)
}
func
toDatastoreKey
(
k
u
.
Key
)
ds
.
Key
{
return
ds
.
NewKey
(
string
(
k
))
return
bs
.
datastore
.
Put
(
block
.
Key
()
.
DsKey
(),
block
.
Data
)
}
This diff is collapsed.
Click to expand it.
blockstore/blockstore_test.go
View file @
612be596
...
...
@@ -44,7 +44,7 @@ func TestValueTypeMismatch(t *testing.T) {
block
:=
testutil
.
NewBlockOrFail
(
t
,
"some data"
)
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
)
...
...
This diff is collapsed.
Click to expand it.
peer/peerstore.go
View file @
612be596
...
...
@@ -37,7 +37,7 @@ func (p *peerstore) Get(i ID) (*Peer, error) {
p
.
RLock
()
defer
p
.
RUnlock
()
k
:=
ds
.
NewKey
(
string
(
i
)
)
k
:=
u
.
Key
(
i
)
.
DsKey
(
)
val
,
err
:=
p
.
peers
.
Get
(
k
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -54,7 +54,7 @@ func (p *peerstore) Put(peer *Peer) error {
p
.
Lock
()
defer
p
.
Unlock
()
k
:=
ds
.
NewKey
(
string
(
peer
.
ID
))
k
:=
u
.
Key
(
peer
.
ID
)
.
DsKey
(
)
return
p
.
peers
.
Put
(
k
,
peer
)
}
...
...
@@ -62,7 +62,7 @@ func (p *peerstore) Delete(i ID) error {
p
.
Lock
()
defer
p
.
Unlock
()
k
:=
ds
.
NewKey
(
string
(
i
)
)
k
:=
u
.
Key
(
i
)
.
DsKey
(
)
return
p
.
peers
.
Delete
(
k
)
}
...
...
@@ -84,7 +84,7 @@ func (p *peerstore) All() (*Map, error) {
pval
,
ok
:=
val
.
(
*
Peer
)
if
ok
{
(
*
ps
)[
u
.
Key
(
k
.
String
()
)]
=
pval
(
*
ps
)[
u
.
Key
(
pval
.
ID
)]
=
pval
}
}
return
ps
,
nil
...
...
This diff is collapsed.
Click to expand it.
routing/dht/dht.go
View file @
612be596
...
...
@@ -13,11 +13,11 @@ import (
peer
"github.com/jbenet/go-ipfs/peer"
kb
"github.com/jbenet/go-ipfs/routing/kbucket"
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"
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"
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"
)
...
...
@@ -328,7 +328,7 @@ func (dht *IpfsDHT) getFromPeerList(ctx context.Context, key u.Key,
func
(
dht
*
IpfsDHT
)
getLocal
(
key
u
.
Key
)
([]
byte
,
error
)
{
dht
.
dslock
.
Lock
()
defer
dht
.
dslock
.
Unlock
()
v
,
err
:=
dht
.
datastore
.
Get
(
ds
.
NewKey
(
string
(
key
)
))
v
,
err
:=
dht
.
datastore
.
Get
(
key
.
DsKey
(
))
if
err
!=
nil
{
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
{
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
...
...
@@ -494,13 +494,19 @@ func (dht *IpfsDHT) ensureConnectedToPeer(pbp *Message_Peer) (*peer.Peer, error)
return
p
,
err
}
//TODO: this should be smarter about which keys it selects.
func
(
dht
*
IpfsDHT
)
loadProvidableKeys
()
error
{
kl
,
err
:=
dht
.
datastore
.
KeyList
()
if
err
!=
nil
{
return
err
}
for
_
,
k
:=
range
kl
{
dht
.
providers
.
AddProvider
(
u
.
Key
(
k
.
Bytes
()),
dht
.
self
)
for
_
,
dsk
:=
range
kl
{
k
:=
u
.
KeyFromDsKey
(
dsk
)
if
len
(
k
)
==
0
{
log
.
Error
(
"loadProvidableKeys error: %v"
,
dsk
)
}
dht
.
providers
.
AddProvider
(
k
,
dht
.
self
)
}
return
nil
}
...
...
This diff is collapsed.
Click to expand it.
routing/dht/handlers.go
View file @
612be596
...
...
@@ -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.
u
.
DOut
(
"[%s] handleGetValue looking into ds
\n
"
,
dht
.
self
.
ID
.
Pretty
())
dskey
:=
ds
.
New
Key
(
pmes
.
GetKey
())
dskey
:=
u
.
Key
(
pmes
.
GetKey
())
.
DsKey
()
iVal
,
err
:=
dht
.
datastore
.
Get
(
dskey
)
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
)
{
dht
.
dslock
.
Lock
()
defer
dht
.
dslock
.
Unlock
()
dskey
:=
ds
.
New
Key
(
pmes
.
GetKey
())
dskey
:=
u
.
Key
(
pmes
.
GetKey
())
.
DsKey
()
err
:=
dht
.
datastore
.
Put
(
dskey
,
pmes
.
GetValue
())
u
.
DOut
(
"[%s] handlePutValue %v %v
\n
"
,
dht
.
self
.
ID
.
Pretty
(),
dskey
,
pmes
.
GetValue
())
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
())
// 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
{
u
.
PErr
(
"unexpected datastore error: %v
\n
"
,
err
)
has
=
false
...
...
This diff is collapsed.
Click to expand it.
routing/mock/routing.go
View file @
612be596
...
...
@@ -33,11 +33,11 @@ func (mr *MockRouter) SetRoutingServer(rs RoutingServer) {
}
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
)
{
v
,
err
:=
mr
.
datastore
.
Get
(
ds
.
NewKey
(
string
(
key
)
))
v
,
err
:=
mr
.
datastore
.
Get
(
key
.
DsKey
(
))
if
err
!=
nil
{
return
nil
,
err
}
...
...
This diff is collapsed.
Click to expand it.
util/util.go
View file @
612be596
...
...
@@ -41,6 +41,18 @@ func (k Key) Pretty() string {
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
func
Hash
(
data
[]
byte
)
(
mh
.
Multihash
,
error
)
{
return
mh
.
Sum
(
data
,
mh
.
SHA2_256
,
-
1
)
...
...
This diff is collapsed.
Click to expand it.
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