Skip to content

Commit 7962903

Browse files
Merge pull request #3290 from ipfs/feat/el-cid-2
The conquest of El Cid, Pt. 2
2 parents 391b78a + 282bdc4 commit 7962903

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+544
-535
lines changed

blocks/blocks.go

+14-19
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"errors"
77
"fmt"
88

9-
key "gx/ipfs/QmYEoKZXHoAToWfhGF3vryhMn3WWhE1o2MasQ8uzY5iDi9/go-key"
10-
119
mh "gx/ipfs/QmYDds3421prZgqKbLpEK7T9Aa2eVdQ7o3YarX1LVLdP2J/go-multihash"
1210
cid "gx/ipfs/QmakyCk6Vnn16WEKjbkxieZmM2YLTzkFWizbmGowoYPjro/go-cid"
1311
u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
@@ -18,58 +16,55 @@ var ErrWrongHash = errors.New("data did not match given hash!")
1816
type Block interface {
1917
Multihash() mh.Multihash
2018
RawData() []byte
21-
Key() key.Key
19+
Cid() *cid.Cid
2220
String() string
2321
Loggable() map[string]interface{}
2422
}
2523

2624
// Block is a singular block of data in ipfs
2725
type BasicBlock struct {
28-
multihash mh.Multihash
29-
data []byte
26+
cid *cid.Cid
27+
data []byte
3028
}
3129

3230
// NewBlock creates a Block object from opaque data. It will hash the data.
3331
func NewBlock(data []byte) *BasicBlock {
34-
return &BasicBlock{data: data, multihash: u.Hash(data)}
32+
// TODO: fix assumptions
33+
return &BasicBlock{data: data, cid: cid.NewCidV0(u.Hash(data))}
3534
}
3635

3736
// NewBlockWithHash creates a new block when the hash of the data
3837
// is already known, this is used to save time in situations where
3938
// we are able to be confident that the data is correct
40-
func NewBlockWithHash(data []byte, h mh.Multihash) (*BasicBlock, error) {
39+
func NewBlockWithCid(data []byte, c *cid.Cid) (*BasicBlock, error) {
4140
if u.Debug {
42-
chk := u.Hash(data)
43-
if string(chk) != string(h) {
41+
// TODO: fix assumptions
42+
chkc := cid.NewCidV0(u.Hash(data))
43+
if !chkc.Equals(c) {
4444
return nil, ErrWrongHash
4545
}
4646
}
47-
return &BasicBlock{data: data, multihash: h}, nil
47+
return &BasicBlock{data: data, cid: c}, nil
4848
}
4949

5050
func (b *BasicBlock) Multihash() mh.Multihash {
51-
return b.multihash
51+
return b.cid.Hash()
5252
}
5353

5454
func (b *BasicBlock) RawData() []byte {
5555
return b.data
5656
}
5757

5858
func (b *BasicBlock) Cid() *cid.Cid {
59-
return cid.NewCidV0(b.multihash)
60-
}
61-
62-
// Key returns the block's Multihash as a Key value.
63-
func (b *BasicBlock) Key() key.Key {
64-
return key.Key(b.multihash)
59+
return b.cid
6560
}
6661

6762
func (b *BasicBlock) String() string {
68-
return fmt.Sprintf("[Block %s]", b.Key())
63+
return fmt.Sprintf("[Block %s]", b.Cid())
6964
}
7065

7166
func (b *BasicBlock) Loggable() map[string]interface{} {
7267
return map[string]interface{}{
73-
"block": b.Key().String(),
68+
"block": b.Cid().String(),
7469
}
7570
}

blocks/blocks_test.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
mh "gx/ipfs/QmYDds3421prZgqKbLpEK7T9Aa2eVdQ7o3YarX1LVLdP2J/go-multihash"
8+
cid "gx/ipfs/QmakyCk6Vnn16WEKjbkxieZmM2YLTzkFWizbmGowoYPjro/go-cid"
89
u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
910
)
1011

@@ -44,12 +45,12 @@ func TestHash(t *testing.T) {
4445
}
4546
}
4647

