-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemIndexer.go
479 lines (435 loc) · 11.4 KB
/
MemIndexer.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// MemIndexer
package GridSearch
import (
"fmt"
"runtime"
"sync"
"time"
)
type memIndexer struct {
memIXData []map[int32][]int32 //Memory index data structure for real-time search
curMem int //0,1 The current map for memory index
counter int //Memory data counter
topGridID int32 //top grid id
dataFlow chan mData //data flow
done chan int //Write files have completed
swap bool //can swap the mem map
nextSeg *MinHeap //Available segment
mergingChan chan bool //Combined index signal channel
/*
The maximum number of index files,
as more and more documents,
this value also increases
*/
segmentUpper int32
firstIXs map[int32][]int32 //first indices
segment struct {
sync.RWMutex
attr map[int32]*SegmentAttr //Segment attributes
}
}
func NewMemIndexer(tid int32) *memIndexer {
mi := memIndexer{
memIXData: make([]map[int32][]int32, 2, 2),
firstIXs: make(map[int32][]int32),
counter: 0,
curMem: 0,
topGridID: tid,
dataFlow: make(chan mData),
done: make(chan int),
swap: true,
segmentUpper: 0,
nextSeg: NewMinHeap(10),
mergingChan: make(chan bool),
}
mi.segment.attr = make(map[int32]*SegmentAttr)
mi.memIXData[0] = make(map[int32][]int32)
mi.memIXData[1] = make(map[int32][]int32)
if FIRSTINDEXMODE == FIRSTINDEXMEM {
mi.loadFirstIXs()
}
mi.loadSnapshot()
go mi.addData()
go mi.process()
return &mi
}
func (mi *memIndexer) loadFirstIXs() bool {
tPath := path(mi.topGridID)
fPath := fmt.Sprintf("%sfir", tPath)
exist := isFileExist(fPath)
if !exist {
print(fPath, "does not exist.")
return true
}
fIXsInt32s, ok := readFile(fPath)
if !ok {
return false
}
t := int32(len(fIXsInt32s)) / (BOTTOMGRIDNUM + 1)
var offset, i int32 = 0, 0
for i = 0; i < t; i++ {
offset = i * (BOTTOMGRIDNUM + 1)
mi.firstIXs[fIXsInt32s[offset]] = fIXsInt32s[1+offset : BOTTOMGRIDNUM+1+offset]
}
return true
}
/*
Persistence save first index
store the first index:
when searching, the first index readed in memory,
so first index File mirroring only need to synchronize
with the second indexes.
*/
func (mi *memIndexer) storeFirstIXs() bool {
tPath := path(mi.topGridID)
buf := []byte{}
for k, v := range mi.firstIXs {
buf = append(buf, Int32ToBytes(k)...)
buf = append(buf, i32ToB(v)...)
}
fPath := fmt.Sprintf("%sfir", tPath)
rmFile(fPath)
writeBufToFile(fPath, buf)
return true
}
func (mi *memIndexer) loadSnapshot() bool {
tPath := path(mi.topGridID)
path := fmt.Sprintf("%ssnapshot", tPath)
exist := isFileExist(path)
if !exist {
print(path, "does not exist.")
return true
}
segAttr, ok := readFile(path)
if !ok {
return false
}
alen := segAttr[0]
var i int32 = 0
for i = 0; i < alen; i++ {
mi.segment.attr[segAttr[i*2+1]] = &SegmentAttr{
size: segAttr[i*2+2],
merging: false,
}
}
mhLen := segAttr[alen+1]
for i = 0; i < mhLen; i++ {
mi.nextSeg.push(segAttr[alen+2+i])
}
mi.segmentUpper = segAttr[alen+1+mhLen+1]
return true
}
func (mi *memIndexer) storeSnapshot() {
tPath := path(mi.topGridID)
//*********SEGMENT*********************
buf := []byte{0}
for k, v := range mi.segment.attr {
buf = append(buf, Int32ToBytes(k)...)
buf = append(buf, Int32ToBytes(v.size)...)
}
alen := int32(len(buf))/4 - 1
buf = append(buf, Int32ToBytes(alen)...)
//*****MINHEAP***********************
buf = append(buf, Int32ToBytes(int32(mi.nextSeg.mhLen))...)
for i := 0; i < mi.nextSeg.mhLen; i++ {
buf = append(buf, Int32ToBytes(mi.nextSeg.mhA[i])...)
}
//*****segmentUpper***********************
buf = append(buf, Int32ToBytes(mi.segmentUpper)...)
path := fmt.Sprintf("%ssnapshot", tPath)
rmFile(path)
writeBufToFile(path, buf)
}
func (mi *memIndexer) close() {
<-time.After(time.Second * 1)
if FIRSTINDEXMODE == FIRSTINDEXMEM {
mi.storeFirstIXs()
}
mi.storeMem(mi.nextSegment(true))
mi.storeSnapshot()
}
func (mi *memIndexer) addData() {
for {
select {
case md := <-mi.dataFlow:
mi.memIXData[mi.curMem][md.bid] = append(mi.memIXData[mi.curMem][md.bid], md.id)
mi.counter++
if mi.counter >= DOCMERGENUM && mi.swap {
mi.swap = false
go mi.mem2File(mi.curMem, mi.nextSegment(false))
mi.curMem = (mi.curMem + 1) % 2
mi.counter = 0
}
case <-mi.done:
mi.swap = true
}
}
}
func (mi *memIndexer) process() {
for {
select {
case <-mi.mergingChan:
go func() {
seg1, seg2, ok := mi.merge2Segs()
if ok {
mi.ixMerge(seg1, seg2, mi.nextSegment(true))
}
}()
}
}
}
func (mi *memIndexer) merge2Segs() (seg1, seg2 int32, ok bool) {
mi.segment.Lock()
defer mi.segment.Unlock()
ok = false
tSegs := sortedKeys(mi.segment.attr)
segs := make([]int32, 2)
i := 0
for _, k := range tSegs {
if !mi.segment.attr[k].merging {
segs[i] = k
i++
if i >= 2 {
break
}
}
}
if i <= 1 {
seg1 = -1
seg2 = -1
return
}
seg1 = segs[0]
seg2 = segs[1]
if mi.segment.attr[seg1].size >= 134217728 {
return
}
mi.segment.attr[seg1].merging = true
mi.segment.attr[seg2].merging = true
ok = true
runtime.Gosched()
return
}
/*
Can not be blocked.
*/
func (mi *memIndexer) nextSegment(merging bool) (ns int32) {
qlen := int32(mi.nextSeg.len())
if qlen <= mi.segmentUpper/3 && mi.segmentUpper >= 2 && !merging {
mi.mergingChan <- true
}
if qlen <= 0 {
mi.segmentUpper++
ns = mi.segmentUpper
} else {
ns = mi.nextSeg.pop()
}
return
}
func (mi *memIndexer) searchInMem(bids []int32) []int32 {
mRes := []int32{}
blen := len(bids) / 2
for i := 0; i < blen; i++ {
var k, s, e int32 = 0, bids[i*2], bids[i*2+1]
for k = s; k <= e; k++ {
ids, ok := mi.memIXData[0][k]
if ok {
mRes = append(mRes, MergeSort(ids)...)
}
ids, ok = mi.memIXData[1][k]
if ok {
mRes = append(mRes, MergeSort(ids)...)
}
}
}
return mRes
}
func (mi *memIndexer) searchInSegments(bids []int32) []int32 {
mRes := []int32{}
sa := []int32{}
mi.segment.Lock()
defer mi.segment.Unlock()
for k, _ := range mi.segment.attr {
sa = append(sa, k)
}
runtime.Gosched()
for _, k := range sa {
mRes = append(mRes, mi.searchInSeg(k, bids)...)
}
return mRes
}
func (mi *memIndexer) searchInSeg(seg int32, bids []int32) []int32 {
tPath := path(mi.topGridID)
sPath := fmt.Sprintf("%s%d.sec", tPath, seg)
fIXs, ok := []int32{}, false
if FIRSTINDEXMODE == FIRSTINDEXFILE {
fPath := fmt.Sprintf("%s%d.fir", tPath, seg)
fIXs, ok = readFile(fPath)
} else {
fIXs, ok = mi.firstIXs[seg]
}
if !ok {
return []int32{}
}
sIXs, ok := readFile(sPath)
if !ok {
return []int32{}
}
mRes := []int32{}
blen := len(bids) / 2
for i := 0; i < blen; i++ {
mRes = append(mRes, mi.getResults(fIXs, sIXs, bids[i*2]-BOTTOMFIRSTGRIDID, bids[i*2+1]-BOTTOMFIRSTGRIDID)...)
}
return mRes
}
func (mi *memIndexer) getResults(fIXs, sIXs []int32, sbid, ebid int32) []int32 {
resIDs := []int32{}
if ebid == BOTTOMGRIDNUM-1 {
resIDs = append(resIDs, sIXs[fIXs[sbid]:]...)
} else {
resIDs = append(resIDs, sIXs[fIXs[sbid]:fIXs[ebid+1]]...)
}
return resIDs
}
func (mi *memIndexer) rmIXFile(seg int32) {
tPath := path(mi.topGridID)
fPath := fmt.Sprintf("%s%d.fir", tPath, seg)
sPath := fmt.Sprintf("%s%d.sec", tPath, seg)
rmFile(fPath)
rmFile(sPath)
}
func (mi *memIndexer) storeMem(seg int32) {
bufLen := BOTTOMGRIDNUM
firstIXs := make([]int32, bufLen, bufLen)
secondIXs := []int32{}
var offset int32 = 0
var i int32 = 0
for i = 0; i < bufLen; i++ {
firstIXs[i] = offset
ids, ok := mi.memIXData[0][i+BOTTOMFIRSTGRIDID]
if ok {
ids = MergeSort(ids)
secondIXs = append(secondIXs, ids...)
offset += int32(len(ids))
}
ids, ok = mi.memIXData[1][i+BOTTOMFIRSTGRIDID]
if ok {
ids = MergeSort(ids)
secondIXs = append(secondIXs, ids...)
offset += int32(len(ids))
}
}
tPath := path(mi.topGridID)
sPath := fmt.Sprintf("%s%d.sec", tPath, seg)
writeBufToFile(sPath, i32ToB(secondIXs))
fPath := fmt.Sprintf("%s%d.fir", tPath, seg)
writeBufToFile(fPath, i32ToB(firstIXs))
mi.segment.Lock()
defer mi.segment.Unlock()
mi.segment.attr[seg] = &SegmentAttr{
offset,
false,
}
runtime.Gosched()
}
/*
The persistence of memory index to a file.
empty memory after Persisted storage completed,
or can not to be a real-time search, will lose data.
*/
func (mi *memIndexer) mem2File(curMem int, seg int32) {
bufLen := BOTTOMGRIDNUM
firstIXs := make([]int32, bufLen, bufLen)
secondIXs := []int32{}
var offset int32 = 0
var i int32 = 0
for i = 0; i < bufLen; i++ {
firstIXs[i] = offset
ids, ok := mi.memIXData[curMem][i+BOTTOMFIRSTGRIDID]
if ok {
ids = MergeSort(ids)
secondIXs = append(secondIXs, ids...)
offset += int32(len(ids))
}
mi.memIXData[curMem][i+BOTTOMFIRSTGRIDID] = []int32{}
}
tPath := path(mi.topGridID)
sPath := fmt.Sprintf("%s%d.sec", tPath, seg)
writeBufToFile(sPath, i32ToB(secondIXs))
if FIRSTINDEXMODE == FIRSTINDEXFILE {
fPath := fmt.Sprintf("%s%d.fir", tPath, seg)
writeBufToFile(fPath, i32ToB(firstIXs))
} else {
mi.firstIXs[seg] = firstIXs
go mi.storeFirstIXs()
}
mi.segment.Lock()
defer mi.segment.Unlock()
mi.segment.attr[seg] = &SegmentAttr{
offset,
false,
}
runtime.Gosched()
mi.done <- curMem
}
/*
index merge.
*/
func (mi *memIndexer) ixMerge(seg1, seg2 int32, seg int32) {
tPath := path(mi.topGridID)
sPath1 := fmt.Sprintf("%s%d.sec", tPath, seg1)
sIXs1, _ := readFile(sPath1)
sPath2 := fmt.Sprintf("%s%d.sec", tPath, seg2)
sIXs2, _ := readFile(sPath2)
fIXs1, fIXs2 := []int32{}, []int32{}
if FIRSTINDEXMODE == FIRSTINDEXFILE {
fPath1 := fmt.Sprintf("%s%d.fir", tPath, seg1)
fIXs1, _ = readFile(fPath1)
fPath2 := fmt.Sprintf("%s%d.fir", tPath, seg2)
fIXs2, _ = readFile(fPath2)
} else {
fIXs1 = mi.firstIXs[seg1]
fIXs2 = mi.firstIXs[seg2]
}
bufLen := BOTTOMGRIDNUM
firstIXs := make([]int32, bufLen, bufLen)
secondIXs := []int32{}
var i, mergeLen int32 = 0, 0
for i = 0; i < bufLen-1; i++ {
firstIXs[i] = mergeLen
mergeSIX := Merge(sIXs1[fIXs1[i]:fIXs1[i+1]], sIXs2[fIXs2[i]:fIXs2[i+1]])
secondIXs = append(secondIXs, mergeSIX...)
mergeLen += int32(len(mergeSIX))
}
firstIXs[i] = mergeLen
mergeSIX := Merge(sIXs1[fIXs1[i]:], sIXs2[fIXs2[i]:])
secondIXs = append(secondIXs, mergeSIX...)
mergeLen += int32(len(mergeSIX))
sPath := fmt.Sprintf("%s%d.sec", tPath, seg)
writeBufToFile(sPath, i32ToB(secondIXs))
if FIRSTINDEXMODE == FIRSTINDEXFILE {
fPath := fmt.Sprintf("%s%d.fir", tPath, seg)
writeBufToFile(fPath, i32ToB(firstIXs))
} else {
mi.firstIXs[seg] = firstIXs
}
//*******************************************
mi.segment.Lock()
defer mi.segment.Unlock()
mi.segment.attr[seg] = &SegmentAttr{
mi.segment.attr[seg1].size + mi.segment.attr[seg2].size,
false,
}
delete(mi.segment.attr, seg1)
delete(mi.segment.attr, seg2)
if FIRSTINDEXMODE == FIRSTINDEXMEM {
delete(mi.firstIXs, seg1)
delete(mi.firstIXs, seg2)
}
//*******************************************
mi.rmIXFile(seg1)
mi.rmIXFile(seg2)
mi.nextSeg.push(seg1) //Recovery segments
mi.nextSeg.push(seg2)
runtime.Gosched()
}