-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathimpact.c
329 lines (281 loc) · 10.9 KB
/
impact.c
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
/* impact_search.c implements impact ordered search as proposed by Anh and
* Moffat
*
* based on code by garcias
*
*/
#include "firstinclude.h"
#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "chash.h"
#include "def.h"
#include "error.h"
#include "heap.h"
#include "impact_build.h"
#include "impact.h"
#include "index.h"
#include "_index.h"
#include "search.h"
#include "vec.h"
#include "vocab.h"
#include "index_querybuild.h"
struct disksrc {
struct search_list_src src; /* parent source structure */
struct alloc alloc; /* buffer allocation object */
char *buf; /* available buffer */
unsigned int bufsize; /* size of stuff in buffer */
unsigned int bufcap; /* capacity of buffer */
unsigned long int bufpos; /* position buffer was read from */
unsigned int size; /* size of the list */
unsigned int pos; /* position in list */
struct index *idx; /* index we're using */
int fd; /* pinned fd */
unsigned int type; /* the type of the fd */
unsigned int fileno; /* the file number of the fd */
unsigned long int offset; /* starting offset of the list */
};
struct term_data {
unsigned int impact; /* current impact */
unsigned int w_qt; /* term weight */
unsigned int blocksize; /* number of docs remaining in block */
unsigned long int docno; /* current docno */
struct vec v; /* vector of in-memory data */
struct search_list_src *src; /* list src */
};
/* internal function to compare phrase term pointers by frequency/estimated
frequency */
static int term_data_cmp(const void *one, const void *two) {
const struct term_data *tdone = one,
*tdtwo = two;
return tdtwo->impact - tdone->impact;
}
/* internal function to compare phrase term pointers by frequency/estimated
frequency */
static int f_t_cmp(const void *one, const void *two) {
const struct conjunct *done = one,
*dtwo = two;
if (done->f_t < dtwo->f_t) {
return -1;
} else if (done->f_t > dtwo->f_t) {
return 1;
} else {
return 0;
}
}
/* decode a block and add contributions into accumulators */
static void impact_decode_block(struct chash *accs, struct term_data *term,
unsigned int blockfine) {
unsigned int contrib = term->impact - blockfine;
unsigned long int docno_d;
int ret;
while (term->blocksize && vec_vbyte_read(&term->v, &docno_d)) {
unsigned long int *fw;
int found;
term->docno += docno_d + 1;
/* should never fail */
ret = chash_luint_luint_find_insert(accs, term->docno, &fw, 0, &found);
assert(ret == CHASH_OK);
*fw += contrib;
term->blocksize--;
}
}
/* decode a block and add contributions into accumulators, but don't create new
* accumulators */
static void impact_decode_block_and(struct chash *accs, struct term_data *term,
unsigned int blockfine) {
unsigned int contrib = term->impact - blockfine;
unsigned long int docno_d;
while (term->blocksize && vec_vbyte_read(&term->v, &docno_d)) {
unsigned long int *fw;
term->docno += docno_d + 1;
if (chash_luint_luint_find(accs, term->docno, &fw) == CHASH_OK) {
*fw += contrib;
}
term->blocksize--;
}
}
static void source_delete(struct term_data *term, unsigned int terms) {
unsigned int i;
for (i = 0; i < terms; i++) {
if (term[i].src) {
term[i].src->delet(term[i].src);
term[i].src = NULL;
}
}
}
enum search_ret impact_ord_eval(struct index *idx, struct query *query,
struct chash *accumulators, unsigned int acc_limit, struct alloc *alloc,
unsigned int mem) {
double norm_B;
unsigned int i,
terms = 0,
blockfine,
blocks_read,
postings_read = 0,
postings = 0,
bytes = 0,
bytes_read = 0;
struct term_data *term,
*largest;
struct disksrc *dsrc;
if (query->terms == 0) {
/* no terms to process */
return SEARCH_OK;
/* allocate space for array */
} else if (!(term = malloc(sizeof(*term) * query->terms))) {
return SEARCH_ENOMEM;
}
/* sort by selectivity (by inverse t_f) */
qsort(query->term, query->terms, sizeof(*query->term), f_t_cmp);
norm_B = pow(idx->impact_stats.w_qt_max / idx->impact_stats.w_qt_min,
idx->impact_stats.w_qt_min
/ (idx->impact_stats.w_qt_max - idx->impact_stats.w_qt_min));
/* initialise data for each query term */
for (i = 0; i < query->terms; i++) {
unsigned int termfine;
double w_qt;
/* initialise src/vec for term */
term[i].v.pos = term[i].v.end = NULL;
term[i].src = NULL;
w_qt = (1 + log(query->term[i].f_qt)) *
log(1 + (idx->impact_stats.avg_f_t / query->term[i].f_t));
w_qt = impact_normalise(w_qt, norm_B,
idx->impact_stats.slope, idx->impact_stats.w_qt_max,
idx->impact_stats.w_qt_min);
term[i].w_qt = impact_quantise(w_qt,
idx->impact_stats.quant_bits, idx->impact_stats.w_qt_max,
idx->impact_stats.w_qt_min);
/* apply term fine to term impact */
termfine = (i < 2) ? 0 : i - 2;
if (termfine < term[i].w_qt) {
term[i].w_qt -= termfine;
/* initialise to highest impact, so we'll select and initialise this
* term before real processing */
term[i].impact = INT_MAX;
terms++;
} else {
/* we won't use this term */
term[i].w_qt = 0;
term[i].impact = 0;
}
term[i].blocksize = 0;
/* XXX */
postings += query->term[i].f_t;
bytes += query->term[i].term.vocab.size;
}
/* get sources for each term (do this in a seperate loop so we've already
* excluded lists that we won't use) */
for (i = 0; i < terms; i++) {
unsigned int memsize = mem / (terms - i);
if (memsize > query->term[i].term.vocab.size) {
memsize = query->term[i].term.vocab.size;
}
if (!(term[i].src
= search_term_src(idx, &query->term[i].term, alloc, memsize))) {
source_delete(term, terms);
free(term);
return SEARCH_EINVAL;
}
mem -= memsize;
}
blockfine = blocks_read = 0;
heap_heapify(term, terms, sizeof(*term), term_data_cmp);
do {
largest = heap_pop(term, &terms, sizeof(*term), term_data_cmp);
if (largest && (largest->impact > blockfine)) {
postings_read += largest->blocksize;
if (chash_size(accumulators) < acc_limit) {
/* reserve enough memory for accumulators and decode */
if (chash_reserve(accumulators, largest->blocksize)
>= largest->blocksize) {
impact_decode_block(accumulators, largest, blockfine);
} else {
assert(!CRASH); ERROR("impact_ord_eval()");
source_delete(term, terms);
free(term);
return SEARCH_EINVAL;
}
} else {
impact_decode_block_and(accumulators, largest, blockfine);
}
if (VEC_LEN(&largest->v) < 2 * VEC_VBYTE_MAX) {
/* need to read more data */
unsigned int bytes;
enum search_ret sret;
if ((sret
= largest->src->readlist(largest->src, VEC_LEN(&largest->v),
(void **) &largest->v.pos, &bytes)) == SEARCH_OK) {
/* read succeeded */
largest->v.end = largest->v.pos + bytes;
} else if (sret == SEARCH_FINISH) {
if (VEC_LEN(&largest->v) || largest->blocksize) {
/* didn't finish properly */
assert(!CRASH); ERROR("impact_ord_eval()");
source_delete(term, terms);
free(term);
return SEARCH_EINVAL;
}
/* otherwise it will be finished below */
} else {
assert(!CRASH); ERROR("impact_ord_eval()");
source_delete(term, terms);
free(term);
return sret;
}
}
if (!largest->blocksize) {
/* need to read the start of the next block */
unsigned long int tmp_bsize,
tmp_impact;
if (vec_vbyte_read(&largest->v, &tmp_bsize)
&& (vec_vbyte_read(&largest->v, &tmp_impact)
/* second read failed, rewind past first vbyte */
|| ((largest->v.pos -= vec_vbyte_len(tmp_bsize)), 0))) {
blocks_read++;
if (blocks_read > terms) {
blockfine++;
}
largest->blocksize = tmp_bsize;
largest->impact = (tmp_impact + 1) * largest->w_qt;
largest->docno = -1;
heap_insert(term, &terms, sizeof(*term), term_data_cmp,
largest);
} else if (!VEC_LEN(&largest->v)) {
/* finished, don't put back on the heap */
dsrc = (void *) largest->src; bytes_read += dsrc->pos;
largest->src->delet(largest->src);
largest->src = NULL;
} else if (largest->impact != INT_MAX) {
/* ensure that this vector is chosen next, as we need the
* next impact score */
largest->impact = INT_MAX;
assert(largest->blocksize == 0);
heap_insert(term, &terms, sizeof(*term), term_data_cmp,
largest);
} else {
/* huh? */
assert(!CRASH); ERROR("impact_ord_eval()");
source_delete(term, terms);
free(term);
return SEARCH_EINVAL;
}
} else {
heap_insert(term, &terms, sizeof(*term), term_data_cmp,
largest);
}
}
} while (largest && (largest->impact > blockfine));
for (i = 0; i < terms; i++) {
dsrc = (void *) term[i].src; bytes_read += dsrc->pos;
}
if (largest) {
largest->src->delet(largest->src);
largest->src = NULL;
}
/* end of ranking */
source_delete(term, terms);
free(term);
return SEARCH_OK;
}