-
Notifications
You must be signed in to change notification settings - Fork 11
/
be_indexer.go
154 lines (121 loc) · 3.03 KB
/
be_indexer.go
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package be_indexer
import (
"fmt"
"strings"
"sync"
)
const (
WildcardFieldName = BEField("_Z_")
)
var (
wildcardQKey = NewQKey(WildcardFieldName, 0)
)
type (
FieldOption struct {
Container string // specify Entries holder for all tokenized value Entries
}
IndexerSettings struct {
FieldConfig map[BEField]FieldOption
}
BEIndex interface {
// addWildcardEID interface used by builder
addWildcardEID(id EntryID)
// set fields desc/settings
setFieldDesc(fieldsData map[BEField]*FieldDesc)
// newContainer indexer need return a valid Container for k size
newContainer(k int) *EntriesContainer
// compileIndexer prepare indexer and optimize index data
compileIndexer() error
// Retrieve scan index data and retrieve satisfied document
Retrieve(queries Assignments, opt ...IndexOpt) (DocIDList, error)
// RetrieveWithCollector scan index data and retrieve satisfied document
RetrieveWithCollector(Assignments, ResultCollector, ...IndexOpt) error
// DumpEntries debug api
DumpEntries(sb *strings.Builder)
DumpIndexInfo(sb *strings.Builder)
}
FieldDesc struct {
FieldOption
ID uint64
Field BEField
}
indexBase struct {
// fieldsData a field settings and resource, if not configured, it will use default parser and container
// for expression values;
fieldsData map[BEField]*FieldDesc
// wildcardEntries hold all entry id that conjunction size is zero;
wildcardEntries Entries
}
)
func (bi *indexBase) setFieldDesc(fieldsData map[BEField]*FieldDesc) {
bi.fieldsData = fieldsData
}
// addWildcardEID append wildcard entry id to Z set
func (bi *indexBase) addWildcardEID(id EntryID) {
bi.wildcardEntries = append(bi.wildcardEntries, id)
}
// collectorPool default collect pool
var collectorPool = sync.Pool{
New: func() interface{} {
return NewDocIDCollector()
},
}
func PickCollector() *DocIDCollector {
return collectorPool.Get().(*DocIDCollector)
}
func PutCollector(c *DocIDCollector) {
if c == nil {
return
}
c.Reset()
collectorPool.Put(c)
}
type (
retrieveContext struct {
dumpStepInfo bool
dumpEntriesDetail bool
collector ResultCollector
assigns Assignments
}
IndexOpt func(ctx *retrieveContext)
)
func WithStepDetail() IndexOpt {
return func(ctx *retrieveContext) {
ctx.dumpStepInfo = true
}
}
func WithDumpEntries() IndexOpt {
return func(ctx *retrieveContext) {
ctx.dumpEntriesDetail = true
}
}
// WithCollector specify a user defined collector
func WithCollector(fn ResultCollector) IndexOpt {
return func(ctx *retrieveContext) {
ctx.collector = fn
}
}
func newRetrieveCtx(ass Assignments, opts ...IndexOpt) retrieveContext {
ctx := retrieveContext{}
ctx.assigns = ass
for _, fn := range opts {
fn(&ctx)
}
return ctx
}
func PrintIndexInfo(index BEIndex) {
if index == nil {
fmt.Println("nil indexer")
}
sb := &strings.Builder{}
index.DumpIndexInfo(sb)
fmt.Println(sb.String())
}
func PrintIndexEntries(index BEIndex) {
if index == nil {
fmt.Println("nil indexer")
}
sb := &strings.Builder{}
index.DumpEntries(sb)
fmt.Println(sb.String())
}