Skip to content

Commit

Permalink
Sort batches of indexes to reduce cursor churn (#99)
Browse files Browse the repository at this point in the history
* Sort batches of indexes to reduce cursor churn
  • Loading branch information
gammazero authored Jan 2, 2024
1 parent 12bd8bf commit 0b8f64b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
12 changes: 12 additions & 0 deletions pebble/pebble.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"io"
"sort"

"github.com/cockroachdb/pebble"
"github.com/ipni/dhstore"
Expand Down Expand Up @@ -48,9 +49,15 @@ func NewPebbleDHStore(path string, opts *pebble.Options) (*PebbleDHStore, error)
}

func (s *PebbleDHStore) MergeIndexes(indexes []dhstore.Index) error {
// Sort indexes to reduce cursor churn.
sort.Slice(indexes, func(i, j int) bool {
return bytes.Compare(indexes[i].Key, indexes[j].Key) == -1
})

keygen := s.p.leaseSimpleKeyer()
defer keygen.Close()
batch := s.db.NewBatch()

for _, index := range indexes {
dmh, err := multihash.Decode(index.Key)
if err != nil {
Expand Down Expand Up @@ -82,6 +89,11 @@ func (s *PebbleDHStore) MergeIndexes(indexes []dhstore.Index) error {
// DeleteIndexes removes dh-multihash to encrypted-valueKey mappings. This is
// the inverse of MergeIndexes.
func (s *PebbleDHStore) DeleteIndexes(indexes []dhstore.Index) error {
// Sort indexes to reduce cursor churn.
sort.Slice(indexes, func(i, j int) bool {
return bytes.Compare(indexes[i].Key, indexes[j].Key) == -1
})

keygen := s.p.leaseSimpleKeyer()
defer keygen.Close()
batch := s.db.NewBatch()
Expand Down
11 changes: 5 additions & 6 deletions pebble/pebble_merger.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ type valueKeysValueMerger struct {
func (s *PebbleDHStore) newValueKeysMerger() *pebble.Merger {
return &pebble.Merger{
Merge: func(k, value []byte) (pebble.ValueMerger, error) {
// Fall back on default merger if the key is not of type multihash, i.e. the only key
// type that corresponds to value-keys.
switch keyPrefix(k[0]) {
case multihashKeyPrefix:
// Use specialized merger for multihash keys.
if keyPrefix(k[0]) == multihashKeyPrefix {
v := &valueKeysValueMerger{s: s}
return v, v.MergeNewer(value)
default:
return pebble.DefaultMerger.Merge(k, value)
}
// Use default merger for non-multihash type keys, i.e. the
// only key type that corresponds to value-keys.
return pebble.DefaultMerger.Merge(k, value)
},
Name: valueKeysMergerName,
}
Expand Down

0 comments on commit 0b8f64b

Please sign in to comment.