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

fix(share/eds): remove Has() and Sync() badger calls in inverted index #2518

Merged
merged 4 commits into from
Jul 31, 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
25 changes: 7 additions & 18 deletions share/eds/inverted_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,24 @@ func (s *simpleInvertedIndex) AddMultihashesForShard(
return fmt.Errorf("failed to create ds batch: %w", err)
}

if err := mhIter.ForEach(func(mh multihash.Multihash) error {
err = mhIter.ForEach(func(mh multihash.Multihash) error {
key := ds.NewKey(string(mh))
ok, err := s.ds.Has(ctx, key)
bz, err := json.Marshal(sk)
adlerjohn marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return fmt.Errorf("failed to check if value for multihash exists %s, err: %w", mh, err)
return fmt.Errorf("failed to marshal shard key to bytes: %w", err)
}

if !ok {
bz, err := json.Marshal(sk)
if err != nil {
return fmt.Errorf("failed to marshal shard key to bytes: %w", err)
}
if err := batch.Put(ctx, key, bz); err != nil {
return fmt.Errorf("failed to put mh=%s, err=%w", mh, err)
}
if err := batch.Put(ctx, key, bz); err != nil {
return fmt.Errorf("failed to put mh=%s, err=%w", mh, err)
}

return nil
}); err != nil {
})
if err != nil {
return fmt.Errorf("failed to add index entry: %w", err)
}

if err := batch.Commit(ctx); err != nil {
return fmt.Errorf("failed to commit batch: %w", err)
}

if err := s.ds.Sync(ctx, ds.Key{}); err != nil {
return fmt.Errorf("failed to sync puts: %w", err)
}
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions share/eds/inverted_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ func TestMultihashesForShard(t *testing.T) {
require.NoError(t, err)
require.Equal(t, []shard.Key{shard.KeyFromString("shard1")}, shardKeys)

// 2. Add mh1 to shard2, and ensure that mh1 still points to shard1
// 2. Add mh1 to shard2, and ensure that mh1 no longer points to shard1
err = invertedIndex.AddMultihashesForShard(ctx, &mockIterator{mhs: mhs[:1]}, shard.KeyFromString("shard2"))
require.NoError(t, err)
shardKeys, err = invertedIndex.GetShardsForMultihash(ctx, mhs[0])
require.NoError(t, err)
require.Equal(t, []shard.Key{shard.KeyFromString("shard1")}, shardKeys)
require.Equal(t, []shard.Key{shard.KeyFromString("shard2")}, shardKeys)
}
Loading