ferret 0.9.0 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +23 -5
- data/TODO +2 -1
- data/ext/analysis.c +838 -177
- data/ext/analysis.h +55 -7
- data/ext/api.c +69 -0
- data/ext/api.h +27 -0
- data/ext/array.c +8 -5
- data/ext/compound_io.c +132 -96
- data/ext/document.c +58 -28
- data/ext/except.c +59 -0
- data/ext/except.h +88 -0
- data/ext/ferret.c +47 -3
- data/ext/ferret.h +3 -0
- data/ext/field.c +15 -9
- data/ext/filter.c +1 -1
- data/ext/fs_store.c +215 -34
- data/ext/global.c +72 -3
- data/ext/global.h +4 -3
- data/ext/hash.c +44 -3
- data/ext/hash.h +9 -0
- data/ext/header.h +58 -0
- data/ext/inc/except.h +88 -0
- data/ext/inc/lang.h +23 -13
- data/ext/ind.c +16 -10
- data/ext/index.h +2 -22
- data/ext/index_io.c +3 -11
- data/ext/index_rw.c +245 -193
- data/ext/lang.h +23 -13
- data/ext/libstemmer.c +92 -0
- data/ext/libstemmer.h +79 -0
- data/ext/modules.h +162 -0
- data/ext/q_boolean.c +34 -21
- data/ext/q_const_score.c +6 -12
- data/ext/q_filtered_query.c +206 -0
- data/ext/q_fuzzy.c +18 -15
- data/ext/q_match_all.c +3 -7
- data/ext/q_multi_phrase.c +10 -14
- data/ext/q_parser.c +29 -2
- data/ext/q_phrase.c +14 -21
- data/ext/q_prefix.c +15 -12
- data/ext/q_range.c +30 -28
- data/ext/q_span.c +13 -21
- data/ext/q_term.c +17 -26
- data/ext/r_analysis.c +693 -21
- data/ext/r_doc.c +11 -12
- data/ext/r_index_io.c +4 -1
- data/ext/r_qparser.c +21 -2
- data/ext/r_search.c +285 -18
- data/ext/ram_store.c +5 -2
- data/ext/search.c +11 -17
- data/ext/search.h +21 -45
- data/ext/similarity.h +67 -0
- data/ext/sort.c +30 -25
- data/ext/stem_ISO_8859_1_danish.c +338 -0
- data/ext/stem_ISO_8859_1_danish.h +16 -0
- data/ext/stem_ISO_8859_1_dutch.c +635 -0
- data/ext/stem_ISO_8859_1_dutch.h +16 -0
- data/ext/stem_ISO_8859_1_english.c +1156 -0
- data/ext/stem_ISO_8859_1_english.h +16 -0
- data/ext/stem_ISO_8859_1_finnish.c +792 -0
- data/ext/stem_ISO_8859_1_finnish.h +16 -0
- data/ext/stem_ISO_8859_1_french.c +1276 -0
- data/ext/stem_ISO_8859_1_french.h +16 -0
- data/ext/stem_ISO_8859_1_german.c +512 -0
- data/ext/stem_ISO_8859_1_german.h +16 -0
- data/ext/stem_ISO_8859_1_italian.c +1091 -0
- data/ext/stem_ISO_8859_1_italian.h +16 -0
- data/ext/stem_ISO_8859_1_norwegian.c +296 -0
- data/ext/stem_ISO_8859_1_norwegian.h +16 -0
- data/ext/stem_ISO_8859_1_porter.c +776 -0
- data/ext/stem_ISO_8859_1_porter.h +16 -0
- data/ext/stem_ISO_8859_1_portuguese.c +1035 -0
- data/ext/stem_ISO_8859_1_portuguese.h +16 -0
- data/ext/stem_ISO_8859_1_spanish.c +1119 -0
- data/ext/stem_ISO_8859_1_spanish.h +16 -0
- data/ext/stem_ISO_8859_1_swedish.c +307 -0
- data/ext/stem_ISO_8859_1_swedish.h +16 -0
- data/ext/stem_KOI8_R_russian.c +701 -0
- data/ext/stem_KOI8_R_russian.h +16 -0
- data/ext/stem_UTF_8_danish.c +344 -0
- data/ext/stem_UTF_8_danish.h +16 -0
- data/ext/stem_UTF_8_dutch.c +653 -0
- data/ext/stem_UTF_8_dutch.h +16 -0
- data/ext/stem_UTF_8_english.c +1176 -0
- data/ext/stem_UTF_8_english.h +16 -0
- data/ext/stem_UTF_8_finnish.c +808 -0
- data/ext/stem_UTF_8_finnish.h +16 -0
- data/ext/stem_UTF_8_french.c +1296 -0
- data/ext/stem_UTF_8_french.h +16 -0
- data/ext/stem_UTF_8_german.c +526 -0
- data/ext/stem_UTF_8_german.h +16 -0
- data/ext/stem_UTF_8_italian.c +1113 -0
- data/ext/stem_UTF_8_italian.h +16 -0
- data/ext/stem_UTF_8_norwegian.c +302 -0
- data/ext/stem_UTF_8_norwegian.h +16 -0
- data/ext/stem_UTF_8_porter.c +794 -0
- data/ext/stem_UTF_8_porter.h +16 -0
- data/ext/stem_UTF_8_portuguese.c +1055 -0
- data/ext/stem_UTF_8_portuguese.h +16 -0
- data/ext/stem_UTF_8_russian.c +709 -0
- data/ext/stem_UTF_8_russian.h +16 -0
- data/ext/stem_UTF_8_spanish.c +1137 -0
- data/ext/stem_UTF_8_spanish.h +16 -0
- data/ext/stem_UTF_8_swedish.c +313 -0
- data/ext/stem_UTF_8_swedish.h +16 -0
- data/ext/stopwords.c +325 -0
- data/ext/store.c +34 -2
- data/ext/tags +2953 -0
- data/ext/term.c +21 -15
- data/ext/termdocs.c +5 -3
- data/ext/utilities.c +446 -0
- data/ext/vector.c +27 -13
- data/lib/ferret/document/document.rb +1 -1
- data/lib/ferret/index/index.rb +44 -6
- data/lib/ferret/query_parser/query_parser.tab.rb +7 -3
- data/lib/rferret.rb +2 -1
- data/test/test_helper.rb +2 -2
- data/test/unit/analysis/ctc_analyzer.rb +401 -0
- data/test/unit/analysis/ctc_tokenstream.rb +423 -0
- data/test/unit/analysis/{tc_letter_tokenizer.rb → rtc_letter_tokenizer.rb} +0 -0
- data/test/unit/analysis/{tc_lower_case_filter.rb → rtc_lower_case_filter.rb} +0 -0
- data/test/unit/analysis/{tc_lower_case_tokenizer.rb → rtc_lower_case_tokenizer.rb} +0 -0
- data/test/unit/analysis/{tc_per_field_analyzer_wrapper.rb → rtc_per_field_analyzer_wrapper.rb} +0 -0
- data/test/unit/analysis/{tc_porter_stem_filter.rb → rtc_porter_stem_filter.rb} +0 -0
- data/test/unit/analysis/{tc_standard_analyzer.rb → rtc_standard_analyzer.rb} +0 -0
- data/test/unit/analysis/{tc_standard_tokenizer.rb → rtc_standard_tokenizer.rb} +0 -0
- data/test/unit/analysis/{tc_stop_analyzer.rb → rtc_stop_analyzer.rb} +0 -0
- data/test/unit/analysis/{tc_stop_filter.rb → rtc_stop_filter.rb} +0 -0
- data/test/unit/analysis/{tc_white_space_analyzer.rb → rtc_white_space_analyzer.rb} +0 -0
- data/test/unit/analysis/{tc_white_space_tokenizer.rb → rtc_white_space_tokenizer.rb} +0 -0
- data/test/unit/analysis/{tc_word_list_loader.rb → rtc_word_list_loader.rb} +0 -0
- data/test/unit/analysis/tc_analyzer.rb +1 -2
- data/test/unit/analysis/{c_token.rb → tc_token.rb} +0 -0
- data/test/unit/document/rtc_field.rb +28 -0
- data/test/unit/document/{c_document.rb → tc_document.rb} +0 -0
- data/test/unit/document/tc_field.rb +82 -12
- data/test/unit/index/{tc_compound_file_io.rb → rtc_compound_file_io.rb} +0 -0
- data/test/unit/index/{tc_field_infos.rb → rtc_field_infos.rb} +0 -0
- data/test/unit/index/{tc_fields_io.rb → rtc_fields_io.rb} +0 -0
- data/test/unit/index/{tc_multiple_term_doc_pos_enum.rb → rtc_multiple_term_doc_pos_enum.rb} +0 -0
- data/test/unit/index/{tc_segment_infos.rb → rtc_segment_infos.rb} +0 -0
- data/test/unit/index/{tc_segment_term_docs.rb → rtc_segment_term_docs.rb} +0 -0
- data/test/unit/index/{tc_segment_term_enum.rb → rtc_segment_term_enum.rb} +0 -0
- data/test/unit/index/{tc_segment_term_vector.rb → rtc_segment_term_vector.rb} +0 -0
- data/test/unit/index/{tc_term_buffer.rb → rtc_term_buffer.rb} +0 -0
- data/test/unit/index/{tc_term_info.rb → rtc_term_info.rb} +0 -0
- data/test/unit/index/{tc_term_infos_io.rb → rtc_term_infos_io.rb} +0 -0
- data/test/unit/index/{tc_term_vectors_io.rb → rtc_term_vectors_io.rb} +0 -0
- data/test/unit/index/{c_index.rb → tc_index.rb} +26 -6
- data/test/unit/index/{c_index_reader.rb → tc_index_reader.rb} +0 -0
- data/test/unit/index/{c_index_writer.rb → tc_index_writer.rb} +0 -0
- data/test/unit/index/{c_term.rb → tc_term.rb} +0 -0
- data/test/unit/index/{c_term_voi.rb → tc_term_voi.rb} +0 -0
- data/test/unit/query_parser/{c_query_parser.rb → rtc_query_parser.rb} +14 -14
- data/test/unit/query_parser/tc_query_parser.rb +24 -16
- data/test/unit/search/{tc_similarity.rb → rtc_similarity.rb} +0 -0
- data/test/unit/search/rtc_sort_field.rb +14 -0
- data/test/unit/search/{c_filter.rb → tc_filter.rb} +11 -11
- data/test/unit/search/{c_fuzzy_query.rb → tc_fuzzy_query.rb} +0 -0
- data/test/unit/search/{c_index_searcher.rb → tc_index_searcher.rb} +0 -0
- data/test/unit/search/{c_search_and_sort.rb → tc_search_and_sort.rb} +0 -0
- data/test/unit/search/{c_sort.rb → tc_sort.rb} +0 -0
- data/test/unit/search/tc_sort_field.rb +20 -7
- data/test/unit/search/{c_spans.rb → tc_spans.rb} +0 -0
- data/test/unit/store/rtc_fs_store.rb +62 -0
- data/test/unit/store/rtc_ram_store.rb +15 -0
- data/test/unit/store/rtm_store.rb +150 -0
- data/test/unit/store/rtm_store_lock.rb +2 -0
- data/test/unit/store/tc_fs_store.rb +54 -40
- data/test/unit/store/tc_ram_store.rb +20 -0
- data/test/unit/store/tm_store.rb +30 -146
- data/test/unit/store/tm_store_lock.rb +66 -0
- data/test/unit/utils/{tc_bit_vector.rb → rtc_bit_vector.rb} +0 -0
- data/test/unit/utils/{tc_date_tools.rb → rtc_date_tools.rb} +0 -0
- data/test/unit/utils/{tc_number_tools.rb → rtc_number_tools.rb} +0 -0
- data/test/unit/utils/{tc_parameter.rb → rtc_parameter.rb} +0 -0
- data/test/unit/utils/{tc_priority_queue.rb → rtc_priority_queue.rb} +0 -0
- data/test/unit/utils/{tc_string_helper.rb → rtc_string_helper.rb} +0 -0
- data/test/unit/utils/{tc_thread.rb → rtc_thread.rb} +0 -0
- data/test/unit/utils/{tc_weak_key_hash.rb → rtc_weak_key_hash.rb} +0 -0
- metadata +360 -289
- data/test/unit/document/c_field.rb +0 -98
- data/test/unit/search/c_sort_field.rb +0 -27
- data/test/unit/store/c_fs_store.rb +0 -76
- data/test/unit/store/c_ram_store.rb +0 -35
- data/test/unit/store/m_store.rb +0 -34
- data/test/unit/store/m_store_lock.rb +0 -68
data/ext/document.c
CHANGED
@@ -7,15 +7,23 @@
|
|
7
7
|
*
|
8
8
|
****************************************************************************/
|
9
9
|
|
10
|
+
static char * const INDEX_NO_STORE_NO_MSG = "it doesn't make sense to have a field that is neither indexed nor stored";
|
11
|
+
static char * const INDEX_NO_TV_YES_MSG = "cannot store term vector information for a field that is not indexed";
|
12
|
+
static char * const INVALID_STORE_VAL_MSG = "Invalid value for store in document field";
|
13
|
+
static char * const INVALID_INDEX_VAL_MSG = "Invalid value for index in document field";
|
14
|
+
static char * const INVALID_TV_VAL_MSG = "Invalid value for term_vector in document field";
|
15
|
+
static char * const BIN_FIELD_STORE_NO_MSG = "It doesn't make sense not to store binary data";
|
16
|
+
|
17
|
+
/**
|
18
|
+
* @throws ARG_ERROR
|
19
|
+
*/
|
10
20
|
inline void df_set(DocField *df, const char *name,
|
11
21
|
char *data, int store, int index, int tv)
|
12
22
|
{
|
13
23
|
if ((index == DF_INDEX_NO) && (store == DF_STORE_NO))
|
14
|
-
|
15
|
-
"it doesn't make sense to have a field that is neither indexed nor stored");
|
24
|
+
RAISE(ARG_ERROR, INDEX_NO_STORE_NO_MSG);
|
16
25
|
if ((index == DF_INDEX_NO) && (tv != DF_TERM_VECTOR_NO))
|
17
|
-
|
18
|
-
"cannot store term vector information for a field that is not indexed");
|
26
|
+
RAISE(ARG_ERROR, INDEX_NO_TV_YES_MSG);
|
19
27
|
df->name = estrdup(name);
|
20
28
|
df->data = data;
|
21
29
|
df->blen = strlen(data);
|
@@ -26,6 +34,9 @@ inline void df_set(DocField *df, const char *name,
|
|
26
34
|
df->boost = 1.0;
|
27
35
|
}
|
28
36
|
|
37
|
+
/*
|
38
|
+
* @throws ARG_ERROR
|
39
|
+
*/
|
29
40
|
DocField *df_create(const char *name, char *data, int store, int index, int tv)
|
30
41
|
{
|
31
42
|
DocField *df = ALLOC(DocField);
|
@@ -57,6 +68,9 @@ void df_destroy_data(void *p)
|
|
57
68
|
free(p);
|
58
69
|
}
|
59
70
|
|
71
|
+
/*
|
72
|
+
* @throws ARG_ERROR
|
73
|
+
*/
|
60
74
|
void df_set_store(DocField *df, int store)
|
61
75
|
{
|
62
76
|
switch (store) {
|
@@ -73,10 +87,13 @@ void df_set_store(DocField *df, int store)
|
|
73
87
|
df->is_compressed = true;
|
74
88
|
break;
|
75
89
|
default:
|
76
|
-
|
90
|
+
RAISE(ARG_ERROR, INVALID_STORE_VAL_MSG);
|
77
91
|
}
|
78
92
|
}
|
79
93
|
|
94
|
+
/*
|
95
|
+
* @throws ARG_ERROR
|
96
|
+
*/
|
80
97
|
void df_set_index(DocField *df, int index)
|
81
98
|
{
|
82
99
|
df->omit_norms = false;
|
@@ -99,10 +116,13 @@ void df_set_index(DocField *df, int index)
|
|
99
116
|
df->omit_norms = true;
|
100
117
|
break;
|
101
118
|
default:
|
102
|
-
|
119
|
+
RAISE(ARG_ERROR, INVALID_INDEX_VAL_MSG);
|
103
120
|
}
|
104
121
|
}
|
105
122
|
|
123
|
+
/*
|
124
|
+
* @throws ARG_ERROR
|
125
|
+
*/
|
106
126
|
void df_set_term_vector(DocField *df, int tv)
|
107
127
|
{
|
108
128
|
switch (tv) {
|
@@ -132,15 +152,17 @@ void df_set_term_vector(DocField *df, int tv)
|
|
132
152
|
df->store_pos = true;
|
133
153
|
break;
|
134
154
|
default:
|
135
|
-
|
136
|
-
"Invalid value %d for term_vector in document field", tv);
|
155
|
+
RAISE(ARG_ERROR, INVALID_TV_VAL_MSG);
|
137
156
|
}
|
138
157
|
}
|
139
158
|
|
159
|
+
/*
|
160
|
+
* @throws ARG_ERROR
|
161
|
+
*/
|
140
162
|
DocField *df_create_binary(char *name, char *data, int blen, int store)
|
141
163
|
{
|
142
164
|
if (store == DF_STORE_NO) {
|
143
|
-
|
165
|
+
RAISE(ARG_ERROR, BIN_FIELD_STORE_NO_MSG);
|
144
166
|
}
|
145
167
|
DocField *df = df_create(name, data, store, DF_INDEX_NO, DF_TERM_VECTOR_NO);
|
146
168
|
df->is_binary = true;
|
@@ -200,7 +222,13 @@ Document *doc_create_keep_data()
|
|
200
222
|
|
201
223
|
void doc_destroy(void *p)
|
202
224
|
{
|
225
|
+
int i;
|
203
226
|
Document *doc = (Document *)p;
|
227
|
+
if (doc->free_data) {
|
228
|
+
for (i = 0; i < doc->dfcnt; i++) {
|
229
|
+
doc->free_data(doc->df_arr[i]);
|
230
|
+
}
|
231
|
+
}
|
204
232
|
free(doc->field_arr);
|
205
233
|
free(doc->df_arr);
|
206
234
|
h_destroy(doc->fields);
|
@@ -209,15 +237,15 @@ void doc_destroy(void *p)
|
|
209
237
|
|
210
238
|
void doc_add_field(Document *doc, DocField *df)
|
211
239
|
{
|
212
|
-
Array *
|
213
|
-
if (
|
214
|
-
|
215
|
-
h_set(doc->fields, estrdup(df->name),
|
240
|
+
Array *fields = (Array *)h_get(doc->fields, df->name);
|
241
|
+
if (fields == NULL) {
|
242
|
+
fields = ary_create(1, NULL);
|
243
|
+
h_set(doc->fields, estrdup(df->name), fields);
|
216
244
|
doc->fcnt++;
|
217
245
|
REALLOC_N(doc->field_arr, Array *, doc->fcnt);
|
218
|
-
doc->field_arr[doc->fcnt-1] =
|
246
|
+
doc->field_arr[doc->fcnt-1] = fields;
|
219
247
|
}
|
220
|
-
ary_append(
|
248
|
+
ary_append(fields, df);
|
221
249
|
doc->dfcnt++;
|
222
250
|
REALLOC_N(doc->df_arr, DocField *, doc->dfcnt);
|
223
251
|
doc->df_arr[doc->dfcnt-1] = df;
|
@@ -225,9 +253,9 @@ void doc_add_field(Document *doc, DocField *df)
|
|
225
253
|
|
226
254
|
DocField *doc_get_field(Document *doc, const char *fname)
|
227
255
|
{
|
228
|
-
Array *
|
229
|
-
if (
|
230
|
-
return
|
256
|
+
Array *fields = (Array *)h_get(doc->fields, fname);
|
257
|
+
if (fields) {
|
258
|
+
return fields->elems[0];
|
231
259
|
} else {
|
232
260
|
return NULL;
|
233
261
|
}
|
@@ -246,20 +274,20 @@ Array *doc_get_fields(Document *doc, const char *fname)
|
|
246
274
|
*/
|
247
275
|
Array *doc_remove_fields(Document *doc, const char *fname)
|
248
276
|
{
|
249
|
-
Array *
|
250
|
-
if (
|
277
|
+
Array *fields = (Array *)h_rem(doc->fields, fname, true);
|
278
|
+
if (fields) {
|
251
279
|
int i, j;
|
252
280
|
doc->fcnt--;
|
253
281
|
for (i = 0; i < doc->fcnt; i++) {
|
254
|
-
if (
|
282
|
+
if (fields == doc->field_arr[i]) {
|
255
283
|
memmove(&doc->field_arr[i],
|
256
284
|
&doc->field_arr[i+1],
|
257
285
|
sizeof(void *) * (doc->fcnt - i));
|
258
286
|
break;
|
259
287
|
}
|
260
288
|
}
|
261
|
-
for (i = 0, j = 0; i < doc->dfcnt && j <
|
262
|
-
if (
|
289
|
+
for (i = 0, j = 0; i < doc->dfcnt && j < fields->size;) {
|
290
|
+
if (fields->elems[j] == doc->df_arr[i]) {
|
263
291
|
memmove(&doc->df_arr[i],
|
264
292
|
&doc->df_arr[i+1],
|
265
293
|
sizeof(void *) * (doc->dfcnt - i - 1));
|
@@ -269,7 +297,8 @@ Array *doc_remove_fields(Document *doc, const char *fname)
|
|
269
297
|
i++;
|
270
298
|
}
|
271
299
|
}
|
272
|
-
|
300
|
+
fields->free_elem = doc->free_data;
|
301
|
+
return fields;
|
273
302
|
} else {
|
274
303
|
return NULL;
|
275
304
|
}
|
@@ -283,6 +312,7 @@ DocField *doc_remove_field(Document *doc, const char *fname)
|
|
283
312
|
df = ary_remove(dfs, 0);
|
284
313
|
if (dfs->size == 0) {
|
285
314
|
Array *fields = doc_remove_fields(doc, fname);
|
315
|
+
fields->free_elem = doc->free_data;
|
286
316
|
ary_destroy(fields);
|
287
317
|
} else {
|
288
318
|
int i;
|
@@ -302,14 +332,14 @@ DocField *doc_remove_field(Document *doc, const char *fname)
|
|
302
332
|
|
303
333
|
bool doc_delete_fields(Document *doc, const char *fname)
|
304
334
|
{
|
305
|
-
Array *
|
306
|
-
if (
|
307
|
-
|
335
|
+
Array *fields = doc_remove_fields(doc, fname);
|
336
|
+
if (fields) {
|
337
|
+
fields->free_elem = doc->free_data;
|
338
|
+
ary_destroy(fields);
|
308
339
|
return true;
|
309
340
|
} else {
|
310
341
|
return false;
|
311
342
|
}
|
312
|
-
return h_del(doc->fields, fname);
|
313
343
|
}
|
314
344
|
|
315
345
|
char *doc_to_s(Document *doc)
|
data/ext/except.c
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
#include "global.h"
|
2
|
+
#include "except.h"
|
3
|
+
|
4
|
+
char * const UNSUPPORTED_ERROR_MSG = "Unsupported operation";
|
5
|
+
char * const EOF_ERROR_MSG = "Read past end of file";
|
6
|
+
|
7
|
+
static thread_key_t exception_stack_key;
|
8
|
+
static thread_once_t exception_stack_key_once = THREAD_ONCE_INIT;
|
9
|
+
|
10
|
+
void exception_stack_alloc()
|
11
|
+
{
|
12
|
+
thread_key_create(&exception_stack_key, NULL);
|
13
|
+
}
|
14
|
+
|
15
|
+
void xpush_context(xcontext_t *context)
|
16
|
+
{
|
17
|
+
xcontext_t *top_context;
|
18
|
+
thread_once(&exception_stack_key_once, *exception_stack_alloc);
|
19
|
+
top_context = thread_getspecific(exception_stack_key);
|
20
|
+
context->next = top_context;
|
21
|
+
thread_setspecific(exception_stack_key, context);
|
22
|
+
context->handled = true;
|
23
|
+
context->in_finally = false;
|
24
|
+
}
|
25
|
+
|
26
|
+
static inline void xraise_context(xcontext_t *context, int excode, char *msg)
|
27
|
+
{
|
28
|
+
context->msg = msg;
|
29
|
+
context->excode = excode;
|
30
|
+
context->handled = false;
|
31
|
+
longjmp(context->jbuf, excode);
|
32
|
+
}
|
33
|
+
|
34
|
+
void xraise(int excode, char *msg)
|
35
|
+
{
|
36
|
+
xcontext_t *top_context;
|
37
|
+
top_context = thread_getspecific(exception_stack_key);
|
38
|
+
if (!top_context) {
|
39
|
+
eprintf(EXCEPTION_CODE, "Error: exception %d not handled: %s", excode, msg);
|
40
|
+
} else if (!top_context->in_finally) {
|
41
|
+
xraise_context(top_context, excode, msg);
|
42
|
+
} else if (top_context->handled) {
|
43
|
+
top_context->msg = msg;
|
44
|
+
top_context->excode = excode;
|
45
|
+
top_context->handled = false;
|
46
|
+
}
|
47
|
+
}
|
48
|
+
|
49
|
+
void xpop_context()
|
50
|
+
{
|
51
|
+
xcontext_t *top_context, *context;
|
52
|
+
thread_once(&exception_stack_key_once, *exception_stack_alloc);
|
53
|
+
top_context = thread_getspecific(exception_stack_key);
|
54
|
+
context = top_context->next;
|
55
|
+
thread_setspecific(exception_stack_key, context);
|
56
|
+
if (!top_context->handled) {
|
57
|
+
xraise_context(context, top_context->excode, top_context->msg);
|
58
|
+
}
|
59
|
+
}
|
data/ext/except.h
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
#ifndef FRT_EXCEPT_H
|
2
|
+
#define FRT_EXCEPT_H
|
3
|
+
|
4
|
+
#include <setjmp.h>
|
5
|
+
#include <ruby.h>
|
6
|
+
|
7
|
+
#define BODY 0
|
8
|
+
#define FINALLY -1
|
9
|
+
#define EXCEPTION 1
|
10
|
+
#define ERROR 1
|
11
|
+
#define IO_ERROR 2
|
12
|
+
#define ARG_ERROR 3
|
13
|
+
#define EOF_ERROR 4
|
14
|
+
#define UNSUPPORTED_ERROR 5
|
15
|
+
#define STATE_ERROR 6
|
16
|
+
#define PARSE_ERROR 7
|
17
|
+
#define MEM_ERROR 8
|
18
|
+
|
19
|
+
typedef struct xcontext_t {
|
20
|
+
jmp_buf jbuf;
|
21
|
+
struct xcontext_t *next;
|
22
|
+
char *msg;
|
23
|
+
volatile int excode;
|
24
|
+
int handled : 1;
|
25
|
+
int in_finally : 1;
|
26
|
+
} xcontext_t;
|
27
|
+
|
28
|
+
RUBY_EXTERN int rb_thread_critical;
|
29
|
+
extern xcontext_t *xtop_context;
|
30
|
+
|
31
|
+
#define TRY\
|
32
|
+
xcontext_t xcontext;\
|
33
|
+
rb_thread_critical = Qtrue;\
|
34
|
+
xcontext.next = xtop_context;\
|
35
|
+
xtop_context = &xcontext;\
|
36
|
+
xcontext.handled = true;\
|
37
|
+
xcontext.in_finally = false;\
|
38
|
+
switch (setjmp(xcontext.jbuf)) {\
|
39
|
+
case BODY:
|
40
|
+
|
41
|
+
|
42
|
+
#define XENDTRY\
|
43
|
+
}\
|
44
|
+
xtop_context = xcontext.next;\
|
45
|
+
if (!xcontext.handled) {\
|
46
|
+
RAISE(xcontext.excode, xcontext.msg);\
|
47
|
+
}\
|
48
|
+
rb_thread_critical = 0;
|
49
|
+
|
50
|
+
#define ENDTRY\
|
51
|
+
}\
|
52
|
+
if (!xcontext.in_finally) {\
|
53
|
+
xtop_context = xcontext.next;\
|
54
|
+
if (!xcontext.handled) {\
|
55
|
+
RAISE(xcontext.excode, xcontext.msg);\
|
56
|
+
}\
|
57
|
+
xcontext.in_finally = 1;\
|
58
|
+
longjmp(xcontext.jbuf, FINALLY);\
|
59
|
+
}\
|
60
|
+
rb_thread_critical = 0;
|
61
|
+
|
62
|
+
#define XFINALLY default: xcontext.in_finally = 1;
|
63
|
+
|
64
|
+
#define XCATCHALL break; default: xcontext.in_finally = 1;
|
65
|
+
|
66
|
+
//fprintf(stderr,"Error occured in %s, %d: %s\n", __FILE__, __LINE__, __func__);
|
67
|
+
#define RAISE(xexcode, xmsg) \
|
68
|
+
do {\
|
69
|
+
if (!xtop_context) {\
|
70
|
+
eprintf(EXCEPTION_CODE, "Error: exception %d not handled: %s", xexcode, xmsg);\
|
71
|
+
} else if (!xtop_context->in_finally) {\
|
72
|
+
xtop_context->msg = xmsg;\
|
73
|
+
xtop_context->excode = xexcode;\
|
74
|
+
xtop_context->handled = false;\
|
75
|
+
longjmp(xtop_context->jbuf, xexcode);\
|
76
|
+
} else if (xtop_context->handled) {\
|
77
|
+
xtop_context->msg = xmsg;\
|
78
|
+
xtop_context->excode = xexcode;\
|
79
|
+
xtop_context->handled = false;\
|
80
|
+
}\
|
81
|
+
} while (0)
|
82
|
+
|
83
|
+
#define HANDLED() xcontext.handled = 1 /* true */
|
84
|
+
|
85
|
+
extern char * const UNSUPPORTED_ERROR_MSG;
|
86
|
+
extern char * const EOF_ERROR_MSG;
|
87
|
+
|
88
|
+
#endif
|
data/ext/ferret.c
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
#include "ferret.h"
|
2
|
+
#include "except.h"
|
2
3
|
#include "hash.h"
|
3
4
|
|
4
5
|
/* Object Map */
|
@@ -22,15 +23,16 @@ VALUE mSpans;
|
|
22
23
|
/*
|
23
24
|
*/
|
24
25
|
|
26
|
+
xcontext_t *xtop_context = NULL;
|
25
27
|
|
26
28
|
unsigned int
|
27
|
-
|
29
|
+
value_hash(const void *key)
|
28
30
|
{
|
29
31
|
return (unsigned int)key;
|
30
32
|
}
|
31
33
|
|
32
34
|
int
|
33
|
-
|
35
|
+
value_eq(const void *key1, const void *key2)
|
34
36
|
{
|
35
37
|
return key1 == key2;
|
36
38
|
}
|
@@ -54,6 +56,15 @@ object_add2(void *key, VALUE obj, const char *file, int line, const char *func)
|
|
54
56
|
h_set(object_map, key, (void *)obj);
|
55
57
|
}
|
56
58
|
|
59
|
+
void
|
60
|
+
//object_set(void *key, VALUE obj)
|
61
|
+
object_set2(void *key, VALUE obj, const char *file, int line, const char *func)
|
62
|
+
{
|
63
|
+
//if (!h_get(object_map, key))
|
64
|
+
//printf("seting %d. now contains %d %s:%d:%s\n", (int)key, ++hash_cnt, file, line, func);
|
65
|
+
h_set(object_map, key, (void *)obj);
|
66
|
+
}
|
67
|
+
|
57
68
|
void
|
58
69
|
//object_del(void *key)
|
59
70
|
object_del2(void *key, const char *file, int line, const char *func)
|
@@ -84,11 +95,44 @@ frt_deref_free(void *p)
|
|
84
95
|
object_del(p);
|
85
96
|
}
|
86
97
|
|
98
|
+
void
|
99
|
+
frt_thread_once(int *once_control, void (*init_routine) (void))
|
100
|
+
{
|
101
|
+
if (*once_control) {
|
102
|
+
init_routine();
|
103
|
+
*once_control = 0;
|
104
|
+
}
|
105
|
+
}
|
106
|
+
|
107
|
+
void
|
108
|
+
frt_thread_key_create(thread_key_t *key, void (*destr_function) (void *))
|
109
|
+
{
|
110
|
+
*key = h_new(&value_hash, &value_eq, NULL, destr_function);
|
111
|
+
}
|
112
|
+
|
113
|
+
void
|
114
|
+
frt_thread_key_delete(thread_key_t key)
|
115
|
+
{
|
116
|
+
h_destroy(key);
|
117
|
+
}
|
118
|
+
|
119
|
+
void
|
120
|
+
frt_thread_setspecific(thread_key_t key, const void *pointer)
|
121
|
+
{
|
122
|
+
h_set(key, (void *)rb_thread_current(), (void *)pointer);
|
123
|
+
}
|
124
|
+
|
125
|
+
void *
|
126
|
+
frt_thread_getspecific(thread_key_t key)
|
127
|
+
{
|
128
|
+
return h_get(key, (void *)rb_thread_current());
|
129
|
+
}
|
130
|
+
|
87
131
|
void
|
88
132
|
Init_ferret_ext(void)
|
89
133
|
{
|
90
134
|
/* initialize object map */
|
91
|
-
object_map = h_new(&
|
135
|
+
object_map = h_new(&value_hash, &value_eq, NULL, NULL);
|
92
136
|
|
93
137
|
/* IDs */
|
94
138
|
id_new = rb_intern("new");
|
data/ext/ferret.h
CHANGED
@@ -32,6 +32,9 @@ extern void Init_qparser();
|
|
32
32
|
//extern void object_add(void *key, VALUE obj);
|
33
33
|
#define object_add(key, obj) object_add2(key, obj, __FILE__, __LINE__, __func__)
|
34
34
|
extern void object_add2(void *key, VALUE obj, const char *file, int line, const char *func);
|
35
|
+
//extern void object_set(void *key, VALUE obj);
|
36
|
+
#define object_set(key, obj) object_set2(key, obj, __FILE__, __LINE__, __func__)
|
37
|
+
extern void object_set2(void *key, VALUE obj, const char *file, int line, const char *func);
|
35
38
|
//extern void object_del(void *key);
|
36
39
|
#define object_del(key) object_del2(key, __FILE__, __LINE__, __func__)
|
37
40
|
extern void object_del2(void *key, const char *file, int line, const char *func);
|