@cryptotaxi247 / kubo / commits / 1173c003f

Create a "write through" BlockService.

Create a block service where all writes are guaranteed to go though to the blockstore. License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>

Kevin Atkinson committed Oct 6, 2016 at 14:54 UTC 1173c003f5b4ce1e991e78d360635a135091c8e3
1 file changed +39 -22
blockservice/blockservice.go
+39 -22
@@ -37,6 +37,9 @@ type BlockService interface {
37 type blockService struct {
38 blockstore blockstore.Blockstore
39 exchange exchange.Interface
40 + // If checkFirst is true then first check that a block doesn't
41 + // already exist to avoid republishing the block on the exchange.
42 + checkFirst bool
43 }
44
45 // NewBlockService creates a BlockService with given datastore instance.
@@ -48,6 +51,21 @@ func New(bs blockstore.Blockstore, rem exchange.Interface) BlockService {
51 return &blockService{
52 blockstore: bs,
53 exchange: rem,
54 + checkFirst: true,
55 + }
56 +}
57 +
58 +// NewWriteThrough ceates a BlockService that guarantees writes will go
59 +// through to the blockstore and are not skipped by cache checks.
60 +func NewWriteThrough(bs blockstore.Blockstore, rem exchange.Interface) BlockService {
61 + if rem == nil {
62 + log.Warning("blockservice running in local (offline) mode.")
63 + }
64 +
65 + return &blockService{
66 + blockstore: bs,
67 + exchange: rem,
68 + checkFirst: false,
69 }
70 }
71
@@ -62,22 +80,19 @@ func (bs *blockService) Exchange() exchange.Interface {
80 // AddBlock adds a particular block to the service, Putting it into the datastore.
81 // TODO pass a context into this if the remote.HasBlock is going to remain here.
82 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.
83 c := o.Cid()
71 - has, err := s.blockstore.Has(c)
72 - if err != nil {
73 - return nil, err
74 - }
84 + if s.checkFirst {
85 + has, err := s.blockstore.Has(c)
86 + if err != nil {
87 + return nil, err
88 + }
89
76 - if has {
77 - return c, nil
90 + if has {
91 + return c, nil
92 + }
93 }
94
80 - err = s.blockstore.Put(o)
95 + err := s.blockstore.Put(o)
96 if err != nil {
97 return nil, err
98 }
@@ -91,17 +106,19 @@ func (s *blockService) AddBlock(o blocks.Block) (*cid.Cid, error) {
106
107 func (s *blockService) AddBlocks(bs []blocks.Block) ([]*cid.Cid, error) {
108 var toput []blocks.Block
94 - for _, b := range bs {
95 - has, err := s.blockstore.Has(b.Cid())
96 - if err != nil {
97 - return nil, err
98 - }
99 -
100 - if has {
101 - continue
109 + if s.checkFirst {
110 + for _, b := range bs {
111 + has, err := s.blockstore.Has(b.Cid())
112 + if err != nil {
113 + return nil, err
114 + }
115 + if has {
116 + continue
117 + }
118 + toput = append(toput, b)
119 }
103 -
104 - toput = append(toput, b)
120 + } else {
121 + toput = bs;
122 }
123
124 err := s.blockstore.PutMany(toput)