ferret 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. data/ext/Makefile +2 -2
  2. data/ext/ferret.c +27 -2
  3. data/ext/ferret.h +59 -16
  4. data/ext/ferret_ext.so +0 -0
  5. data/ext/index_io.c +72 -77
  6. data/ext/priority_queue.c +150 -145
  7. data/ext/ram_directory.c +47 -42
  8. data/ext/segment_merge_queue.c +4 -8
  9. data/ext/segment_term_enum.c +324 -0
  10. data/ext/similarity.c +59 -0
  11. data/ext/string_helper.c +2 -2
  12. data/ext/tags +150 -46
  13. data/ext/term.c +107 -152
  14. data/ext/term_buffer.c +105 -174
  15. data/ext/term_infos_reader.c +54 -0
  16. data/ext/terminfo.c +160 -0
  17. data/ext/token.c +93 -0
  18. data/lib/ferret.rb +1 -1
  19. data/lib/ferret/analysis/analyzers.rb +18 -0
  20. data/lib/ferret/analysis/standard_tokenizer.rb +19 -14
  21. data/lib/ferret/analysis/token.rb +8 -1
  22. data/lib/ferret/analysis/tokenizers.rb +10 -5
  23. data/lib/ferret/document/field.rb +33 -11
  24. data/lib/ferret/index/document_writer.rb +3 -2
  25. data/lib/ferret/index/field_infos.rb +38 -12
  26. data/lib/ferret/index/fields_io.rb +10 -4
  27. data/lib/ferret/index/index.rb +20 -4
  28. data/lib/ferret/index/index_reader.rb +19 -4
  29. data/lib/ferret/index/index_writer.rb +1 -1
  30. data/lib/ferret/index/multi_reader.rb +21 -7
  31. data/lib/ferret/index/segment_merge_info.rb +24 -22
  32. data/lib/ferret/index/segment_merge_queue.rb +2 -2
  33. data/lib/ferret/index/segment_merger.rb +28 -11
  34. data/lib/ferret/index/segment_reader.rb +19 -4
  35. data/lib/ferret/index/segment_term_enum.rb +3 -11
  36. data/lib/ferret/index/term_buffer.rb +13 -16
  37. data/lib/ferret/index/term_doc_enum.rb +8 -5
  38. data/lib/ferret/index/term_enum.rb +2 -2
  39. data/lib/ferret/index/term_info.rb +1 -5
  40. data/lib/ferret/index/term_infos_io.rb +2 -0
  41. data/lib/ferret/query_parser/query_parser.tab.rb +7 -7
  42. data/lib/ferret/search/phrase_scorer.rb +0 -1
  43. data/lib/ferret/search/similarity.rb +2 -2
  44. data/lib/ferret/search/term_scorer.rb +2 -2
  45. data/lib/ferret/store/directory.rb +2 -0
  46. data/lib/ferret/store/fs_store.rb +16 -3
  47. data/lib/ferret/store/ram_store.rb +2 -2
  48. data/test/unit/document/tc_field.rb +9 -0
  49. data/test/unit/index/tc_field_infos.rb +29 -21
  50. data/test/unit/index/tc_index.rb +44 -7
  51. data/test/unit/index/tc_term_buffer.rb +3 -3
  52. data/test/unit/index/tc_term_info.rb +1 -1
  53. data/test/unit/query_parser/tc_query_parser.rb +1 -1
  54. data/test/unit/search/tc_index_searcher.rb +3 -0
  55. data/test/unit/store/tc_fs_store.rb +47 -16
  56. data/test/unit/store/tc_ram_store.rb +1 -1
  57. metadata +8 -3
data/ext/term.c CHANGED
@@ -10,179 +10,137 @@
10
10
  void
11
11
  frt_term_free(void *p)
12
12
  {
13
- Term *term;
14
- term = (Term *)p;
15
- free((void *)(term->field));
16
- free((void *)(term->text));
17
- free(p);
13
+ Term *term = (Term *)p;
14
+ free(term->text);
15
+ free(p);
16
+ }
17
+
18
+ void
19
+ frt_term_mark(void *p)
20
+ {
21
+ Term *term = (Term *)p;
22
+ rb_gc_mark(term->field);
18
23
  }
19
24
 
20
25
  static VALUE
