Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReadWrite: faster Has() by using the in-memory index instead of reading on disk #393

Merged
merged 1 commit into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion v2/blockstore/readwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,29 @@ func (b *ReadWrite) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
}

func (b *ReadWrite) Has(ctx context.Context, key cid.Cid) (bool, error) {
return b.ronly.Has(ctx, key)
if !b.opts.StoreIdentityCIDs {
// If we don't store identity CIDs then we can return them straight away as if they are here,
// otherwise we need to check for their existence.
// Note, we do this without locking, since there is no shared information to lock for in order to perform the check.
if _, ok, err := store.IsIdentity(key); err != nil {
return false, err
} else if ok {
return true, nil
}
}

if ctx.Err() != nil {
return false, ctx.Err()
}

b.ronly.mu.Lock()
defer b.ronly.mu.Unlock()

if b.ronly.closed {
return false, errClosed
}

return b.idx.HasMultihash(key.Hash())
}

func (b *ReadWrite) Get(ctx context.Context, key cid.Cid) (blocks.Block, error) {
Expand Down
6 changes: 3 additions & 3 deletions v2/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/multiformats/go-varint"
)

// CarIndexNone is a sentinal value used as a multicodec code for the index indicating no index.
// CarIndexNone is a sentinel value used as a multicodec code for the index indicating no index.
const CarIndexNone = 0x300000

type (
Expand Down Expand Up @@ -46,7 +46,7 @@ type (
// Unmarshal decodes the index from its serial form.
// Note, this function will copy the entire index into memory.
//
// Do not unmarshal index from untrusted CARv2 files. Instead the index should be
// Do not unmarshal index from untrusted CARv2 files. Instead, the index should be
// regenerated from the CARv2 data payload.
Unmarshal(r io.Reader) error

Expand Down Expand Up @@ -84,7 +84,7 @@ type (
// and the ForEach function returns the error to the user.
//
// An index may contain multiple offsets corresponding to the same multihash, e.g. via duplicate blocks.
// In such cases, the given function may be called multiple times with the same multhihash but different offset.
// In such cases, the given function may be called multiple times with the same multihash but different offset.
//
// The order of calls to the given function is deterministic, but entirely index-specific.
ForEach(func(multihash.Multihash, uint64) error) error
Expand Down
31 changes: 28 additions & 3 deletions v2/internal/store/insertionindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,10 @@ func (ii *InsertionIndex) Flatten(codec multicodec.Code) (index.Index, error) {
// but it's separate as it allows us to compare Record.Cid directly,
// whereas GetAll just provides Record.Offset.

func (ii *InsertionIndex) HasExactCID(c cid.Cid) bool {
func (ii *InsertionIndex) HasExactCID(c cid.Cid) (bool, error) {
d, err := multihash.Decode(c.Hash())
if err != nil {
panic(err)
return false, err
}
entry := recordDigest{digest: d.Digest}

Expand All @@ -235,5 +235,30 @@ func (ii *InsertionIndex) HasExactCID(c cid.Cid) bool {
return true
}
ii.items.AscendGreaterOrEqual(entry, iter)
return found
return found, nil
}

func (ii *InsertionIndex) HasMultihash(mh multihash.Multihash) (bool, error) {
d, err := multihash.Decode(mh)
if err != nil {
return false, err
}
entry := recordDigest{digest: d.Digest}

found := false
iter := func(i llrb.Item) bool {
existing := i.(recordDigest)
if !bytes.Equal(existing.digest, entry.digest) {
// We've already looked at all entries with matching digests.
return false
}
if bytes.Equal(existing.Record.Cid.Hash(), mh) {
found = true
return false
}
// Continue looking in ascending order.
return true
}
ii.items.AscendGreaterOrEqual(entry, iter)
return found, nil
}
8 changes: 6 additions & 2 deletions v2/internal/store/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ func ShouldPut(
}

if !blockstoreAllowDuplicatePuts {
if blockstoreUseWholeCIDs && idx.HasExactCID(c) {
return false, nil // deduplicated by CID
if blockstoreUseWholeCIDs {
has, err := idx.HasExactCID(c)
if err != nil {
return false, err
}
return !has, nil // deduplicated by CID
}
if !blockstoreUseWholeCIDs {
_, err := idx.Get(c)
Expand Down