sdsykes-ferret 0.11.6.19

Sign up to get free protection for your applications and to get access to all the features.
Files changed (195) hide show
  1. data/CHANGELOG +24 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README +102 -0
  4. data/Rakefile +338 -0
  5. data/TODO +17 -0
  6. data/TUTORIAL +231 -0
  7. data/bin/ferret-browser +79 -0
  8. data/ext/analysis.c +1555 -0
  9. data/ext/analysis.h +219 -0
  10. data/ext/api.c +69 -0
  11. data/ext/api.h +27 -0
  12. data/ext/array.c +123 -0
  13. data/ext/array.h +53 -0
  14. data/ext/bitvector.c +540 -0
  15. data/ext/bitvector.h +272 -0
  16. data/ext/compound_io.c +383 -0
  17. data/ext/config.h +42 -0
  18. data/ext/document.c +156 -0
  19. data/ext/document.h +53 -0
  20. data/ext/except.c +120 -0
  21. data/ext/except.h +168 -0
  22. data/ext/extconf.rb +14 -0
  23. data/ext/ferret.c +402 -0
  24. data/ext/ferret.h +91 -0
  25. data/ext/filter.c +156 -0
  26. data/ext/fs_store.c +483 -0
  27. data/ext/global.c +418 -0
  28. data/ext/global.h +117 -0
  29. data/ext/hash.c +567 -0
  30. data/ext/hash.h +473 -0
  31. data/ext/hashset.c +170 -0
  32. data/ext/hashset.h +187 -0
  33. data/ext/header.h +58 -0
  34. data/ext/helper.c +62 -0
  35. data/ext/helper.h +13 -0
  36. data/ext/inc/lang.h +48 -0
  37. data/ext/inc/threading.h +31 -0
  38. data/ext/index.c +6425 -0
  39. data/ext/index.h +961 -0
  40. data/ext/lang.h +66 -0
  41. data/ext/libstemmer.c +92 -0
  42. data/ext/libstemmer.h +79 -0
  43. data/ext/mempool.c +87 -0
  44. data/ext/mempool.h +35 -0
  45. data/ext/modules.h +162 -0
  46. data/ext/multimapper.c +310 -0
  47. data/ext/multimapper.h +51 -0
  48. data/ext/posh.c +1006 -0
  49. data/ext/posh.h +1007 -0
  50. data/ext/priorityqueue.c +151 -0
  51. data/ext/priorityqueue.h +143 -0
  52. data/ext/q_boolean.c +1608 -0
  53. data/ext/q_const_score.c +161 -0
  54. data/ext/q_filtered_query.c +209 -0
  55. data/ext/q_fuzzy.c +268 -0
  56. data/ext/q_match_all.c +148 -0
  57. data/ext/q_multi_term.c +677 -0
  58. data/ext/q_parser.c +2825 -0
  59. data/ext/q_phrase.c +1126 -0
  60. data/ext/q_prefix.c +100 -0
  61. data/ext/q_range.c +350 -0
  62. data/ext/q_span.c +2402 -0
  63. data/ext/q_term.c +337 -0
  64. data/ext/q_wildcard.c +171 -0
  65. data/ext/r_analysis.c +2575 -0
  66. data/ext/r_index.c +3472 -0
  67. data/ext/r_qparser.c +585 -0
  68. data/ext/r_search.c +4105 -0
  69. data/ext/r_store.c +513 -0
  70. data/ext/r_utils.c +963 -0
  71. data/ext/ram_store.c +471 -0
  72. data/ext/search.c +1741 -0
  73. data/ext/search.h +885 -0
  74. data/ext/similarity.c +150 -0
  75. data/ext/similarity.h +82 -0
  76. data/ext/sort.c +983 -0
  77. data/ext/stem_ISO_8859_1_danish.c +338 -0
  78. data/ext/stem_ISO_8859_1_danish.h +16 -0
  79. data/ext/stem_ISO_8859_1_dutch.c +635 -0
  80. data/ext/stem_ISO_8859_1_dutch.h +16 -0
  81. data/ext/stem_ISO_8859_1_english.c +1156 -0
  82. data/ext/stem_ISO_8859_1_english.h +16 -0
  83. data/ext/stem_ISO_8859_1_finnish.c +792 -0
  84. data/ext/stem_ISO_8859_1_finnish.h +16 -0
  85. data/ext/stem_ISO_8859_1_french.c +1276 -0
  86. data/ext/stem_ISO_8859_1_french.h +16 -0
  87. data/ext/stem_ISO_8859_1_german.c +512 -0
  88. data/ext/stem_ISO_8859_1_german.h +16 -0
  89. data/ext/stem_ISO_8859_1_italian.c +1091 -0
  90. data/ext/stem_ISO_8859_1_italian.h +16 -0
  91. data/ext/stem_ISO_8859_1_norwegian.c +296 -0
  92. data/ext/stem_ISO_8859_1_norwegian.h +16 -0
  93. data/ext/stem_ISO_8859_1_porter.c +776 -0
  94. data/ext/stem_ISO_8859_1_porter.h +16 -0
  95. data/ext/stem_ISO_8859_1_portuguese.c +1035 -0
  96. data/ext/stem_ISO_8859_1_portuguese.h +16 -0
  97. data/ext/stem_ISO_8859_1_spanish.c +1119 -0
  98. data/ext/stem_ISO_8859_1_spanish.h +16 -0
  99. data/ext/stem_ISO_8859_1_swedish.c +307 -0
  100. data/ext/stem_ISO_8859_1_swedish.h +16 -0
  101. data/ext/stem_KOI8_R_russian.c +701 -0
  102. data/ext/stem_KOI8_R_russian.h +16 -0
  103. data/ext/stem_UTF_8_danish.c +344 -0
  104. data/ext/stem_UTF_8_danish.h +16 -0
  105. data/ext/stem_UTF_8_dutch.c +653 -0
  106. data/ext/stem_UTF_8_dutch.h +16 -0
  107. data/ext/stem_UTF_8_english.c +1176 -0
  108. data/ext/stem_UTF_8_english.h +16 -0
  109. data/ext/stem_UTF_8_finnish.c +808 -0
  110. data/ext/stem_UTF_8_finnish.h +16 -0
  111. data/ext/stem_UTF_8_french.c +1296 -0
  112. data/ext/stem_UTF_8_french.h +16 -0
  113. data/ext/stem_UTF_8_german.c +526 -0
  114. data/ext/stem_UTF_8_german.h +16 -0
  115. data/ext/stem_UTF_8_italian.c +1113 -0
  116. data/ext/stem_UTF_8_italian.h +16 -0
  117. data/ext/stem_UTF_8_norwegian.c +302 -0
  118. data/ext/stem_UTF_8_norwegian.h +16 -0
  119. data/ext/stem_UTF_8_porter.c +794 -0
  120. data/ext/stem_UTF_8_porter.h +16 -0
  121. data/ext/stem_UTF_8_portuguese.c +1055 -0
  122. data/ext/stem_UTF_8_portuguese.h +16 -0
  123. data/ext/stem_UTF_8_russian.c +709 -0
  124. data/ext/stem_UTF_8_russian.h +16 -0
  125. data/ext/stem_UTF_8_spanish.c +1137 -0
  126. data/ext/stem_UTF_8_spanish.h +16 -0
  127. data/ext/stem_UTF_8_swedish.c +313 -0
  128. data/ext/stem_UTF_8_swedish.h +16 -0
  129. data/ext/stopwords.c +401 -0
  130. data/ext/store.c +692 -0
  131. data/ext/store.h +777 -0
  132. data/ext/term_vectors.c +352 -0
  133. data/ext/threading.h +31 -0
  134. data/ext/utilities.c +446 -0
  135. data/ext/win32.h +54 -0
  136. data/lib/ferret.rb +29 -0
  137. data/lib/ferret/browser.rb +246 -0
  138. data/lib/ferret/browser/s/global.js +192 -0
  139. data/lib/ferret/browser/s/style.css +148 -0
  140. data/lib/ferret/browser/views/document/list.rhtml +49 -0
  141. data/lib/ferret/browser/views/document/show.rhtml +27 -0
  142. data/lib/ferret/browser/views/error/index.rhtml +7 -0
  143. data/lib/ferret/browser/views/help/index.rhtml +8 -0
  144. data/lib/ferret/browser/views/home/index.rhtml +29 -0
  145. data/lib/ferret/browser/views/layout.rhtml +22 -0
  146. data/lib/ferret/browser/views/term-vector/index.rhtml +4 -0
  147. data/lib/ferret/browser/views/term/index.rhtml +199 -0
  148. data/lib/ferret/browser/views/term/termdocs.rhtml +1 -0
  149. data/lib/ferret/browser/webrick.rb +14 -0
  150. data/lib/ferret/document.rb +130 -0
  151. data/lib/ferret/field_infos.rb +44 -0
  152. data/lib/ferret/index.rb +786 -0
  153. data/lib/ferret/number_tools.rb +157 -0
  154. data/lib/ferret_version.rb +3 -0
  155. data/setup.rb +1555 -0
  156. data/test/test_all.rb +5 -0
  157. data/test/test_helper.rb +24 -0
  158. data/test/threading/number_to_spoken.rb +132 -0
  159. data/test/threading/thread_safety_index_test.rb +79 -0
  160. data/test/threading/thread_safety_read_write_test.rb +76 -0
  161. data/test/threading/thread_safety_test.rb +133 -0
  162. data/test/unit/analysis/tc_analyzer.rb +548 -0
  163. data/test/unit/analysis/tc_token_stream.rb +646 -0
  164. data/test/unit/index/tc_index.rb +762 -0
  165. data/test/unit/index/tc_index_reader.rb +699 -0
  166. data/test/unit/index/tc_index_writer.rb +437 -0
  167. data/test/unit/index/th_doc.rb +315 -0
  168. data/test/unit/largefile/tc_largefile.rb +46 -0
  169. data/test/unit/query_parser/tc_query_parser.rb +238 -0
  170. data/test/unit/search/tc_filter.rb +135 -0
  171. data/test/unit/search/tc_fuzzy_query.rb +147 -0
  172. data/test/unit/search/tc_index_searcher.rb +61 -0
  173. data/test/unit/search/tc_multi_searcher.rb +128 -0
  174. data/test/unit/search/tc_multiple_search_requests.rb +58 -0
  175. data/test/unit/search/tc_search_and_sort.rb +179 -0
  176. data/test/unit/search/tc_sort.rb +49 -0
  177. data/test/unit/search/tc_sort_field.rb +27 -0
  178. data/test/unit/search/tc_spans.rb +190 -0
  179. data/test/unit/search/tm_searcher.rb +384 -0
  180. data/test/unit/store/tc_fs_store.rb +77 -0
  181. data/test/unit/store/tc_ram_store.rb +35 -0
  182. data/test/unit/store/tm_store.rb +34 -0
  183. data/test/unit/store/tm_store_lock.rb +68 -0
  184. data/test/unit/tc_document.rb +81 -0
  185. data/test/unit/ts_analysis.rb +2 -0
  186. data/test/unit/ts_index.rb +2 -0
  187. data/test/unit/ts_largefile.rb +4 -0
  188. data/test/unit/ts_query_parser.rb +2 -0
  189. data/test/unit/ts_search.rb +2 -0
  190. data/test/unit/ts_store.rb +2 -0
  191. data/test/unit/ts_utils.rb +2 -0
  192. data/test/unit/utils/tc_bit_vector.rb +295 -0
  193. data/test/unit/utils/tc_number_tools.rb +117 -0
  194. data/test/unit/utils/tc_priority_queue.rb +106 -0
  195. metadata +285 -0
