Skip to content

Commit

Permalink
fix(share/eds): remove Has() and Sync() badger calls in inverted index (
Browse files Browse the repository at this point in the history
#2518)

Remove unnecessary `Has()` and `Sync()` badger calls in inverted index.
Removal of calls greatly improve `Put()` performance on eds.Store
  • Loading branch information
walldiss authored Jul 31, 2023
1 parent 794360f commit 208bb86
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
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)
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)
}

0 comments on commit 208bb86

Please sign in to comment.