forked from apache/doris-thirdparty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFieldInfos.cpp
289 lines (258 loc) · 9.86 KB
/
FieldInfos.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
/*------------------------------------------------------------------------------
* Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
*
* Distributable under the terms of either the Apache License (Version 2.0) or
* the GNU Lesser General Public License, as specified in the COPYING file.
------------------------------------------------------------------------------*/
#include "CLucene/_ApiHeader.h"
#include "_FieldInfos.h"
#include "CLucene/store/Directory.h"
#include "CLucene/document/Document.h"
#include "CLucene/document/Field.h"
////#include "CLucene/util/VoidMap.h"
#include "CLucene/util/Misc.h"
#include "CLucene/util/_StringIntern.h"
#include "CLucene/store/IndexInput.h"
#include "CLucene/store/IndexOutput.h"
CL_NS_USE(store)
CL_NS_USE(document)
CL_NS_USE(util)
CL_NS_DEF(index)
FieldInfo::FieldInfo(const TCHAR *_fieldName,
const bool _isIndexed,
const int32_t _fieldNumber,
const bool _storeTermVector,
const bool _storeOffsetWithTermVector,
const bool _storePositionWithTermVector,
const bool _omitNorms,
const bool _hasProx,
const bool _storePayloads) : name(CLStringIntern::intern(_fieldName )),
isIndexed(_isIndexed),
number(_fieldNumber),
storeTermVector(_storeTermVector),
storeOffsetWithTermVector(_storeOffsetWithTermVector),
storePositionWithTermVector(_storePositionWithTermVector),
omitNorms(_omitNorms), hasProx(_hasProx),
storePayloads(_storePayloads) {
}
FieldInfo::~FieldInfo(){
CL_NS(util)::CLStringIntern::unintern(name);
}
FieldInfo* FieldInfo::clone() {
return _CLNEW FieldInfo(name, isIndexed, number, storeTermVector, storePositionWithTermVector,
storeOffsetWithTermVector, omitNorms, hasProx, storePayloads);
}
FieldInfos::FieldInfos():
byName(false,false),byNumber(true) {
}
FieldInfos::~FieldInfos(){
byName.clear();
byNumber.clear();
}
FieldInfos::FieldInfos(Directory* d, const char* name):
byName(false,false),byNumber(true)
{
IndexInput* input = d->openInput(name);
try {
read(input);
} _CLFINALLY (
input->close();
_CLDELETE(input);
);
}
FieldInfos* FieldInfos::clone()
{
FieldInfos* fis = _CLNEW FieldInfos();
const size_t numField = byNumber.size();
for(size_t i=0;i<numField;i++) {
FieldInfo* fi = byNumber[i]->clone();
fis->byNumber.push_back(fi);
fis->byName.put( fi->name, fi);
}
return fis;
}
void FieldInfos::add(const Document* doc) {
const Document::FieldsType& fields = *doc->getFields();
Field* field;
for ( Document::FieldsType::const_iterator itr = fields.begin() ; itr != fields.end() ; itr++ ){
field = *itr;
add(field->name(), field->isIndexed(), field->isTermVectorStored(), field->isStorePositionWithTermVector(),
field->isStoreOffsetWithTermVector(), field->getOmitNorms(), !field->getOmitTermFreqAndPositions(), false);
}
}
bool FieldInfos::hasProx() {
int numFields = byNumber.size();
for (int i = 0; i < numFields; i++) {
FieldInfo* fi = fieldInfo(i);
if (fi->isIndexed && fi->hasProx) {
return true;
}
}
return false;
}
IndexVersion FieldInfos::getIndexVersion() {
int numFields = byNumber.size();
for (int i = 0; i < numFields; i++) {
FieldInfo* fi = fieldInfo(i);
if (fi->indexVersion_ > IndexVersion::kV0) {
return fi->indexVersion_;
}
}
return IndexVersion::kV0;
}
void FieldInfos::addIndexed(const TCHAR** names, const bool storeTermVectors, const bool storePositionWithTermVector,
const bool storeOffsetWithTermVector) {
size_t i = 0;
while (names[i]) {
add(names[i], true, storeTermVectors, storePositionWithTermVector, storeOffsetWithTermVector);
++i;
}
}
void FieldInfos::add(const TCHAR** names, const bool isIndexed, const bool storeTermVectors,
const bool storePositionWithTermVector, const bool storeOffsetWithTermVector,
const bool omitNorms, const bool hasProx, const bool storePayloads,
IndexVersion indexVersion) {
size_t i = 0;
while ( names[i] != NULL ){
add(names[i], isIndexed, storeTermVectors, storePositionWithTermVector,
storeOffsetWithTermVector, omitNorms, hasProx, storePayloads, indexVersion);
++i;
}
}
FieldInfo* FieldInfos::add(const TCHAR* name, const bool isIndexed, const bool storeTermVector,
const bool storePositionWithTermVector,
const bool storeOffsetWithTermVector, const bool omitNorms,
const bool hasProx, const bool storePayloads,
IndexVersion indexVersion) {
FieldInfo* fi = fieldInfo(name);
if (fi == NULL) {
return addInternal(name, isIndexed, storeTermVector, storePositionWithTermVector,
storeOffsetWithTermVector, omitNorms, hasProx, storePayloads,
indexVersion);
} else {
if (fi->isIndexed != isIndexed) {
fi->isIndexed = true; // once indexed, always index
}
if (fi->storeTermVector != storeTermVector) {
fi->storeTermVector = true; // once vector, always vector
}
if (fi->storePositionWithTermVector != storePositionWithTermVector) {
fi->storePositionWithTermVector = true; // once vector, always vector
}
if (fi->storeOffsetWithTermVector != storeOffsetWithTermVector) {
fi->storeOffsetWithTermVector = true; // once vector, always vector
}
if (fi->omitNorms != omitNorms) {
fi->omitNorms = false; // once norms are stored, always store
}
if (fi->hasProx != hasProx) {
fi->hasProx = true;
}
if (fi->storePayloads != storePayloads) {
fi->storePayloads = true;
}
if (fi->indexVersion_ != indexVersion) {
fi->indexVersion_ = indexVersion;
}
}
return fi;
}
FieldInfo* FieldInfos::addInternal(const TCHAR* name, const bool isIndexed,
const bool storeTermVector,
const bool storePositionWithTermVector,
const bool storeOffsetWithTermVector, const bool omitNorms,
const bool hasProx, const bool storePayloads,
IndexVersion indexVersion) {
FieldInfo* fi = _CLNEW FieldInfo(name, isIndexed, byNumber.size(), storeTermVector,
storePositionWithTermVector, storeOffsetWithTermVector,
omitNorms, hasProx, storePayloads);
fi->setIndexVersion(indexVersion);
byNumber.push_back(fi);
byName.put( fi->name, fi);
return fi;
}
int32_t FieldInfos::fieldNumber(const TCHAR* fieldName)const {
FieldInfo* fi = fieldInfo(fieldName);
return (fi!=NULL) ? fi->number : -1;
}
FieldInfo* FieldInfos::fieldInfo(const TCHAR* fieldName) const {
FieldInfo* ret = byName.get(fieldName);
return ret;
}
const TCHAR* FieldInfos::fieldName(const int32_t fieldNumber) const {
FieldInfo* fi = fieldInfo(fieldNumber);
return (fi==NULL)?LUCENE_BLANK_STRING:fi->name;
}
FieldInfo* FieldInfos::fieldInfo(const int32_t fieldNumber) const {
if ( fieldNumber < 0 || (size_t)fieldNumber >= byNumber.size() )
return NULL;
return byNumber[fieldNumber];
}
size_t FieldInfos::size()const {
return byNumber.size();
}
bool FieldInfos::hasVectors() const{
for (size_t i = 0; i < size(); i++) {
if (fieldInfo(i)->storeTermVector)
return true;
}
return false;
}
void FieldInfos::write(Directory* d, const char* name) const{
IndexOutput* output = d->createOutput(name);
try {
write(output);
} _CLFINALLY (
output->close();
_CLDELETE(output);
);
}
void FieldInfos::write(IndexOutput* output) const{
output->writeVInt(static_cast<int32_t>(size()));
FieldInfo* fi;
uint8_t bits;
for (size_t i = 0; i < size(); ++i) {
fi = fieldInfo(i);
bits = 0x0;
if (fi->isIndexed) bits |= IS_INDEXED;
if (fi->storeTermVector) bits |= STORE_TERMVECTOR;
if (fi->storePositionWithTermVector) bits |= STORE_POSITIONS_WITH_TERMVECTOR;
if (fi->storeOffsetWithTermVector) bits |= STORE_OFFSET_WITH_TERMVECTOR;
if (fi->omitNorms) bits |= OMIT_NORMS;
if (fi->storePayloads) bits |= STORE_PAYLOADS;
if (fi->hasProx) bits |= TERM_FREQ_AND_POSITIONS;
if (fi->getIndexVersion() > IndexVersion::kV0) {
bits |= 0x80;
}
output->writeString(fi->name,_tcslen(fi->name));
output->writeByte(bits);
if (fi->getIndexVersion() > IndexVersion::kV0) {
output->writeVInt(static_cast<int32_t>(fi->getIndexVersion()));
}
}
}
void FieldInfos::read(IndexInput* input) {
int32_t size = input->readVInt();//read in the size
uint8_t bits;
bool isIndexed,storeTermVector,storePositionsWithTermVector,storeOffsetWithTermVector,omitNorms,hasProx,storePayloads;
for (int32_t i = 0; i < size; ++i){
TCHAR* name = input->readString(); //we could read name into a string buffer, but we can't be sure what the maximum field length will be.
bits = input->readByte();
isIndexed = (bits & IS_INDEXED) != 0;
storeTermVector = (bits & STORE_TERMVECTOR) != 0;
storePositionsWithTermVector = (bits & STORE_POSITIONS_WITH_TERMVECTOR) != 0;
storeOffsetWithTermVector = (bits & STORE_OFFSET_WITH_TERMVECTOR) != 0;
omitNorms = (bits & OMIT_NORMS) != 0;
storePayloads = (bits & STORE_PAYLOADS) != 0;
hasProx = (bits & TERM_FREQ_AND_POSITIONS) != 0;
FieldInfo* fi = addInternal(name, isIndexed, storeTermVector, storePositionsWithTermVector, storeOffsetWithTermVector, omitNorms, hasProx, storePayloads);
if ((bits & 0x80) == 0) {
fi->setIndexVersion(IndexVersion::kV0);
} else {
IndexVersion indexVersion = (IndexVersion)input->readVInt();
fi->setIndexVersion(indexVersion);
}
_CLDELETE_CARRAY(name);
}
}
CL_NS_END