internal/index/batch.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | package index
import (
"github.com/Southclaws/fault"
"github.com/Southclaws/fault/fmsg"
"github.com/blevesearch/bleve/v2"
"github.com/blevesearch/bleve/v2/document"
)
type Batcher struct {
batch *bleve.Batch
write *WriteIndex
err error
n int
max int
}
func (index *WriteIndex) NewBatcher() *Batcher {
return &Batcher{
batch: index.index.NewBatch(),
write: index,
max: index.batchSize,
}
}
func (b *Batcher) Size() int {
return b.max
}
func (b *Batcher) Flush() error {
b.flush()
return b.err
}
func (b *Batcher) flush() {
if b.n == 0 {
return
}
b.write.log.Debug("flushing batch", "size", b.n)
err := b.write.index.Batch(b.batch)
if err != nil {
b.err = &BatchError{fault.Wrap(err, fmsg.Withf("could not flush batch"))}
return
}
b.Reset()
}
func (b *Batcher) Reset() {
b.err = nil
b.n = 0
b.batch.Reset()
}
func (b *Batcher) countAndFlush() {
if b.n++; b.n >= b.max {
b.flush()
}
}
func (b *Batcher) IndexAdvanced(doc *document.Document) error {
err := b.batch.IndexAdvanced(doc)
if err != nil {
return fault.Wrap(err, fmsg.Withf("could not index document"))
}
b.countAndFlush()
if b.err != nil {
return b.err
}
return nil
}
func (b *Batcher) Delete(id string) {
b.batch.Delete(id)
}
|