Skip to content

Commit 63d326c

Browse files
committed
Make Golint happy in the blocks submodule.
This has required changing the order of some parameters and adding HashOnRead to the Blockstore interface (which I have in turn added to all the wrapper implementations). License: MIT Signed-off-by: Hector Sanjuan <[email protected]>
1 parent ff997c1 commit 63d326c

File tree

15 files changed

+147
-54
lines changed

15 files changed

+147
-54
lines changed

blocks/blocks.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
// package blocks contains the lowest level of IPFS data structures,
2-
// the raw block with a checksum.
1+
// Package blocks contains the lowest level of IPFS data structures.
2+
// A block is raw data accompanied by a CID. The CID contains the multihash
3+
// corresponding to the block.
34
package blocks
45

56
import (
@@ -11,16 +12,20 @@ import (
1112
mh "gx/ipfs/QmbZ6Cee2uHjG7hf19qLHppgKDRtaG4CVtMzdmK9VCVqLu/go-multihash"
1213
)
1314

14-
var ErrWrongHash = errors.New("data did not match given hash!")
15+
// ErrWrongHash is returned when the Cid of a block is not the expected
16+
// according to the contents. It is currently used only when debugging.
17+
var ErrWrongHash = errors.New("data did not match given hash")
1518

19+
// Block provides abstraction for blocks implementations.
1620
type Block interface {
1721
RawData() []byte
1822
Cid() *cid.Cid
1923
String() string
2024
Loggable() map[string]interface{}
2125
}
2226

23-
// Block is a singular block of data in ipfs
27+
// A BasicBlock is a singular block of data in ipfs. It implements the Block
28+
// interface.
2429
type BasicBlock struct {
2530
cid *cid.Cid
2631
data []byte
@@ -32,9 +37,9 @@ func NewBlock(data []byte) *BasicBlock {
3237
return &BasicBlock{data: data, cid: cid.NewCidV0(u.Hash(data))}
3338
}
3439

35-
// NewBlockWithHash creates a new block when the hash of the data
40+
// NewBlockWithCid creates a new block when the hash of the data
3641
// is already known, this is used to save time in situations where
37-
// we are able to be confident that the data is correct
42+
// we are able to be confident that the data is correct.
3843
func NewBlockWithCid(data []byte, c *cid.Cid) (*BasicBlock, error) {
3944
if u.Debug {
4045
chkc, err := c.Prefix().Sum(data)
@@ -49,22 +54,27 @@ func NewBlockWithCid(data []byte, c *cid.Cid) (*BasicBlock, error) {
4954
return &BasicBlock{data: data, cid: c}, nil
5055
}
5156

57+
// Multihash returns the hash contained in the block CID.
5258
func (b *BasicBlock) Multihash() mh.Multihash {
5359
return b.cid.Hash()
5460
}
5561

62+
// RawData returns the block raw contents as a byte slice.
5663
func (b *BasicBlock) RawData() []byte {
5764
return b.data
5865
}
5966

67+
// Cid returns the content identifier of the block.
6068
func (b *BasicBlock) Cid() *cid.Cid {
6169
return b.cid
6270
}
6371

72+
// String provides a human-readable representation of the block CID.
6473
func (b *BasicBlock) String() string {
6574
return fmt.Sprintf("[Block %s]", b.Cid())
6675
}
6776

77+
// Loggable returns a go-log loggable item.
6878
func (b *BasicBlock) Loggable() map[string]interface{} {
6979
return map[string]interface{}{
7080
"block": b.Cid().String(),

blocks/blockstore/arc_cache.go

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import (
1111
lru "gx/ipfs/QmVYxfoJQiZijTgPNHCHgHELvQpbsJNTg6Crmc3dQkj3yy/golang-lru"
1212
)
1313

14+
// arccache wraps a BlockStore with an Adaptive Replacement Cache (ARC) for
15+
// block Cids. This provides block access-time improvements, allowing
16+
// to short-cut many searches without query-ing the underlying datastore.
1417
type arccache struct {
1518
arc *lru.ARCCache
1619
blockstore Blockstore
@@ -128,6 +131,10 @@ func (b *arccache) PutMany(bs []blocks.Block) error {
128131
return nil
129132
}
130133

134+
func (b *arccache) HashOnRead(enabled bool) {
135+
b.blockstore.HashOnRead(enabled)
136+
}
137+
131138
func (b *arccache) addCache(c *cid.Cid, has bool) {
132139
b.arc.Add(c.KeyString(), has)
133140
}

blocks/blockstore/arc_cache_test.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,24 @@ import (
1313

1414
var exampleBlock = blocks.NewBlock([]byte("foo"))
1515

16-
func testArcCached(bs Blockstore, ctx context.Context) (*arccache, error) {
16+
func testArcCached(ctx context.Context, bs Blockstore) (*arccache, error) {
1717
if ctx == nil {
1818
ctx = context.TODO()
1919
}
2020
opts := DefaultCacheOpts()
2121
opts.HasBloomFilterSize = 0
2222
opts.HasBloomFilterHashes = 0
23-
bbs, err := CachedBlockstore(bs, ctx, opts)
23+
bbs, err := CachedBlockstore(ctx, bs, opts)
2424
if err == nil {
2525
return bbs.(*arccache), nil
26-
} else {
27-
return nil, err
2826
}
27+
return nil, err
2928
}
3029

31-
func createStores(t *testing.T) (*arccache, *blockstore, *callbackDatastore) {
30+
func createStores(t *testing.T) (*arccache, Blockstore, *callbackDatastore) {
3231
cd := &callbackDatastore{f: func() {}, ds: ds.NewMapDatastore()}
3332
bs := NewBlockstore(syncds.MutexWrap(cd))
34-
arc, err := testArcCached(bs, nil)
33+
arc, err := testArcCached(nil, bs)
3534
if err != nil {
3635
t.Fatal(err)
3736
}

blocks/blockstore/blockstore.go

+35-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// package blockstore implements a thin wrapper over a datastore, giving a
1+
// Package blockstore implements a thin wrapper over a datastore, giving a
22
// clean interface for Getting and Putting block objects.
33
package blockstore
44

@@ -23,22 +23,36 @@ var log = logging.Logger("blockstore")
2323
// BlockPrefix namespaces blockstore datastores
2424
var BlockPrefix = ds.NewKey("blocks")
2525

26-
var ValueTypeMismatch = errors.New("the retrieved value is not a Block")
26+
// ErrValueTypeMismatch is an error returned when the item retrieved from
27+
// the datatstore is not a block.
28+
var ErrValueTypeMismatch = errors.New("the retrieved value is not a Block")
29+
30+
// ErrHashMismatch is an error returned when the hash of a block
31+
// is different than expected.
2732
var ErrHashMismatch = errors.New("block in storage has different hash than requested")
2833

34+
// ErrNotFound is an error returned when a block is not found.
2935
var ErrNotFound = errors.New("blockstore: block not found")
3036

31-
// Blockstore wraps a Datastore
37+
// Blockstore wraps a Datastore block-centered methods and provides a layer
38+
// of abstraction which allows to add different caching strategies.
3239
type Blockstore interface {
3340
DeleteBlock(*cid.Cid) error
3441
Has(*cid.Cid) (bool, error)
3542
Get(*cid.Cid) (blocks.Block, error)
3643
Put(blocks.Block) error
3744
PutMany([]blocks.Block) error
38-
45+
// AllKeysChan returns a channel from which
46+
// the CIDs in the Blockstore can be read. It should respect
47+
// the given context, closing the channel if it becomes Done.
3948
AllKeysChan(ctx context.Context) (<-chan *cid.Cid, error)
49+
// HashOnRead specifies if every read block should be
50+
// rehashed to make sure it matches its CID.
51+
HashOnRead(enabled bool)
4052
}
4153

54+
// GCLocker abstract functionality to lock a blockstore when performing
55+
// garbage-collection operations.
4256
type GCLocker interface {
4357
// GCLock locks the blockstore for garbage collection. No operations
4458
// that expect to finish with a pin should ocurr simultaneously.
@@ -56,11 +70,15 @@ type GCLocker interface {
5670
GCRequested() bool
5771
}
5872

73+
// GCBlockstore is a blockstore that can safely run garbage-collection
74+
// operations.
5975
type GCBlockstore interface {
6076
Blockstore
6177
GCLocker
6278
}
6379

80+
// NewGCBlockstore returns a default implementation of GCBlockstore
81+
// using the given Blockstore and GCLocker.
6482
func NewGCBlockstore(bs Blockstore, gcl GCLocker) GCBlockstore {
6583
return gcBlockstore{bs, gcl}
6684
}
@@ -70,7 +88,9 @@ type gcBlockstore struct {
7088
GCLocker
7189
}
7290

73-
func NewBlockstore(d ds.Batching) *blockstore {
91+
// NewBlockstore returns a default Blockstore implementation
92+
// using the provided datastore.Batching backend.
93+
func NewBlockstore(d ds.Batching) Blockstore {
7494
var dsb ds.Batching
7595
dd := dsns.Wrap(d, BlockPrefix)
7696
dsb = dd
@@ -108,7 +128,7 @@ func (bs *blockstore) Get(k *cid.Cid) (blocks.Block, error) {
108128
}
109129
bdata, ok := maybeData.([]byte)
110130
if !ok {
111-
return nil, ValueTypeMismatch
131+
return nil, ErrValueTypeMismatch
112132
}
113133

114134
if bs.rehash {
@@ -122,9 +142,8 @@ func (bs *blockstore) Get(k *cid.Cid) (blocks.Block, error) {
122142
}
123143

124144
return blocks.NewBlockWithCid(bdata, rbcid)
125-
} else {
126-
return blocks.NewBlockWithCid(bdata, k)
127145
}
146+
return blocks.NewBlockWithCid(bdata, k)
128147
}
129148

130149
func (bs *blockstore) Put(block blocks.Block) error {
@@ -162,8 +181,8 @@ func (bs *blockstore) Has(k *cid.Cid) (bool, error) {
162181
return bs.datastore.Has(dshelp.CidToDsKey(k))
163182
}
164183

165-
func (s *blockstore) DeleteBlock(k *cid.Cid) error {
166-
err := s.datastore.Delete(dshelp.CidToDsKey(k))
184+
func (bs *blockstore) DeleteBlock(k *cid.Cid) error {
185+
err := bs.datastore.Delete(dshelp.CidToDsKey(k))
167186
if err == ds.ErrNotFound {
168187
return ErrNotFound
169188
}
@@ -173,7 +192,7 @@ func (s *blockstore) DeleteBlock(k *cid.Cid) error {
173192
// AllKeysChan runs a query for keys from the blockstore.
174193
// this is very simplistic, in the future, take dsq.Query as a param?
175194
//
176-
// AllKeysChan respects context
195+
// AllKeysChan respects context.
177196
func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan *cid.Cid, error) {
178197

179198
// KeysOnly, because that would be _a lot_ of data.
@@ -220,7 +239,9 @@ func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan *cid.Cid, error)
220239
return output, nil
221240
}
222241

223-
func NewGCLocker() *gclocker {
242+
// NewGCLocker returns a default implementation of
243+
// GCLocker using standard [RW] mutexes.
244+
func NewGCLocker() GCLocker {
224245
return &gclocker{}
225246
}
226247

@@ -230,6 +251,8 @@ type gclocker struct {
230251
gcreqlk sync.Mutex
231252
}
232253

254+
// Unlocker represents an object which can Unlock
255+
// something.
233256
type Unlocker interface {
234257
Unlock()
235258
}

blocks/blockstore/blockstore_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func TestAllKeysRespectsContext(t *testing.T) {
186186

187187
}
188188

189-
func TestValueTypeMismatch(t *testing.T) {
189+
func TestErrValueTypeMismatch(t *testing.T) {
190190
block := blocks.NewBlock([]byte("some data"))
191191

192192
datastore := ds.NewMapDatastore()
@@ -196,7 +196,7 @@ func TestValueTypeMismatch(t *testing.T) {
196196
blockstore := NewBlockstore(ds_sync.MutexWrap(datastore))
197197

198198
_, err := blockstore.Get(block.Cid())
199-
if err != ValueTypeMismatch {
199+
if err != ErrValueTypeMismatch {
200200
t.Fatal(err)
201201
}
202202
}

blocks/blockstore/bloom_cache.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ import (
1212
bloom "gx/ipfs/QmeiMCBkYHxkDkDfnDadzz4YxY5ruL5Pj499essE4vRsGM/bbloom"
1313
)
1414

15-
// bloomCached returns Blockstore that caches Has requests using Bloom filter
16-
// Size is size of bloom filter in bytes
17-
func bloomCached(bs Blockstore, ctx context.Context, bloomSize, hashCount int) (*bloomcache, error) {
15+
// bloomCached returns a Blockstore that caches Has requests using a Bloom
16+
// filter. bloomSize is size of bloom filter in bytes. hashCount specifies the
17+
// number of hashing functions in the bloom filter (usually known as k).
18+
func bloomCached(ctx context.Context, bs Blockstore, bloomSize, hashCount int) (*bloomcache, error) {
1819
bl, err := bloom.New(float64(bloomSize), float64(hashCount))
1920
if err != nil {
2021
return nil, err
@@ -165,6 +166,10 @@ func (b *bloomcache) PutMany(bs []blocks.Block) error {
165166
return nil
166167
}
167168

169+
func (b *bloomcache) HashOnRead(enabled bool) {
170+
b.blockstore.HashOnRead(enabled)
171+
}
172+
168173
func (b *bloomcache) AllKeysChan(ctx context.Context) (<-chan *cid.Cid, error) {
169174
return b.blockstore.AllKeysChan(ctx)
170175
}

blocks/blockstore/bloom_cache_test.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@ import (
1414
syncds "gx/ipfs/QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364/go-datastore/sync"
1515
)
1616

17-
func testBloomCached(bs Blockstore, ctx context.Context) (*bloomcache, error) {
17+
func testBloomCached(ctx context.Context, bs Blockstore) (*bloomcache, error) {
1818
if ctx == nil {
1919
ctx = context.TODO()
2020
}
2121
opts := DefaultCacheOpts()
2222
opts.HasARCCacheSize = 0
23-
bbs, err := CachedBlockstore(bs, ctx, opts)
23+
bbs, err := CachedBlockstore(ctx, bs, opts)
2424
if err == nil {
2525
return bbs.(*bloomcache), nil
26-
} else {
27-
return nil, err
2826
}
27+
return nil, err
2928
}
3029

3130
func TestPutManyAddsToBloom(t *testing.T) {
@@ -34,7 +33,7 @@ func TestPutManyAddsToBloom(t *testing.T) {
3433
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
3534
defer cancel()
3635

37-
cachedbs, err := testBloomCached(bs, ctx)
36+
cachedbs, err := testBloomCached(ctx, bs)
3837

3938
select {
4039
case <-cachedbs.rebuildChan:
@@ -65,7 +64,7 @@ func TestPutManyAddsToBloom(t *testing.T) {
6564

6665
func TestReturnsErrorWhenSizeNegative(t *testing.T) {
6766
bs := NewBlockstore(syncds.MutexWrap(ds.NewMapDatastore()))
68-
_, err := bloomCached(bs, context.TODO(), -1, 1)
67+
_, err := bloomCached(context.TODO(), bs, -1, 1)
6968
if err == nil {
7069
t.Fail()
7170
}
@@ -80,7 +79,7 @@ func TestHasIsBloomCached(t *testing.T) {
8079
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
8180
defer cancel()
8281

83-
cachedbs, err := testBloomCached(bs, ctx)
82+
cachedbs, err := testBloomCached(ctx, bs)
8483
if err != nil {
8584
t.Fatal(err)
8685
}

blocks/blockstore/caching.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import (
77
"gx/ipfs/QmRg1gKTHzc3CZXSKzem8aR4E3TubFhbgXwfVuWnSK5CC5/go-metrics-interface"
88
)
99

10+
// CacheOpts wraps options for CachedBlockStore().
1011
// Next to each option is it aproximate memory usage per unit
1112
type CacheOpts struct {
1213
HasBloomFilterSize int // 1 byte
1314
HasBloomFilterHashes int // No size, 7 is usually best, consult bloom papers
1415
HasARCCacheSize int // 32 bytes
1516
}
1617

18+
// DefaultCacheOpts returns a CacheOpts initialized with default values.
1719
func DefaultCacheOpts() CacheOpts {
1820
return CacheOpts{
1921
HasBloomFilterSize: 512 << 10,
@@ -22,8 +24,12 @@ func DefaultCacheOpts() CacheOpts {
2224
}
2325
}
2426

25-
func CachedBlockstore(bs Blockstore,
26-
ctx context.Context, opts CacheOpts) (cbs Blockstore, err error) {
27+
// CachedBlockstore returns a blockstore wrapped in an ARCCache and
28+
// then in a bloom filter cache, if the options indicate it.
29+
func CachedBlockstore(
30+
ctx context.Context,
31+
bs Blockstore,
32+
opts CacheOpts) (cbs Blockstore, err error) {
2733
cbs = bs
2834

2935
if opts.HasBloomFilterSize < 0 || opts.HasBloomFilterHashes < 0 ||
@@ -42,7 +48,7 @@ func CachedBlockstore(bs Blockstore,
4248
}
4349
if opts.HasBloomFilterSize != 0 {
4450
// *8 because of bytes to bits conversion
45-
cbs, err = bloomCached(cbs, ctx, opts.HasBloomFilterSize*8, opts.HasBloomFilterHashes)
51+
cbs, err = bloomCached(ctx, cbs, opts.HasBloomFilterSize*8, opts.HasBloomFilterHashes)
4652
}
4753

4854
return cbs, err

0 commit comments

Comments
 (0)