/
btreeindex.go
262 lines (228 loc) · 6.33 KB
/
btreeindex.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
//
// btree inverted index's data structure
//
// |<-------------btree------------>| <--posting list-->|
// |<-intermediate->|<--leaf node-->|
// - --- --- --- --- --- ---
// | | - | | | - | | | | |
// - --- --- --- --- --- ---
// /
// - - --- --- --- --- --- ---
// | | - | | - | | | - | | | | |
// - - --- --- --- --- --- ---
// /
// - - - --- --- --- --- --- ---
// | | - | | - | | - | | | - | | | | |
// - - - --- --- --- --- --- ---
// \
// - - --- --- --- --- --- ---
// | | - | | - | | | - | | | | |
// - - --- --- --- --- --- ---
// |<--in memory--> | <-----------on disk-------------->|
//
//
package index
import (
"bytes"
"encoding/binary"
"io"
"os"
"sort"
"unsafe"
"github.com/awesomefly/easysearch/util"
btree "github.com/awesomefly/gobtree"
)
var DefaultConfig = btree.Config{
IndexConfig: btree.IndexConfig{
Sectorsize: 512,
Flistsize: 1000 * btree.OFFSET_SIZE,
Blocksize: 512,
},
Maxlevel: 4,
RebalanceThrs: 30,
AppendRatio: 0.7,
DrainRate: 100,
MaxLeafCache: 0, // intermediate node in memory and leaf node in disk
Sync: false,
Nocache: false,
}
type BTreeIndex struct {
//skip-list vs btree:
//https://stackoverflow.com/questions/256511/skip-list-vs-binary-search-tree/28270537#28270537
BT *btree.BTree
IndexFile string
property Property
}
func NewBTreeIndex(file string) *BTreeIndex {
conf := DefaultConfig
conf.Idxfile, conf.Kvfile = file+".idx", file+".kv"
bt := BTreeIndex{
IndexFile: file,
BT: btree.NewBTree(btree.NewStore(conf)), // todo: 索引文件太大,索引压缩、posting list压缩
property: Property{
docNum: 0,
tokenCount: 0,
dataRange: DataRange{Start: 0, End: 0},
},
}
bt.Load()
return &bt
}
func (bt *BTreeIndex) Save() {
file := bt.IndexFile + ".sum"
os.Create(file)
// Index store
fd, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE, 0660)
if err != nil {
panic(err.Error())
}
buffer := bytes.NewBuffer([]byte{})
if err := binary.Write(buffer, binary.LittleEndian, int32(bt.property.docNum)); err != nil {
panic(err)
}
if err := binary.Write(buffer, binary.LittleEndian, int32(bt.property.tokenCount)); err != nil {
panic(err)
}
if err := binary.Write(buffer, binary.LittleEndian, int32(bt.property.dataRange.Start)); err != nil {
panic(err)
}
if err := binary.Write(buffer, binary.LittleEndian, int32(bt.property.dataRange.End)); err != nil {
panic(err)
}
if _, err := fd.Write(buffer.Bytes()); err != nil {
panic(err)
}
fd.Close()
}
func (bt *BTreeIndex) Load() {
// Index store
file := bt.IndexFile + ".sum"
fd, err := os.OpenFile(file, os.O_RDONLY|os.O_CREATE, 0660)
if err != nil {
panic(err.Error())
}
data := make([]byte, unsafe.Sizeof(bt.property.docNum)+unsafe.Sizeof(bt.property.tokenCount))
if n, err := fd.Read(data); err != nil {
if n == 0 && err == io.EOF {
return
}
panic(err.Error())
}
buffer := bytes.NewBuffer(data)
if err := binary.Read(buffer, binary.LittleEndian, (*int32)(unsafe.Pointer(&bt.property.docNum))); err != nil {
panic(err.Error())
}
if err := binary.Read(buffer, binary.LittleEndian, (*int32)(unsafe.Pointer(&bt.property.tokenCount))); err != nil {
panic(err.Error())
}
if err := binary.Read(buffer, binary.LittleEndian, (*int32)(unsafe.Pointer(&bt.property.dataRange.Start))); err != nil {
panic(err.Error())
}
if err := binary.Read(buffer, binary.LittleEndian, (*int32)(unsafe.Pointer(&bt.property.dataRange.End))); err != nil {
panic(err.Error())
}
fd.Close()
}
func (bt *BTreeIndex) Close() {
bt.BT.Drain()
bt.BT.Close()
bt.Save()
}
func (bt *BTreeIndex) Clear() {
bt.Close()
// delete deprecated index
os.Remove(bt.IndexFile + ".sum")
os.Remove(bt.IndexFile + ".idx")
os.Remove(bt.IndexFile + ".kv")
}
func (bt *BTreeIndex) Keys() []string {
keys := make(sort.StringSlice, bt.Property().tokenCount)
ch := bt.BT.KeySet()
for {
key := <-ch
if key == nil {
break
}
keys = append(keys, string(key))
}
return keys
}
func (bt *BTreeIndex) Lookup(token string, dirty bool) PostingList {
key := &btree.TestKey{K: token}
var ch chan []byte
if dirty {
ch = bt.BT.LookupDirty(key)
} else {
ch = bt.BT.Lookup(key)
}
values := make([][]byte, 0)
for {
x := <-ch
if x == nil {
break
}
values = append(values, x)
}
if len(values) == 0 {
return nil
}
var p PostingList
p.FromBytes(values[0])
return p
}
// Add 该方法比较低效,批量插入文档会在posting list后不段追加新文档,但postinglist并未预留空间,
// 因此需要移动到新的空间,导致文件数据拷贝
func (bt *BTreeIndex) Add(docs []Document) {
for _, doc := range docs {
tokens := util.Analyze(doc.Text)
for _, token := range tokens {
//log.Printf("token:%s", token)
key := &btree.TestKey{K: token}
postingList := bt.Lookup(token, true)
if postingList != nil {
if last := postingList.Find(doc.ID); last != nil {
// Don't add same ID twice. But should update frequency
last.TF++
last.QualityScore = CalDocScore(last.TF, 0)
bt.BT.Insert(key, postingList)
continue
}
}
item := Doc{
ID: int32(doc.ID),
DocLen: int32(len(tokens)),
TF: 1,
QualityScore: CalDocScore(1, 0),
}
//add to posting list & sort by score
postingList = append(postingList, item)
sort.Slice(postingList, func(i, j int) bool {
return postingList[i].QualityScore > postingList[j].QualityScore
})
bt.BT.Insert(key, postingList)
}
bt.property.docNum++
bt.property.tokenCount += len(tokens)
}
bt.BT.Drain()
}
func (bt *BTreeIndex) Insert(key string, pl PostingList) {
bt.BT.Insert(&btree.TestKey{K: key}, pl)
bt.property.docNum += pl.Len()
bt.property.tokenCount++
}
func (bt *BTreeIndex) Get(term string) []Doc {
if postingList := bt.Lookup(term, false); postingList != nil {
return postingList
}
return nil
}
func (bt *BTreeIndex) Property() *Property {
return &bt.property
}
func (bt *BTreeIndex) SetProperty(p Property) {
bt.property = p
}
func (bt *BTreeIndex) Retrieval(must []string, should []string, not []string, k int, r int, m SearchModel) []Doc {
return DoRetrieval(bt, must, should, not, k, r, m)
}