Make BlockService an interface.
License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
Kevin Atkinson committed
Oct 5, 2016 at 23:54 UTC
dfd5e9aa1e80d13b68081c35b2f72256f6ec3172
5 files changed
+53
-35
blockservice/blockservice.go
+45
-27
@@ -23,34 +23,52 @@ var ErrNotFound = errors.New("blockservice: key not found")
23
// BlockService is a hybrid block datastore. It stores data in a local
24
// datastore and may retrieve data from a remote Exchange.
25
// It uses an internal `datastore.Datastore` instance to store values.
26
-type BlockService struct {
27
- // TODO don't expose underlying impl details
28
- Blockstore blockstore.Blockstore
29
- Exchange exchange.Interface
26
+type BlockService interface {
27
+ Blockstore() blockstore.Blockstore
28
+ Exchange() exchange.Interface
29
+ AddBlock(o blocks.Block) (*cid.Cid, error)
30
+ AddBlocks(bs []blocks.Block) ([]*cid.Cid, error)
31
+ GetBlock(ctx context.Context, c *cid.Cid) (blocks.Block, error)
32
+ GetBlocks(ctx context.Context, ks []*cid.Cid) <-chan blocks.Block
33
+ DeleteBlock(o blocks.Block) error
34
+ Close() error
35
+}
36
+
37
+type blockService struct {
38
+ blockstore blockstore.Blockstore
39
+ exchange exchange.Interface
40
}
41
42
// NewBlockService creates a BlockService with given datastore instance.
33
-func New(bs blockstore.Blockstore, rem exchange.Interface) *BlockService {
43
+func New(bs blockstore.Blockstore, rem exchange.Interface) BlockService {
44
if rem == nil {
45
log.Warning("blockservice running in local (offline) mode.")
46
}
47
38
- return &BlockService{
39
- Blockstore: bs,
40
- Exchange: rem,
48
+ return &blockService{
49
+ blockstore: bs,
50
+ exchange: rem,
51
}
52
}
53
54
+func (bs *blockService) Blockstore() blockstore.Blockstore {
55
+ return bs.blockstore
56
+}
57
+
58
+func (bs *blockService) Exchange() exchange.Interface {
59
+ return bs.exchange
60
+}
61
+
62
// AddBlock adds a particular block to the service, Putting it into the datastore.
63
// TODO pass a context into this if the remote.HasBlock is going to remain here.
46
-func (s *BlockService) AddBlock(o blocks.Block) (*cid.Cid, error) {
64
+func (s *blockService) AddBlock(o blocks.Block) (*cid.Cid, error) {
65
// TODO: while this is a great optimization, we should think about the
66
// possibility of streaming writes directly to disk. If we can pass this object
67
// all the way down to the datastore without having to 'buffer' its data,
68
// we could implement a `WriteTo` method on it that could do a streaming write
69
// of the content, saving us (probably) considerable memory.
70
c := o.Cid()
53
- has, err := s.Blockstore.Has(c)
71
+ has, err := s.blockstore.Has(c)
72
if err != nil {
73
return nil, err
74
}
@@ -59,22 +77,22 @@ func (s *BlockService) AddBlock(o blocks.Block) (*cid.Cid, error) {
77
return c, nil
78
}
79
62
- err = s.Blockstore.Put(o)
80
+ err = s.blockstore.Put(o)
81
if err != nil {
82
return nil, err
83
}
84
67
- if err := s.Exchange.HasBlock(o); err != nil {
85
+ if err := s.exchange.HasBlock(o); err != nil {
86
return nil, errors.New("blockservice is closed")
87
}
88
89
return c, nil
90
}
91
74
-func (s *BlockService) AddBlocks(bs []blocks.Block) ([]*cid.Cid, error) {
92
+func (s *blockService) AddBlocks(bs []blocks.Block) ([]*cid.Cid, error) {
93
var toput []blocks.Block
94
for _, b := range bs {
77
- has, err := s.Blockstore.Has(b.Cid())
95
+ has, err := s.blockstore.Has(b.Cid())
96
if err != nil {
97
return nil, err
98
}
@@ -86,14 +104,14 @@ func (s *BlockService) AddBlocks(bs []blocks.Block) ([]*cid.Cid, error) {
104
toput = append(toput, b)
105
}
106
89
- err := s.Blockstore.PutMany(toput)
107
+ err := s.blockstore.PutMany(toput)
108
if err != nil {
109
return nil, err
110
}
111
112
var ks []*cid.Cid
113
for _, o := range toput {
96
- if err := s.Exchange.HasBlock(o); err != nil {
114
+ if err := s.exchange.HasBlock(o); err != nil {
115
return nil, fmt.Errorf("blockservice is closed (%s)", err)
116
}
117
@@ -104,19 +122,19 @@ func (s *BlockService) AddBlocks(bs []blocks.Block) ([]*cid.Cid, error) {
122
123
// GetBlock retrieves a particular block from the service,
124
// Getting it from the datastore using the key (hash).
107
-func (s *BlockService) GetBlock(ctx context.Context, c *cid.Cid) (blocks.Block, error) {
125
+func (s *blockService) GetBlock(ctx context.Context, c *cid.Cid) (blocks.Block, error) {
126
log.Debugf("BlockService GetBlock: '%s'", c)
127
110
- block, err := s.Blockstore.Get(c)
128
+ block, err := s.blockstore.Get(c)
129
if err == nil {
130
return block, nil
131
}
132
115
- if err == blockstore.ErrNotFound && s.Exchange != nil {
133
+ if err == blockstore.ErrNotFound && s.exchange != nil {
134
// TODO be careful checking ErrNotFound. If the underlying
135
// implementation changes, this will break.
136
log.Debug("Blockservice: Searching bitswap")
119
- blk, err := s.Exchange.GetBlock(ctx, c)
137
+ blk, err := s.exchange.GetBlock(ctx, c)
138
if err != nil {
139
if err == blockstore.ErrNotFound {
140
return nil, ErrNotFound
@@ -137,13 +155,13 @@ func (s *BlockService) GetBlock(ctx context.Context, c *cid.Cid) (blocks.Block,
155
// GetBlocks gets a list of blocks asynchronously and returns through
156
// the returned channel.
157
// NB: No guarantees are made about order.
140
-func (s *BlockService) GetBlocks(ctx context.Context, ks []*cid.Cid) <-chan blocks.Block {
158
+func (s *blockService) GetBlocks(ctx context.Context, ks []*cid.Cid) <-chan blocks.Block {
159
out := make(chan blocks.Block, 0)
160
go func() {
161
defer close(out)
162
var misses []*cid.Cid
163
for _, c := range ks {
146
- hit, err := s.Blockstore.Get(c)
164
+ hit, err := s.blockstore.Get(c)
165
if err != nil {
166
misses = append(misses, c)
167
continue
@@ -160,7 +178,7 @@ func (s *BlockService) GetBlocks(ctx context.Context, ks []*cid.Cid) <-chan bloc
178
return
179
}
180
163
- rblocks, err := s.Exchange.GetBlocks(ctx, misses)
181
+ rblocks, err := s.exchange.GetBlocks(ctx, misses)
182
if err != nil {
183
log.Debugf("Error with GetBlocks: %s", err)
184
return
@@ -178,11 +196,11 @@ func (s *BlockService) GetBlocks(ctx context.Context, ks []*cid.Cid) <-chan bloc
196
}
197
198
// DeleteBlock deletes a block in the blockservice from the datastore
181
-func (s *BlockService) DeleteBlock(o blocks.Block) error {
182
- return s.Blockstore.DeleteBlock(o.Cid())
199
+func (s *blockService) DeleteBlock(o blocks.Block) error {
200
+ return s.blockstore.DeleteBlock(o.Cid())
201
}
202
185
-func (s *BlockService) Close() error {
203
+func (s *blockService) Close() error {
204
log.Debug("blockservice is shutting down...")
187
- return s.Exchange.Close()
205
+ return s.exchange.Close()
206
}
blockservice/test/mock.go
+2
-2
@@ -9,13 +9,13 @@ import (
9
)
10
11
// Mocks returns |n| connected mock Blockservices
12
-func Mocks(n int) []*BlockService {
12
+func Mocks(n int) []BlockService {
13
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(0))
14
sg := bitswap.NewTestSessionGenerator(net)
15
16
instances := sg.Instances(n)
17
18
- var servs []*BlockService
18
+ var servs []BlockService
19
for _, i := range instances {
20
servs = append(servs, New(i.Blockstore(), i.Exchange))
21
}
core/core.go
+1
-1
@@ -96,7 +96,7 @@ type IpfsNode struct {
96
// Services
97
Peerstore pstore.Peerstore // storage for other Peer instances
98
Blockstore bstore.GCBlockstore // the block store (lower level)
99
- Blocks *bserv.BlockService // the block service, get/add blocks.
99
+ Blocks bserv.BlockService // the block service, get/add blocks.
100
DAG merkledag.DAGService // the merkle dag service, get/add objects.
101
Resolver *path.Resolver // the path resolution system
102
Reporter metrics.Reporter
merkledag/merkledag.go
+4
-4
@@ -41,7 +41,7 @@ type LinkService interface {
41
GetOfflineLinkService() LinkService
42
}
43
44
-func NewDAGService(bs *bserv.BlockService) *dagService {
44
+func NewDAGService(bs bserv.BlockService) *dagService {
45
return &dagService{Blocks: bs}
46
}
47
@@ -51,7 +51,7 @@ func NewDAGService(bs *bserv.BlockService) *dagService {
51
// TODO: should cache Nodes that are in memory, and be
52
// able to free some of them when vm pressure is high
53
type dagService struct {
54
- Blocks *bserv.BlockService
54
+ Blocks bserv.BlockService
55
}
56
57
// Add adds a node to the dagService, storing the block in the BlockService
@@ -113,8 +113,8 @@ func (n *dagService) GetLinks(ctx context.Context, c *cid.Cid) ([]*Link, error)
113
}
114
115
func (n *dagService) GetOfflineLinkService() LinkService {
116
- if n.Blocks.Exchange.IsOnline() {
117
- bsrv := bserv.New(n.Blocks.Blockstore, offline.Exchange(n.Blocks.Blockstore))
116
+ if n.Blocks.Exchange().IsOnline() {
117
+ bsrv := bserv.New(n.Blocks.Blockstore(), offline.Exchange(n.Blocks.Blockstore()))
118
return NewDAGService(bsrv)
119
} else {
120
return n
merkledag/merkledag_test.go
+1
-1
@@ -237,7 +237,7 @@ func TestFetchGraph(t *testing.T) {
237
}
238
239
// create an offline dagstore and ensure all blocks were fetched
240
- bs := bserv.New(bsis[1].Blockstore, offline.Exchange(bsis[1].Blockstore))
240
+ bs := bserv.New(bsis[1].Blockstore(), offline.Exchange(bsis[1].Blockstore()))
241
242
offline_ds := NewDAGService(bs)
243