forked from sphinxsearch/sphinx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsphinxrlp.h
392 lines (308 loc) · 10.3 KB
/
sphinxrlp.h
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
//
// $Id$
//
//
// Copyright (c) 2001-2016, Andrew Aksyonoff
// Copyright (c) 2008-2016, Sphinx Technologies Inc
// All rights reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License. You should have
// received a copy of the GPL license along with this program; if you
// did not, you can find it at http://www.gnu.org/
//
#ifndef _sphinxrlp_
#define _sphinxrlp_
#include "sphinxstd.h"
//////////////////////////////////////////////////////////////////////////
#if USE_RLP
extern CSphString g_sRLPRoot;
extern CSphString g_sRLPEnv;
extern int g_iRLPMaxBatchSize;
extern int g_iRLPMaxBatchDocs;
struct StoredDoc_t
{
CSphMatch m_tDocInfo;
CSphVector<CSphString> m_dStrAttrs;
CSphVector<DWORD> m_dMva;
CSphTightVector<BYTE*> m_dFields;
CSphVector<int> m_dFieldLengths;
CSphTightVector< CSphVector<BYTE> > m_dFieldStorage;
};
// these are used to separate text before passing it to RLP
const int PROXY_DOCUMENT_START = 0xFFFA;
const int PROXY_FIELD_START = 0xFFFB;
const int PROXY_CHUNK_SEPARATOR = 0xFFFC;
const int PROXY_MARKER_LEN = 3;
#define COPY_MARKER(_ptr,_marker) \
{\
*_ptr++ = ' '; \
*_ptr++ = _marker[0]; \
*_ptr++ = _marker[1]; \
*_ptr++ = _marker[2]; \
*_ptr++ = ' '; \
}
#define CMP_MARKER(_ptr, _marker) \
( _ptr[0]==_marker[0] && _ptr[1]==_marker[1] && _ptr[2]==_marker[2] )
// proxy source
template <class T>
class CSphSource_Proxy : public T
{
public:
explicit CSphSource_Proxy ( const char * sSourceName )
: T ( sSourceName )
, m_dBatchedDocs ( g_iRLPMaxBatchDocs )
, m_iDocStart ( 0 )
, m_iDocCount ( 0 )
, m_pBatchFieldFilter ( NULL )
{
assert ( sphUTF8Encode ( m_pMarkerDocStart, PROXY_DOCUMENT_START )==PROXY_MARKER_LEN );
sphUTF8Encode ( m_pMarkerDocStart, PROXY_DOCUMENT_START );
sphUTF8Encode ( m_pMarkerFieldStart, PROXY_FIELD_START );
const int INITIAL_BUFFER_SIZE = 1048576;
m_dDocBuffer.Reserve ( INITIAL_BUFFER_SIZE );
}
// we run field filters on batches of documents, therefore we don't want
// CSphSource_Document to run them again in IterateDocument
virtual void SetFieldFilter ( ISphFieldFilter * pFilter )
{
m_pBatchFieldFilter = pFilter;
}
virtual BYTE ** NextDocument ( CSphString & sError )
{
if ( !IsDocCacheEmpty() )
return CopyDoc();
if ( m_dFieldLengths.GetLength()!=T::m_tSchema.m_dFields.GetLength() )
{
int nFields = T::m_tSchema.m_dFields.GetLength();
m_dFieldLengths.Resize ( nFields );
m_dFieldHasChinese.Resize ( nFields );
}
m_iDocStart = 0;
const int MAX_INDEX_LEN = 8;
m_dDocBuffer.Resize(0);
while ( !IsDocCacheFull() && m_dDocBuffer.GetLength() < g_iRLPMaxBatchSize )
{
BYTE ** pFields = T::NextDocument ( sError );
if ( !pFields )
break;
const int * pFieldLengths = T::GetFieldLengths();
int iTotalFieldLen = 0;
bool bDocHasChinese = false;
for ( int i = 0; i < T::m_tSchema.m_dFields.GetLength(); i++ )
{
m_dFieldLengths[i] = pFieldLengths[i];
m_dFieldHasChinese[i] = sphDetectChinese ( pFields[i], m_dFieldLengths[i] );
if ( m_dFieldHasChinese[i] )
iTotalFieldLen += PROXY_MARKER_LEN+MAX_INDEX_LEN+m_dFieldLengths[i]+3; // _[marker]_[field_id]_[field text]
bDocHasChinese |= m_dFieldHasChinese[i];
}
// no point in caching the document if its the first document and there's nothing CJK in it
if ( IsDocCacheEmpty() && !bDocHasChinese )
{
T::m_tState.m_dFieldLengths.Resize ( m_dFieldLengths.GetLength() );
ARRAY_FOREACH ( i, m_dFieldLengths )
T::m_tState.m_dFieldLengths[i] = m_dFieldLengths[i];
return pFields;
}
int iCurDoc;
StoredDoc_t * pDoc = PushDoc ( iCurDoc );
int nFields = T::m_tSchema.m_dFields.GetLength();
CopyDocInfo ( pDoc->m_tDocInfo, T::m_tDocInfo );
pDoc->m_dMva = T::m_dMva;
pDoc->m_dStrAttrs = T::m_dStrAttrs;
pDoc->m_dFields.Resize ( nFields );
pDoc->m_dFieldLengths.Resize ( nFields );
pDoc->m_dFieldStorage.Resize ( nFields );
// fields that don't have chinese text will be stored in those docs
// fields that have chinese text will be added later, after a RLP field filter pass
for ( int i = 0; i < T::m_tSchema.m_dFields.GetLength(); i++ )
if ( !m_dFieldHasChinese[i] )
{
int iFieldLength = m_dFieldLengths[i];
pDoc->m_dFieldStorage[i].Resize ( iFieldLength+1 );
BYTE * pStart = pDoc->m_dFieldStorage[i].Begin();
memcpy ( pStart, pFields[i], iFieldLength );
pDoc->m_dFields[i] = pStart;
pDoc->m_dFieldLengths[i] = iFieldLength;
// stripper still doesn't know about field lengths, so we need to add a zero
pDoc->m_dFields[i][iFieldLength] = '\0';
}
// document doesnt have any CJK, so no copying/segmenting/etc
if ( !bDocHasChinese )
continue;
int iOldBufferLen = m_dDocBuffer.GetLength();
m_dDocBuffer.Resize ( iOldBufferLen+PROXY_MARKER_LEN+MAX_INDEX_LEN+2+iTotalFieldLen );
BYTE * pCurDocPtr = &(m_dDocBuffer[iOldBufferLen]);
// document start tag
COPY_MARKER ( pCurDocPtr, m_pMarkerDocStart );
// document id
AddNumber ( pCurDocPtr, iCurDoc );
// flatten all fields(+markers) to one buffer
for ( int i = 0; i < T::m_tSchema.m_dFields.GetLength(); i++ )
if ( m_dFieldHasChinese[i] )
{
COPY_MARKER ( pCurDocPtr, m_pMarkerFieldStart );
// field id
AddNumber ( pCurDocPtr, i );
*pCurDocPtr++ = ' ';
int iFieldLen = m_dFieldLengths[i];
memcpy ( pCurDocPtr, pFields[i], iFieldLen );
pCurDocPtr += iFieldLen;
}
m_dDocBuffer.Resize ( pCurDocPtr-m_dDocBuffer.Begin() );
}
if ( IsDocCacheEmpty() )
return NULL;
assert ( m_pBatchFieldFilter && !T::m_pFieldFilter );
int iResultLen = m_pBatchFieldFilter->Apply ( m_dDocBuffer.Begin(), m_dDocBuffer.GetLength(), m_dResult, false );
assert ( iResultLen );
BYTE * pSegmentedStart = m_dResult.Begin();
BYTE * pSegmentedEnd = pSegmentedStart+iResultLen;
BYTE * pFieldStart = NULL;
StoredDoc_t * pCurDoc = NULL;
int iFieldId = -1;
while ( pSegmentedStart < pSegmentedEnd )
{
BYTE * pTmp = pSegmentedStart;
int iCode = sphUTF8Decode ( (const BYTE * &)pSegmentedStart );
if ( iCode!=PROXY_DOCUMENT_START && iCode!=PROXY_FIELD_START )
continue;
// we have a segmented field that we need to place into the appropriate cached document
if ( pFieldStart )
{
assert ( pCurDoc && iFieldId!=-1 );
pCurDoc->m_dFields[iFieldId] = pFieldStart;
pCurDoc->m_dFieldLengths[iFieldId] = pTmp-pFieldStart;
// stripper still doesn't know about field lengths, so we need to add a zero
pTmp[-1] = '\0';
}
if ( iCode==PROXY_DOCUMENT_START )
{
// fetch document number
int iDoc = ReadNumber ( pSegmentedStart, pSegmentedEnd );
pCurDoc = &(m_dBatchedDocs[iDoc]);
iFieldId = -1;
pFieldStart = NULL;
} else if ( iCode==PROXY_FIELD_START )
{
// fetch field id
iFieldId = ReadNumber ( pSegmentedStart, pSegmentedEnd );
pFieldStart = pSegmentedStart;
}
}
// last field
if ( pFieldStart )
{
assert ( pCurDoc && iFieldId!=-1 );
pCurDoc->m_dFields[iFieldId] = pFieldStart;
pCurDoc->m_dFieldLengths[iFieldId] = pSegmentedEnd-pFieldStart;
}
return CopyDoc ();
}
virtual const int * GetFieldLengths () const
{
return T::m_tState.m_dFieldLengths.Begin();
}
private:
CSphSource_Document * m_pSource;
CSphFixedVector<StoredDoc_t> m_dBatchedDocs;
CSphTightVector<BYTE> m_dDocBuffer;
CSphVector<BYTE> m_dResult;
CSphTightVector<int> m_dFieldLengths;
CSphVector<bool> m_dFieldHasChinese;
int m_iDocStart;
int m_iDocCount;
ISphFieldFilter * m_pBatchFieldFilter;
BYTE m_pMarkerDocStart[PROXY_MARKER_LEN];
BYTE m_pMarkerFieldStart[PROXY_MARKER_LEN];
bool IsDocCacheEmpty() const { return !m_iDocCount; }
bool IsDocCacheFull() const { return m_iDocCount==m_dBatchedDocs.GetLength(); }
StoredDoc_t * PushDoc ( int & iId )
{
assert ( !IsDocCacheFull() );
iId = (m_iDocStart+m_iDocCount) % m_dBatchedDocs.GetLength();
m_iDocCount++;
return &(m_dBatchedDocs[iId]);
}
StoredDoc_t * PopDoc()
{
assert ( !IsDocCacheEmpty() );
StoredDoc_t * pDoc = &(m_dBatchedDocs[m_iDocStart]);
m_iDocStart = (m_iDocStart+1) % m_dBatchedDocs.GetLength();
m_iDocCount--;
return pDoc;
}
BYTE ** CopyDoc ()
{
StoredDoc_t * pDoc = PopDoc();
assert ( pDoc );
CopyDocInfo ( T::m_tDocInfo, pDoc->m_tDocInfo );
T::m_tState.m_dFields = pDoc->m_dFields.Begin();
T::m_tState.m_dFieldLengths.SwapData ( pDoc->m_dFieldLengths );
T::m_dMva.SwapData ( pDoc->m_dMva );
T::m_dStrAttrs.SwapData ( pDoc->m_dStrAttrs );
return T::m_tState.m_dFields;
}
void CopyDocInfo ( CSphMatch & tTo, const CSphMatch & tFrom )
{
if ( tFrom.m_pDynamic )
{
int iDynamic = T::m_tSchema.GetRowSize();
if ( !tTo.m_pDynamic )
tTo.Reset ( iDynamic );
memcpy ( tTo.m_pDynamic, tFrom.m_pDynamic, iDynamic*sizeof(CSphRowitem) );
}
tTo.m_pStatic = NULL;
tTo.m_uDocID = tFrom.m_uDocID;
tTo.m_iWeight = tFrom.m_iWeight;
tTo.m_iTag = tFrom.m_iTag;
}
void AddNumber ( BYTE * & pPtr, int iNumber )
{
char szTmp [256];
int iLen = snprintf ( szTmp, sizeof(szTmp), "%d", iNumber );
iLen = iLen>=0 ? iLen : sizeof(szTmp);
memcpy ( pPtr, szTmp, iLen );
pPtr += iLen;
}
int ReadNumber ( BYTE * & pPtr, BYTE * pEndPtr )
{
int iCode = sphUTF8Decode ( (const BYTE * &)pPtr );
assert ( iCode==' ' );
const BYTE * pNumber = pPtr;
do
{
iCode = sphUTF8Decode ( (const BYTE * &)pPtr );
} while ( pPtr < pEndPtr && iCode!=' ');
assert ( iCode==' ' );
BYTE uTmp = *pPtr;
*pPtr = '\0';
int iDoc = atoi ( (char *)pNumber );
*pPtr = uTmp;
return iDoc;
}
};
template<typename T>
T * CreateSourceWithProxy ( const char * sSourceName, bool bProxy )
{
if ( bProxy )
return new CSphSource_Proxy<T> ( sSourceName );
else
return new T ( sSourceName );
}
#else // USE_RLP
template<typename T>
T * CreateSourceWithProxy ( const char * sSourceName, bool )
{
return new T ( sSourceName );
}
#endif // USE_RLP
bool sphRLPCheckConfig ( CSphIndexSettings & tSettings, CSphString & sError );
bool sphSpawnRLPFilter ( ISphFieldFilter * & pFieldFilter, const CSphIndexSettings & m_tSettings, const CSphTokenizerSettings & tTokSettings, const char * szIndex, CSphString & sError );
void sphConfigureRLP ( CSphConfigSection & hCommon );
void sphRLPDone();
#endif // _sphinxrlp_
//
// $Id$
//