summaryrefslogtreecommitdiffstats
path: root/3rdparty/clucene/src/CLucene/document/Field.cpp
blob: 8cd88a36b221a49451625dcb028201c8dabe5f97 (plain)
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
/*------------------------------------------------------------------------------
* 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/StdHeader.h"
#include "CLucene/util/Reader.h"
#include "Field.h"
#include "CLucene/util/Misc.h"
#include "CLucene/util/StringIntern.h"
#include "CLucene/util/StringBuffer.h"

CL_NS_USE(util)
CL_NS_DEF(document) 

Field::Field(const TCHAR* Name, const TCHAR* String, bool store, bool index, bool token, const bool storeTermVector)
{
//Func - Constructor
//Pre  - Name != NULL and contains the name of the field
//       String != NULL and contains the value of the field
//       store indicates if the field must be stored
//       index indicates if the field must be indexed
//       token indicates if the field must be tokenized
//Post - The instance has been created

	CND_PRECONDITION(Name != NULL, "Name is NULL");
	CND_PRECONDITION(String != NULL,"String is NULL");
	CND_PRECONDITION(!(!index && storeTermVector),"cannot store a term vector for fields that are not indexed.");

	_name        = CLStringIntern::intern( Name CL_FILELINE);
	_stringValue = stringDuplicate( String );
	_readerValue = NULL;
	_streamValue = NULL;
	boost=1.0f;
	omitNorms = false;

	int cfg = 0;
	if ( store )
		cfg |= STORE_YES;
	if ( index && token )
		cfg |= INDEX_TOKENIZED;
	else if ( index && !token )
		cfg |= INDEX_UNTOKENIZED;

	if ( storeTermVector )
		_CLTHROWA(CL_ERR_IllegalArgument,"Stored term vector is deprecated with using this constructor");

	setConfig(cfg);
}

Field::Field(const TCHAR* Name, Reader* reader, bool store, bool index, bool token, const bool storeTermVector)
{
//Func - Constructor
//Pre  - Name != NULL and contains the name of the field
//       reader != NULL and contains a Reader
//       store indicates if the field must be stored
//       index indicates if the field must be indexed
//       token indicates if the field must be tokenized
//Post - The instance has been created

	CND_PRECONDITION(Name != NULL, "Name is NULL");
	CND_PRECONDITION(reader != NULL, "reader is NULL");

	_name        = CLStringIntern::intern( Name  CL_FILELINE);
	_stringValue = NULL;
	_readerValue = reader;
	_streamValue = NULL;
	boost=1.0f;
	omitNorms = false;

	int cfg = 0;
	if ( store )
		cfg |= STORE_YES;
	if ( index && token )
		cfg |= INDEX_TOKENIZED;
	else if ( index && !token )
		cfg |= INDEX_UNTOKENIZED;

	if ( storeTermVector )
		_CLTHROWA(CL_ERR_IllegalArgument,"Stored term vector is deprecated with using this constructor");

	setConfig(cfg);
}

Field::Field(const TCHAR* Name, Reader* reader, int config)
{
	CND_PRECONDITION(Name != NULL, "Name is NULL");
	CND_PRECONDITION(reader != NULL, "reader is NULL");

	_name        = CLStringIntern::intern( Name  CL_FILELINE);
	_stringValue = NULL;
	_readerValue = reader;
	_streamValue = NULL;
	boost=1.0f;
	omitNorms = false;

	setConfig(config);
}


Field::Field(const TCHAR* Name, const TCHAR* Value, int config)
{
	CND_PRECONDITION(Name != NULL, "Name is NULL");
	CND_PRECONDITION(Value != NULL, "value is NULL");

	_name        = CLStringIntern::intern( Name  CL_FILELINE);
	_stringValue = stringDuplicate( Value );
	_readerValue = NULL;
	_streamValue = NULL;
	boost=1.0f;
	omitNorms = false;

	setConfig(config);
}

Field::Field(const TCHAR* Name, jstreams::StreamBase<char>* Value, int config)
{
	CND_PRECONDITION(Name != NULL, "Name is NULL");
	CND_PRECONDITION(Value != NULL, "value is NULL");

	_name        = CLStringIntern::intern( Name  CL_FILELINE);
	_stringValue = NULL;
	_readerValue = NULL;
	_streamValue = Value;
	boost=1.0f;
	omitNorms = false;

	setConfig(config);
}

Field::~Field(){
//Func - Destructor
//Pre  - true
//Post - Instance has been destroyed

	CLStringIntern::unintern(_name);
	_CLDELETE_CARRAY(_stringValue);
	_CLDELETE(_readerValue);
	_CLVDELETE( _streamValue );
}


/*===============FIELDS=======================*/
const TCHAR* Field::name() 		{ return _name; } ///<returns reference
TCHAR* Field::stringValue()		{ return _stringValue; } ///<returns reference
Reader* Field::readerValue()	{ return _readerValue; } ///<returns reference
jstreams::StreamBase<char>* Field::streamValue()	{ return _streamValue; } ///<returns reference

