@cryptotaxi247 / kubo / commits / a5241b767

block service

Juan Batiz-Benet committed Jul 5, 2014 at 15:32 UTC a5241b767bc008a22bff697679eb55fd3f9e47b6
2 files changed +115 -4
blocks/blocks.go
+53 -4
@@ -1,14 +1,63 @@
1 package blocks
2
3 import (
4 - "github.com/jbenet/go-ipfs/bitswap"
5 - "github.com/jbenet/go-ipfs/storage"
4 + "fmt"
5 + ds "github.com/jbenet/datastore.go"
6 + u "github.com/jbenet/go-ipfs/util"
7 + mh "github.com/jbenet/go-multihash"
8 )
9
10 // Blocks is the ipfs blocks service. It is the way
11 // to retrieve blocks by the higher level ipfs modules
12
13 +type Block struct {
14 + Multihash mh.Multihash
15 + Data []byte
16 +}
17 +
18 +func NewBlock(data []byte) (*Block, error) {
19 + h, err := u.Hash(data)
20 + if err != nil {
21 + return nil, err
22 + }
23 + return &Block{Data: data, Multihash: h}, nil
24 +}
25 +
26 +func (b *Block) Key() u.Key {
27 + return u.Key(b.Multihash)
28 +}
29 +
30 type BlockService struct {
12 - Local *storage.Storage
13 - Remote *bitswap.BitSwap
31 + Datastore ds.Datastore
32 + // Remote *bitswap.BitSwap // eventually.
33 +}
34 +
35 +func NewBlockService(d ds.Datastore) (*BlockService, error) {
36 + if d == nil {
37 + return nil, fmt.Errorf("BlockService requires valid datastore")
38 + }
39 + return &BlockService{Datastore: d}, nil
40 +}
41 +
42 +func (s *BlockService) AddBlock(b *Block) error {
43 + dsk := ds.NewKey(string(b.Key()))
44 + return s.Datastore.Put(dsk, b.Data)
45 +}
46 +
47 +func (s *BlockService) GetBlock(k u.Key) (*Block, error) {
48 + dsk := ds.NewKey(string(k))
49 + datai, err := s.Datastore.Get(dsk)
50 + if err != nil {
51 + return nil, err
52 + }
53 +
54 + data, ok := datai.([]byte)
55 + if !ok {
56 + return nil, fmt.Errorf("data associated with %s is not a []byte", k)
57 + }
58 +
59 + return &Block{
60 + Multihash: mh.Multihash(k),
61 + Data: data,
62 + }, nil
63 }
blocks/blocks_test.go new
+62
@@ -0,0 +1,62 @@
1 +package blocks
2 +
3 +import (
4 + "bytes"
5 + "fmt"
6 + ds "github.com/jbenet/datastore.go"
7 + u "github.com/jbenet/go-ipfs/util"
8 + "testing"
9 +)
10 +
11 +func TestBlocks(t *testing.T) {
12 +
13 + d := ds.NewMapDatastore()
14 + bs, err := NewBlockService(d)
15 + if err != nil {
16 + t.Error("failed to construct block service", err)
17 + return
18 + }
19 +
20 + b, err := NewBlock([]byte("beep boop"))
21 + if err != nil {
22 + t.Error("failed to construct block", err)
23 + return
24 + }
25 +
26 + h, err := u.Hash([]byte("beep boop"))
27 + if err != nil {
28 + t.Error("failed to hash data", err)
29 + return
30 + }
31 +
32 + if !bytes.Equal(b.Multihash, h) {
33 + t.Error("Block Multihash and data multihash not equal")
34 + }
35 +
36 + if b.Key() != u.Key(h) {
37 + t.Error("Block key and data multihash key not equal")
38 + }
39 +
40 + err = bs.AddBlock(b)
41 + if err != nil {
42 + t.Error("failed to add block to BlockService", err)
43 + return
44 + }
45 +
46 + b2, err := bs.GetBlock(b.Key())
47 + if err != nil {
48 + t.Error("failed to retrieve block from BlockService", err)
49 + return
50 + }
51 +
52 + if b.Key() != b2.Key() {
53 + t.Error("Block keys not equal.")
54 + }
55 +
56 + if !bytes.Equal(b.Data, b2.Data) {
57 + t.Error("Block data is not equal.")
58 + }
59 +
60 + fmt.Printf("key: %s\n", b.Key())
61 + fmt.Printf("data: %v\n", b.Data)
62 +}