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-blockservice
Commits
b36159b9
Commit
b36159b9
authored
Oct 07, 2016
by
Jeromy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cid: integrate cid into bitswap and blockstores
License: MIT Signed-off-by:
Jeromy
<
why@ipfs.io
>
parent
ca11d848
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
34 deletions
+20
-34
blockservice.go
blockservice.go
+12
-25
test/blocks_test.go
test/blocks_test.go
+8
-9
No files found.
blockservice.go
View file @
b36159b9
...
...
@@ -10,7 +10,6 @@ import (
blocks
"github.com/ipfs/go-ipfs/blocks"
"github.com/ipfs/go-ipfs/blocks/blockstore"
exchange
"github.com/ipfs/go-ipfs/exchange"
key
"gx/ipfs/QmYEoKZXHoAToWfhGF3vryhMn3WWhE1o2MasQ8uzY5iDi9/go-key"
context
"context"
logging
"gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
...
...
@@ -30,12 +29,6 @@ type BlockService struct {
Exchange
exchange
.
Interface
}
// an Object is simply a typed block
type
Object
interface
{
Cid
()
*
cid
.
Cid
blocks
.
Block
}
// NewBlockService creates a BlockService with given datastore instance.
func
New
(
bs
blockstore
.
Blockstore
,
rem
exchange
.
Interface
)
*
BlockService
{
if
rem
==
nil
{
...
...
@@ -50,14 +43,14 @@ func New(bs blockstore.Blockstore, rem exchange.Interface) *BlockService {
// AddBlock adds a particular block to the service, Putting it into the datastore.
// TODO pass a context into this if the remote.HasBlock is going to remain here.
func
(
s
*
BlockService
)
Add
Object
(
o
Object
)
(
*
cid
.
Cid
,
error
)
{
func
(
s
*
BlockService
)
Add
Block
(
o
blocks
.
Block
)
(
*
cid
.
Cid
,
error
)
{
// TODO: while this is a great optimization, we should think about the
// possibility of streaming writes directly to disk. If we can pass this object
// all the way down to the datastore without having to 'buffer' its data,
// we could implement a `WriteTo` method on it that could do a streaming write
// of the content, saving us (probably) considerable memory.
c
:=
o
.
Cid
()
has
,
err
:=
s
.
Blockstore
.
Has
(
key
.
Key
(
c
.
Hash
())
)
has
,
err
:=
s
.
Blockstore
.
Has
(
c
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -78,13 +71,10 @@ func (s *BlockService) AddObject(o Object) (*cid.Cid, error) {
return
c
,
nil
}
func
(
s
*
BlockService
)
Add
Object
s
(
bs
[]
Object
)
([]
*
cid
.
Cid
,
error
)
{
func
(
s
*
BlockService
)
Add
Block
s
(
bs
[]
blocks
.
Block
)
([]
*
cid
.
Cid
,
error
)
{
var
toput
[]
blocks
.
Block
var
toputcids
[]
*
cid
.
Cid
for
_
,
b
:=
range
bs
{
c
:=
b
.
Cid
()
has
,
err
:=
s
.
Blockstore
.
Has
(
key
.
Key
(
c
.
Hash
()))
has
,
err
:=
s
.
Blockstore
.
Has
(
b
.
Cid
())
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -94,7 +84,6 @@ func (s *BlockService) AddObjects(bs []Object) ([]*cid.Cid, error) {
}
toput
=
append
(
toput
,
b
)
toputcids
=
append
(
toputcids
,
c
)
}
err
:=
s
.
Blockstore
.
PutMany
(
toput
)
...
...
@@ -108,8 +97,7 @@ func (s *BlockService) AddObjects(bs []Object) ([]*cid.Cid, error) {
return
nil
,
fmt
.
Errorf
(
"blockservice is closed (%s)"
,
err
)
}
c
:=
o
.
(
Object
)
.
Cid
()
// cast is safe, we created these
ks
=
append
(
ks
,
c
)
ks
=
append
(
ks
,
o
.
Cid
())
}
return
ks
,
nil
}
...
...
@@ -119,7 +107,7 @@ func (s *BlockService) AddObjects(bs []Object) ([]*cid.Cid, error) {
func
(
s
*
BlockService
)
GetBlock
(
ctx
context
.
Context
,
c
*
cid
.
Cid
)
(
blocks
.
Block
,
error
)
{
log
.
Debugf
(
"BlockService GetBlock: '%s'"
,
c
)
block
,
err
:=
s
.
Blockstore
.
Get
(
key
.
Key
(
c
.
Hash
())
)
block
,
err
:=
s
.
Blockstore
.
Get
(
c
)
if
err
==
nil
{
return
block
,
nil
}
...
...
@@ -128,7 +116,7 @@ func (s *BlockService) GetBlock(ctx context.Context, c *cid.Cid) (blocks.Block,
// TODO be careful checking ErrNotFound. If the underlying
// implementation changes, this will break.
log
.
Debug
(
"Blockservice: Searching bitswap"
)
blk
,
err
:=
s
.
Exchange
.
GetBlock
(
ctx
,
key
.
Key
(
c
.
Hash
())
)
blk
,
err
:=
s
.
Exchange
.
GetBlock
(
ctx
,
c
)
if
err
!=
nil
{
if
err
==
blockstore
.
ErrNotFound
{
return
nil
,
ErrNotFound
...
...
@@ -153,12 +141,11 @@ func (s *BlockService) GetBlocks(ctx context.Context, ks []*cid.Cid) <-chan bloc
out
:=
make
(
chan
blocks
.
Block
,
0
)
go
func
()
{
defer
close
(
out
)
var
misses
[]
key
.
Key
var
misses
[]
*
cid
.
Cid
for
_
,
c
:=
range
ks
{
k
:=
key
.
Key
(
c
.
Hash
())
hit
,
err
:=
s
.
Blockstore
.
Get
(
k
)
hit
,
err
:=
s
.
Blockstore
.
Get
(
c
)
if
err
!=
nil
{
misses
=
append
(
misses
,
k
)
misses
=
append
(
misses
,
c
)
continue
}
log
.
Debug
(
"Blockservice: Got data in datastore"
)
...
...
@@ -191,8 +178,8 @@ func (s *BlockService) GetBlocks(ctx context.Context, ks []*cid.Cid) <-chan bloc
}
// DeleteBlock deletes a block in the blockservice from the datastore
func
(
s
*
BlockService
)
Delete
Object
(
o
Object
)
error
{
return
s
.
Blockstore
.
DeleteBlock
(
o
.
Key
())
func
(
s
*
BlockService
)
Delete
Block
(
o
blocks
.
Block
)
error
{
return
s
.
Blockstore
.
DeleteBlock
(
o
.
Cid
())
}
func
(
s
*
BlockService
)
Close
()
error
{
...
...
test/blocks_test.go
View file @
b36159b9
...
...
@@ -2,6 +2,7 @@ package bstest
import
(
"bytes"
"context"
"fmt"
"testing"
"time"
...
...
@@ -10,9 +11,7 @@ import (
blockstore
"github.com/ipfs/go-ipfs/blocks/blockstore"
.
"github.com/ipfs/go-ipfs/blockservice"
offline
"github.com/ipfs/go-ipfs/exchange/offline"
key
"gx/ipfs/QmYEoKZXHoAToWfhGF3vryhMn3WWhE1o2MasQ8uzY5iDi9/go-key"
"context"
cid
"gx/ipfs/QmakyCk6Vnn16WEKjbkxieZmM2YLTzkFWizbmGowoYPjro/go-cid"
u
"gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
ds
"gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore"
...
...
@@ -44,11 +43,11 @@ func TestBlocks(t *testing.T) {
t
.
Error
(
"Block Multihash and data multihash not equal"
)
}
if
o
.
Key
()
!=
key
.
Key
(
h
)
{
if
!
o
.
Cid
()
.
Equals
(
cid
.
NewCidV0
(
h
)
)
{
t
.
Error
(
"Block key and data multihash key not equal"
)
}
k
,
err
:=
bs
.
Add
Object
(
o
)
k
,
err
:=
bs
.
Add
Block
(
o
)
if
err
!=
nil
{
t
.
Error
(
"failed to add block to BlockService"
,
err
)
return
...
...
@@ -66,7 +65,7 @@ func TestBlocks(t *testing.T) {
return
}
if
o
.
Key
()
!=
b2
.
Key
(
)
{
if
!
o
.
Cid
()
.
Equals
(
b2
.
Cid
()
)
{
t
.
Error
(
"Block keys not equal."
)
}
...
...
@@ -93,7 +92,7 @@ func TestGetBlocksSequential(t *testing.T) {
var
cids
[]
*
cid
.
Cid
for
_
,
o
:=
range
objs
{
cids
=
append
(
cids
,
o
.
Cid
())
servs
[
0
]
.
Add
Object
(
o
)
servs
[
0
]
.
Add
Block
(
o
)
}
t
.
Log
(
"one instance at a time, get blocks concurrently"
)
...
...
@@ -102,12 +101,12 @@ func TestGetBlocksSequential(t *testing.T) {
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
time
.
Second
*
50
)
defer
cancel
()
out
:=
servs
[
i
]
.
GetBlocks
(
ctx
,
cids
)
gotten
:=
make
(
map
[
key
.
Key
]
blocks
.
Block
)
gotten
:=
make
(
map
[
string
]
blocks
.
Block
)
for
blk
:=
range
out
{
if
_
,
ok
:=
gotten
[
blk
.
Key
()];
ok
{
if
_
,
ok
:=
gotten
[
blk
.
Cid
()
.
KeyString
()];
ok
{
t
.
Fatal
(
"Got duplicate block!"
)
}
gotten
[
blk
.
Key
()]
=
blk
gotten
[
blk
.
Cid
()
.
KeyString
()]
=
blk
}
if
len
(
gotten
)
!=
len
(
objs
)
{
t
.
Fatalf
(
"Didnt get enough blocks back: %d/%d"
,
len
(
gotten
),
len
(
objs
))
...
...
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