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_search_FieldCache_
8#define _lucene_search_FieldCache_
9
10#if defined(_LUCENE_PRAGMA_ONCE)
11# pragma once
12#endif
13
14#include "CLucene/index/IndexReader.h"
15#include "Sort.h"
16
17
18CL_NS_DEF(search)
19
20class FieldCacheAuto; //predefine
21
22/**
23 * Expert: Maintains caches of term values.
24 *
25 */
26class FieldCache :LUCENE_BASE {
27public:
28 virtual ~FieldCache(){
29 }
30
31 /** Expert: Stores term text values and document ordering data. */
32 class StringIndex:LUCENE_BASE {
33 public:
34 /** All the term values, in natural order. */
35 TCHAR** lookup;
36
37 /** For each document, an index into the lookup array. */
38 int32_t* order;
39
40 int count;
41
42 /** Creates one of these objects
43 Consumes all memory given.
44 */
45 StringIndex (int32_t* values, TCHAR** lookup, int count) {
46 this->count = count;
47 this->order = values;
48 this->lookup = lookup;
49 }
50
51 ~StringIndex(){
52 _CLDELETE_ARRAY(order);
53
54 for ( int i=0;i<count;i++ )
55 _CLDELETE_CARRAY(lookup[i]);
56 _CLDELETE_ARRAY(lookup);
57 }
58 };
59
60
61 /** Indicator for FieldCache::StringIndex values in the cache.
62 NOTE: the value assigned to this constant must not be
63 the same as any of those in SortField!!
64 */
65 static int32_t STRING_INDEX;
66
67 /** Expert: The cache used internally by sorting and range query classes. */
68 static FieldCache* DEFAULT;
69
70 /** Checks the internal cache for an appropriate entry, and if none is
71 * found, reads the terms in <code>field</code> as integers and returns an array
72 * of size <code>reader.maxDoc()</code> of the value each document
73 * has in the given field.
74 * @param reader Used to get field values.
75 * @param field Which field contains the integers.
76 * @return The values in the given field for each document.
77 * @throws IOException If any error occurs.
78 */
79 virtual FieldCacheAuto* getInts (CL_NS(index)::IndexReader* reader, const TCHAR* field) = 0;
80
81 /** Checks the internal cache for an appropriate entry, and if
82 * none is found, reads the terms in <code>field</code> as floats and returns an array
83 * of size <code>reader.maxDoc()</code> of the value each document
84 * has in the given field.
85 * @param reader Used to get field values.
86 * @param field Which field contains the floats.
87 * @return The values in the given field for each document.
88 * @throws IOException If any error occurs.
89 */
90 virtual FieldCacheAuto* getFloats (CL_NS(index)::IndexReader* reader, const TCHAR* field) = 0;
91
92 /** Checks the internal cache for an appropriate entry, and if none
93 * is found, reads the term values in <code>field</code> and returns an array
94 * of size <code>reader.maxDoc()</code> containing the value each document
95 * has in the given field.
96 * @param reader Used to get field values.
97 * @param field Which field contains the strings.
98 * @return The values in the given field for each document.
99 * @throws IOException If any error occurs.
100 */
101 virtual FieldCacheAuto* getStrings (CL_NS(index)::IndexReader* reader, const TCHAR* field) = 0;
102
103 /** Checks the internal cache for an appropriate entry, and if none
104 * is found reads the term values in <code>field</code> and returns
105 * an array of them in natural order, along with an array telling
106 * which element in the term array each document uses.
107 * @param reader Used to get field values.
108 * @param field Which field contains the strings.
109 * @return Array of terms and index into the array for each document.
110 * @throws IOException If any error occurs.
111 */
112 virtual FieldCacheAuto* getStringIndex (CL_NS(index)::IndexReader* reader, const TCHAR* field) = 0;
113
114 /** Checks the internal cache for an appropriate entry, and if
115 * none is found reads <code>field</code> to see if it contains integers, floats
116 * or strings, and then calls one of the other methods in this class to get the
117 * values. For string values, a FieldCache::StringIndex is returned. After
118 * calling this method, there is an entry in the cache for both
119 * type <code>AUTO</code> and the actual found type.
120 * @param reader Used to get field values.
121 * @param field Which field contains the values.
122 * @return int32_t[], qreal[] or FieldCache::StringIndex.
123 * @throws IOException If any error occurs.
124 */
125 virtual FieldCacheAuto* getAuto (CL_NS(index)::IndexReader* reader, const TCHAR* field) = 0;
126
127 /** Checks the internal cache for an appropriate entry, and if none
128 * is found reads the terms out of <code>field</code> and calls the given SortComparator
129 * to get the sort values. A hit in the cache will happen if <code>reader</code>,
130 * <code>field</code>, and <code>comparator</code> are the same (using <code>equals()</code>)
131 * as a previous call to this method.
132 * @param reader Used to get field values.
133 * @param field Which field contains the values.
134 * @param comparator Used to convert terms into something to sort by.
135 * @return Array of sort objects, one for each document.
136 * @throws IOException If any error occurs.
137 */
138 virtual FieldCacheAuto* getCustom (CL_NS(index)::IndexReader* reader, const TCHAR* field, SortComparator* comparator) = 0;
139};
140
141/** A class holding an AUTO field. In java lucene an Object
142 is used, but we use this.
143 contentType:
144 1 - integer array
145 2 - float array
146 3 - FieldCache::StringIndex object
147 This class is also used when returning getInt, getFloat, etc
148 because we have no way of returning the size of the array and this
149 class can be used to determine the array size
150*/
151class FieldCacheAuto:LUCENE_BASE{
152public:
153 enum{
154 INT_ARRAY=1,
155 FLOAT_ARRAY=2,
156 STRING_INDEX=3,
157 STRING_ARRAY=4,
158 COMPARABLE_ARRAY=5,
159 SORT_COMPARATOR=6,
160 SCOREDOC_COMPARATOR=7
161 };
162
163 FieldCacheAuto(int32_t len, int32_t type);
164 ~FieldCacheAuto();
165 ///if contents should be deleted too, depending on type
166 bool ownContents;
167 int32_t contentLen; //number of items in the list
168 uint8_t contentType;
169 int32_t* intArray; //item 1
170 qreal* floatArray; //item 2
171 FieldCache::StringIndex* stringIndex; //item 3
172 TCHAR** stringArray; //item 4
173 CL_NS(util)::Comparable** comparableArray; //item 5
174 SortComparator* sortComparator; //item 6
175 ScoreDocComparator* scoreDocComparator; //item 7
176
177};
178
179
180CL_NS_END
181
182#endif
183