1 /*------------------------------------------------------------------------------
2 * Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
3 *
4 * Distributable under the terms of either the Apache License (Version 2.0) or
5 * the GNU Lesser General Public License, as specified in the COPYING file.
6 ------------------------------------------------------------------------------*/
7 #ifndef _lucene_index_FieldInfos_
8 #define _lucene_index_FieldInfos_
9
10 #include "CLucene/store/Directory.h"
11
CL_CLASS_DEF(document,Document)12 CL_CLASS_DEF(document,Document)
13 CL_CLASS_DEF(document,Field)
14
15 CL_NS_DEF(index)
16
17 class FieldInfo :LUCENE_BASE{
18 public:
19 //name of the field
20 const TCHAR* name;
21
22 //Is field indexed? true = yes false = no
23 bool isIndexed;
24
25 //field number
26 const int32_t number;
27
28 // true if term vector for this field should be stored
29 bool storeTermVector;
30 bool storeOffsetWithTermVector;
31 bool storePositionWithTermVector;
32
33 bool omitNorms; // omit norms associated with indexed fields
34
35 bool storePayloads; // whether this field stores payloads together with term positions
36
37 //Func - Constructor
38 // Initialises FieldInfo.
39 // na holds the name of the field
40 // tk indicates whether this field is indexed or not
41 // nu indicates its number
42 //Pre - na != NULL and holds the name of the field
43 // tk is true or false
44 // number >= 0
45 //Post - The FieldInfo instance has been created and initialized.
46 // name holds the duplicated string of na
47 // isIndexed = tk
48 // number = nu
49 FieldInfo(const TCHAR* fieldName,
50 const bool isIndexed,
51 const int32_t fieldNumber,
52 const bool storeTermVector,
53 const bool storeOffsetWithTermVector,
54 const bool storePositionWithTermVector,
55 const bool omitNorms,
56 const bool storePayloads);
57
58 //Func - Destructor
59 //Pre - true
60 //Post - The instance has been destroyed
61 ~FieldInfo();
62
63 /* Clones this
64 * @memory - caller is responsible for deleting the returned object
65 */
66 FieldInfo* clone();
67 };
68
69 /** Access to the Field Info file that describes document fields and whether or
70 * not they are indexed. Each segment has a separate Field Info file. Objects
71 * of this class are thread-safe for multiple readers, but only one thread can
72 * be adding documents at a time, with no other reader or writer threads
73 * accessing this object.
74 */
75 class CLUCENE_EXPORT FieldInfos :LUCENE_BASE{
76 //we now use internd field names, so we can use the voidCompare
77 //to directly compare the strings
78 typedef CL_NS(util)::CLHashMap<const TCHAR*,FieldInfo*,
79 CL_NS(util)::Compare::TChar,CL_NS(util)::Equals::TChar > defByName;
80 defByName byName;
81
82 CL_NS(util)::CLArrayList<FieldInfo*,CL_NS(util)::Deletor::Object<FieldInfo> > byNumber;
83 public:
84 enum{
85 IS_INDEXED = 0x1,
86 STORE_TERMVECTOR = 0x2,
87 STORE_POSITIONS_WITH_TERMVECTOR = 0x4,
88 STORE_OFFSET_WITH_TERMVECTOR = 0x8,
89 OMIT_NORMS = 0x10,
90 STORE_PAYLOADS = 0x20
91 };
92
93 FieldInfos();
94 ~FieldInfos();
95
96 /**
97 * Construct a FieldInfos object using the directory and the name of the file
98 * IndexInput
99 * @param d The directory to open the IndexInput from
100 * @param name The name of the file to open the IndexInput from in the Directory
101 * @throws IOException
102 */
103 FieldInfos(CL_NS(store)::Directory* d, const char* name);
104
105 /**
106 * Returns a deep clone of this FieldInfos instance.
107 * @memory caller is responisble for deleting returned object
108 */
109 FieldInfos* clone();
110
111 /** Adds field info for a Document. */
112 void add(const CL_NS(document)::Document* doc);
113
114 /**
115 * Add fields that are indexed. Whether they have termvectors has to be specified.
116 *
117 * @param names The names of the fields. An array of TCHARs, last item has to be NULL
118 * @param storeTermVectors Whether the fields store term vectors or not
119 * @param storePositionWithTermVector treu if positions should be stored.
120 * @param storeOffsetWithTermVector true if offsets should be stored
121 */
122 void addIndexed(const TCHAR** names, const bool storeTermVectors, const bool storePositionWithTermVector, const bool storeOffsetWithTermVector);
123
124 /**
125 * Assumes the fields are not storing term vectors.
126 *
127 * @param names The names of the fields
128 * @param isIndexed Whether the fields are indexed or not
129 *
130 * @see #add(TCHAR*, bool)
131 */
132 void add(const TCHAR** names, const bool isIndexed, const bool storeTermVector=false,
133 const bool storePositionWithTermVector=false, const bool storeOffsetWithTermVector=false,
134 const bool omitNorms=false, const bool storePayloads=false);
135
136 // Merges in information from another FieldInfos.
137 void add(FieldInfos* other);
138
139 /** If the field is not yet known, adds it. If it is known, checks to make
140 * sure that the isIndexed flag is the same as was given previously for this
141 * field. If not - marks it as being indexed. Same goes for the TermVector
142 * parameters.
143 *
144 * @param name The name of the field
145 * @param isIndexed true if the field is indexed
146 * @param storeTermVector true if the term vector should be stored
147 * @param storePositionWithTermVector true if the term vector with positions should be stored
148 * @param storeOffsetWithTermVector true if the term vector with offsets should be stored
149 * @param omitNorms true if the norms for the indexed field should be omitted
150 * @param storePayloads true if payloads should be stored for this field
151 */
152 FieldInfo* add(const TCHAR* name, const bool isIndexed, const bool storeTermVector=false,
153 const bool storePositionWithTermVector=false, const bool storeOffsetWithTermVector=false, const bool omitNorms=false, const bool storePayloads=false);
154
155 // was void
156 FieldInfo* addInternal( const TCHAR* name,const bool isIndexed, const bool storeTermVector,
157 const bool storePositionWithTermVector, const bool storeOffsetWithTermVector, const bool omitNorms, const bool storePayloads);
158
159 int32_t fieldNumber(const TCHAR* fieldName)const;
160
161 /**
162 * Return the fieldinfo object referenced by the fieldNumber.
163 * @param fieldNumber
164 * @return the FieldInfo object or null when the given fieldNumber
165 * doesn't exist.
166 */
167 FieldInfo* fieldInfo(const TCHAR* fieldName) const;
168
169 /**
170 * Return the fieldName identified by its number.
171 *
172 * @param fieldNumber
173 * @return the fieldName or an empty string when the field
174 * with the given number doesn't exist.
175 */
176 const TCHAR* fieldName(const int32_t fieldNumber)const;
177
178 /**
179 * Return the fieldinfo object referenced by the fieldNumber.
180 * @param fieldNumber
181 * @return the FieldInfo object or null when the given fieldNumber
182 * doesn't exist.
183 */
184 FieldInfo* fieldInfo(const int32_t fieldNumber) const;
185
186 size_t size()const;
187 bool hasVectors() const;
188
189
190 void write(CL_NS(store)::Directory* d, const char* name) const;
191 void write(CL_NS(store)::IndexOutput* output) const;
192
193 private:
194 void read(CL_NS(store)::IndexInput* input);
195
196 };
197 CL_NS_END
198 #endif
199