@@ -0,0 +1,337 @@
1
+ #include <string.h>
2
+ #include "search.h"
3
+
4
+ #define TQ(query) ((TermQuery *)(query))
5
+ #define TSc(scorer) ((TermScorer *)(scorer))
6
+
7
+ /***************************************************************************
8
+ *
9
+ * TermScorer
10
+ *
11
+ ***************************************************************************/
12
+
13
+ #define SCORE_CACHE_SIZE 32
14
+ #define TDE_READ_SIZE 32
15
+
16
+ typedef struct TermScorer
17
+ {
18
+ Scorer super;
19
+ int docs[TDE_READ_SIZE];
20
+ int freqs[TDE_READ_SIZE];
21
+ int pointer;
22
+ int pointer_max;
23
+ float score_cache[SCORE_CACHE_SIZE];
24
+ Weight *weight;
25
+ TermDocEnum *tde;
26
+ uchar *norms;
27
+ float weight_value;
28
+ } TermScorer;
29
+
30
+ static float tsc_score(Scorer *self)
31
+ {
32
+ TermScorer *ts = TSc(self);
33
+ int freq = ts->freqs[ts->pointer];
34
+ float score;
35
+ /* compute tf(f)*weight */
36
+ if (freq < SCORE_CACHE_SIZE) { /* check cache */
37
+ score = ts->score_cache[freq]; /* cache hit */
38
+ }
39
+ else {
40
+ /* cache miss */
41
+ score = sim_tf(self->similarity, (float)freq) * ts->weight_value;
42
+ }
43
+ /* normalize for field */
44
+ score *= sim_decode_norm(self->similarity, ts->norms[self->doc]);
45
+ return score;
46
+ }
47
+
48
+ static bool tsc_next(Scorer *self)
49
+ {
50
+ TermScorer *ts = TSc(self);
51
+
52
+ ts->pointer++;
53
+ if (ts->pointer >= ts->pointer_max) {
54
+ /* refill buffer */
55
+ ts->pointer_max = ts->tde->read(ts->tde, ts->docs, ts->freqs,
56
+ TDE_READ_SIZE);
57
+ if (ts->pointer_max != 0) {
58
+ ts->pointer = 0;
59
+ }
60
+ else {
61
+ return false;
62
+ }
63
+ }
64
+ self->doc = ts->docs[ts->pointer];
65
+ return true;
66
+ }
67
+
68
+ static bool tsc_skip_to(Scorer *self, int doc_num)
69
+ {
70
+ TermScorer *ts = TSc(self);
71
+ TermDocEnum *tde = ts->tde;
72
+
73
+ /* first scan in cache */
74
+ while (++(ts->pointer) < ts->pointer_max) {
75
+ if (ts->docs[ts->pointer] >= doc_num) {
76
+ self->doc = ts->docs[ts->pointer];
77
+ return true;
78
+ }
79
+ }
80
+
81
+ /* not found in cache, seek underlying stream */
82
+ if (tde->skip_to(tde, doc_num)) {
83
+ ts->pointer_max = 1;
84
+ ts->pointer = 0;
85
+ ts->docs[0] = self->doc = tde->doc_num(tde);
86
+ ts->freqs[0] = tde->freq(tde);
87
+ return true;
88
+ }
89
+ else {
90
+ return false;
91
+ }
92
+ }
93
+
94
+ static Explanation *tsc_explain(Scorer *self, int doc_num)
95
+ {
96
+ TermScorer *ts = TSc(self);
97
+ Query *query = ts->weight->get_query(ts->weight);
98
+ int tf = 0;
99
+
100
+ tsc_skip_to(self, doc_num);
101
+ if (self->doc == doc_num) {
102
+ tf = ts->freqs[ts->pointer];
103
+ }
104
+ return expl_new(sim_tf(self->similarity, (float)tf),
105
+ "tf(term_freq(%s:%s)=%d)",
106
+ TQ(query)->field, TQ(query)->term, tf);
107
+ }
108
+
109
+ static void tsc_destroy(Scorer *self)
110
+ {
111
+ TSc(self)->tde->close(TSc(self)->tde);
112
+ scorer_destroy_i(self);
113
+ }
114
+
115
+ static Scorer *tsc_new(Weight *weight, TermDocEnum *tde, uchar *norms)
116
+ {
117
+ int i;
118
+ Scorer *self = scorer_new(TermScorer, weight->similarity);
119
+ TSc(self)->weight = weight;
120
+ TSc(self)->tde = tde;
121
+ TSc(self)->norms = norms;
122
+ TSc(self)->weight_value = weight->value;
123
+
124
+ for (i = 0; i < SCORE_CACHE_SIZE; i++) {
125
+ TSc(self)->score_cache[i]
126
+ = sim_tf(self->similarity, (float)i) * TSc(self)->weight_value;
127
+ }
128
+
129
+ self->score = &tsc_score;
130
+ self->next = &tsc_next;
131
+ self->skip_to = &tsc_skip_to;
132
+ self->explain = &tsc_explain;
133
+ self->destroy = &tsc_destroy;
134
+ return self;
135
+ }
136
+
137
+ /***************************************************************************
138
+ *
139
+ * TermWeight
140
+ *
141
+ ***************************************************************************/
142
+
143
+ static Scorer *tw_scorer(Weight *self, IndexReader *ir)
144
+ {
145
+ TermQuery *tq = TQ(self->query);
146
+ TermDocEnum *tde = ir_term_docs_for(ir, tq->field, tq->term);
147
+ if (!tde) {
148
+ return NULL;
149
+ }
150
+
151
+ return tsc_new(self, tde, ir_get_norms(ir, tq->field));
152
+ }
153
+
154
+ static Explanation *tw_explain(Weight *self, IndexReader *ir, int doc_num)
155
+ {
156
+ Explanation *qnorm_expl;
157
+ Explanation *field_expl;
158
+ Scorer *scorer;
159
+ Explanation *tf_expl;
160
+ uchar *field_norms;
161
+ float field_norm;
162
+ Explanation *field_norm_expl;
163
+
164
+ char *query_str = self->query->to_s(self->query, "");
165
+ TermQuery *tq = TQ(self->query);
166
+ char *term = tq->term;
167
+ char *field = tq->field;
168
+
169
+ Explanation *expl = expl_new(0.0, "weight(%s in %d), product of:",
170
+ query_str, doc_num);
171
+
172
+ /* We need two of these as it's included in both the query explanation
173
+ * and the field explanation */
174
+ Explanation *idf_expl1 = expl_new(self->idf, "idf(doc_freq=%d)",
175
+ ir_doc_freq(ir, field, term));
176
+ Explanation *idf_expl2 = expl_new(self->idf, "idf(doc_freq=%d)",
177
+ ir_doc_freq(ir, field, term));
178
+
179
+ /* explain query weight */
180
+ Explanation *query_expl = expl_new(0.0, "query_weight(%s), product of:",
181
+ query_str);
182
+ free(query_str);
183
+
184
+ if (self->query->boost != 1.0) {
185
+ expl_add_detail(query_expl, expl_new(self->query->boost, "boost"));
186
+ }
187
+
188
+ expl_add_detail(query_expl, idf_expl1);
189
+
190
+ qnorm_expl = expl_new(self->qnorm, "query_norm");
191
+ expl_add_detail(query_expl, qnorm_expl);
192
+
193
+ query_expl->value = self->query->boost
194
+ * idf_expl1->value * qnorm_expl->value;
195
+
196
+ expl_add_detail(expl, query_expl);
197
+
198
+ /* explain field weight */
199
+ field_expl = expl_new(0.0, "field_weight(%s:%s in %d), product of:",
200
+ field, term, doc_num);
201
+
202
+ scorer = self->scorer(self, ir);
203
+ tf_expl = scorer->explain(scorer, doc_num);
204
+ scorer->destroy(scorer);
205
+ expl_add_detail(field_expl, tf_expl);
206
+ expl_add_detail(field_expl, idf_expl2);
207
+
208
+ field_norms = ir_get_norms(ir, field);
209
+ field_norm = (field_norms
210
+ ? sim_decode_norm(self->similarity, field_norms[doc_num])
211
+ : (float)0.0);
212
+ field_norm_expl = expl_new(field_norm, "field_norm(field=%s, doc=%d)",
213
+ field, doc_num);
214
+
215
+ expl_add_detail(field_expl, field_norm_expl);
216
+
217
+ field_expl->value = tf_expl->value * idf_expl2->value
218
+ * field_norm_expl->value;
219
+
220
+ /* combine them */
221
+ if (query_expl->value == 1.0) {
222
+ expl_destroy(expl);
223
+ return field_expl;
224
+ } else {
225
+ expl->value = (query_expl->value * field_expl->value);
226
+ expl_add_detail(expl, field_expl);
227
+ return expl;
228
+ }
229
+ }
230
+
231
+ static char *tw_to_s(Weight *self)
232
+ {
233
+ return strfmt("TermWeight(%f)", self->value);
234
+ }
235
+
236
+ static Weight *tw_new(Query *query, Searcher *searcher)
237
+ {
238
+ Weight *self = w_new(Weight, query);
239
+ self->scorer = &tw_scorer;
240
+ self->explain = &tw_explain;
241
+ self->to_s = &tw_to_s;
242
+
243
+ self->similarity = query->get_similarity(query, searcher);
244
+ self->idf = sim_idf(self->similarity,
245
+ searcher->doc_freq(searcher,
246
+ TQ(query)->field,
247
+ TQ(query)->term),
248
+ searcher->max_doc(searcher)); /* compute idf */
249
+
250
+ return self;
251
+ }
252
+
253
+ /***************************************************************************
254
+ *
255
+ * TermQuery
256
+ *
257
+ ***************************************************************************/
258
+
259
+ static void tq_destroy(Query *self)
260
+ {
261
+ free(TQ(self)->term);
262
+ free(TQ(self)->field);
263
+ q_destroy_i(self);
264
+ }
265
+
266
+ static char *tq_to_s(Query *self, const char *field)
267
+ {
268
+ size_t flen = strlen(TQ(self)->field);
269
+ size_t tlen = strlen(TQ(self)->term);
270
+ char *buffer = ALLOC_N(char, 34 + flen + tlen);
271
+ char *b = buffer;
272
+ if (strcmp(field, TQ(self)->field) != 0) {
273
+ memcpy(b, TQ(self)->field, sizeof(char) * flen);
274
+ b[flen] = ':';
275
+ b += flen + 1;
276
+ }
277
+ memcpy(b, TQ(self)->term, tlen);
278
+ b += tlen;
279
+ *b = 0;
280
+ if (self->boost != 1.0) {
281
+ *b = '^';
282
+ dbl_to_s(b+1, self->boost);
283
+ }
284
+ return buffer;
285
+ }
286
+
287
+ static void tq_extract_terms(Query *self, HashSet *terms)
288
+ {
289
+ hs_add(terms, term_new(TQ(self)->field, TQ(self)->term));
290
+ }
291
+
292
+ static unsigned long tq_hash(Query *self)
293
+ {
294
+ return str_hash(TQ(self)->term) ^ str_hash(TQ(self)->field);
295
+ }
296
+
297
+ static int tq_eq(Query *self, Query *o)
298
+ {
299
+ return (strcmp(TQ(self)->term, TQ(o)->term) == 0)
300
+ && (strcmp(TQ(self)->field, TQ(o)->field) == 0);
301
+ }
302
+
303
+ static MatchVector *tq_get_matchv_i(Query *self, MatchVector *mv,
304
+ TermVector *tv)
305
+ {
306
+ if (strcmp(tv->field, TQ(self)->field) == 0) {
307
+ int i;
308
+ TVTerm *tv_term = tv_get_tv_term(tv, TQ(self)->term);
309
+ if (tv_term) {
310
+ for (i = 0; i < tv_term->freq; i++) {
311
+ int pos = tv_term->positions[i];
312
+ matchv_add(mv, pos, pos);
313
+ }
314
+ }
315
+ }
316
+ return mv;
317
+ }
318
+
319
+ Query *tq_new(const char *field, const char *term)
320
+ {
321
+ Query *self = q_new(TermQuery);
322
+
323
+ TQ(self)->field = estrdup(field);
324
+ TQ(self)->term = estrdup(term);
325
+
326
+ self->type = TERM_QUERY;
327
+ self->extract_terms = &tq_extract_terms;
328
+ self->to_s = &tq_to_s;
329
+ self->hash = &tq_hash;
330
+ self->eq = &tq_eq;
331
+
332
+ self->destroy_i = &tq_destroy;
333
+ self->create_weight_i = &tw_new;
334
+ self->get_matchv_i = &tq_get_matchv_i;
335
+
336
+ return self;
337
+ }
@@ -0,0 +1,171 @@
1
+ #include <string.h>
2
+ #include "search.h"
3
+
4
+ /****************************************************************************
5
+ *
6
+ * WildCardQuery
7
+ *
8
+ ****************************************************************************/
9
+
10
+ #define WCQ(query) ((WildCardQuery *)(query))
11
+
12
+ static char *wcq_to_s(Query *self, const char *current_field)
13
+ {
14
+ char *buffer, *bptr;
15
+ const char *field = WCQ(self)->field;
16
+ const char *pattern = WCQ(self)->pattern;
17
+ size_t flen = strlen(field);
18
+ size_t plen = strlen(pattern);
19
+ bptr = buffer = ALLOC_N(char, plen + flen + 35);
20
+
21
+ if (strcmp(field, current_field) != 0) {
22
+ sprintf(bptr, "%s:", field);
23
+ bptr += flen + 1;
24
+ }
25
+ sprintf(bptr, "%s", pattern);
26
+ bptr += plen;
27
+
28
+ if (self->boost != 1.0) {
29
+ *bptr = '^';
30
+ dbl_to_s(++bptr, self->boost);
31
+ }
32
+
33
+ return buffer;
34
+ }
35
+
36
+ bool wc_match(const char *pattern, const char *text)
37
+ {
38
+ const char *p = pattern, *t = text, *xt;
39
+
40
+ /* include '\0' as we need to match empty string */
41
+ const char *text_last = t + strlen(t);
42
+
43
+ for (;; p++, t++) {
44
+
45
+ /* end of text so make sure end of pattern doesn't matter */
46
+ if (*t == '\0') {
47
+ while (*p) {
48
+ if (*p != WILD_STRING) {
49
+ return false;
50
+ }
51
+ p++;
52
+ }
53
+ return true;
54
+ }
55
+
56
+ /* If we've gone past the end of the pattern, return false. */
57
+ if (*p == '\0') {
58
+ return false;
59
+ }
60
+
61
+ /* Match a single character, so continue. */
62
+ if (*p == WILD_CHAR) {
63
+ continue;
64
+ }
65
+
66
+ if (*p == WILD_STRING) {
67
+ /* Look at the character beyond the '*'. */
68
+ p++;
69
+ /* Examine the string, starting at the last character. */
70
+ for (xt = text_last; xt >= t; xt--) {
71
+ if (wc_match(p, xt)) return true;
72
+ }
73
+ return false;
74
+ }
75
+ if (*p != *t) {
76
+ return false;
77
+ }
78
+ }
79
+
80
+ return false;
81
+ }
82
+
83
+ static Query *wcq_rewrite(Query *self, IndexReader *ir)
84
+ {
85
+ Query *q;
86
+ const char *field = WCQ(self)->field;
87
+ const char *pattern = WCQ(self)->pattern;
88
+ const char *first_star = strchr(pattern, WILD_STRING);
89
+ const char *first_ques = strchr(pattern, WILD_CHAR);
90
+
91
+ if (NULL == first_star && NULL == first_ques) {
92
+ q = tq_new(field, pattern);
93
+ q->boost = self->boost;
94
+ }
95
+ else {
96
+ const int field_num = fis_get_field_num(ir->fis, field);
97
+ q = multi_tq_new_conf(field, MTQMaxTerms(self), 0.0);
98
+
99
+ if (field_num >= 0) {
100
+ TermEnum *te;
101
+ char prefix[MAX_WORD_SIZE] = "";
102
+ int prefix_len;
103
+
104
+ pattern = (first_ques && (!first_star || first_star > first_ques))
105
+ ? first_ques : first_star;
106
+
107
+ prefix_len = (int)(pattern - WCQ(self)->pattern);
108
+
109
+ if (prefix_len > 0) {
110
+ memcpy(prefix, WCQ(self)->pattern, prefix_len);
111
+ prefix[prefix_len] = '\0';
112
+ }
113
+
114
+ te = ir->terms_from(ir, field_num, prefix);
115
+
116
+ if (te != NULL) {
117
+ const char *term = te->curr_term;
118
+ const char *pat_term = term + prefix_len;
119
+ do {
120
+ if (prefix && strncmp(term, prefix, prefix_len) != 0) {
121
+ break;
122
+ }
123
+
124
+ if (wc_match(pattern, pat_term)) {
125
+ multi_tq_add_term(q, term);
126
+ }
127
+ } while (te->next(te) != NULL);
128
+ te->close(te);
129
+ }
130
+ }
131
+ }
132
+
133
+ return q;
134
+ }
135
+
136
+ static void wcq_destroy(Query *self)
137
+ {
138
+ free(WCQ(self)->field);
139
+ free(WCQ(self)->pattern);
140
+ q_destroy_i(self);
141
+ }
142
+
143
+ static unsigned long wcq_hash(Query *self)
144
+ {
145
+ return str_hash(WCQ(self)->field) ^ str_hash(WCQ(self)->pattern);
146
+ }
147
+
148
+ static int wcq_eq(Query *self, Query *o)
149
+ {
150
+ return (strcmp(WCQ(self)->pattern, WCQ(o)->pattern) == 0)
151
+ && (strcmp(WCQ(self)->field, WCQ(o)->field) == 0);
152
+ }
153
+
154
+ Query *wcq_new(const char *field, const char *pattern)
155
+ {
156
+ Query *self = q_new(WildCardQuery);
157
+
158
+ WCQ(self)->field = estrdup(field);
159
+ WCQ(self)->pattern = estrdup(pattern);
160
+ MTQMaxTerms(self) = WILD_CARD_QUERY_MAX_TERMS;
161
+
162
+ self->type = WILD_CARD_QUERY;
163
+ self->rewrite = &wcq_rewrite;
164
+ self->to_s = &wcq_to_s;
165
+ self->hash = &wcq_hash;
166
+ self->eq = &wcq_eq;
167
+ self->destroy_i = &wcq_destroy;
168
+ self->create_weight_i = &q_create_weight_unsup;
169
+
170
+ return self;
171
+ }