bool	Field::isStored() 	{ return (config & STORE_YES) != 0; }
bool 	Field::isIndexed() 	{ return (config & INDEX_TOKENIZED)!=0 || (config & INDEX_UNTOKENIZED)!=0; }
bool 	Field::isTokenized() 	{ return (config & INDEX_TOKENIZED) != 0; }
bool 	Field::isCompressed() 	{ return (config & STORE_COMPRESS) != 0; }
bool 	Field::isBinary() 	{ return _streamValue!=NULL; }

bool	Field::isTermVectorStored() { return (config & TERMVECTOR_YES) != 0; }
bool	Field::isStoreOffsetWithTermVector() { return (config & TERMVECTOR_YES) != 0 && (config & TERMVECTOR_WITH_OFFSETS) != 0; }
bool	Field::isStorePositionWithTermVector() { return (config & TERMVECTOR_YES) != 0 && (config & TERMVECTOR_WITH_POSITIONS) != 0; }

bool Field::getOmitNorms() { return omitNorms; }
void Field::setOmitNorms(bool omitNorms) { this->omitNorms=omitNorms; }

void Field::setBoost(qreal boost) { this->boost = boost; }
qreal Field::getBoost() { return boost; }

void Field::setConfig(int x){
	int newConfig=0;

	//set storage settings
	if ( (x & STORE_YES) || (x & STORE_COMPRESS) ){
		newConfig |= STORE_YES;
		if ( x & STORE_COMPRESS )
			newConfig |= STORE_COMPRESS;
	}else
		newConfig |= STORE_NO;

	if ( (x & INDEX_NO)==0 ){
		bool index=false;

		if ( x & INDEX_NONORMS ){
			newConfig |= INDEX_NONORMS;
			index = true;
		}

		if ( x & INDEX_TOKENIZED && x & INDEX_UNTOKENIZED )
			_CLTHROWA(CL_ERR_IllegalArgument,"it doesn't make sense to have an untokenised and tokenised field");
		if ( x & INDEX_TOKENIZED ){
			newConfig |= INDEX_TOKENIZED;
			index = true;
		}
		if ( x & INDEX_UNTOKENIZED ){
			newConfig |= INDEX_UNTOKENIZED;
			index = true;
		}
		if ( !index )
			newConfig |= INDEX_NO;
	}else
		newConfig |= INDEX_NO;

	if ( newConfig & INDEX_NO && newConfig & STORE_NO )
		_CLTHROWA(CL_ERR_IllegalArgument,"it doesn't make sense to have a field that is neither indexed nor stored");

	//set termvector settings
	if ( (x & TERMVECTOR_NO) == 0 ){
		bool termVector=false;
		if ( x & TERMVECTOR_YES ){
			termVector=true;
		}
		if ( x & TERMVECTOR_WITH_OFFSETS ){
			newConfig |= TERMVECTOR_WITH_OFFSETS;
			termVector=true;
		}
		if ( x & TERMVECTOR_WITH_POSITIONS ){
			newConfig |= TERMVECTOR_WITH_POSITIONS;
			termVector=true;
		}
		if ( termVector ){
			if ( newConfig & INDEX_NO )
				_CLTHROWA(CL_ERR_IllegalArgument,"cannot store a term vector for fields that are not indexed.");

			newConfig |= TERMVECTOR_YES;
		}else
			newConfig |= TERMVECTOR_NO;
	}else
		newConfig |= TERMVECTOR_NO;

	config = newConfig;
}