47-
func TestKey(t *testing.T) {
48+
func TestCid(t *testing.T) {
4849
data := []byte("yet another data")
4950
block := NewBlock(data)
50-
key := block.Key()
51+
c := block.Cid()
5152

52-
if !bytes.Equal(block.Multihash(), key.ToMultihash()) {
53+
if !bytes.Equal(block.Multihash(), c.Hash()) {
5354
t.Error("key contains wrong data")
5455
}
5556
}
@@ -66,8 +67,10 @@ func TestManualHash(t *testing.T) {
6667
t.Fatal(err)
6768
}
6869

70+
c := cid.NewCidV0(hash)
71+
6972
u.Debug = false
70-
block, err := NewBlockWithHash(data, hash)
73+
block, err := NewBlockWithCid(data, c)
7174
if err != nil {
7275
t.Fatal(err)
7376
}
@@ -77,7 +80,7 @@ func TestManualHash(t *testing.T) {
7780
}
7881

7982
data[5] = byte((uint32(data[5]) + 5) % 256) // Transfrom hash to be different
80-
block, err = NewBlockWithHash(data, hash)
83+
block, err = NewBlockWithCid(data, c)
8184
if err != nil {
8285
t.Fatal(err)
8386
}
@@ -88,7 +91,7 @@ func TestManualHash(t *testing.T) {
8891

8992
u.Debug = true
9093

91-
block, err = NewBlockWithHash(data, hash)
94+
block, err = NewBlockWithCid(data, c)
9295
if err != ErrWrongHash {
9396
t.Fatal(err)
9497
}

blocks/blockstore/arc_cache.go

+27-17
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package blockstore
22

33
import (
4-
key "gx/ipfs/QmYEoKZXHoAToWfhGF3vryhMn3WWhE1o2MasQ8uzY5iDi9/go-key"
4+
"context"
55

66
"github.com/ipfs/go-ipfs/blocks"
77

8-
context "context"
98
"gx/ipfs/QmRg1gKTHzc3CZXSKzem8aR4E3TubFhbgXwfVuWnSK5CC5/go-metrics-interface"
109
lru "gx/ipfs/QmVYxfoJQiZijTgPNHCHgHELvQpbsJNTg6Crmc3dQkj3yy/golang-lru"
10+
cid "gx/ipfs/QmakyCk6Vnn16WEKjbkxieZmM2YLTzkFWizbmGowoYPjro/go-cid"
1111
ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore"
1212
)
1313

@@ -31,7 +31,7 @@ func newARCCachedBS(ctx context.Context, bs Blockstore, lruSize int) (*arccache,
3131
return c, nil
3232
}
3333

34-
func (b *arccache) DeleteBlock(k key.Key) error {
34+
func (b *arccache) DeleteBlock(k *cid.Cid) error {
3535
if has, ok := b.hasCached(k); ok && !has {
3636
return ErrNotFound
3737
}
@@ -40,7 +40,7 @@ func (b *arccache) DeleteBlock(k key.Key) error {
4040
err := b.blockstore.DeleteBlock(k)
4141
switch err {
4242
case nil, ds.ErrNotFound, ErrNotFound:
43-
b.arc.Add(k, false)
43+
b.addCache(k, false)
4444
return err
4545
default:
4646
return err
@@ -49,56 +49,62 @@ func (b *arccache) DeleteBlock(k key.Key) error {
4949

5050
// if ok == false has is inconclusive
5151
// if ok == true then has respons to question: is it contained
52-
func (b *arccache) hasCached(k key.Key) (has bool, ok bool) {
52+
func (b *arccache) hasCached(k *cid.Cid) (has bool, ok bool) {
5353
b.total.Inc()
54-
if k == "" {
54+
if k == nil {
55+
log.Error("nil cid in arccache")
5556
// Return cache invalid so the call to blockstore happens
5657
// in case of invalid key and correct error is created.
5758
return false, false
5859
}
5960

60-
h, ok := b.arc.Get(k)
61+
h, ok := b.arc.Get(k.KeyString())
6162
if ok {
6263
b.hits.Inc()
6364
return h.(bool), true
6465
}
6566
return false, false
6667
}
6768

68-
func (b *arccache) Has(k key.Key) (bool, error) {
69+
func (b *arccache) Has(k *cid.Cid) (bool, error) {
6970
if has, ok := b.hasCached(k); ok {
7071
return has, nil
7172
}
7273

7374
res, err := b.blockstore.Has(k)
7475
if err == nil {
75-
b.arc.Add(k, res)
76+
b.addCache(k, res)
7677
}
7778
return res, err
7879
}
7980

80-
func (b *arccache) Get(k key.Key) (blocks.Block, error) {
81+
func (b *arccache) Get(k *cid.Cid) (blocks.Block, error) {
82+
if k == nil {
83+
log.Error("nil cid in arc cache")
84+
return nil, ErrNotFound
85+
}
86+
8187
if has, ok := b.hasCached(k); ok && !has {
8288
return nil, ErrNotFound
8389
}
8490

8591
bl, err := b.blockstore.Get(k)
8692
if bl == nil && err == ErrNotFound {
87-
b.arc.Add(k, false)
93+
b.addCache(k, false)
8894
} else if bl != nil {
89-
b.arc.Add(k, true)
95+
b.addCache(k, true)
9096
}
9197
return bl, err
9298
}
9399

94100
func (b *arccache) Put(bl blocks.Block) error {
95-
if has, ok := b.hasCached(bl.Key()); ok && has {
101+
if has, ok := b.hasCached(bl.Cid()); ok && has {
96102
return nil
97103
}
98104

99105
err := b.blockstore.Put(bl)
100106
if err == nil {
101-
b.arc.Add(bl.Key(), true)
107+
b.addCache(bl.Cid(), true)
102108
}
103109
return err
104110
}
@@ -108,7 +114,7 @@ func (b *arccache) PutMany(bs []blocks.Block) error {
108114
for _, block := range bs {
109115
// call put on block if result is inconclusive or we are sure that
110116
// the block isn't in storage
111-
if has, ok := b.hasCached(block.Key()); !ok || (ok && !has) {
117+
if has, ok := b.hasCached(block.Cid()); !ok || (ok && !has) {
112118
good = append(good, block)
113119
}
114120
}
@@ -117,12 +123,16 @@ func (b *arccache) PutMany(bs []blocks.Block) error {
117123
return err
118124
}
119125
for _, block := range good {
120-
b.arc.Add(block.Key(), true)
126+
b.addCache(block.Cid(), true)
121127
}
122128
return nil
123129
}
124130

125-
func (b *arccache) AllKeysChan(ctx context.Context) (<-chan key.Key, error) {
131+
func (b *arccache) addCache(c *cid.Cid, has bool) {
132+
b.arc.Add(c.KeyString(), has)
133+
}
134+
135+
func (b *arccache) AllKeysChan(ctx context.Context) (<-chan *cid.Cid, error) {
126136
return b.blockstore.AllKeysChan(ctx)
127137
}
128138

0 commit comments

Comments
 (0)