WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 3750bbb

Browse files
authored
Merge pull request #1 from mrproliu/merge-callback
2 parents e54f64b + 0e5a574 commit 3750bbb

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

config.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import (
1818
"io"
1919
"log"
2020

21+
"github.com/RoaringBitmap/roaring"
22+
segment "github.com/blugelabs/bluge_segment_api"
23+
2124
"github.com/blugelabs/bluge/analysis"
2225
"github.com/blugelabs/bluge/analysis/analyzer"
2326
"github.com/blugelabs/bluge/index"
@@ -78,6 +81,12 @@ func (config Config) WithSearchStartFunc(f func(size uint64) error) Config {
7881
return config
7982
}
8083

84+
func (config Config) WithPrepareMergeCallback(f func(src []*roaring.Bitmap, segments []segment.Segment, id uint64) (
85+
dest []*roaring.Bitmap, err error)) Config {
86+
config.indexConfig.PrepareMergeFunc = f
87+
return config
88+
}
89+
8190
func DefaultConfig(path string) Config {
8291
indexConfig := index.DefaultConfig(path)
8392
return defaultConfig(indexConfig)

index/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121

2222
"github.com/blugelabs/bluge/index/mergeplan"
2323
"github.com/blugelabs/ice"
24+
25+
"github.com/RoaringBitmap/roaring"
2426
)
2527

2628
type Config struct {
@@ -80,6 +82,9 @@ type Config struct {
8082
virtualFields map[string][]segment.Field
8183

8284
CacheMaxBytes int
85+
86+
// PerFieldSimilarity allows specifying a custom callback before merge operation
87+
PrepareMergeFunc func(src []*roaring.Bitmap, segments []segment.Segment, id uint64) (dest []*roaring.Bitmap, err error)
8388
}
8489

8590
func (config Config) WithSegmentType(typ string) Config {

index/merge.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ func (s *Writer) executeMergeTask(merges chan *segmentMerge, task *mergeplan.Mer
206206
// decrement the ref counts on skipping introduction.
207207
// FIXME stale file that won't get cleaned up
208208
_ = seg.Close()
209+
_ = s.directory.Remove(ItemKindSegment, newSegmentID)
209210
}
210211
}
211212

@@ -363,9 +364,18 @@ func (s *Writer) mergeSegmentBases(merges chan *segmentMerge, snapshot *Snapshot
363364

364365
func (s *Writer) merge(segments []segment.Segment, drops []*roaring.Bitmap, id uint64) (
365366
[][]uint64, error) {
367+
var err error
368+
if s.config.PrepareMergeFunc != nil {
369+
// call the prepare merge function
370+
drops, err = s.config.PrepareMergeFunc(drops, segments, id)
371+
if err != nil {
372+
return nil, fmt.Errorf("prepare merge failed: %w", err)
373+
}
374+
}
375+
366376
merger := s.segPlugin.Merge(segments, drops, s.config.MergeBufferSize)
367377

368-
err := s.directory.Persist(ItemKindSegment, id, merger, s.closeCh)
378+
err = s.directory.Persist(ItemKindSegment, id, merger, s.closeCh)
369379
if err != nil {
370380
return nil, err
371381
}

0 commit comments

Comments
 (0)