21
26
  frt_term_alloc(VALUE klass)
22
27
  {
23
- Term *term;
24
- term = (Term *)ALLOC(Term);
25
- term->field = (char *)ALLOC_N(char, 1);
26
- term->text = (char *)ALLOC_N(char, 1);
27
-
28
- VALUE rbuffer = Data_Wrap_Struct(klass, NULL, frt_term_free, term);
29
- return rbuffer;
28
+ Term *term = ALLOC(Term);
29
+ MEMZERO(term, Term, 1);
30
+ term->field = Qnil;
31
+ return Data_Wrap_Struct(klass, frt_term_mark, frt_term_free, term);
30
32
  }
31
33
 
34
+ #define GET_TERM Term *term; Data_Get_Struct(self, Term, term)
32
35
  VALUE
33
36
  frt_term_set(VALUE self, VALUE rfield, VALUE rtext)
34
37
  {
35
- Term *term;
36
- int flen = RSTRING(rfield)->len;
37
- int tlen = RSTRING(rtext)->len;
38
- char *field = RSTRING(rfield)->ptr;
39
- char *text = RSTRING(rtext)->ptr;
40
- Data_Get_Struct(self, Term, term);
38
+ int tlen;
39
+ GET_TERM;
41
40
 
42
- REALLOC_N(term->field, char, flen + 1);
43
- REALLOC_N(term->text, char, tlen + 1);
44
-
45
- MEMCPY(term->field, field, char, flen);
46
- MEMCPY(term->text, text, char, tlen);
47
- term->flen = flen;
48
- term->tlen = tlen;
49
-
50
- return Qnil;
41
+ tlen = RSTRING(rtext)->len;
42
+ term->field = rfield;
43
+ REALLOC_N(term->text, char, tlen + 1);
44
+ MEMCPY(term->text, RSTRING(rtext)->ptr, char, tlen);
45
+ term->tlen = tlen;
46
+
47
+ return Qnil;
51
48
  }
52
49
 
53
50
  static VALUE
54
51
  frt_term_init(VALUE self, VALUE rfield, VALUE rtext)
55
52
  {
56
- frt_term_set(self, rfield, rtext);
57
- return self;
53
+ frt_term_set(self, rfield, rtext);
54
+ return self;
58
55
  }
59
56
 
60
57
  static VALUE
61
58
  frt_term_get_text(VALUE self)
62
59
  {
63
- Term *term;
64
- Data_Get_Struct(self, Term, term);
65
- return rb_str_new(term->text, term->tlen);
60
+ GET_TERM;
61
+ return rb_str_new(term->text, term->tlen);
66
62
  }
67
63
 
68
64
  static VALUE
69
65
  frt_term_set_text(VALUE self, VALUE rtext)
70
66
  {
71
- Term *term;
72
- int tlen = RSTRING(rtext)->len;
73
- char *text = RSTRING(rtext)->ptr;
74
- Data_Get_Struct(self, Term, term);
67
+ int tlen;
68
+ char *text;
69
+ GET_TERM;
70
+ tlen = RSTRING(rtext)->len;
71
+ text = RSTRING(rtext)->ptr;
75
72
 
76
- REALLOC_N(term->text, char, tlen + 1);
77
-
78
- MEMCPY(term->text, text, char, tlen);
79
- term->tlen = tlen;
80
-
81
- return Qnil;
73
+ REALLOC_N(term->text, char, tlen + 1);
74
+
75
+ MEMCPY(term->text, text, char, tlen);
76
+ term->tlen = tlen;
77
+
78
+ return Qnil;
82
79
  }
83
80
 
84
81
  static VALUE
85
82
  frt_term_get_field(VALUE self)
86
83
  {
87
- Term *term;
88
- Data_Get_Struct(self, Term, term);
89
- return rb_str_new(term->field, term->flen);
84
+ GET_TERM;
85
+ return term->field;
90
86
  }
91
87
 
92
88
  static VALUE
93
89
  frt_term_set_field(VALUE self, VALUE rfield)
