-
Notifications
You must be signed in to change notification settings - Fork 11
/
index_scanner.go
277 lines (251 loc) · 6.5 KB
/
index_scanner.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package be_indexer
import (
"fmt"
"reflect"
"strings"
)
/*
IndexScan
a scanner for indexer, it helps to retrieve result document id from posting entries
currently, it is used by BEIndex, but as a part of design, it should top on BEIndex
that seems more reasonable. so may be next version, should be refactored(fixed).
*/
const (
LinearSkipDistance = 8
)
type (
QKey struct {
field BEField
value interface{}
}
// EntriesCursor represent a posting list for one Assign
// (age, 15): [1, 2, 5, 19, 22]
// cursor: ^
EntriesCursor struct {
key QKey
cursor int // current cur cursor
entries Entries
idSize int
curEID EntryID // 为了加速计算
}
EntriesCursors []EntriesCursor
// FieldCursor for a boolean expression: {"tag", "in", [1, 2, 3]}
// tag_2: [ID5]
// tag_1: [ID1, ID2, ID7]
FieldCursor struct {
current *EntriesCursor
cursorGroup EntriesCursors
}
FieldCursors []FieldCursor
)
func NewQKey(field BEField, v interface{}) QKey {
key := QKey{field: field, value: v}
return key
}
func (key *QKey) String() string {
switch v := key.value.(type) {
case string:
return fmt.Sprintf("[%s,%s]", key.field, v)
case int8, int16, int, int32, int64, uint8, uint16, uint, uint32, uint64:
return fmt.Sprintf("[%s,%d]", key.field, v)
default:
fmt.Println("unknown type", reflect.TypeOf(key.value).String())
}
return fmt.Sprintf("[%s,%+v]", key.field, key.value)
}
func NewEntriesCursor(key QKey, entries Entries) EntriesCursor {
eid := NULLENTRY
if len(entries) > 0 {
eid = entries[0]
}
return EntriesCursor{
key: key,
cursor: 0,
curEID: eid,
entries: entries,
idSize: len(entries),
}
}
func (ec *EntriesCursor) GetCurEntryID() EntryID {
return ec.curEID
}
func (ec *EntriesCursor) linearSkipTo(id EntryID) EntryID {
for ec.cursor < ec.idSize && ec.entries[ec.cursor] < id {
ec.cursor++
}
if ec.cursor >= ec.idSize {
ec.curEID = NULLENTRY
} else {
ec.curEID = ec.entries[ec.cursor]
}
return ec.curEID
}
func (ec *EntriesCursor) SkipTo(id EntryID) EntryID {
if ec.curEID >= id {
return ec.curEID
}
oc := ec.cursor
bound := 1
rightSideIndex := oc + bound
for rightSideIndex < ec.idSize && ec.entries[rightSideIndex] < id { // notice: can't use <=
ec.cursor = rightSideIndex
bound = bound << 1
rightSideIndex = oc + bound
}
if rightSideIndex > ec.idSize {
rightSideIndex = ec.idSize
}
// id in the range: [ec.cursor, rightSideIndex)
// reuse `bound` as `mid` value
// fmt.Printf("cur:%d idx-range:[%d,%d) values:%v", oc, ec.cursor, rightSideIndex, ec.entries[ec.cursor:rightSideIndex])
for ec.cursor < rightSideIndex && ec.entries[ec.cursor] < id {
// if rightSideIndex-ec.cursor < LinearSkipDistance {
// return ec.linearSkipTo(id)
//}
bound = (ec.cursor + rightSideIndex) >> 1
if ec.entries[bound] >= id {
rightSideIndex = bound
} else {
ec.cursor = bound + 1
}
}
if ec.cursor >= ec.idSize {
ec.curEID = NULLENTRY
} else {
ec.curEID = ec.entries[ec.cursor]
}
return ec.curEID
}
// Len FieldCursors sort API
func (s FieldCursors) Len() int { return len(s) }
func (s FieldCursors) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s FieldCursors) Less(i, j int) bool {
return s[i].GetCurEntryID() < s[j].GetCurEntryID()
}
// Sort golang's internal sort.Sort method have obvious overhead in performance.(runtime convTSlice)
// so here use a simple insert sort replace it. bz not much Element, may another quickSort here later
func (s FieldCursors) Sort() {
x := len(s)
if x <= 1 {
return
}
// Do ShellSort pass with gap 6
// It could be written in this simplified form cause b-a <= 12
//if x <= 12 { // make it seems sorted
// for i := 6; i < x; i++ {
// if s[i].GetCurEntryID() < s[i-6].GetCurEntryID() {
// s[i], s[i-6] = s[i-6], s[i]
// }
// }
//}
for i := 1; i < x; i++ {
for j := i; j > 0 && s[j].GetCurEntryID() < s[j-1].GetCurEntryID(); j-- {
s[j], s[j-1] = s[j-1], s[j]
}
}
}
func (s FieldCursors) Dump() string {
sb := &strings.Builder{}
for _, fc := range s {
fc.DumpEntries(sb)
}
return sb.String()
}
func (s FieldCursors) DumpJustCursors() string {
sb := &strings.Builder{}
for idx, fc := range s {
if idx > 0 {
sb.WriteString("\n")
}
fc.DumpCursorEntryID(sb)
}
return sb.String()
}
func NewFieldCursor(cursors ...EntriesCursor) FieldCursor {
scanner := FieldCursor{
current: nil,
cursorGroup: cursors,
}
for idx := range scanner.cursorGroup {
cursor := scanner.cursorGroup[idx]
if scanner.current == nil || cursor.curEID < scanner.current.curEID {
scanner.current = &cursor
}
}
return scanner
}
func (fc *FieldCursor) ReachEnd() bool {
return fc.current.curEID.IsNULLEntry()
}
func (fc *FieldCursor) GetCurEntryID() EntryID {
return fc.current.curEID
}
func (fc *FieldCursor) SkipTo(id EntryID) (newMin EntryID) {
newMin = NULLENTRY
for idx := range fc.cursorGroup {
cur := &fc.cursorGroup[idx]
if eid := cur.SkipTo(id); eid <= newMin {
newMin = eid
fc.current = cur
}
}
return
}
func (fc *FieldCursor) DumpEntries(sb *strings.Builder) {
sb.WriteString("============== Field:%s Cursors ==============\n")
for idx := range fc.cursorGroup {
it := &fc.cursorGroup[idx]
if it == fc.current {
sb.WriteString(">")
} else {
sb.WriteString(" ")
}
it.DumpEntries(sb)
sb.WriteString("\n")
}
}
func (fc *FieldCursor) DumpCursorEntryID(sb *strings.Builder) {
sb.WriteString("============== Field Cursor EID ==============\n")
for idx := range fc.cursorGroup {
cursor := &fc.cursorGroup[idx]
if cursor == fc.current {
sb.WriteString(">")
} else {
sb.WriteString(" ")
}
sb.WriteString(cursor.key.String())
curEIDStr := cursor.GetCurEntryID().DocString()
sb.WriteString(fmt.Sprintf(",idx:%02d,EID:%s\n", cursor.cursor, curEIDStr))
}
}
// DumpEntries in normal cases, posting-list has thousands/million ids,
// so here only dump part of (nearby) ids about current cursor
// [age,12]^<2,false>:<1,true>,<2,false><nil,nil>
func (ec *EntriesCursor) DumpEntries(sb *strings.Builder) {
sb.WriteString(ec.key.String())
sb.WriteString(fmt.Sprintf(",idx:%02d,EID:", ec.cursor))
left := ec.cursor - 2
if left < 0 {
left = 0
}
right := ec.cursor + 10
if right >= len(ec.entries) {
right = len(ec.entries)
}
if left > 0 {
sb.WriteString("...,")
}
// [left,right)
for i := left; i < right; i++ {
if i == ec.cursor {
sb.WriteString("^")
}
sb.WriteString(ec.entries[i].DocString())
if i != right-1 {
sb.WriteString(",")
}
}
if remain := len(ec.entries) - right; remain > 0 {
sb.WriteString(fmt.Sprintf("...another %d", remain))
}
}