forked from gigablast/open-source-search-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IndexReadInfo.cpp
344 lines (325 loc) · 12.3 KB
/
IndexReadInfo.cpp
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
#include "gb-include.h"
#include "IndexReadInfo.h"
#include "Datedb.h"
IndexReadInfo::IndexReadInfo() {
m_numLists = 0;
m_isDone = false;
}
// . initialize initial read info
// . sets m_readSizes[i] for each list
// . sets startKey/endKey for each list, too
// . startKey set passed endKey to indicate no reading
void IndexReadInfo::init ( Query *q ,
int64_t *termFreqs ,
int32_t docsWanted , char callNum ,
int32_t stage0 ,
int32_t *tierStage,
bool useDateLists , bool sortByDate ,
uint32_t date1 , uint32_t date2 ,
bool isDebug ) {
// save ptr but don't copy
m_q = q;
m_useDateLists = useDateLists;
m_sortByDate = sortByDate;
m_date1 = date1;
m_date2 = date2;
m_isDebug = isDebug;
if ( m_useDateLists ) m_ks = 16;
else m_ks = 12;
m_hks = m_ks - 6;
// . now set m_includeList array
// . set to false if we determine termId to be ousted due to dpf
// . loop over each termId in the query
for ( int32_t i = 0 ; i < m_q->getNumTerms() ; i++ ) {
// ignore some
//m_ignore [i] = m_q->m_ignore[i];
// no need to gen keys if ignored
//if ( m_ignore[i] ) continue;
// nothing ignored initially
m_ignore[i] = false;
// make our arrays 1-1 with those in Query class, q
if ( m_useDateLists ) {
// remember, date is complemented in the key, so use
// the larger date for the startKey
*(key128_t *)&m_startKeys [i*m_ks] =
g_datedb.makeStartKey(m_q->getTermId(i),m_date2);
*(key128_t *)&m_endKeys [i*m_ks] =
g_datedb.makeEndKey (m_q->getTermId(i),m_date1);
continue;
}
*(key_t *)&m_startKeys [i*m_ks] =
g_indexdb.makeStartKey ( m_q->getTermId(i) );
*(key_t *)&m_endKeys [i*m_ks] =
g_indexdb.makeEndKey ( m_q->getTermId(i) );
}
// no negatives
for ( int32_t i = 0; i < MAX_TIERS; i++ ){
if ( tierStage[i] < 0 )
tierStage[i] = 0;
}
// -1 means to use default
if ( stage0 <= 0 ) {
// adjust for dateLists, proportionally
if ( m_useDateLists )
m_stage[0] = (tierStage[0] * (16-6)) / (12-6);
else
m_stage[0] = tierStage[0]; // STAGE0;
}
else
m_stage[0] = stage0 * m_hks + 6;
// for all the other stages just get the same tier size
for ( int32_t i = 1; i < MAX_TIERS; i++ ){
// adjust for dateLists, proportionally
if ( m_useDateLists )
m_stage[i] = (tierStage[i] * (16-6)) / (12-6);
else
m_stage[i] = tierStage[i];
}
// set # of lists
m_numLists = m_q->getNumTerms();
// we're not done yet, we haven't even begun
m_isDone = false;
// . how many docs do we need to read to get docsWanted hits?
// . HITS = (X2 * ... * XN) / T^N
// . where Xi is docs read from each list
// . T is the total # of docs in the index
// . this assumes no dependence between the words
// . So let's just start off reading 10,000, then 30k more then 60k
// . So we break up our 100k truncation limit that way
int32_t toRead = m_stage[(int)callNum];
int64_t def = getStage0Default() ;
int64_t *tf = termFreqs ;
// . ...but if we're only reading 1 list...
// . keys are 6 bytes each, first key is 12 bytes
// . this made our result count inaccurate
// . since we had to round up to half a PAGE_SIZE
// (#defined to be 16k in RdbMap.h) we would never estimate at lower
// than about 4,000 docids for one-word queries
// . so, since we're going to read at least a PAGE_SIZE anyway,
// removing this should not slow us down!!
// . actually, should speed us up if all the guys site cluster which
// is especially probable for rare terms --- all from the same site
// . SECONDLY, now i use Msg39::getStageNum() to do prettier clustering
// and that requires us to be consistent with our stages from Next
// 10 to Next 10
//if ( m_q->getNumTerms() <= 1 ) toRead = docsWanted * 6 + 6;
// now loop through all non-ignored lists
for ( int32_t i = 0 ; i < m_numLists ; i++ ) {
// ignore lists that should be
if ( m_ignore[i] ) { m_readSizes[i]=0; continue; }
// don't include excluded lists in this calculation
if ( m_q->m_qterms[i].m_termSign == '-' )
m_readSizes[i] = m_stage[MAX_TIERS - 1] ; // STAGESUM;
else if ( m_q->m_qterms[i].m_underNOT )
m_readSizes[i] = m_stage[MAX_TIERS - 1] ; // STAGESUM;
else if ( m_q->m_qterms[i].m_piped )
m_readSizes[i] = m_stage[MAX_TIERS - 1] ; // STAGESUM;
//m_readSizes[i] = g_indexdb.getTruncationLimit() *6+6;
// m_readSizes[i] = g_indexdb.getTruncationLimit()*6 ;
// . this is set to max if we got more than 1 ignored list
// . later we will use dynamic truncation
/*else if (useNewTierSizing && m_q->m_termFreqs[i] > tierStage2)
m_readSizes[i] = tierStage2;
else if (useNewTierSizing && m_q->m_termFreqs[i] > tierStage1)
m_readSizes[i] = tierStage1;*/
else m_readSizes[i] = toRead;
// . when user specifies the s0=X cgi parm and X is like 4M
// try to avoid allocating so much space when we do not need
// . mark is using s0 to get exact hit counts
int64_t max = tf[i] * m_hks+m_hks +GB_INDEXDB_PAGE_SIZE*10 ;
if ( max < def ) max = def;
if ( m_readSizes[i] > max ) m_readSizes[i] = max;
// debug msg
if ( m_isDebug || g_conf.m_logDebugQuery )
logf ( LOG_DEBUG,"query: ReadInfo: "
"newreadSizes[%"INT32"]=%"INT32"",i,
m_readSizes[i] );
// sanity check
if ( m_readSizes[i] > ( 500 * 1024 * 1024 ) ||
m_readSizes[i] < 0 ){
log( "minRecSize = %"INT32"", m_readSizes[i] );
char *xx=NULL; *xx=0;
}
}
// return for now
return;
}
int32_t IndexReadInfo::getStage0Default ( ) { return STAGE0; }
// . updates m_readSizes
// . sets m_isDone to true if all lists are exhausted
void IndexReadInfo::update ( IndexList *lists, int32_t numLists,
char callNum ) {
// loop over all lists and update m_startKeys[i]
for ( int32_t i = 0 ; i < numLists ; i++ ) {
// ignore lists that should be
if ( m_ignore[i] ) continue;
// . how many docIds did we read into this list?
// . double the size since the lists are compress to half now
//int32_t docsRead = lists[i].getListSize() / 6 ;
// . remove the endKey put at the end by RdbList::appendLists()
// . iff we did NOT do a merge
//if ( ! didMerge && docsRead > 0 ) docsRead--;
// debug
//log("startKey for list #%"INT32" is n1=%"XINT32",n0=%"XINT64" "
// "(docsRead=%"INT32")",
// i,m_startKeys[i].n1,m_startKeys[i].n0,docsRead);
// . if we read less than supposed to, this list is exhausted
// so we set m_ignore[i] to true so we don't read again
// . we also now update termFreq to it's exact value
// . ok this condition doesn't apply now because when we
// append lists so that they are all less than a common
// endKey some lose some keys so the minRecSizes goes down
// . we should just see that if the # read is 0!
//if ( docsRead < m_docsToRead[i] ) {
if ( lists[i].getListSize() < m_readSizes[i] ) {
m_ignore [i] = true;
//m_readSizes[i] = 0;
continue;
}
// if we didn't meet our quota...
//else if ( docsRead < m_docsToRead[i] )
// m_startKeys [i] = m_endKeys [i] ;
// point to last compressed 6 byte key in list
char *list = (char *)lists[i].getList();
int32_t listSize = lists[i].getListSize();
// don't seg fault
if ( listSize < m_hks ) {
m_ignore [i] = true;
// keep the old readsize
// m_readSizes[i] = 0;
continue;
}
// we now do NOT call appendLists() again since
// we're using fast superMerges
//char *lastPart = list + listSize - 6;
char *lastPart = list + listSize - m_hks;
// . we update m_startKey to the endKey of each list
// . get the startKey now
//key_t startKey = m_startKeys[i];
char *startKey = &m_startKeys[i*m_ks];
// . load lastPart into lower 6 bytes of "startKey"
// . little endian
//gbmemcpy ( &startKey , lastPart , 6 );
gbmemcpy ( startKey , lastPart , m_hks );
// debug msg
//log("pre-startKey for list #%"INT32" is n1=%"XINT32",n0=%"XINT64"",
// i,startKey.n1,startKey.n0);
// sanity checks
//if ( startKey < m_startKeys[i] ) {
if ( KEYCMP(startKey,&m_startKeys[i*m_ks],m_ks)<0 ) {
log("query: bad startKey. "
"a.n1=%016"XINT64" a.n0=%016"XINT64" < "
"b.n1=%016"XINT64" b.n0=%016"XINT64"" ,
KEY1(startKey,m_ks),
KEY0(startKey ),
KEY1(&m_startKeys[i*m_ks],m_ks),
KEY0(&m_startKeys[i*m_ks] ));
//startKey.n1 = 0xffffffff;
//startKey.n0 = 0xffffffffffffffffLL;
}
// update startKey to read the next piece now
//m_startKeys[i] = startKey;
KEYSET(&m_startKeys[i*m_ks],startKey,m_ks);
// add 1 to startKey
//m_startKeys[i] += (uint32_t) 1;
KEYADD(&m_startKeys[i*m_ks],1,m_ks);
// debug msg
//log("NOW startKey for list #%"INT32" is n1=%"XINT32",n0=%"XINT64"",
// i,m_startKeys[i].n1,m_startKeys[i].n0);
// . increase termFreqs if we read more than was estimated
// . no! just changes # of total results when clicking Next 10
//if ( docsRead > m_q->m_termFreqs[i] )
// m_q->m_termFreqs[i] = docsRead;
}
// break if a list can read more, if it can read more, that is
int32_t i;
for ( i = 0 ; i < numLists ; i++ ) if ( ! m_ignore[i] ) break;
// if all lists are exhausted, set m_isDone
if ( i >= numLists ) { m_isDone = true; return; }
// . based on # of results we got how much more should we have to read
// to get what we want, "docsWanted"
// . just base it on linear proportion
// . keep in mind, if we double the amount to read we will quadruple
// the results if reading 2 indexLists, x8 if reading from 3.
// . that doesn't take into account phrases though...
// . let's just do it this way
// loop over all lists and update m_startKeys[i] and m_totalDocsRead
for ( int32_t i = 0 ; i < numLists ; i++ ) {
// ignore lists that should be
if ( m_ignore[i] ) continue;
// update each list's docs to read
m_readSizes[i] = m_stage[(int)callNum];
/* if ( m_readSizes[i] < m_stage[0])
m_readSizes[i] = m_stage0;
else if ( m_readSizes[i] < m_stage[1])
m_readSizes[i] = m_stage1;
else
m_readSizes[i] = m_stage2;*/
// debug msg
log("newreadSizes[%"INT32"]=%"INT32"",i,m_readSizes[i]);
}
}
// . updates m_readSizes
// . sets m_isDone to true if all lists are exhausted
// . used by virtual split in msg3b to check if we're done or not.
void IndexReadInfo::update ( int64_t *termFreqs,
int32_t numLists,
char callNum ) {
// loop over all lists and update m_startKeys[i]
for ( int32_t i = 0 ; i < numLists ; i++ ) {
// ignore lists that should be
if ( m_ignore[i] ) continue;
// . how many bytes did we read ? Since these are
// . half keys, multiply termFreqs by 6 and add 6 for the
// . first key which is full 12 bytes
int64_t listSize = termFreqs[i] * 6 + 6;
if ( listSize < m_readSizes[i] ) {
m_ignore [i] = true;
//m_readSizes[i] = 0;
continue;
}
// if we didn't meet our quota...
//else if ( docsRead < m_docsToRead[i] )
// m_startKeys [i] = m_endKeys [i] ;
// point to last compressed 6 byte key in list
//char *list = (char *)lists[i].getList();
// don't seg fault
if ( listSize < m_hks ) {
m_ignore [i] = true;
//m_readSizes[i] = 0;
continue;
}
}
// break if a list can read more, if it can read more, that is
int32_t i;
for ( i = 0 ; i < numLists ; i++ ) if ( ! m_ignore[i] ) break;
// if all lists are exhausted, set m_isDone
if ( i >= numLists ) { m_isDone = true; return; }
// . based on # of results we got how much more should we have to read
// to get what we want, "docsWanted"
// . just base it on linear proportion
// . keep in mind, if we double the amount to read we will quadruple
// the results if reading 2 indexLists, x8 if reading from 3.
// . that doesn't take into account phrases though...
// . let's just do it this way
// loop over all lists and update m_startKeys[i] and m_totalDocsRead
for ( int32_t i = 0 ; i < numLists ; i++ ) {
// debug msg
//log("oldreadSizes[%"INT32"]=%"INT32"",i,m_readSizes[i]);
// update each list's docs to read if we're not on the last
// tier
if ( !m_ignore[i] && callNum < MAX_TIERS &&
m_readSizes[i] < m_stage[(int)callNum] )
m_readSizes[i] = m_stage[(int)callNum];
/*if ( m_readSizes[i] < m_stage0)
m_readSizes[i] = m_stage0;
else if ( m_readSizes[i] < m_stage1)
m_readSizes[i] = m_stage1;
else
m_readSizes[i] = m_stage2;*/
// debug msg
if ( m_isDebug || g_conf.m_logDebugQuery )
logf ( LOG_DEBUG,"query: ReadInfo: "
"newreadSizes[%"INT32"]=%"INT32"",i,m_readSizes[i] );
}
}