Skip to content
This repository was archived by the owner on Jun 19, 2023. It is now read-only.

Commit b598a29

Browse files
hsanjuanJorropo
authored andcommitted
Use ipld.ErrNotFound for NotFound errors
1 parent ac66423 commit b598a29

8 files changed

+40
-24
lines changed

arc_cache.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
lru "github.com/hashicorp/golang-lru"
99
blocks "github.com/ipfs/go-block-format"
1010
cid "github.com/ipfs/go-cid"
11+
ipld "github.com/ipfs/go-ipld-format"
1112
metrics "github.com/ipfs/go-metrics-interface"
1213
)
1314

@@ -135,15 +136,15 @@ func (b *arccache) Has(ctx context.Context, k cid.Cid) (bool, error) {
135136

136137
func (b *arccache) GetSize(ctx context.Context, k cid.Cid) (int, error) {
137138
if !k.Defined() {
138-
return -1, ErrNotFound
139+
return -1, ipld.ErrNotFound{Cid: k}
139140
}
140141

141142
key := cacheKey(k)
142143

143144
if has, blockSize, ok := b.queryCache(key); ok {
144145
if !has {
145146
// don't have it, return
146-
return -1, ErrNotFound
147+
return -1, ipld.ErrNotFound{Cid: k}
147148
}
148149
if blockSize >= 0 {
149150
// have it and we know the size
@@ -156,7 +157,7 @@ func (b *arccache) GetSize(ctx context.Context, k cid.Cid) (int, error) {
156157
defer b.unlock(key, false)
157158

158159
blockSize, err := b.blockstore.GetSize(ctx, k)
159-
if err == ErrNotFound {
160+
if ipld.IsNotFound(err) {
160161
b.cacheHave(key, false)
161162
} else if err == nil {
162163
b.cacheSize(key, blockSize)
@@ -176,15 +177,15 @@ func (b *arccache) View(ctx context.Context, k cid.Cid, callback func([]byte) er
176177
}
177178

178179
if !k.Defined() {
179-
return ErrNotFound
180+
return ipld.ErrNotFound{Cid: k}
180181
}
181182

182183
key := cacheKey(k)
183184

184185
if has, _, ok := b.queryCache(key); ok && !has {
185186
// short circuit if the cache deterministically tells us the item
186187
// doesn't exist.
187-
return ErrNotFound
188+
return ipld.ErrNotFound{Cid: k}
188189
}
189190

190191
b.lock(key, false)
@@ -197,7 +198,7 @@ func (b *arccache) View(ctx context.Context, k cid.Cid, callback func([]byte) er
197198
cberr = callback(buf)
198199
return nil
199200
}); err != nil {
200-
if err == ErrNotFound {
201+
if ipld.IsNotFound(err) {
201202
b.cacheHave(key, false)
202203
}
203204
return err
@@ -210,20 +211,20 @@ func (b *arccache) View(ctx context.Context, k cid.Cid, callback func([]byte) er
210211

211212
func (b *arccache) Get(ctx context.Context, k cid.Cid) (blocks.Block, error) {
212213
if !k.Defined() {
213-
return nil, ErrNotFound
214+
return nil, ipld.ErrNotFound{Cid: k}
214215
}
215216

216217
key := cacheKey(k)
217218

218219
if has, _, ok := b.queryCache(key); ok && !has {
219-
return nil, ErrNotFound
220+
return nil, ipld.ErrNotFound{Cid: k}
220221
}
221222

222223
b.lock(key, false)
223224
defer b.unlock(key, false)
224225

225226
bl, err := b.blockstore.Get(ctx, k)
226-
if bl == nil && err == ErrNotFound {
227+
if bl == nil && ipld.IsNotFound(err) {
227228
b.cacheHave(key, false)
228229
} else if bl != nil {
229230
b.cacheSize(key, len(bl.RawData()))

arc_cache_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
cid "github.com/ipfs/go-cid"
1313
ds "github.com/ipfs/go-datastore"
1414
syncds "github.com/ipfs/go-datastore/sync"
15+
ipld "github.com/ipfs/go-ipld-format"
1516
)
1617

1718
var exampleBlock = blocks.NewBlock([]byte("foo"))
@@ -111,7 +112,7 @@ func TestGetFillsCache(t *testing.T) {
111112
if has, err := arc.Has(bg, exampleBlock.Cid()); has || err != nil {
112113
t.Fatal("has was true but there is no such block")
113114
}
114-
if _, err := arc.GetSize(bg, exampleBlock.Cid()); err != ErrNotFound {
115+
if _, err := arc.GetSize(bg, exampleBlock.Cid()); !ipld.IsNotFound(err) {
115116
t.Fatal("getsize was true but there is no such block")
116117
}
117118

@@ -139,7 +140,7 @@ func TestGetAndDeleteFalseShortCircuit(t *testing.T) {
139140

140141
trap("get hit datastore", cd, t)
141142

142-
if bl, err := arc.Get(bg, exampleBlock.Cid()); bl != nil || err != ErrNotFound {
143+
if bl, err := arc.Get(bg, exampleBlock.Cid()); bl != nil || !ipld.IsNotFound(err) {
143144
t.Fatal("get returned invalid result")
144145
}
145146

@@ -226,7 +227,7 @@ func TestGetSizeMissingZeroSizeBlock(t *testing.T) {
226227
arc.Get(bg, missingBlock.Cid())
227228

228229
trap("has hit datastore", cd, t)
229-
if _, err := arc.GetSize(bg, missingBlock.Cid()); err != ErrNotFound {
230+
if _, err := arc.GetSize(bg, missingBlock.Cid()); !ipld.IsNotFound(err) {
230231
t.Fatal("getsize returned invalid result")
231232
}
232233
}

blockstore.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
dsns "github.com/ipfs/go-datastore/namespace"
1515
dsq "github.com/ipfs/go-datastore/query"
1616
dshelp "github.com/ipfs/go-ipfs-ds-help"
17+
ipld "github.com/ipfs/go-ipld-format"
1718
logging "github.com/ipfs/go-log"
1819
uatomic "go.uber.org/atomic"
1920
)
@@ -27,9 +28,6 @@ var BlockPrefix = ds.NewKey("blocks")
2728
// is different than expected.
2829
var ErrHashMismatch = errors.New("block in storage has different hash than requested")
2930

30-
// ErrNotFound is an error returned when a block is not found.
31-
var ErrNotFound = errors.New("blockstore: block not found")
32-
3331
// Blockstore wraps a Datastore block-centered methods and provides a layer
3432
// of abstraction which allows to add different caching strategies.
3533
type Blockstore interface {
@@ -143,12 +141,12 @@ func (bs *blockstore) HashOnRead(enabled bool) {
143141

144142
func (bs *blockstore) Get(ctx context.Context, k cid.Cid) (blocks.Block, error) {
145143
if !k.Defined() {
146-
log.Error("undefined cid in blockstore")
147-
return nil, ErrNotFound
144+
logger.Error("undefined cid in blockstore")
145+
return nil, ipld.ErrNotFound{Cid: k}
148146
}
149147
bdata, err := bs.datastore.Get(ctx, dshelp.MultihashToDsKey(k.Hash()))
150148
if err == ds.ErrNotFound {
151-
return nil, ErrNotFound
149+
return nil, ipld.ErrNotFound{Cid: k}
152150
}
153151
if err != nil {
154152
return nil, err
@@ -206,7 +204,7 @@ func (bs *blockstore) Has(ctx context.Context, k cid.Cid) (bool, error) {
206204
func (bs *blockstore) GetSize(ctx context.Context, k cid.Cid) (int, error) {
207205
size, err := bs.datastore.GetSize(ctx, dshelp.MultihashToDsKey(k.Hash()))
208206
if err == ds.ErrNotFound {
209-
return -1, ErrNotFound
207+
return -1, ipld.ErrNotFound{Cid: k}
210208
}
211209
return size, err
212210
}

blockstore_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
dsq "github.com/ipfs/go-datastore/query"
1313
ds_sync "github.com/ipfs/go-datastore/sync"
1414
u "github.com/ipfs/go-ipfs-util"
15+
ipld "github.com/ipfs/go-ipld-format"
1516
)
1617

1718
func TestGetWhenKeyNotPresent(t *testing.T) {
@@ -30,7 +31,7 @@ func TestGetWhenKeyNotPresent(t *testing.T) {
3031
func TestGetWhenKeyIsNil(t *testing.T) {
3132
bs := NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore()))
3233
_, err := bs.Get(bg, cid.Cid{})
33-
if err != ErrNotFound {
34+
if !ipld.IsNotFound(err) {
3435
t.Fail()
3536
}
3637
}

bloom_cache.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
bloom "github.com/ipfs/bbloom"
1010
blocks "github.com/ipfs/go-block-format"
1111
cid "github.com/ipfs/go-cid"
12+
ipld "github.com/ipfs/go-ipld-format"
1213
metrics "github.com/ipfs/go-metrics-interface"
1314
)
1415

@@ -156,7 +157,7 @@ func (b *bloomcache) Has(ctx context.Context, k cid.Cid) (bool, error) {
156157

157158
func (b *bloomcache) GetSize(ctx context.Context, k cid.Cid) (int, error) {
158159
if has, ok := b.hasCached(k); ok && !has {
159-
return -1, ErrNotFound
160+
return -1, ipld.ErrNotFound{Cid: k}
160161
}
161162

162163
return b.blockstore.GetSize(ctx, k)
@@ -172,14 +173,14 @@ func (b *bloomcache) View(ctx context.Context, k cid.Cid, callback func([]byte)
172173
}
173174

174175
if has, ok := b.hasCached(k); ok && !has {
175-
return ErrNotFound
176+
return ipld.ErrNotFound{Cid: k}
176177
}
177178
return b.viewer.View(ctx, k, callback)
178179
}
179180

180181
func (b *bloomcache) Get(ctx context.Context, k cid.Cid) (blocks.Block, error) {
181182
if has, ok := b.hasCached(k); ok && !has {
182-
return nil, ErrNotFound
183+
return nil, ipld.ErrNotFound{Cid: k}
183184
}
184185

185186
return b.blockstore.Get(ctx, k)

bloom_cache_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
ds "github.com/ipfs/go-datastore"
1212
dsq "github.com/ipfs/go-datastore/query"
1313
syncds "github.com/ipfs/go-datastore/sync"
14+
ipld "github.com/ipfs/go-ipld-format"
1415
)
1516

1617
var bg = context.Background()
@@ -65,7 +66,7 @@ func TestPutManyAddsToBloom(t *testing.T) {
6566
t.Fatal(err)
6667
}
6768
blockSize, err = cachedbs.GetSize(bg, block2.Cid())
68-
if err != nil && err != ErrNotFound {
69+
if err != nil && !ipld.IsNotFound(err) {
6970
t.Fatal(err)
7071
}
7172
if blockSize > -1 || has {

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/ipfs/go-datastore v0.5.0
99
github.com/ipfs/go-ipfs-ds-help v1.1.0
1010
github.com/ipfs/go-ipfs-util v0.0.2
11+
github.com/ipfs/go-ipld-format v0.2.1-0.20220302134852-d02e0e18fc65
1112
github.com/ipfs/go-log v0.0.1
1213
github.com/ipfs/go-metrics-interface v0.0.1
1314
github.com/multiformats/go-multihash v0.0.14

go.sum

+12
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@ github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zV
77
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
88
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
99
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
10+
github.com/gxed/hashland/keccakpg v0.0.1/go.mod h1:kRzw3HkwxFU1mpmPP8v1WyQzwdGfmKFJ6tItnhQ67kU=
11+
github.com/gxed/hashland/murmur3 v0.0.1/go.mod h1:KjXop02n4/ckmZSnY2+HKcLud/tcmvhST0bie/0lS48=
1012
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
1113
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
1214
github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
1315
github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
16+
github.com/ipfs/go-block-format v0.0.2/go.mod h1:AWR46JfpcObNfg3ok2JHDUfdiHRgWhJgCQF+KIgOPJY=
1417
github.com/ipfs/go-block-format v0.0.3 h1:r8t66QstRp/pd/or4dpnbVfXT5Gt7lOqRvC+/dDTpMc=
1518
github.com/ipfs/go-block-format v0.0.3/go.mod h1:4LmD4ZUw0mhO+JSKdpWwrzATiEfM7WWgQ8H5l6P8MVk=
19+
github.com/ipfs/go-cid v0.0.1/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM=
20+
github.com/ipfs/go-cid v0.0.2/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM=
1621
github.com/ipfs/go-cid v0.0.5/go.mod h1:plgt+Y5MnOey4vO4UlUazGqdbEXuFYitED67FexhXog=
1722
github.com/ipfs/go-cid v0.0.7 h1:ysQJVJA3fNDF1qigJbsSQOdjhVLsOEoPdh0+R97k3jY=
1823
github.com/ipfs/go-cid v0.0.7/go.mod h1:6Ux9z5e+HpkQdckYoX1PG/6xqKspzlEIR5SDmgqgC/I=
@@ -23,8 +28,11 @@ github.com/ipfs/go-detect-race v0.0.1/go.mod h1:8BNT7shDZPo99Q74BpGMK+4D8Mn4j46U
2328
github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw=
2429
github.com/ipfs/go-ipfs-ds-help v1.1.0 h1:yLE2w9RAsl31LtfMt91tRZcrx+e61O5mDxFRR994w4Q=
2530
github.com/ipfs/go-ipfs-ds-help v1.1.0/go.mod h1:YR5+6EaebOhfcqVCyqemItCLthrpVNot+rsOU/5IatU=
31+
github.com/ipfs/go-ipfs-util v0.0.1/go.mod h1:spsl5z8KUnrve+73pOhSVZND1SIxPW5RyBCNzQxlJBc=
2632
github.com/ipfs/go-ipfs-util v0.0.2 h1:59Sswnk1MFaiq+VcaknX7aYEyGyGDAA73ilhEK2POp8=
2733
github.com/ipfs/go-ipfs-util v0.0.2/go.mod h1:CbPtkWJzjLdEcezDns2XYaehFVNXG9zrdrtMecczcsQ=
34+
github.com/ipfs/go-ipld-format v0.2.1-0.20220302134852-d02e0e18fc65 h1:xxnD+fUS7hziDAnfrn3qsl0ql18DOjq4rwvzBTCr1iA=
35+
github.com/ipfs/go-ipld-format v0.2.1-0.20220302134852-d02e0e18fc65/go.mod h1:co/SdBE8h99968X0hViiw1MNlh6fvxxnHpvVLnH7jSM=
2836
github.com/ipfs/go-log v0.0.1 h1:9XTUN/rW64BCG1YhPK9Hoy3q8nr4gOmHHBpgFdfw6Lc=
2937
github.com/ipfs/go-log v0.0.1/go.mod h1:kL1d2/hzSpI0thNYjiKfjanbVNU+IIGA/WnNESY9leM=
3038
github.com/ipfs/go-metrics-interface v0.0.1 h1:j+cpbjYvu4R8zbleSs36gvB7jR+wsL2fGD6n0jO4kdg=
@@ -46,6 +54,7 @@ github.com/mattn/go-isatty v0.0.5 h1:tHXDdz1cpzGaovsTB+TVB8q90WEokoVmfMqoVcrLUgw
4654
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
4755
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g=
4856
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ=
57+
github.com/minio/sha256-simd v0.0.0-20190131020904-2d45a736cd16/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U=
4958
github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771 h1:MHkK1uRtFbVqvAgvWxafZe54+5uBxLluGylDiKgdhwo=
5059
github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM=
5160
github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8=
@@ -58,6 +67,7 @@ github.com/multiformats/go-base36 v0.1.0/go.mod h1:kFGE83c6s80PklsHO9sRn2NCoffoR
5867
github.com/multiformats/go-multibase v0.0.1/go.mod h1:bja2MqRZ3ggyXtZSEDKpl0uO/gviWFaSteVbWT51qgs=
5968
github.com/multiformats/go-multibase v0.0.3 h1:l/B6bJDQjvQ5G52jw4QGSYeOTZoAwIO77RblWplfIqk=
6069
github.com/multiformats/go-multibase v0.0.3/go.mod h1:5+1R4eQrT3PkYZ24C3W2Ue2tPwIdYQD509ZjSb5y9Oc=
70+
github.com/multiformats/go-multihash v0.0.1/go.mod h1:w/5tugSrLEbWqlcgJabL3oHFKTwfvkofsjW2Qa1ct4U=
6171
github.com/multiformats/go-multihash v0.0.13/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc=
6272
github.com/multiformats/go-multihash v0.0.14 h1:QoBceQYQQtNUuf6s7wHxnE2c8bhbMqhfGzNI032se/I=
6373
github.com/multiformats/go-multihash v0.0.14/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc=
@@ -79,6 +89,7 @@ go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk=
7989
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
8090
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
8191
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
92+
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
8293
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
8394
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
8495
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8 h1:1wopBVtVdWnn03fZelqdXTqk7U7zPQCb+T4rbU9ZEoU=
@@ -93,6 +104,7 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwL
93104
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
94105
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
95106
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
107+
golang.org/x/sys v0.0.0-20190219092855-153ac476189d/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
96108
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
97109
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
98110
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

0 commit comments

Comments
 (0)