2017-09-29 18:42:37 +02:00
|
|
|
package mem
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/RoaringBitmap/roaring"
|
|
|
|
"github.com/blevesearch/bleve/index"
|
|
|
|
"github.com/blevesearch/bleve/index/scorch/segment"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Dictionary is the in-memory representation of the term dictionary
|
|
|
|
type Dictionary struct {
|
|
|
|
segment *Segment
|
|
|
|
field string
|
|
|
|
fieldID uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostingsList returns the postings list for the specified term
|
|
|
|
func (d *Dictionary) PostingsList(term string, except *roaring.Bitmap) segment.PostingsList {
|
|
|
|
return &PostingsList{
|
|
|
|
dictionary: d,
|
|
|
|
term: term,
|
2017-12-01 13:26:47 +01:00
|
|
|
postingsID: d.segment.Dicts[d.fieldID][term],
|
2017-09-29 18:42:37 +02:00
|
|
|
except: except,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterator returns an iterator for this dictionary
|
|
|
|
func (d *Dictionary) Iterator() segment.DictionaryIterator {
|
|
|
|
return &DictionaryIterator{
|
|
|
|
d: d,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrefixIterator returns an iterator which only visits terms having the
|
|
|
|
// the specified prefix
|
|
|
|
func (d *Dictionary) PrefixIterator(prefix string) segment.DictionaryIterator {
|
2017-12-01 13:26:47 +01:00
|
|
|
offset := sort.SearchStrings(d.segment.DictKeys[d.fieldID], prefix)
|
2017-09-29 18:42:37 +02:00
|
|
|
return &DictionaryIterator{
|
|
|
|
d: d,
|
|
|
|
prefix: prefix,
|
|
|
|
offset: offset,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RangeIterator returns an iterator which only visits terms between the
|
|
|
|
// start and end terms. NOTE: bleve.index API specifies the end is inclusive.
|
|
|
|
func (d *Dictionary) RangeIterator(start, end string) segment.DictionaryIterator {
|
2017-12-01 13:26:47 +01:00
|
|
|
offset := sort.SearchStrings(d.segment.DictKeys[d.fieldID], start)
|
2017-09-29 18:42:37 +02:00
|
|
|
return &DictionaryIterator{
|
|
|
|
d: d,
|
|
|
|
offset: offset,
|
|
|
|
end: end,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DictionaryIterator is an iterator for term dictionary
|
|
|
|
type DictionaryIterator struct {
|
|
|
|
d *Dictionary
|
|
|
|
prefix string
|
|
|
|
end string
|
|
|
|
offset int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next returns the next entry in the dictionary
|
|
|
|
func (d *DictionaryIterator) Next() (*index.DictEntry, error) {
|
2017-12-01 13:26:47 +01:00
|
|
|
if d.offset > len(d.d.segment.DictKeys[d.d.fieldID])-1 {
|
2017-09-29 18:42:37 +02:00
|
|
|
return nil, nil
|
|
|
|
}
|
2017-12-01 13:26:47 +01:00
|
|
|
next := d.d.segment.DictKeys[d.d.fieldID][d.offset]
|
2017-09-29 18:42:37 +02:00
|
|
|
// check prefix
|
|
|
|
if d.prefix != "" && !strings.HasPrefix(next, d.prefix) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
// check end (bleve.index API demands inclusive end)
|
|
|
|
if d.end != "" && next > d.end {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
d.offset++
|
2017-12-01 13:26:47 +01:00
|
|
|
postingID := d.d.segment.Dicts[d.d.fieldID][next]
|
2017-09-29 18:42:37 +02:00
|
|
|
return &index.DictEntry{
|
|
|
|
Term: next,
|
2017-12-01 13:26:47 +01:00
|
|
|
Count: d.d.segment.Postings[postingID-1].GetCardinality(),
|
2017-09-29 18:42:37 +02:00
|
|
|
}, nil
|
|
|
|
}
|