94
90
  {
95
- Term *term;
96
- int flen = RSTRING(rfield)->len;
97
- char *field = RSTRING(rfield)->ptr;
98
- Data_Get_Struct(self, Term, term);
99
-
100
- REALLOC_N(term->field, char, flen + 1);
101
-
102
- MEMCPY(term->field, field, char, flen);
103
- term->flen = flen;
104
-
105
- return Qnil;
91
+ GET_TERM;
92
+ term->field = rfield;
93
+ return Qnil;
106
94
  }
107
95
 
108
96
  VALUE
109
97
  frt_term_to_s(VALUE self)
110
98
  {
111
- Term *term;
112
- int tlen, flen;
113
- Data_Get_Struct(self, Term, term);
114
- tlen = term->tlen;
115
- flen = term->flen;
116
- char res[flen + tlen + 1];
117
- char delim[] = ":";
118
-
119
- MEMCPY(res, term->field, char, flen);
120
- MEMCPY(res + flen, delim, char, 1);
121
- MEMCPY(res + flen + 1, term->text, char, tlen);
122
- return rb_str_new(res, tlen + flen + 1 );
99
+ int tlen, flen;
100
+ char delim[] = ":";
101
+ char *res;
102
+ GET_TERM;
103
+ tlen = term->tlen;
104
+ flen = RSTRING(term->field)->len;
105
+ res = alloca(flen + tlen + 1);
106
+
107
+ MEMCPY(res, StringValuePtr(term->field), char, flen);
108
+ MEMCPY(res + flen, delim, char, 1);
109
+ MEMCPY(res + flen + 1, term->text, char, tlen);
110
+ return rb_str_new(res, tlen + flen + 1 );
111
+ }
112
+
113
+ inline int
114
+ frt_term_cmp(Term *t1, Term *t2)
115
+ {
116
+ int comp, size, my_len, o_len;
117
+
118
+ my_len = RSTRING(t1->field)->len;
119
+ o_len = RSTRING(t2->field)->len;
120
+ size = my_len >= o_len ? o_len : my_len;
121
+ comp = memcmp(RSTRING(t1->field)->ptr, RSTRING(t2->field)->ptr, size);
122
+ if (comp == 0) {
123
+ if (my_len == o_len) {
124
+ my_len = t1->tlen;
125
+ o_len = t2->tlen;
126
+ size = my_len >= o_len ? o_len : my_len;
127
+ comp = memcmp(t1->text, t2->text, size);
128
+ if(comp == 0 && my_len != o_len)
129
+ comp = my_len > o_len ? 1 : -1;
130
+ } else {
131
+ comp = my_len > o_len ? 1 : -1;
132
+ }
133
+ }
134
+ return comp;
123
135
  }
124
136
 
125
- VALUE
126
- frt_term_compare_to(VALUE self, VALUE rother)
127
- {
128
- int comp, size, mylen, olen;
129
- Term *term, *other;
130
- Data_Get_Struct(self, Term, term);
131
- Data_Get_Struct(rother, Term, other);
132
-
133
- mylen = term->flen;
134
- olen = other->flen;
135
- size = mylen >= olen ? olen : mylen;
136
- comp = memcmp(term->field, other->field, size);
137
- if(comp == 0){
138
- if(mylen == olen){
139
- mylen = term->tlen;
140
- olen = other->tlen;
141
- size = mylen >= olen ? olen : mylen;
142
- comp = memcmp(term->text, other->text, size);
143
- if(comp == 0 && mylen != olen)
144
- comp = mylen > olen ? 1 : -1;
145
- } else
146
- comp = mylen > olen ? 1 : -1;
147
- }
148
- /*
149
- comp = strcmp(term->field, other->field);
150
- if(comp == 0)
151
- comp = strcmp(term->text, other->text);
152
- */
153
- return INT2FIX(comp);
154
- }
155
-
156
- /* keep in synch with fuction above */
157
137
  int
158
138
  frt_term_compare_to_int(VALUE self, VALUE rother)
