-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtrindex.go
325 lines (263 loc) · 6.55 KB
/
trindex.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
package trindex
// TODO:
// - Support both case-sensitive and case-insensitive
// - Support preprocessing of inputs (like removing special chars, renaming umlauts (ö -> oe), etc.)
// - 2 storage engines (provide an Sample() method to detect whether to use the short or the large storage
// [large storage will be used when avg(size(sample)) >= 500 bytes and uses a bitmap; short storage uses list of 8 bytes
// integer items]
import (
"bufio"
"container/heap"
"encoding/binary"
"fmt"
"os"
"sync"
"sync/atomic"
//"time"
)
const (
writeCacheSize = 250000
cacheDocIDSize = 1e7
cacheSize = 1024 * 1024 * 700 // 512 MiB, given in byte
//sampleTresholdCount = 50
//sampleTresholdLargeStorage = 500 // in bytes, we change to a bitmap storage for large files (= huge amount of trigrams)
)
type storageType int
const (
storageShort storageType = iota
storageLong
)
type Index struct {
filename string
sample_count int
sample_size int
item_id uint64
item_db_lock sync.Mutex
item_db *os.File
cache map[uint64]uint32
write_cache []uint64
storageEngine storageType
storage IStorage
}
type Result struct {
index int
ID uint64
Similarity float64
}
type ResultSet []*Result
func (r *Result) String() string {
return fmt.Sprintf("<Result ID=%d Similarity=%.2f>", r.ID, r.Similarity)
}
/*
func (r *Result) Less(other llrb.Item) bool {
return r.Similarity < other.(*Result).Similarity
}
*/
func (rs ResultSet) Less(i, j int) bool {
return rs[i].Similarity > rs[j].Similarity
}
func (rs ResultSet) Len() int {
return len(rs)
}
func (rs ResultSet) Swap(i, j int) {
rs[i], rs[j] = rs[j], rs[i]
rs[i].index = i
rs[j].index = j
}
func (rs *ResultSet) Push(x interface{}) {
n := len(*rs)
item := x.(*Result)
item.index = n
*rs = append(*rs, item)
}
func (rs *ResultSet) Pop() interface{} {
old := *rs
n := len(old)
item := old[n-1]
item.index = -1
*rs = old[0 : n-1]
return item
}
func NewIndex(filename string) *Index {
idx := &Index{
filename: filename,
storage: newListStorage(filename),
cache: make(map[uint64]uint32),
write_cache: make([]uint64, 0, writeCacheSize),
}
fd, err := os.OpenFile(fmt.Sprintf("%s.docs", filename), os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
panic(err)
}
idx.item_db = fd
offset, err := fd.Seek(0, 2)
if err != nil {
panic(err)
}
if offset == 0 {
// Write init item
err = binary.Write(fd, binary.LittleEndian, uint64(0))
if err != nil {
panic(err)
}
} else {
// Read last item id
_, err = fd.Seek(0, 0)
if err != nil {
panic(err)
}
err = binary.Read(fd, binary.LittleEndian, &idx.item_id)
if err != nil {
panic(err)
}
// For the time being, read min(item_id, 5000000) IDs into cache.
// We could skip these step but have it in the library for performance reasons.
r := bufio.NewReader(fd)
var tmp_id uint32
for i := uint64(0); i < uint64(min(int(idx.item_id), 5000000)); i++ {
err = binary.Read(r, binary.LittleEndian, &tmp_id)
if err != nil {
panic(err)
}
idx.cache[uint64(i+1)] = tmp_id
}
}
return idx
}
// It's important to close the index to flush all writes to disk.
func (idx *Index) Close() {
idx.storage.Close()
idx.item_db_lock.Lock()
_, err := idx.item_db.Seek(0, 0)
if err != nil {
panic(err)
}
err = binary.Write(idx.item_db, binary.LittleEndian, idx.item_id)
if err != nil {
panic(err)
}
idx.item_db_lock.Unlock()
idx.flushWriteCache()
if err = idx.item_db.Close(); err != nil {
panic(err)
}
}
// Inserts a document to the index. It is safe for concurrent use.
func (idx *Index) Insert(data string) uint64 {
new_doc_id := atomic.AddUint64(&idx.item_id, 1)
trigrams := trigramize(data)
for _, trigram := range trigrams {
idx.storage.AddItem(trigram, new_doc_id)
}
if len(idx.cache) > cacheDocIDSize {
counter := 0
treshold := int(cacheDocIDSize / 4)
// Flush anything to disk before we delete something from cache
idx.flushWriteCache()
idx.item_db_lock.Lock()
for doc_id, _ := range idx.cache {
counter++
delete(idx.cache, doc_id)
if counter >= treshold {
break
}
}
idx.item_db_lock.Unlock()
}
if len(idx.write_cache) >= writeCacheSize {
idx.flushWriteCache()
}
idx.item_db_lock.Lock()
defer idx.item_db_lock.Unlock()
idx.cache[new_doc_id] = uint32(len(trigrams))
idx.write_cache = append(idx.write_cache, new_doc_id)
return new_doc_id
}
func (idx *Index) getTotalTrigrams(doc_id uint64) int {
idx.item_db_lock.Lock()
defer idx.item_db_lock.Unlock()
count, has := idx.cache[doc_id]
if has {
return int(count)
}
if doc_id <= 0 {
panic("doc_id <= 0 not available")
}
_, err := idx.item_db.Seek(int64(8+(doc_id-1)*4), 0)
if err != nil {
panic(err)
}
var rtv uint32
err = binary.Read(idx.item_db, binary.LittleEndian, &rtv)
if err != nil {
panic(err)
}
idx.cache[doc_id] = rtv
// TODO: Put an cache invalidator here
return int(rtv)
}
func (idx *Index) flushWriteCache() {
idx.item_db_lock.Lock()
defer idx.item_db_lock.Unlock()
// write_cache
for _, doc_id := range idx.write_cache {
_, err := idx.item_db.Seek(int64(8+(doc_id-1)*4), 0)
if err != nil {
panic(err)
}
err = binary.Write(idx.item_db, binary.LittleEndian, idx.cache[doc_id])
if err != nil {
panic(err)
}
}
idx.write_cache = make([]uint64, 0, writeCacheSize)
}
func (idx *Index) Query(query string, max_results int, skip float64) ResultSet {
trigrams := trigramize(query)
trigrams_len := float64(len(trigrams))
//stime := time.Now()
temp_map := make(map[uint64]int)
for _, trigram := range trigrams {
doc_ids := idx.storage.GetItems(trigram)
for _, id := range doc_ids {
c, has := temp_map[id]
if has {
temp_map[id] = c + 1
} else {
temp_map[id] = 1
}
}
}
/*
etime := time.Now().Sub(stime)
fmt.Printf("[%s] Time to get all document IDs per trigram took: %s\n", query, etime)
stime = time.Now()
*/
heap_list := make(ResultSet, 0, max_results+1)
heap.Init(&heap_list)
for id, count := range temp_map {
x := trigrams_len / float64(idx.getTotalTrigrams(id))
if x > 1 {
x = 1.0 / x
}
s := (float64(count) / trigrams_len) - (1.0 - x)
if s < skip {
continue
}
heap.Push(&heap_list, &Result{
ID: id,
Similarity: s,
})
}
//etime = time.Now().Sub(stime)
//fmt.Printf("[%s] Time to calculate top X took: %s\n", query, etime)
if len(heap_list) > 0 {
item_count := min(len(heap_list), max_results)
result_set := make(ResultSet, 0, item_count)
for i := 0; i < item_count; i++ {
result_set = append(result_set, heap.Pop(&heap_list).(*Result))
}
return result_set
}
return nil
}