diff --git a/storage/benchmarks/go.mod b/storage/benchmarks/go.mod index 5b89c565..0de635d7 100644 --- a/storage/benchmarks/go.mod +++ b/storage/benchmarks/go.mod @@ -16,7 +16,7 @@ require ( github.com/alexbrainman/goissue34681 v0.0.0-20191006012335-3fc7a47baff5 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/google/uuid v1.3.0 // indirect - github.com/ipfs/go-cid v0.4.0 // indirect + github.com/ipfs/go-cid v0.4.1 // indirect github.com/ipfs/go-datastore v0.6.0 // indirect github.com/ipfs/go-log v1.0.3 // indirect github.com/ipfs/go-log/v2 v2.0.3 // indirect @@ -27,7 +27,7 @@ require ( github.com/multiformats/go-base32 v0.0.3 // indirect github.com/multiformats/go-base36 v0.1.0 // indirect github.com/multiformats/go-multibase v0.0.3 // indirect - github.com/multiformats/go-multihash v0.2.1 // indirect + github.com/multiformats/go-multihash v0.2.2 // indirect github.com/multiformats/go-varint v0.0.6 // indirect github.com/opentracing/opentracing-go v1.1.0 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect diff --git a/storage/benchmarks/go.sum b/storage/benchmarks/go.sum index 1d74bdfc..cccfb65e 100644 --- a/storage/benchmarks/go.sum +++ b/storage/benchmarks/go.sum @@ -12,8 +12,8 @@ github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm4 github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/ipfs/go-cid v0.4.0 h1:a4pdZq0sx6ZSxbCizebnKiMCx/xI/aBBFlB73IgH4rA= -github.com/ipfs/go-cid v0.4.0/go.mod h1:uQHwDeX4c6CtyrFwdqyhpNcxVewur1M7l7fNU7LKwZk= +github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s= +github.com/ipfs/go-cid v0.4.1/go.mod h1:uQHwDeX4c6CtyrFwdqyhpNcxVewur1M7l7fNU7LKwZk= github.com/ipfs/go-datastore v0.5.0/go.mod h1:9zhEApYMTl17C8YDp7JmU7sQZi2/wqiYh73hakZ90Bk= github.com/ipfs/go-datastore v0.6.0 h1:JKyz+Gvz1QEZw0LsX1IBn+JFCJQH4SJVFtM4uWU0Myk= github.com/ipfs/go-datastore v0.6.0/go.mod h1:rt5M3nNbSO/8q1t4LNkLyUwRs8HupMeN/8O4Vn9YAT8= @@ -52,8 +52,8 @@ github.com/multiformats/go-base36 v0.1.0 h1:JR6TyF7JjGd3m6FbLU2cOxhC0Li8z8dLNGQ8 github.com/multiformats/go-base36 v0.1.0/go.mod h1:kFGE83c6s80PklsHO9sRn2NCoffoRdUUOENyW/Vv6sM= 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.2.1 h1:aem8ZT0VA2nCHHk7bPJ1BjUbHNciqZC/d16Vve9l108= -github.com/multiformats/go-multihash v0.2.1/go.mod h1:WxoMcYG85AZVQUyRyo9s4wULvW5qrI9vb2Lt6evduFc= +github.com/multiformats/go-multihash v0.2.2 h1:Uu7LWs/PmWby1gkj1S1DXx3zyd3aVabA4FiMKn/2tAc= +github.com/multiformats/go-multihash v0.2.2/go.mod h1:dXgKXCXjBzdscBLk9JkjINiEsCKRVch90MdaGiKsvSM= github.com/multiformats/go-varint v0.0.6 h1:gk85QWKxh3TazbLxED/NlDVv8+q+ReFJk7Y2W/KhfNY= github.com/multiformats/go-varint v0.0.6/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU= diff --git a/storage/memstore/memstore.go b/storage/memstore/memstore.go index b484e834..7a0369e0 100644 --- a/storage/memstore/memstore.go +++ b/storage/memstore/memstore.go @@ -57,7 +57,7 @@ func (store *Store) Get(ctx context.Context, key string) ([]byte, error) { store.beInitialized() content, exists := store.Bag[key] if !exists { - return nil, storage.ErrNotFound{Key: key} + return nil, storage.NewErrNotFoundForKey(key) } cpy := make([]byte, len(content)) copy(cpy, content) @@ -83,7 +83,7 @@ func (store *Store) Put(ctx context.Context, key string, content []byte) error { func (store *Store) GetStream(ctx context.Context, key string) (io.ReadCloser, error) { content, exists := store.Bag[key] if !exists { - return nil, storage.ErrNotFound{Key: key} + return nil, storage.NewErrNotFoundForKey(key) } return noopCloser{bytes.NewReader(content)}, nil } @@ -92,7 +92,7 @@ func (store *Store) GetStream(ctx context.Context, key string) (io.ReadCloser, e func (store *Store) Peek(ctx context.Context, key string) ([]byte, io.Closer, error) { content, exists := store.Bag[key] if !exists { - return nil, nil, storage.ErrNotFound{Key: key} + return nil, nil, storage.NewErrNotFoundForKey(key) } return content, noopCloser{nil}, nil } diff --git a/storage/notfound.go b/storage/notfound.go index 0795a5a6..8496d856 100644 --- a/storage/notfound.go +++ b/storage/notfound.go @@ -7,8 +7,9 @@ import ( ) // ErrNotFound is a 404, but for block storage systems. It is returned when -// a block is not found. The Key is typically the binary form of a CID -// (CID#KeyString()). +// a block is not found. The Cid property may be cid.Undef if the NotFound error +// was not created with a specific CID (e.g. when using a non-CID key in a +// storage Get operation). // // ErrNotFound implements `interface{NotFound() bool}`, which makes it roughly // compatible with the legacy github.com/ipfs/go-ipld-format#ErrNotFound. @@ -23,20 +24,30 @@ import ( // matching function that should be able to determine whether an ErrNotFound, // either new or legacy, exists within a wrapped error chain. type ErrNotFound struct { - Key string + Cid cid.Cid } // NewErrNotFound is a convenience factory that creates a new ErrNotFound error // from a CID. func NewErrNotFound(c cid.Cid) ErrNotFound { - return ErrNotFound{Key: c.KeyString()} + return ErrNotFound{Cid: c} +} + +// NewErrNotFound is a convenience factory that creates a new ErrNotFound error +// from a key. If the key is a CID#KeyString(), then it will be cast to a CID, +// otherwise the Cid of the ErrNotFound will be cid.Undef. +func NewErrNotFoundForKey(key string) ErrNotFound { + if c, err := cid.Cast([]byte(key)); err == nil { + return ErrNotFound{Cid: c} + } + return ErrNotFound{Cid: cid.Undef} } func (e ErrNotFound) Error() string { - if c, err := cid.Cast([]byte(e.Key)); err == nil && c != cid.Undef { - return "ipld: could not find " + c.String() + if e.Cid == cid.Undef { + return "ipld: could not find node" } - return "ipld: could not find " + e.Key + return "ipld: could not find " + e.Cid.String() } // NotFound always returns true, and is used to feature-test for ErrNotFound diff --git a/storage/notfound_test.go b/storage/notfound_test.go index 56d207d2..f8a223a5 100644 --- a/storage/notfound_test.go +++ b/storage/notfound_test.go @@ -10,14 +10,14 @@ import ( ) func TestNotFound(t *testing.T) { - nf := storage.ErrNotFound{Key: "foo"} + nf := storage.ErrNotFound{} if !storage.IsNotFound(nf) { t.Fatal("expected ErrNotFound to be a NotFound error") } if !errors.Is(nf, storage.ErrNotFound{}) { t.Fatal("expected ErrNotFound to be a NotFound error") } - if nf.Error() != "ipld: could not find foo" { + if nf.Error() != "ipld: could not find node" { t.Fatal("unexpected error message") }