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

Commit

Permalink
Use ipld.ErrNotFound for NotFound errors
Browse files Browse the repository at this point in the history
  • Loading branch information
hsanjuan authored and Jorropo committed Mar 17, 2022
1 parent ac66423 commit b598a29
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 24 deletions.
19 changes: 10 additions & 9 deletions arc_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
lru "github.com/hashicorp/golang-lru"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
metrics "github.com/ipfs/go-metrics-interface"
)

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

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

key := cacheKey(k)

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

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

if !k.Defined() {
return ErrNotFound
return ipld.ErrNotFound{Cid: k}
}

key := cacheKey(k)

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

b.lock(key, false)
Expand All @@ -197,7 +198,7 @@ func (b *arccache) View(ctx context.Context, k cid.Cid, callback func([]byte) er
cberr = callback(buf)
return nil
}); err != nil {
if err == ErrNotFound {
if ipld.IsNotFound(err) {
b.cacheHave(key, false)
}
return err
Expand All @@ -210,20 +211,20 @@ func (b *arccache) View(ctx context.Context, k cid.Cid, callback func([]byte) er

func (b *arccache) Get(ctx context.Context, k cid.Cid) (blocks.Block, error) {
if !k.Defined() {
return nil, ErrNotFound
return nil, ipld.ErrNotFound{Cid: k}
}

key := cacheKey(k)

if has, _, ok := b.queryCache(key); ok && !has {
return nil, ErrNotFound
return nil, ipld.ErrNotFound{Cid: k}
}

b.lock(key, false)
defer b.unlock(key, false)

bl, err := b.blockstore.Get(ctx, k)
if bl == nil && err == ErrNotFound {
if bl == nil && ipld.IsNotFound(err) {
b.cacheHave(key, false)
} else if bl != nil {
b.cacheSize(key, len(bl.RawData()))
Expand Down
7 changes: 4 additions & 3 deletions arc_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
cid "github.com/ipfs/go-cid"
ds "github.com/ipfs/go-datastore"
syncds "github.com/ipfs/go-datastore/sync"
ipld "github.com/ipfs/go-ipld-format"
)

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

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

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

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

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

trap("has hit datastore", cd, t)
if _, err := arc.GetSize(bg, missingBlock.Cid()); err != ErrNotFound {
if _, err := arc.GetSize(bg, missingBlock.Cid()); !ipld.IsNotFound(err) {
t.Fatal("getsize returned invalid result")
}
}
Expand Down
12 changes: 5 additions & 7 deletions blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
dsns "github.com/ipfs/go-datastore/namespace"
dsq "github.com/ipfs/go-datastore/query"
dshelp "github.com/ipfs/go-ipfs-ds-help"
ipld "github.com/ipfs/go-ipld-format"
logging "github.com/ipfs/go-log"
uatomic "go.uber.org/atomic"
)
Expand All @@ -27,9 +28,6 @@ var BlockPrefix = ds.NewKey("blocks")
// is different than expected.
var ErrHashMismatch = errors.New("block in storage has different hash than requested")

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

// Blockstore wraps a Datastore block-centered methods and provides a layer
// of abstraction which allows to add different caching strategies.
type Blockstore interface {
Expand Down Expand Up @@ -143,12 +141,12 @@ func (bs *blockstore) HashOnRead(enabled bool) {

func (bs *blockstore) Get(ctx context.Context, k cid.Cid) (blocks.Block, error) {
if !k.Defined() {
log.Error("undefined cid in blockstore")
return nil, ErrNotFound
logger.Error("undefined cid in blockstore")
return nil, ipld.ErrNotFound{Cid: k}
}
bdata, err := bs.datastore.Get(ctx, dshelp.MultihashToDsKey(k.Hash()))
if err == ds.ErrNotFound {
return nil, ErrNotFound
return nil, ipld.ErrNotFound{Cid: k}
}
if err != nil {
return nil, err
Expand Down Expand Up @@ -206,7 +204,7 @@ func (bs *blockstore) Has(ctx context.Context, k cid.Cid) (bool, error) {
func (bs *blockstore) GetSize(ctx context.Context, k cid.Cid) (int, error) {
size, err := bs.datastore.GetSize(ctx, dshelp.MultihashToDsKey(k.Hash()))
if err == ds.ErrNotFound {
return -1, ErrNotFound
return -1, ipld.ErrNotFound{Cid: k}
}
return size, err
}
Expand Down
3 changes: 2 additions & 1 deletion blockstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
dsq "github.com/ipfs/go-datastore/query"
ds_sync "github.com/ipfs/go-datastore/sync"
u "github.com/ipfs/go-ipfs-util"
ipld "github.com/ipfs/go-ipld-format"
)

func TestGetWhenKeyNotPresent(t *testing.T) {
Expand All @@ -30,7 +31,7 @@ func TestGetWhenKeyNotPresent(t *testing.T) {
func TestGetWhenKeyIsNil(t *testing.T) {
bs := NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore()))
_, err := bs.Get(bg, cid.Cid{})
if err != ErrNotFound {
if !ipld.IsNotFound(err) {
t.Fail()
}
}
Expand Down
7 changes: 4 additions & 3 deletions bloom_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
bloom "github.com/ipfs/bbloom"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
metrics "github.com/ipfs/go-metrics-interface"
)

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

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

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

if has, ok := b.hasCached(k); ok && !has {
return ErrNotFound
return ipld.ErrNotFound{Cid: k}
}
return b.viewer.View(ctx, k, callback)
}

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

return b.blockstore.Get(ctx, k)
Expand Down
3 changes: 2 additions & 1 deletion bloom_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
ds "github.com/ipfs/go-datastore"
dsq "github.com/ipfs/go-datastore/query"
syncds "github.com/ipfs/go-datastore/sync"
ipld "github.com/ipfs/go-ipld-format"
)

var bg = context.Background()
Expand Down Expand Up @@ -65,7 +66,7 @@ func TestPutManyAddsToBloom(t *testing.T) {
t.Fatal(err)
}
blockSize, err = cachedbs.GetSize(bg, block2.Cid())
if err != nil && err != ErrNotFound {
if err != nil && !ipld.IsNotFound(err) {
t.Fatal(err)
}
if blockSize > -1 || has {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/ipfs/go-datastore v0.5.0
github.com/ipfs/go-ipfs-ds-help v1.1.0
github.com/ipfs/go-ipfs-util v0.0.2
github.com/ipfs/go-ipld-format v0.2.1-0.20220302134852-d02e0e18fc65
github.com/ipfs/go-log v0.0.1
github.com/ipfs/go-metrics-interface v0.0.1
github.com/multiformats/go-multihash v0.0.14
Expand Down
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zV
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gxed/hashland/keccakpg v0.0.1/go.mod h1:kRzw3HkwxFU1mpmPP8v1WyQzwdGfmKFJ6tItnhQ67kU=
github.com/gxed/hashland/murmur3 v0.0.1/go.mod h1:KjXop02n4/ckmZSnY2+HKcLud/tcmvhST0bie/0lS48=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
github.com/ipfs/go-block-format v0.0.2/go.mod h1:AWR46JfpcObNfg3ok2JHDUfdiHRgWhJgCQF+KIgOPJY=
github.com/ipfs/go-block-format v0.0.3 h1:r8t66QstRp/pd/or4dpnbVfXT5Gt7lOqRvC+/dDTpMc=
github.com/ipfs/go-block-format v0.0.3/go.mod h1:4LmD4ZUw0mhO+JSKdpWwrzATiEfM7WWgQ8H5l6P8MVk=
github.com/ipfs/go-cid v0.0.1/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM=
github.com/ipfs/go-cid v0.0.2/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM=
github.com/ipfs/go-cid v0.0.5/go.mod h1:plgt+Y5MnOey4vO4UlUazGqdbEXuFYitED67FexhXog=
github.com/ipfs/go-cid v0.0.7 h1:ysQJVJA3fNDF1qigJbsSQOdjhVLsOEoPdh0+R97k3jY=
github.com/ipfs/go-cid v0.0.7/go.mod h1:6Ux9z5e+HpkQdckYoX1PG/6xqKspzlEIR5SDmgqgC/I=
Expand All @@ -23,8 +28,11 @@ github.com/ipfs/go-detect-race v0.0.1/go.mod h1:8BNT7shDZPo99Q74BpGMK+4D8Mn4j46U
github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw=
github.com/ipfs/go-ipfs-ds-help v1.1.0 h1:yLE2w9RAsl31LtfMt91tRZcrx+e61O5mDxFRR994w4Q=
github.com/ipfs/go-ipfs-ds-help v1.1.0/go.mod h1:YR5+6EaebOhfcqVCyqemItCLthrpVNot+rsOU/5IatU=
github.com/ipfs/go-ipfs-util v0.0.1/go.mod h1:spsl5z8KUnrve+73pOhSVZND1SIxPW5RyBCNzQxlJBc=
github.com/ipfs/go-ipfs-util v0.0.2 h1:59Sswnk1MFaiq+VcaknX7aYEyGyGDAA73ilhEK2POp8=
github.com/ipfs/go-ipfs-util v0.0.2/go.mod h1:CbPtkWJzjLdEcezDns2XYaehFVNXG9zrdrtMecczcsQ=
github.com/ipfs/go-ipld-format v0.2.1-0.20220302134852-d02e0e18fc65 h1:xxnD+fUS7hziDAnfrn3qsl0ql18DOjq4rwvzBTCr1iA=
github.com/ipfs/go-ipld-format v0.2.1-0.20220302134852-d02e0e18fc65/go.mod h1:co/SdBE8h99968X0hViiw1MNlh6fvxxnHpvVLnH7jSM=
github.com/ipfs/go-log v0.0.1 h1:9XTUN/rW64BCG1YhPK9Hoy3q8nr4gOmHHBpgFdfw6Lc=
github.com/ipfs/go-log v0.0.1/go.mod h1:kL1d2/hzSpI0thNYjiKfjanbVNU+IIGA/WnNESY9leM=
github.com/ipfs/go-metrics-interface v0.0.1 h1:j+cpbjYvu4R8zbleSs36gvB7jR+wsL2fGD6n0jO4kdg=
Expand All @@ -46,6 +54,7 @@ github.com/mattn/go-isatty v0.0.5 h1:tHXDdz1cpzGaovsTB+TVB8q90WEokoVmfMqoVcrLUgw
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g=
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ=
github.com/minio/sha256-simd v0.0.0-20190131020904-2d45a736cd16/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U=
github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771 h1:MHkK1uRtFbVqvAgvWxafZe54+5uBxLluGylDiKgdhwo=
github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM=
github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8=
Expand All @@ -58,6 +67,7 @@ github.com/multiformats/go-base36 v0.1.0/go.mod h1:kFGE83c6s80PklsHO9sRn2NCoffoR
github.com/multiformats/go-multibase v0.0.1/go.mod h1:bja2MqRZ3ggyXtZSEDKpl0uO/gviWFaSteVbWT51qgs=
github.com/multiformats/go-multibase v0.0.3 h1:l/B6bJDQjvQ5G52jw4QGSYeOTZoAwIO77RblWplfIqk=
github.com/multiformats/go-multibase v0.0.3/go.mod h1:5+1R4eQrT3PkYZ24C3W2Ue2tPwIdYQD509ZjSb5y9Oc=
github.com/multiformats/go-multihash v0.0.1/go.mod h1:w/5tugSrLEbWqlcgJabL3oHFKTwfvkofsjW2Qa1ct4U=
github.com/multiformats/go-multihash v0.0.13/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc=
github.com/multiformats/go-multihash v0.0.14 h1:QoBceQYQQtNUuf6s7wHxnE2c8bhbMqhfGzNI032se/I=
github.com/multiformats/go-multihash v0.0.14/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc=
Expand All @@ -79,6 +89,7 @@ go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk=
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8 h1:1wopBVtVdWnn03fZelqdXTqk7U7zPQCb+T4rbU9ZEoU=
Expand All @@ -93,6 +104,7 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwL
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190219092855-153ac476189d/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down

0 comments on commit b598a29

Please sign in to comment.