159
139
  {
160
- int comp, size, mylen, olen;
161
- Term *term, *other;
162
- Data_Get_Struct(self, Term, term);
163
- Data_Get_Struct(rother, Term, other);
164
-
165
- mylen = term->flen;
166
- olen = other->flen;
167
- size = mylen >= olen ? olen : mylen;
168
- comp = memcmp(term->field, other->field, size);
169
- if(comp == 0){
170
- if(mylen == olen){
171
- mylen = term->tlen;
172
- olen = other->tlen;
173
- size = mylen >= olen ? olen : mylen;
174
- comp = memcmp(term->text, other->text, size);
175
- if(comp == 0 && mylen != olen)
176
- comp = mylen > olen ? 1 : -1;
177
- } else
178
- comp = mylen > olen ? 1 : -1;
179
- }
180
- /*
181
- comp = strcmp(term->field, other->field);
182
- if(comp == 0)
183
- comp = strcmp(term->text, other->text);
184
- */
185
- return comp;
140
+ Term *other;
141
+ GET_TERM;
142
+ Data_Get_Struct(rother, Term, other);
143
+ return frt_term_cmp(term, other);
186
144
  }
187
145
 
188
146
  VALUE
@@ -218,21 +176,18 @@ frt_term_eq(VALUE self, VALUE rother)
218
176
  }
219
177
 
220
178
 
221
- /*
222
179
  static VALUE
223
180
  frt_term_compare_to(VALUE self, VALUE other)
224
181
  {
225
- return INT2FIX(frt_term_compare_to_int(self, other));
182
+ return INT2FIX(frt_term_compare_to_int(self, other));
226
183
  }
227
- */
228
184
 
229
185
  static VALUE
230
186
  frt_term_hash(VALUE self)
231
187
  {
232
- Term *term;
233
- Data_Get_Struct(self, Term, term);
188
+ GET_TERM;
234
189
  return INT2FIX(frt_hash(term->text, term->tlen) +
235
- frt_hash(term->field, term->flen));
190
+ frt_hash(RSTRING(term->field)->ptr, RSTRING(term->field)->len));
236
191
  }
237
192
 
238
193
  /****************************************************************************
@@ -244,24 +199,24 @@ frt_term_hash(VALUE self)
244
199
  void
245
200
  Init_term(void)
246
201
  {
247
- /* Term */
248
- cTerm = rb_define_class_under(mIndex, "Term", rb_cObject);
249
- rb_define_alloc_func(cTerm, frt_term_alloc);
250
- rb_include_module(cTerm, rb_mComparable);
251
-
252
- rb_define_method(cTerm, "initialize", frt_term_init, 2);
253
- rb_define_method(cTerm, "set!", frt_term_set, 2);
254
- rb_define_method(cTerm, "to_s", frt_term_to_s, 0);
255
- rb_define_method(cTerm, "<=>", frt_term_compare_to, 1);
256
- rb_define_method(cTerm, "<", frt_term_lt, 1);
257
- rb_define_method(cTerm, ">", frt_term_gt, 1);
258
- rb_define_method(cTerm, "<=", frt_term_le, 1);
259
- rb_define_method(cTerm, ">=", frt_term_ge, 1);
260
- rb_define_method(cTerm, "eql?", frt_term_eq, 1);
261
- rb_define_method(cTerm, "==", frt_term_eq, 1);
262
- rb_define_method(cTerm, "text", frt_term_get_text, 0);
263
- rb_define_method(cTerm, "text=", frt_term_set_text, 1);
264
- rb_define_method(cTerm, "field", frt_term_get_field, 0);
265
- rb_define_method(cTerm, "field=", frt_term_set_field, 1);
266
- rb_define_method(cTerm, "hash", frt_term_hash, 0);
202
+ /* Term */
203
+ cTerm = rb_define_class_under(mIndex, "Term", rb_cObject);
204
+ rb_define_alloc_func(cTerm, frt_term_alloc);
205
+ rb_include_module(cTerm, rb_mComparable);
206
+
207
+ rb_define_method(cTerm, "initialize", frt_term_init, 2);
208
+ rb_define_method(cTerm, "set!", frt_term_set, 2);
209
+ rb_define_method(cTerm, "to_s", frt_term_to_s, 0);
210
+ rb_define_method(cTerm, "<=>", frt_term_compare_to, 1);
211
+ rb_define_method(cTerm, "<", frt_term_lt, 1);
212
+ rb_define_method(cTerm, ">", frt_term_gt, 1);
213
+ rb_define_method(cTerm, "<=", frt_term_le, 1);
214
+ rb_define_method(cTerm, ">=", frt_term_ge, 1);
215
+ rb_define_method(cTerm, "eql?", frt_term_eq, 1);
216
+ rb_define_method(cTerm, "==", frt_term_eq, 1);
217
+ rb_define_method(cTerm, "text", frt_term_get_text, 0);
218
+ rb_define_method(cTerm, "text=", frt_term_set_text, 1);
219
+ rb_define_method(cTerm, "field", frt_term_get_field, 0);
220
+ rb_define_method(cTerm, "field=", frt_term_set_field, 1);
221
+ rb_define_method(cTerm, "hash", frt_term_hash, 0);
267
222
  }
