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
54ff6ea4
Commit
54ff6ea4
authored
Aug 26, 2014
by
Jeromy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bitswap first working commit!
parent
ffc60abb
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
15 deletions
+30
-15
blockservice.go
blockservice.go
+30
-15
No files found.
blockservice.go
View file @
54ff6ea4
...
...
@@ -2,6 +2,7 @@ package blockservice
import
(
"fmt"
"time"
ds
"github.com/jbenet/datastore.go"
bitswap
"github.com/jbenet/go-ipfs/bitswap"
...
...
@@ -19,18 +20,27 @@ type BlockService struct {
}
// NewBlockService creates a BlockService with given datastore instance.
func
NewBlockService
(
d
ds
.
Datastore
)
(
*
BlockService
,
error
)
{
func
NewBlockService
(
d
ds
.
Datastore
,
rem
*
bitswap
.
BitSwap
)
(
*
BlockService
,
error
)
{
if
d
==
nil
{
return
nil
,
fmt
.
Errorf
(
"BlockService requires valid datastore"
)
}
return
&
BlockService
{
Datastore
:
d
},
nil
if
rem
==
nil
{
return
nil
,
fmt
.
Errorf
(
"BlockService requires a valid bitswap"
)
}
return
&
BlockService
{
Datastore
:
d
,
Remote
:
rem
},
nil
}
// 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
))
return
k
,
s
.
Datastore
.
Put
(
dsk
,
b
.
Data
)
u
.
DOut
(
"storing [%s] in datastore
\n
"
,
k
.
Pretty
())
err
:=
s
.
Datastore
.
Put
(
dsk
,
b
.
Data
)
if
err
!=
nil
{
return
k
,
err
}
err
=
s
.
Remote
.
HaveBlock
(
b
.
Key
())
return
k
,
err
}
// GetBlock retrieves a particular block from the service,
...
...
@@ -38,17 +48,22 @@ func (s *BlockService) AddBlock(b *blocks.Block) (u.Key, error) {
func
(
s
*
BlockService
)
GetBlock
(
k
u
.
Key
)
(
*
blocks
.
Block
,
error
)
{
dsk
:=
ds
.
NewKey
(
string
(
k
))
datai
,
err
:=
s
.
Datastore
.
Get
(
dsk
)
if
err
!=
nil
{
return
nil
,
err
}
data
,
ok
:=
datai
.
([]
byte
)
if
err
==
nil
{
bdata
,
ok
:=
datai
.
([]
byte
)
if
!
ok
{
return
nil
,
fmt
.
Errorf
(
"data associated with %s is not a []byte"
,
k
)
}
return
&
blocks
.
Block
{
Multihash
:
mh
.
Multihash
(
k
),
Data
:
data
,
Data
:
b
data
,
},
nil
}
else
if
err
==
ds
.
ErrNotFound
{
blk
,
err
:=
s
.
Remote
.
GetBlock
(
k
,
time
.
Second
*
5
)
if
err
!=
nil
{
return
nil
,
err
}
return
blk
,
nil
}
else
{
return
nil
,
u
.
ErrNotFound
}
}
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