TCHAR* Field::toString() {
    CL_NS(util)::StringBuffer result;
	if (isStored()) {
      result.append( _T("stored") );
    }
    if (isIndexed()) {
      if (result.length() > 0)
        result.append( _T(",") );
      result.append( _T("indexed") );
    }
    if (isTokenized()) {
      if (result.length() > 0)
        result.append( _T(",") );
      result.append( _T("tokenized") );
    }
    if (isTermVectorStored()) {
      if (result.length() > 0)
        result.append( _T(",") );
      result.append( _T("termVector") );
    }
    if (isStoreOffsetWithTermVector()) { 
      if (result.length() > 0) 
        result.appendChar( ',' ); 
      result.append( _T("termVectorOffsets") ); 
    } 
    if (isStorePositionWithTermVector()) { 
      if (result.length() > 0) 
        result.appendChar( ',' ); 
      result.append( _T("termVectorPosition") ); 
    } 
    if (isBinary()) {
      if (result.length() > 0)
        result.appendChar( ',' );
      result.append( _T("binary") );
    }
    if (getOmitNorms()) {
      result.append( _T(",omitNorms") );
    }
    result.appendChar('<');
    result.append(name());
    result.appendChar(':');
    
    if (_stringValue != NULL)
    	result.append(_stringValue);
    else if ( _readerValue != NULL )
    	result.append( _T("Reader") );
    else if ( _streamValue != NULL )
    	result.append( _T("Stream") );
    else
    	result.append( _T("NULL") );
    
    result.appendChar('>');
    return result.toString();
}


Field* Field::Keyword(const TCHAR* Name, const TCHAR* Value) {
	return _CLNEW Field(Name,Value,Field::STORE_YES | Field::INDEX_UNTOKENIZED);
}

Field* Field::UnIndexed(const TCHAR* Name, const TCHAR* Value) {
	return _CLNEW Field(Name,Value,Field::STORE_YES | Field::INDEX_NO);
}

Field* Field::Text(const TCHAR* Name, const TCHAR* Value, const bool storeTermVector) {
	if ( storeTermVector )
		return _CLNEW Field(Name,Value,Field::STORE_YES | Field::INDEX_TOKENIZED | Field::TERMVECTOR_YES);
	else
		return _CLNEW Field(Name,Value,Field::STORE_YES | Field::INDEX_TOKENIZED);
}

Field* Field::UnStored(const TCHAR* Name, const TCHAR* Value, const bool storeTermVector) {
	if ( storeTermVector )
		return _CLNEW Field(Name,Value,Field::STORE_NO | Field::INDEX_TOKENIZED | Field::TERMVECTOR_YES);
	else
		return _CLNEW Field(Name,Value,Field::STORE_NO | Field::INDEX_TOKENIZED);
}

Field* Field::Text(const TCHAR* Name, Reader* Value, const bool storeTermVector) {
	if ( storeTermVector )
		return _CLNEW Field(Name,Value,Field::INDEX_TOKENIZED | Field::TERMVECTOR_YES);
	else
		return _CLNEW Field(Name,Value,Field::INDEX_TOKENIZED);
}

CL_NS_END