data/ext/term_buffer.c CHANGED
@@ -1,6 +1,7 @@
1
1
  #include "ferret.h"
2
2
 
3
- ID field_name;
3
+ ID id_field_name;
4
+ ID id_field_array;
4
5
 
5
6
  /****************************************************************************
6
7
  *
@@ -11,115 +12,90 @@ ID field_name;
11
12
  void
12
13
  frt_termbuffer_free(void *p)
13
14
  {
14
- TermBuffer *tb;
15
- tb = (TermBuffer *)p;
16
- free((void *)(tb->text));
17
- free((void *)(tb->field));
18
- free(p);
15
+ Term *tb = (Term *)p;
16
+ free(tb->text);
17
+ free(p);
18
+ }
19
+
20
+ void
21
+ frt_termbuffer_mark(void *p)
22
+ {
23
+ Term *tb = (Term *)p;
24
+ rb_gc_mark(tb->field);
19
25
  }
20
26
 
21
27
  static VALUE
22
28
  frt_termbuffer_alloc(VALUE klass)
23
29
  {
24
- TermBuffer *tb;
25
- tb = (TermBuffer *)ALLOC(TermBuffer);
26
- tb->text = NULL;
27
- tb->field = NULL;
28
- tb->tlen = 0;
29
- tb->flen = 0;
30
-
31
- VALUE rbuffer = Data_Wrap_Struct(klass, NULL, frt_termbuffer_free, tb);
32
- return rbuffer;
30
+ Term *tb = ALLOC(Term);
31
+ MEMZERO(tb, Term, 1);
32
+ tb->field = Qnil;
33
+ return Data_Wrap_Struct(klass, frt_termbuffer_mark, frt_termbuffer_free, tb);
33
34
  }
34
35
 
35
36
  static VALUE
36
37
  frt_termbuffer_init(VALUE self)
37
38
  {
38
- rb_iv_set(self, "@term", Qnil);
39
+ rb_iv_set(self, "@term", Qnil);
39
40
  return Qnil;
40
41
  }
41
42
 
43
+ #define GET_TB Term *tb; Data_Get_Struct(self, Term, tb)
42
44
  static VALUE
43
45
  frt_termbuffer_get_text_length(VALUE self)
44
46
  {
45
-
46
- TermBuffer *tb;
47
- Data_Get_Struct(self, TermBuffer, tb);
47
+ GET_TB;
48
48
  return INT2FIX(tb->tlen);
49
49
  }
50
50
 
51
51
  static VALUE
52
52
  frt_termbuffer_get_text(VALUE self)
53
53
  {
54
-
55
- TermBuffer *tb;
56
- Data_Get_Struct(self, TermBuffer, tb);
54
+ GET_TB;
57
55
  return rb_str_new(tb->text, tb->tlen);
58
56
  }
59
57
 
60
58
  static VALUE
61
59
  frt_termbuffer_get_field_name(VALUE self)
62
60
  {
63
-
64
- TermBuffer *tb;
65
- Data_Get_Struct(self, TermBuffer, tb);
66
- return rb_str_new(tb->field, tb->flen);
61
+ GET_TB;
62
+ return tb->field;
67
63
  }
68
64
 
69
65
  static VALUE
70
66
  frt_termbuffer_reset(VALUE self)
71
67
  {
72
- TermBuffer *tb;
73
- Data_Get_Struct(self, TermBuffer, tb);
68
+ GET_TB;
74
69
 
75
- tb->field = NULL;
76
- tb->text = NULL;
77
- tb->tlen = 0;
78
- tb->flen = 0;
70
+ free(tb->text);
71
+ MEMZERO(tb, Term, 1);
72
+ tb->field = Qnil;
79
73
 
80
- return Qnil;
74
+ return Qnil;
81
75
  }
82
76
 
83
- static VALUE
77
+ VALUE
84
78
  frt_termbuffer_to_term(VALUE self)
85
79
  {
86
- TermBuffer *tb;
87
- Data_Get_Struct(self, TermBuffer, tb);
80
+ GET_TB;
88
81
 
89
- if(tb->field == NULL) {
90
- return Qnil;
82
+ if(NIL_P(tb->field)) {
83
+ return Qnil;
91
84
  } else {
92
- VALUE field = rb_str_new(tb->field, tb->flen);
93
- VALUE text = rb_str_new(tb->text, tb->tlen);
94
- return rb_funcall(cTerm, frt_newobj, 2, field, text);
95
- }
85
+ VALUE args[2];
86
+ args[0] = tb->field;
87
+ args[1] = rb_str_new(tb->text, tb->tlen);
88
+ return rb_class_new_instance(2, args, cTerm);
89
+ }
96
90
  }
97
91
 
98
92
  int
99
93
  frt_termbuffer_compare_to_int(VALUE self, VALUE rother)
100
94
  {
101
- int comp, size, my_len, o_len;
102
- TermBuffer *tb, *other;
103
- Data_Get_Struct(self, TermBuffer, tb);
104
- Data_Get_Struct(rother, TermBuffer, other);
105
-
106
- my_len = tb->flen;
107
- o_len = other->flen;
108
- size = my_len >= o_len ? o_len : my_len;
109
- comp = memcmp(tb->field, other->field, size);
110
- if(comp == 0){
111
- if(my_len == o_len) {
112
- my_len = tb->tlen;
113
- o_len = other->tlen;
114
- size = my_len >= o_len ? o_len : my_len;
115
- comp = memcmp(tb->text, other->text, size);
116
- if(comp == 0 && my_len != o_len)
117
- comp = my_len > o_len ? 1 : -1;
118
- } else {
119
- comp = my_len > o_len ? 1 : -1;
120
- }
121
- }
122
- return comp;
95
+ Term *other;
96
+ GET_TB;
97
+ Data_Get_Struct(rother, Term, other);
98
+ return frt_term_cmp(tb, other);
123
99
  }
124
100
 
125
101
  VALUE
@@ -157,105 +133,60 @@ frt_termbuffer_eq(VALUE self, VALUE rother)
157
133
  static VALUE
158
134
  frt_termbuffer_compare_to(VALUE self, VALUE rother)
159
135
  {
160
- return INT2FIX(frt_termbuffer_compare_to_int(self, rother));
161
- }
162
-
163
- static VALUE
164
- frt_termbuffer_set_term(VALUE self, VALUE rterm)
165
- {
166
- TermBuffer *tb;
167
- Term *term;
168
- int tlen, flen;
169
-
170
- Data_Get_Struct(self, TermBuffer, tb);
171
- Data_Get_Struct(rterm, Term, term);
172
-
173
- tlen = term->tlen;
174
- flen = term->flen;
175
-
176
- if(tb->field == NULL){
177
- tb->field = (char *)ALLOC_N(char, flen+1);
178
- tb->text = (char *)ALLOC_N(char, tlen+1);
179
- } else {
180
- REALLOC_N(tb->text, char, tlen+1);
181
- REALLOC_N(tb->field, char, flen+1);
182
- }
183
-
184
- tb->flen = flen;
185
- tb->tlen = tlen;
186
- MEMCPY(tb->text, term->text, char, tlen);
187
- MEMCPY(tb->field, term->field, char, flen);
188
-
189
- return Qnil;
136
+ return INT2FIX(frt_termbuffer_compare_to_int(self, rother));
190
137
  }
191
138
 
192
- static VALUE
139
+ VALUE
193
140
  frt_termbuffer_init_copy(VALUE self, VALUE rother)
194
141
  {
195
- TermBuffer *tb, *other;
196
- int tlen, flen;
197
-
198
- Data_Get_Struct(self, TermBuffer, tb);
199
- Data_Get_Struct(rother, TermBuffer, other);
200
-
201
- tlen = other->tlen;
202
- flen = other->flen;
203
-
204
- if(tb->field == NULL){
205
- tb->field = (char *)ALLOC_N(char, flen+1);
206
- tb->text = (char *)ALLOC_N(char, tlen+1);
207
- } else {
208
- REALLOC_N(tb->text, char, tlen+1);
209
- REALLOC_N(tb->field, char, flen+1);
210
- }
211
-
212
- tb->flen = flen;
213
- tb->tlen = tlen;
214
- MEMCPY(tb->text, other->text, char, tlen);
215
- MEMCPY(tb->field, other->field, char, flen);
216
-
217
- return Qnil;
142
+ Term *tb_other;
143
+ int tlen;
144
+ GET_TB;
145
+ Data_Get_Struct(rother, Term, tb_other);
146
+
147
+ tlen = tb_other->tlen;
148
+ REALLOC_N(tb->text, char, tlen+1);
149
+ tb->tlen = tlen;
150
+ MEMCPY(tb->text, tb_other->text, char, tlen);
151
+
152
+ tb->field = tb_other->field;
153
+
154
+ return Qnil;
218
155
  }
219
156
 
220
- static VALUE
221
- frt_termbuffer_read(VALUE self, VALUE input, VALUE info)
222
- {
223
- TermBuffer *tb;
224
- int tlen, flen, start, length;
225
- VALUE field, fnum;
226
- Data_Get_Struct(self, TermBuffer, tb);
227
-
228
- start = frt_read_vint(input);
229
- length = frt_read_vint(input);
230
- tlen = start + length;
231
-
232
- if(tb->field == NULL){
233
- tb->text = (char *)ALLOC_N(char, tlen+1);
234
- } else {
235
- REALLOC_N(tb->text, char, tlen+1);
236
- }
237
-
238
- frt_read_chars(input, tb->text, start, length);
239
- fnum = INT2FIX(frt_read_vint(input));
240
- field = rb_funcall(info, field_name, 1, fnum);
241
- flen = RSTRING(field)->len;
242
-
243
- REALLOC_N(tb->field, char, flen+1);
157
+ VALUE
158
+ frt_termbuffer_read(VALUE self, VALUE rinput, VALUE rfield_infos)
159
+ {
160
+ IndexBuffer *input;
161
+ int tlen, start, length, fnum;
162
+ GET_TB;
163
+ Data_Get_Struct(rinput, IndexBuffer, input);
164
+
165
+ start = frt_read_vint(rinput, input);
166
+ length = frt_read_vint(rinput, input);
167
+ tlen = start + length;
168
+ REALLOC_N(tb->text, char, tlen+1);
169
+
170
+ frt_read_chars(rinput, tb->text, start, length);
171
+ fnum = frt_read_vint(rinput, input);
172
+ if (fnum < 0) {
173
+ tb->field = rb_str_new("", 0);
174
+ } else {
175
+ tb->field = rb_ivar_get(
176
+ rb_ary_entry(rb_ivar_get(rfield_infos, id_field_array), fnum),
177
+ id_field_name);
178
+ }
244
179
 
245
- MEMCPY(tb->field, RSTRING(field)->ptr, char, flen);
246
-
247
- tb->flen = flen;
248
180
  tb->tlen = tlen;
249
- return Qnil;
181
+ return Qnil;
250
182
  }
251
183
 
252
184
  static VALUE
253
185
  frt_termbuffer_hash(VALUE self)
254
186
  {
255
- TermBuffer *tb;
256
- Data_Get_Struct(self, TermBuffer, tb);
187
+ GET_TB;
257
188
  return INT2FIX(frt_hash(tb->text, tb->tlen) +
258
- frt_hash(tb->field, tb->flen));
189
+ frt_hash(RSTRING(tb->field)->ptr, RSTRING(tb->field)->len));
259
190
  }
260
191
 
261
192
  /****************************************************************************
@@ -268,32 +199,32 @@ frt_termbuffer_hash(VALUE self)
268
199
  void
269
200
  Init_term_buffer(void) {
270
201
  /* IDs */
