ferret 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README +109 -0
- data/Rakefile +275 -0
- data/TODO +9 -0
- data/TUTORIAL +197 -0
- data/ext/extconf.rb +3 -0
- data/ext/ferret.c +23 -0
- data/ext/ferret.h +85 -0
- data/ext/index_io.c +543 -0
- data/ext/priority_queue.c +227 -0
- data/ext/ram_directory.c +316 -0
- data/ext/segment_merge_queue.c +41 -0
- data/ext/string_helper.c +42 -0
- data/ext/tags +240 -0
- data/ext/term.c +261 -0
- data/ext/term_buffer.c +299 -0
- data/ext/util.c +12 -0
- data/lib/ferret.rb +41 -0
- data/lib/ferret/analysis.rb +11 -0
- data/lib/ferret/analysis/analyzers.rb +93 -0
- data/lib/ferret/analysis/standard_tokenizer.rb +65 -0
- data/lib/ferret/analysis/token.rb +79 -0
- data/lib/ferret/analysis/token_filters.rb +86 -0
- data/lib/ferret/analysis/token_stream.rb +26 -0
- data/lib/ferret/analysis/tokenizers.rb +107 -0
- data/lib/ferret/analysis/word_list_loader.rb +27 -0
- data/lib/ferret/document.rb +2 -0
- data/lib/ferret/document/document.rb +152 -0
- data/lib/ferret/document/field.rb +304 -0
- data/lib/ferret/index.rb +26 -0
- data/lib/ferret/index/compound_file_io.rb +343 -0
- data/lib/ferret/index/document_writer.rb +288 -0
- data/lib/ferret/index/field_infos.rb +259 -0
- data/lib/ferret/index/fields_io.rb +175 -0
- data/lib/ferret/index/index.rb +228 -0
- data/lib/ferret/index/index_file_names.rb +33 -0
- data/lib/ferret/index/index_reader.rb +462 -0
- data/lib/ferret/index/index_writer.rb +488 -0
- data/lib/ferret/index/multi_reader.rb +363 -0
- data/lib/ferret/index/multiple_term_doc_pos_enum.rb +105 -0
- data/lib/ferret/index/segment_infos.rb +130 -0
- data/lib/ferret/index/segment_merge_info.rb +47 -0
- data/lib/ferret/index/segment_merge_queue.rb +16 -0
- data/lib/ferret/index/segment_merger.rb +337 -0
- data/lib/ferret/index/segment_reader.rb +380 -0
- data/lib/ferret/index/segment_term_enum.rb +178 -0
- data/lib/ferret/index/segment_term_vector.rb +58 -0
- data/lib/ferret/index/term.rb +49 -0
- data/lib/ferret/index/term_buffer.rb +88 -0
- data/lib/ferret/index/term_doc_enum.rb +283 -0
- data/lib/ferret/index/term_enum.rb +52 -0
- data/lib/ferret/index/term_info.rb +41 -0
- data/lib/ferret/index/term_infos_io.rb +312 -0
- data/lib/ferret/index/term_vector_offset_info.rb +20 -0
- data/lib/ferret/index/term_vectors_io.rb +552 -0
- data/lib/ferret/query_parser.rb +274 -0
- data/lib/ferret/query_parser/query_parser.tab.rb +819 -0
- data/lib/ferret/search.rb +49 -0
- data/lib/ferret/search/boolean_clause.rb +100 -0
- data/lib/ferret/search/boolean_query.rb +303 -0
- data/lib/ferret/search/boolean_scorer.rb +294 -0
- data/lib/ferret/search/caching_wrapper_filter.rb +40 -0
- data/lib/ferret/search/conjunction_scorer.rb +99 -0
- data/lib/ferret/search/disjunction_sum_scorer.rb +203 -0
- data/lib/ferret/search/exact_phrase_scorer.rb +32 -0
- data/lib/ferret/search/explanation.rb +41 -0
- data/lib/ferret/search/field_cache.rb +216 -0
- data/lib/ferret/search/field_doc.rb +31 -0
- data/lib/ferret/search/field_sorted_hit_queue.rb +184 -0
- data/lib/ferret/search/filter.rb +11 -0
- data/lib/ferret/search/filtered_query.rb +130 -0
- data/lib/ferret/search/filtered_term_enum.rb +79 -0
- data/lib/ferret/search/fuzzy_query.rb +153 -0
- data/lib/ferret/search/fuzzy_term_enum.rb +244 -0
- data/lib/ferret/search/hit_collector.rb +34 -0
- data/lib/ferret/search/hit_queue.rb +11 -0
- data/lib/ferret/search/index_searcher.rb +173 -0
- data/lib/ferret/search/match_all_docs_query.rb +104 -0
- data/lib/ferret/search/multi_phrase_query.rb +204 -0
- data/lib/ferret/search/multi_term_query.rb +65 -0
- data/lib/ferret/search/non_matching_scorer.rb +22 -0
- data/lib/ferret/search/phrase_positions.rb +55 -0
- data/lib/ferret/search/phrase_query.rb +217 -0
- data/lib/ferret/search/phrase_scorer.rb +153 -0
- data/lib/ferret/search/prefix_query.rb +47 -0
- data/lib/ferret/search/query.rb +111 -0
- data/lib/ferret/search/query_filter.rb +51 -0
- data/lib/ferret/search/range_filter.rb +103 -0
- data/lib/ferret/search/range_query.rb +139 -0
- data/lib/ferret/search/req_excl_scorer.rb +125 -0
- data/lib/ferret/search/req_opt_sum_scorer.rb +70 -0
- data/lib/ferret/search/score_doc.rb +38 -0
- data/lib/ferret/search/score_doc_comparator.rb +114 -0
- data/lib/ferret/search/scorer.rb +91 -0
- data/lib/ferret/search/similarity.rb +278 -0
- data/lib/ferret/search/sloppy_phrase_scorer.rb +47 -0
- data/lib/ferret/search/sort.rb +105 -0
- data/lib/ferret/search/sort_comparator.rb +60 -0
- data/lib/ferret/search/sort_field.rb +87 -0
- data/lib/ferret/search/spans.rb +12 -0
- data/lib/ferret/search/spans/near_spans_enum.rb +304 -0
- data/lib/ferret/search/spans/span_first_query.rb +79 -0
- data/lib/ferret/search/spans/span_near_query.rb +108 -0
- data/lib/ferret/search/spans/span_not_query.rb +130 -0
- data/lib/ferret/search/spans/span_or_query.rb +176 -0
- data/lib/ferret/search/spans/span_query.rb +25 -0
- data/lib/ferret/search/spans/span_scorer.rb +74 -0
- data/lib/ferret/search/spans/span_term_query.rb +105 -0
- data/lib/ferret/search/spans/span_weight.rb +84 -0
- data/lib/ferret/search/spans/spans_enum.rb +44 -0
- data/lib/ferret/search/term_query.rb +128 -0
- data/lib/ferret/search/term_scorer.rb +181 -0
- data/lib/ferret/search/top_docs.rb +24 -0
- data/lib/ferret/search/top_field_docs.rb +17 -0
- data/lib/ferret/search/weight.rb +54 -0
- data/lib/ferret/search/wildcard_query.rb +26 -0
- data/lib/ferret/search/wildcard_term_enum.rb +61 -0
- data/lib/ferret/stemmers.rb +1 -0
- data/lib/ferret/stemmers/porter_stemmer.rb +218 -0
- data/lib/ferret/store.rb +5 -0
- data/lib/ferret/store/buffered_index_io.rb +191 -0
- data/lib/ferret/store/directory.rb +139 -0
- data/lib/ferret/store/fs_store.rb +338 -0
- data/lib/ferret/store/index_io.rb +259 -0
- data/lib/ferret/store/ram_store.rb +282 -0
- data/lib/ferret/utils.rb +7 -0
- data/lib/ferret/utils/bit_vector.rb +105 -0
- data/lib/ferret/utils/date_tools.rb +138 -0
- data/lib/ferret/utils/number_tools.rb +91 -0
- data/lib/ferret/utils/parameter.rb +41 -0
- data/lib/ferret/utils/priority_queue.rb +120 -0
- data/lib/ferret/utils/string_helper.rb +47 -0
- data/lib/ferret/utils/weak_key_hash.rb +51 -0
- data/rake_utils/code_statistics.rb +106 -0
- data/setup.rb +1551 -0
- data/test/benchmark/tb_ram_store.rb +76 -0
- data/test/benchmark/tb_rw_vint.rb +26 -0
- data/test/longrunning/tc_numbertools.rb +60 -0
- data/test/longrunning/tm_store.rb +19 -0
- data/test/test_all.rb +9 -0
- data/test/test_helper.rb +6 -0
- data/test/unit/analysis/tc_analyzer.rb +21 -0
- data/test/unit/analysis/tc_letter_tokenizer.rb +20 -0
- data/test/unit/analysis/tc_lower_case_filter.rb +20 -0
- data/test/unit/analysis/tc_lower_case_tokenizer.rb +27 -0
- data/test/unit/analysis/tc_per_field_analyzer_wrapper.rb +39 -0
- data/test/unit/analysis/tc_porter_stem_filter.rb +16 -0
- data/test/unit/analysis/tc_standard_analyzer.rb +20 -0
- data/test/unit/analysis/tc_standard_tokenizer.rb +20 -0
- data/test/unit/analysis/tc_stop_analyzer.rb +20 -0
- data/test/unit/analysis/tc_stop_filter.rb +14 -0
- data/test/unit/analysis/tc_white_space_analyzer.rb +21 -0
- data/test/unit/analysis/tc_white_space_tokenizer.rb +20 -0
- data/test/unit/analysis/tc_word_list_loader.rb +32 -0
- data/test/unit/document/tc_document.rb +47 -0
- data/test/unit/document/tc_field.rb +80 -0
- data/test/unit/index/tc_compound_file_io.rb +107 -0
- data/test/unit/index/tc_field_infos.rb +119 -0
- data/test/unit/index/tc_fields_io.rb +167 -0
- data/test/unit/index/tc_index.rb +140 -0
- data/test/unit/index/tc_index_reader.rb +622 -0
- data/test/unit/index/tc_index_writer.rb +57 -0
- data/test/unit/index/tc_multiple_term_doc_pos_enum.rb +80 -0
- data/test/unit/index/tc_segment_infos.rb +74 -0
- data/test/unit/index/tc_segment_term_docs.rb +17 -0
- data/test/unit/index/tc_segment_term_enum.rb +60 -0
- data/test/unit/index/tc_segment_term_vector.rb +71 -0
- data/test/unit/index/tc_term.rb +22 -0
- data/test/unit/index/tc_term_buffer.rb +57 -0
- data/test/unit/index/tc_term_info.rb +19 -0
- data/test/unit/index/tc_term_infos_io.rb +192 -0
- data/test/unit/index/tc_term_vector_offset_info.rb +18 -0
- data/test/unit/index/tc_term_vectors_io.rb +108 -0
- data/test/unit/index/th_doc.rb +244 -0
- data/test/unit/query_parser/tc_query_parser.rb +84 -0
- data/test/unit/search/tc_filter.rb +113 -0
- data/test/unit/search/tc_fuzzy_query.rb +136 -0
- data/test/unit/search/tc_index_searcher.rb +188 -0
- data/test/unit/search/tc_search_and_sort.rb +98 -0
- data/test/unit/search/tc_similarity.rb +37 -0
- data/test/unit/search/tc_sort.rb +48 -0
- data/test/unit/search/tc_sort_field.rb +27 -0
- data/test/unit/search/tc_spans.rb +153 -0
- data/test/unit/store/tc_fs_store.rb +84 -0
- data/test/unit/store/tc_ram_store.rb +35 -0
- data/test/unit/store/tm_store.rb +180 -0
- data/test/unit/store/tm_store_lock.rb +68 -0
- data/test/unit/ts_analysis.rb +16 -0
- data/test/unit/ts_document.rb +4 -0
- data/test/unit/ts_index.rb +18 -0
- data/test/unit/ts_query_parser.rb +3 -0
- data/test/unit/ts_search.rb +10 -0
- data/test/unit/ts_store.rb +6 -0
- data/test/unit/ts_utils.rb +10 -0
- data/test/unit/utils/tc_bit_vector.rb +65 -0
- data/test/unit/utils/tc_date_tools.rb +50 -0
- data/test/unit/utils/tc_number_tools.rb +59 -0
- data/test/unit/utils/tc_parameter.rb +40 -0
- data/test/unit/utils/tc_priority_queue.rb +62 -0
- data/test/unit/utils/tc_string_helper.rb +21 -0
- data/test/unit/utils/tc_weak_key_hash.rb +25 -0
- metadata +251 -0
@@ -0,0 +1,227 @@
|
|
1
|
+
#include "ferret.h"
|
2
|
+
|
3
|
+
ID less_than, put_heap;
|
4
|
+
/****************************************************************************
|
5
|
+
*
|
6
|
+
* PriorityQueue Methods
|
7
|
+
*
|
8
|
+
****************************************************************************/
|
9
|
+
|
10
|
+
void
|
11
|
+
frt_priq_free(void *p)
|
12
|
+
{
|
13
|
+
free(p);
|
14
|
+
}
|
15
|
+
|
16
|
+
static VALUE
|
17
|
+
frt_priq_alloc(VALUE klass)
|
18
|
+
{
|
19
|
+
PriorityQueue *priq;
|
20
|
+
priq = (PriorityQueue *)ALLOC(PriorityQueue);
|
21
|
+
priq->len = 0;
|
22
|
+
priq->size = 0;
|
23
|
+
VALUE rpriq = Data_Wrap_Struct(klass, NULL, frt_priq_free, priq);
|
24
|
+
return rpriq;
|
25
|
+
}
|
26
|
+
|
27
|
+
static VALUE
|
28
|
+
frt_priq_init(VALUE self, VALUE rsize)
|
29
|
+
{
|
30
|
+
VALUE heap;
|
31
|
+
PriorityQueue *priq;
|
32
|
+
|
33
|
+
int size = FIX2INT(rsize);
|
34
|
+
heap = rb_ary_new2(size+1);
|
35
|
+
Data_Get_Struct(self, PriorityQueue, priq);
|
36
|
+
|
37
|
+
priq->heap = RARRAY(heap)->ptr;
|
38
|
+
priq->size = size;
|
39
|
+
rb_iv_set(self, "@heap", heap);
|
40
|
+
return self;
|
41
|
+
}
|
42
|
+
|
43
|
+
void
|
44
|
+
priq_up(PriorityQueue *priq, VALUE self, VALUE rary)
|
45
|
+
{
|
46
|
+
int i,j;
|
47
|
+
VALUE *heap, node;
|
48
|
+
|
49
|
+
i = priq->len;
|
50
|
+
heap = priq->heap;
|
51
|
+
node = heap[i];
|
52
|
+
j = i >> 1;
|
53
|
+
while((j > 0) && rb_funcall(self, less_than, 2, node, heap[j])){
|
54
|
+
heap[i] = heap[j];
|
55
|
+
i = j;
|
56
|
+
j = j >> 1;
|
57
|
+
}
|
58
|
+
rb_ary_store(rary, i, node);
|
59
|
+
}
|
60
|
+
|
61
|
+
void
|
62
|
+
priq_down(PriorityQueue *priq, VALUE self, VALUE rary)
|
63
|
+
{
|
64
|
+
int i, j, k, len;
|
65
|
+
VALUE *heap, node;
|
66
|
+
|
67
|
+
i = 1;
|
68
|
+
heap = priq->heap;
|
69
|
+
len = priq->len;
|
70
|
+
node = heap[i];
|
71
|
+
j = i << 1;
|
72
|
+
k = j + 1;
|
73
|
+
|
74
|
+
if ((k <= len) && rb_funcall(self, less_than, 2, heap[k], heap[j]))
|
75
|
+
j = k;
|
76
|
+
|
77
|
+
while((j <= len) && rb_funcall(self, less_than, 2, heap[j], node)){
|
78
|
+
heap[i] = heap[j];
|
79
|
+
i = j;
|
80
|
+
j = i << 1;
|
81
|
+
k = j + 1;
|
82
|
+
if((k <= len) && rb_funcall(self, less_than, 2, heap[k], heap[j]))
|
83
|
+
j = k;
|
84
|
+
}
|
85
|
+
rb_ary_store(rary,i, node);
|
86
|
+
}
|
87
|
+
|
88
|
+
static VALUE
|
89
|
+
frt_priq_push(VALUE self, VALUE e)
|
90
|
+
{
|
91
|
+
PriorityQueue *priq;
|
92
|
+
Data_Get_Struct(self, PriorityQueue, priq);
|
93
|
+
int len = priq->len;
|
94
|
+
VALUE rary = rb_iv_get(self, "@heap");
|
95
|
+
|
96
|
+
len++;
|
97
|
+
rb_ary_store(rary, len, e);
|
98
|
+
priq->len = len;
|
99
|
+
priq_up(priq, self, rary);
|
100
|
+
|
101
|
+
return Qnil;
|
102
|
+
}
|
103
|
+
|
104
|
+
static VALUE
|
105
|
+
frt_priq_insert(VALUE self, VALUE e)
|
106
|
+
{
|
107
|
+
PriorityQueue *priq;
|
108
|
+
VALUE *heap, rary;
|
109
|
+
int len, size;
|
110
|
+
rary = rb_iv_get(self, "@heap");
|
111
|
+
|
112
|
+
Data_Get_Struct(self, PriorityQueue, priq);
|
113
|
+
len = priq->len;
|
114
|
+
size = priq->size;
|
115
|
+
heap = priq->heap;
|
116
|
+
|
117
|
+
if(len < size){
|
118
|
+
frt_priq_push(self, e);
|
119
|
+
return 1;
|
120
|
+
} else if ((len > 0) && !rb_funcall(self, less_than, 2, e, heap[1])) {
|
121
|
+
heap[1] = e;
|
122
|
+
priq_down(priq, self, rary);
|
123
|
+
return 1;
|
124
|
+
} else {
|
125
|
+
return 0;
|
126
|
+
}
|
127
|
+
}
|
128
|
+
|
129
|
+
static VALUE
|
130
|
+
frt_priq_top(VALUE self)
|
131
|
+
{
|
132
|
+
PriorityQueue *priq;
|
133
|
+
VALUE *heap;
|
134
|
+
int len;
|
135
|
+
|
136
|
+
Data_Get_Struct(self, PriorityQueue, priq);
|
137
|
+
len = priq->len;
|
138
|
+
heap = priq->heap;
|
139
|
+
|
140
|
+
if(len > 0)
|
141
|
+
return heap[1];
|
142
|
+
else
|
143
|
+
return Qnil;
|
144
|
+
}
|
145
|
+
|
146
|
+
static VALUE
|
147
|
+
frt_priq_pop(VALUE self)
|
148
|
+
{
|
149
|
+
PriorityQueue *priq;
|
150
|
+
VALUE *heap, rary;
|
151
|
+
int len;
|
152
|
+
|
153
|
+
Data_Get_Struct(self, PriorityQueue, priq);
|
154
|
+
rary = rb_iv_get(self, "@heap");
|
155
|
+
len = priq->len;
|
156
|
+
heap = priq->heap;
|
157
|
+
|
158
|
+
if(len > 0){
|
159
|
+
VALUE res = heap[1];
|
160
|
+
heap[1] = heap[len];
|
161
|
+
heap[len] = Qnil;
|
162
|
+
len--;
|
163
|
+
priq->len = len;
|
164
|
+
priq_down(priq, self, rary);
|
165
|
+
return res;
|
166
|
+
} else
|
167
|
+
return Qnil;
|
168
|
+
}
|
169
|
+
|
170
|
+
static VALUE
|
171
|
+
frt_priq_clear(VALUE self)
|
172
|
+
{
|
173
|
+
PriorityQueue *priq;
|
174
|
+
Data_Get_Struct(self, PriorityQueue, priq);
|
175
|
+
VALUE heap = rb_ary_new2(priq->size+1);
|
176
|
+
rb_iv_set(self, "@heap", heap);
|
177
|
+
priq->heap = RARRAY(heap)->ptr;
|
178
|
+
priq->len = 0;
|
179
|
+
return Qnil;
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
static VALUE
|
184
|
+
frt_priq_size(VALUE self)
|
185
|
+
{
|
186
|
+
PriorityQueue *priq;
|
187
|
+
Data_Get_Struct(self, PriorityQueue, priq);
|
188
|
+
return INT2FIX(priq->len);
|
189
|
+
}
|
190
|
+
|
191
|
+
static VALUE
|
192
|
+
frt_priq_adjust_top(VALUE self)
|
193
|
+
{
|
194
|
+
PriorityQueue *priq;
|
195
|
+
Data_Get_Struct(self, PriorityQueue, priq);
|
196
|
+
VALUE rary = rb_iv_get(self, "@heap");
|
197
|
+
|
198
|
+
priq_down(priq, self, rary);
|
199
|
+
|
200
|
+
return Qnil;
|
201
|
+
}
|
202
|
+
|
203
|
+
|
204
|
+
/****************************************************************************
|
205
|
+
*
|
206
|
+
* Init Function
|
207
|
+
*
|
208
|
+
****************************************************************************/
|
209
|
+
|
210
|
+
void
|
211
|
+
Init_priority_queue(void)
|
212
|
+
{
|
213
|
+
less_than = rb_intern("less_than");
|
214
|
+
put_heap = rb_intern("put_heap");
|
215
|
+
|
216
|
+
cPriorityQueue = rb_define_class_under(mUtils, "PriorityQueue", rb_cObject);
|
217
|
+
rb_define_alloc_func(cPriorityQueue, frt_priq_alloc);
|
218
|
+
|
219
|
+
rb_define_method(cPriorityQueue, "initialize", frt_priq_init, 1);
|
220
|
+
rb_define_method(cPriorityQueue, "pop", frt_priq_pop, 0);
|
221
|
+
rb_define_method(cPriorityQueue, "top", frt_priq_top, 0);
|
222
|
+
rb_define_method(cPriorityQueue, "clear", frt_priq_clear, 0);
|
223
|
+
rb_define_method(cPriorityQueue, "insert", frt_priq_insert, 1);
|
224
|
+
rb_define_method(cPriorityQueue, "push", frt_priq_push, 1);
|
225
|
+
rb_define_method(cPriorityQueue, "size", frt_priq_size, 0);
|
226
|
+
rb_define_method(cPriorityQueue, "adjust_top", frt_priq_adjust_top, 0);
|
227
|
+
}
|
data/ext/ram_directory.c
ADDED
@@ -0,0 +1,316 @@
|
|
1
|
+
#include "ferret.h"
|
2
|
+
|
3
|
+
ID flush, seek;
|
4
|
+
|
5
|
+
/****************************************************************************
|
6
|
+
*
|
7
|
+
* RAMFile Methods
|
8
|
+
*
|
9
|
+
****************************************************************************/
|
10
|
+
|
11
|
+
void
|
12
|
+
frt_rf_free(void *p)
|
13
|
+
{
|
14
|
+
int i;
|
15
|
+
RAMFile *rf = (RAMFile *)p;
|
16
|
+
|
17
|
+
for (i = 0; i < rf->bufcnt; i++) {
|
18
|
+
free(rf->buffers[i]);
|
19
|
+
}
|
20
|
+
free(rf->buffers);
|
21
|
+
free(rf);
|
22
|
+
}
|
23
|
+
|
24
|
+
void
|
25
|
+
frt_rf_mark(void *p)
|
26
|
+
{
|
27
|
+
RAMFile *rf = (RAMFile *)p;
|
28
|
+
rb_gc_mark(rf->mtime);
|
29
|
+
}
|
30
|
+
|
31
|
+
static VALUE
|
32
|
+
frt_rf_alloc(VALUE klass)
|
33
|
+
{
|
34
|
+
RAMFile *rf;
|
35
|
+
byte_t *buf;
|
36
|
+
|
37
|
+
rf = (RAMFile *)ALLOC(RAMFile);
|
38
|
+
buf = (byte_t *)ALLOC_N(byte_t, BUFFER_SIZE);
|
39
|
+
rf->buffers = (void **)ALLOC_N(void *, 1);
|
40
|
+
rf->buffers[0] = buf;
|
41
|
+
rf->bufcnt = 1;
|
42
|
+
rf->length = 0;
|
43
|
+
rf->mtime = rb_funcall(rb_cTime, frt_newobj, 0);
|
44
|
+
|
45
|
+
VALUE rrf = Data_Wrap_Struct(klass, frt_rf_mark, frt_rf_free, rf);
|
46
|
+
return rrf;
|
47
|
+
}
|
48
|
+
|
49
|
+
void
|
50
|
+
frt_rf_extend(RAMFile *rf)
|
51
|
+
{
|
52
|
+
byte_t *buf = (byte_t *)ALLOC_N(byte_t, BUFFER_SIZE);
|
53
|
+
rf->bufcnt++;
|
54
|
+
REALLOC_N(rf->buffers, void *, rf->bufcnt);
|
55
|
+
rf->buffers[rf->bufcnt - 1] = buf;
|
56
|
+
}
|
57
|
+
|
58
|
+
static VALUE
|
59
|
+
frt_rf_length(VALUE self)
|
60
|
+
{
|
61
|
+
RAMFile *rf;
|
62
|
+
Data_Get_Struct(self, RAMFile, rf);
|
63
|
+
|
64
|
+
return INT2FIX(rf->length);
|
65
|
+
}
|
66
|
+
|
67
|
+
/****************************************************************************
|
68
|
+
*
|
69
|
+
* RAMIndexOutput Methods
|
70
|
+
*
|
71
|
+
****************************************************************************/
|
72
|
+
|
73
|
+
static VALUE
|
74
|
+
frt_rio_init(VALUE self, VALUE ramfile)
|
75
|
+
{
|
76
|
+
rb_iv_set(self, "file", ramfile);
|
77
|
+
rb_iv_set(self, "pointer", INT2FIX(0));
|
78
|
+
return self;
|
79
|
+
}
|
80
|
+
|
81
|
+
static VALUE
|
82
|
+
frt_rio_length(VALUE self)
|
83
|
+
{
|
84
|
+
VALUE file = rb_iv_get(self, "file");
|
85
|
+
RAMFile *rf;
|
86
|
+
Data_Get_Struct(file, RAMFile, rf);
|
87
|
+
return INT2FIX(rf->length);
|
88
|
+
}
|
89
|
+
|
90
|
+
void
|
91
|
+
frt_extend_buffer_if_necessary(RAMFile *rf, int bufnum)
|
92
|
+
{
|
93
|
+
while (bufnum >= rf->bufcnt) {
|
94
|
+
frt_rf_extend(rf);
|
95
|
+
}
|
96
|
+
}
|
97
|
+
|
98
|
+
static VALUE
|
99
|
+
frt_rio_flush_buffer(VALUE self, VALUE rsrc, VALUE rlen)
|
100
|
+
{
|
101
|
+
int buffer_number, buffer_offset, bytes_in_buffer, bytes_to_copy;
|
102
|
+
int src_offset;
|
103
|
+
int len = FIX2INT(rlen);
|
104
|
+
//char *src = StringValuePtr(rsrc);
|
105
|
+
int pointer = FIX2INT(rb_iv_get(self, "pointer"));
|
106
|
+
|
107
|
+
VALUE file = rb_iv_get(self, "file");
|
108
|
+
RAMFile *rf;
|
109
|
+
Data_Get_Struct(file, RAMFile, rf);
|
110
|
+
|
111
|
+
buffer_number = (int)(pointer / BUFFER_SIZE);
|
112
|
+
buffer_offset = pointer % BUFFER_SIZE;
|
113
|
+
bytes_in_buffer = BUFFER_SIZE - buffer_offset;
|
114
|
+
bytes_to_copy = bytes_in_buffer < len ? bytes_in_buffer : len;
|
115
|
+
|
116
|
+
frt_extend_buffer_if_necessary(rf, buffer_number);
|
117
|
+
|
118
|
+
byte_t *buffer = rf->buffers[buffer_number];
|
119
|
+
MEMCPY(buffer + buffer_offset, RSTRING(rsrc)->ptr, byte_t, bytes_to_copy);
|
120
|
+
|
121
|
+
if (bytes_to_copy < len) {
|
122
|
+
src_offset = bytes_to_copy;
|
123
|
+
bytes_to_copy = len - bytes_to_copy;
|
124
|
+
buffer_number += 1;
|
125
|
+
frt_extend_buffer_if_necessary(rf, buffer_number);
|
126
|
+
buffer = rf->buffers[buffer_number];
|
127
|
+
|
128
|
+
MEMCPY(buffer, RSTRING(rsrc)->ptr + src_offset, byte_t, bytes_to_copy);
|
129
|
+
}
|
130
|
+
pointer += len;
|
131
|
+
rb_iv_set(self, "pointer", INT2FIX(pointer));
|
132
|
+
|
133
|
+
if (pointer > rf->length)
|
134
|
+
rf->length = pointer;
|
135
|
+
|
136
|
+
rf->mtime = rb_funcall(rb_cTime, frt_newobj, 0);
|
137
|
+
return Qnil;
|
138
|
+
}
|
139
|
+
|
140
|
+
static VALUE
|
141
|
+
frt_rio_seek(VALUE self, VALUE rpos)
|
142
|
+
{
|
143
|
+
rb_call_super(1, &rpos);
|
144
|
+
|
145
|
+
rb_iv_set(self, "pointer", rpos);
|
146
|
+
|
147
|
+
return rpos;
|
148
|
+
}
|
149
|
+
|
150
|
+
static VALUE
|
151
|
+
frt_rio_reset(VALUE self)
|
152
|
+
{
|
153
|
+
VALUE file = rb_iv_get(self, "file");
|
154
|
+
RAMFile *rf;
|
155
|
+
Data_Get_Struct(file, RAMFile, rf);
|
156
|
+
|
157
|
+
rf->length = 0;
|
158
|
+
|
159
|
+
rb_funcall(self, seek, 1, INT2FIX(0));
|
160
|
+
|
161
|
+
return Qnil;
|
162
|
+
}
|
163
|
+
|
164
|
+
static VALUE
|
165
|
+
frt_rio_close(VALUE self)
|
166
|
+
{
|
167
|
+
VALUE file = rb_iv_get(self, "file");
|
168
|
+
RAMFile *rf;
|
169
|
+
Data_Get_Struct(file, RAMFile, rf);
|
170
|
+
rf->mtime = rb_funcall(rb_cTime, frt_newobj, 0);
|
171
|
+
rb_call_super(0, NULL);
|
172
|
+
return Qnil;
|
173
|
+
}
|
174
|
+
|
175
|
+
static VALUE
|
176
|
+
frt_rio_write_to(VALUE self, VALUE routput)
|
177
|
+
{
|
178
|
+
int i, len;
|
179
|
+
VALUE file = rb_iv_get(self, "file");
|
180
|
+
RAMFile *rf;
|
181
|
+
Data_Get_Struct(file, RAMFile, rf);
|
182
|
+
|
183
|
+
rb_funcall(self, flush, 0);
|
184
|
+
|
185
|
+
int last_buffer_number = (int)(rf->length / BUFFER_SIZE);
|
186
|
+
int last_buffer_offset = rf->length % BUFFER_SIZE;
|
187
|
+
|
188
|
+
for (i = 0; i < rf->bufcnt; i++) {
|
189
|
+
len = ((i == last_buffer_number) ? last_buffer_offset : BUFFER_SIZE);
|
190
|
+
frt_write_bytes(routput, rf->buffers[i], len);
|
191
|
+
}
|
192
|
+
|
193
|
+
return Qnil;
|
194
|
+
}
|
195
|
+
|
196
|
+
/****************************************************************************
|
197
|
+
*
|
198
|
+
* RAMIndexInput Methods
|
199
|
+
*
|
200
|
+
****************************************************************************/
|
201
|
+
|
202
|
+
static VALUE
|
203
|
+
frt_rii_init(VALUE self, VALUE ramfile)
|
204
|
+
{
|
205
|
+
rb_iv_set(self, "file", ramfile);
|
206
|
+
rb_iv_set(self, "pointer", INT2FIX(0));
|
207
|
+
return self;
|
208
|
+
}
|
209
|
+
|
210
|
+
static VALUE
|
211
|
+
frt_rii_length(VALUE self)
|
212
|
+
{
|
213
|
+
VALUE file = rb_iv_get(self, "file");
|
214
|
+
RAMFile *rf;
|
215
|
+
Data_Get_Struct(file, RAMFile, rf);
|
216
|
+
return INT2FIX(rf->length);
|
217
|
+
}
|
218
|
+
|
219
|
+
static VALUE
|
220
|
+
frt_rii_read_internal(VALUE self, VALUE rb, VALUE roffset, VALUE rlen)
|
221
|
+
{
|
222
|
+
VALUE file = rb_iv_get(self, "file");
|
223
|
+
RAMFile *rf;
|
224
|
+
Data_Get_Struct(file, RAMFile, rf);
|
225
|
+
|
226
|
+
int buffer_number, buffer_offset, bytes_in_buffer, bytes_to_copy;
|
227
|
+
int offset = FIX2INT(roffset);
|
228
|
+
int len = FIX2INT(rlen);
|
229
|
+
int remainder = len;
|
230
|
+
int pointer = FIX2INT(rb_iv_get(self, "pointer"));
|
231
|
+
int start = pointer;
|
232
|
+
byte_t *buffer;
|
233
|
+
byte_t *b = (byte_t *)StringValuePtr(rb);
|
234
|
+
|
235
|
+
while (remainder > 0) {
|
236
|
+
buffer_number = (int)(start / BUFFER_SIZE);
|
237
|
+
buffer_offset = start % BUFFER_SIZE;
|
238
|
+
bytes_in_buffer = BUFFER_SIZE - buffer_offset;
|
239
|
+
|
240
|
+
if (bytes_in_buffer >= remainder) {
|
241
|
+
bytes_to_copy = remainder;
|
242
|
+
} else {
|
243
|
+
bytes_to_copy = bytes_in_buffer;
|
244
|
+
}
|
245
|
+
buffer = rf->buffers[buffer_number];
|
246
|
+
MEMCPY(b + offset, buffer + buffer_offset, byte_t, bytes_to_copy);
|
247
|
+
offset += bytes_to_copy;
|
248
|
+
start += bytes_to_copy;
|
249
|
+
remainder -= bytes_to_copy;
|
250
|
+
}
|
251
|
+
|
252
|
+
rb_iv_set(self, "pointer", INT2FIX(pointer + len));
|
253
|
+
return Qnil;
|
254
|
+
}
|
255
|
+
|
256
|
+
static VALUE
|
257
|
+
frt_rii_seek_internal(VALUE self, VALUE rpos)
|
258
|
+
{
|
259
|
+
rb_iv_set(self, "pointer", rpos);
|
260
|
+
return Qnil;
|
261
|
+
}
|
262
|
+
|
263
|
+
static VALUE
|
264
|
+
frt_rii_close(VALUE self)
|
265
|
+
{
|
266
|
+
return Qnil;
|
267
|
+
}
|
268
|
+
|
269
|
+
/****************************************************************************
|
270
|
+
*
|
271
|
+
* Init Function
|
272
|
+
*
|
273
|
+
****************************************************************************/
|
274
|
+
|
275
|
+
void
|
276
|
+
Init_ram_directory(void)
|
277
|
+
{
|
278
|
+
// IDs
|
279
|
+
flush = rb_intern("flush");
|
280
|
+
seek = rb_intern("seek");
|
281
|
+
|
282
|
+
// RAMDirectory
|
283
|
+
VALUE cDirectory = rb_define_class_under(mStore, "Directory", rb_cObject);
|
284
|
+
cRAMDirectory = rb_define_class_under(mStore, "RAMDirectory", cDirectory);
|
285
|
+
|
286
|
+
// RAMFile
|
287
|
+
VALUE cRAMFile = rb_define_class_under(cRAMDirectory, "RAMFile", rb_cObject);
|
288
|
+
rb_define_alloc_func(cRAMFile, frt_rf_alloc);
|
289
|
+
|
290
|
+
// Methods
|
291
|
+
rb_define_method(cRAMFile, "length", frt_rf_length, 0);
|
292
|
+
|
293
|
+
// RAMIndexOutput
|
294
|
+
cRAMIndexOut = rb_define_class_under(cRAMDirectory, "RAMIndexOutput", cBufferedIndexOut);
|
295
|
+
//rb_define_alloc_func(cRAMIndexOut, frt_ramio_alloc);
|
296
|
+
|
297
|
+
// Methods
|
298
|
+
rb_define_method(cRAMIndexOut, "initialize", frt_rio_init, 1);
|
299
|
+
rb_define_method(cRAMIndexOut, "length", frt_rio_length, 0);
|
300
|
+
rb_define_method(cRAMIndexOut, "flush_buffer", frt_rio_flush_buffer, 2);
|
301
|
+
rb_define_method(cRAMIndexOut, "reset", frt_rio_reset, 0);
|
302
|
+
rb_define_method(cRAMIndexOut, "seek", frt_rio_seek, 1);
|
303
|
+
rb_define_method(cRAMIndexOut, "close", frt_rio_close, 0);
|
304
|
+
rb_define_method(cRAMIndexOut, "write_to", frt_rio_write_to, 1);
|
305
|
+
|
306
|
+
// RAMIndexInput
|
307
|
+
cRAMIndexIn = rb_define_class_under(cRAMDirectory, "RAMIndexInput", cBufferedIndexIn);
|
308
|
+
//rb_define_alloc_func(cRAMIndexIn, frt_ramio_alloc);
|
309
|
+
|
310
|
+
// Methods
|
311
|
+
rb_define_method(cRAMIndexIn, "initialize", frt_rii_init, 1);
|
312
|
+
rb_define_method(cRAMIndexIn, "length", frt_rii_length, 0);
|
313
|
+
rb_define_method(cRAMIndexIn, "read_internal", frt_rii_read_internal, 3);
|
314
|
+
rb_define_method(cRAMIndexIn, "seek_internal", frt_rii_seek_internal, 1);
|
315
|
+
rb_define_method(cRAMIndexIn, "close", frt_rii_close, 0);
|
316
|
+
}
|