271
- field_name = rb_intern("name");
202
+ id_field_name = rb_intern("@name");
203
+ id_field_array = rb_intern("@fi_array");
272
204
 
273
- /* TermBuffer */
274
- cTermBuffer = rb_define_class_under(mIndex, "TermBuffer", rb_cObject);
275
- rb_define_alloc_func(cTermBuffer, frt_termbuffer_alloc);
276
- rb_include_module(cTermBuffer, rb_mComparable);
205
+ /* TermBuffer */
206
+ cTermBuffer = rb_define_class_under(mIndex, "TermBuffer", rb_cObject);
207
+ rb_define_alloc_func(cTermBuffer, frt_termbuffer_alloc);
208
+ rb_include_module(cTermBuffer, rb_mComparable);
277
209
 
278
210
  /* Methods */
279
- rb_define_method(cTermBuffer, "initialize", frt_termbuffer_init, 0);
280
- rb_define_method(cTermBuffer, "initialize_copy", frt_termbuffer_init_copy, 1);
281
- rb_define_method(cTermBuffer, "text", frt_termbuffer_get_text, 0);
282
- rb_define_method(cTermBuffer, "field", frt_termbuffer_get_field_name, 0);
283
- rb_define_method(cTermBuffer, "text_length", frt_termbuffer_get_text_length, 0);
284
- rb_define_method(cTermBuffer, "<=>", frt_termbuffer_compare_to, 1);
285
- rb_define_method(cTermBuffer, "<", frt_termbuffer_lt, 1);
286
- rb_define_method(cTermBuffer, ">", frt_termbuffer_gt, 1);
287
- rb_define_method(cTermBuffer, "<=", frt_termbuffer_le, 1);
288
- rb_define_method(cTermBuffer, ">=", frt_termbuffer_ge, 1);
289
- rb_define_method(cTermBuffer, "eql?", frt_termbuffer_eq, 1);
290
- rb_define_method(cTermBuffer, "==", frt_termbuffer_eq, 1);
291
- rb_define_method(cTermBuffer, "hash", frt_termbuffer_hash, 0);
292
- rb_define_method(cTermBuffer, "read", frt_termbuffer_read, 2);
293
- rb_define_method(cTermBuffer, "reset", frt_termbuffer_reset, 0);
294
- rb_define_method(cTermBuffer, "to_term", frt_termbuffer_to_term, 0);
295
- rb_define_method(cTermBuffer, "term", frt_termbuffer_to_term, 0);
296
- rb_define_method(cTermBuffer, "term=", frt_termbuffer_set_term, 1);
297
- rb_define_method(cTermBuffer, "set!", frt_termbuffer_init_copy, 1);
298
- rb_define_method(cTermBuffer, "text_str", frt_termbuffer_get_text, 0);
211
+ rb_define_method(cTermBuffer, "initialize", frt_termbuffer_init, 0);
212
+ rb_define_method(cTermBuffer, "initialize_copy", frt_termbuffer_init_copy, 1);
213
+ rb_define_method(cTermBuffer, "text", frt_termbuffer_get_text, 0);
214
+ rb_define_method(cTermBuffer, "field", frt_termbuffer_get_field_name, 0);
215
+ rb_define_method(cTermBuffer, "text_length", frt_termbuffer_get_text_length, 0);
216
+ rb_define_method(cTermBuffer, "<=>", frt_termbuffer_compare_to, 1);
217
+ rb_define_method(cTermBuffer, "<", frt_termbuffer_lt, 1);
218
+ rb_define_method(cTermBuffer, ">", frt_termbuffer_gt, 1);
219
+ rb_define_method(cTermBuffer, "<=", frt_termbuffer_le, 1);
220
+ rb_define_method(cTermBuffer, ">=", frt_termbuffer_ge, 1);
221
+ rb_define_method(cTermBuffer, "eql?", frt_termbuffer_eq, 1);
222
+ rb_define_method(cTermBuffer, "==", frt_termbuffer_eq, 1);
223
+ rb_define_method(cTermBuffer, "hash", frt_termbuffer_hash, 0);
224
+ rb_define_method(cTermBuffer, "read", frt_termbuffer_read, 2);
225
+ rb_define_method(cTermBuffer, "reset", frt_termbuffer_reset, 0);
226
+ rb_define_method(cTermBuffer, "to_term", frt_termbuffer_to_term, 0);
227
+ rb_define_method(cTermBuffer, "term", frt_termbuffer_to_term, 0);
228
+ rb_define_method(cTermBuffer, "term=", frt_termbuffer_init_copy, 1);
229
+ rb_define_method(cTermBuffer, "set!", frt_termbuffer_init_copy, 1);
299
230
  }