Skip to content

Commit

Permalink
Updated the ruby bindings to use the new Symbols
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://davebalmain.com/home/dave/repos/ferret/trunk@1041 06fd6eb0-0002-0410-a719-e5602cce40bc
  • Loading branch information
dave committed Apr 24, 2008
1 parent ad7faee commit 0a55ed6
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 80 deletions.
4 changes: 2 additions & 2 deletions ruby/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ ext/helper.c
ext/helper.h
ext/index.c
ext/index.h
ext/intern.c
ext/intern.h
ext/internal.h
ext/libstemmer.h
ext/mempool.c
Expand Down Expand Up @@ -139,6 +137,8 @@ ext/stem_UTF_8_turkish.h
ext/stopwords.c
ext/store.c
ext/store.h
ext/symbol.c
ext/symbol.h
ext/term_vectors.c
ext/win32.h
test/temp
Expand Down
16 changes: 8 additions & 8 deletions ruby/ext/ferret.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "hash.h"
#include "hashset.h"
#include "threading.h"
#include "intern.h"
#include "symbol.h"
#include "internal.h"

/* Object Map */
Expand Down Expand Up @@ -178,25 +178,25 @@ rstrdup(VALUE rstr)
return new;
}

const char *
Symbol
rintern(VALUE rstr)
{
char *old = rs2s(rstr);
return frt_intern(old);
}

char *
Symbol
frb_field(VALUE rfield)
{
switch (TYPE(rfield)) {
case T_SYMBOL:
return rb_id2name(SYM2ID(rfield));
return I(rb_id2name(SYM2ID(rfield)));
case T_STRING:
return rs2s(rfield);
return I(rs2s(rfield));
default:
rb_raise(rb_eArgError, "field name must be a symbol");
return NULL;
}
return NULL;
}

/*
Expand Down Expand Up @@ -289,10 +289,10 @@ void FRT_EXIT(const char *err_type, const char *fmt, ...)
static ID id_field;
static ID id_text;

VALUE frb_get_term(const char *field, const char *text)
VALUE frb_get_term(Symbol field, const char *text)
{
return rb_struct_new(cTerm,
ID2SYM(rb_intern(field)),
SYM2RSYM(field),
rb_str_new2(text),
NULL);
}
Expand Down
8 changes: 5 additions & 3 deletions ruby/ext/ferret.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "global.h"
#include "hashset.h"
#include "document.h"
#include "symbol.h"
#include "internal.h"

/* IDs */
Expand Down Expand Up @@ -64,12 +65,12 @@ extern void frb_deref_free(void *p);
extern void frb_create_dir(VALUE rpath);
extern VALUE frb_hs_to_rb_ary(HashSet *hs);
extern void *frb_rb_data_ptr(VALUE val);
extern char * frb_field(VALUE rfield);
extern VALUE frb_get_term(const char *field, const char *term);
extern Symbol frb_field(VALUE rfield);
extern VALUE frb_get_term(Symbol field, const char *term);
extern char *json_concat_string(char *s, char *field);
extern char *rs2s(VALUE rstr);
extern char *rstrdup(VALUE rstr);
extern const char *rintern(VALUE rstr);
extern Symbol rintern(VALUE rstr);
#define Frt_Make_Struct(klass)\
rb_data_object_alloc(klass,NULL,(RUBY_DATA_FUNC)NULL,(RUBY_DATA_FUNC)NULL)

Expand All @@ -89,5 +90,6 @@ extern const char *rintern(VALUE rstr);

#endif

#define SYM2RSYM(_sym) ID2SYM(rb_intern(S(_sym)))
#define frb_mark_cclass(klass) rb_ivar_set(klass, id_cclass, Qtrue)
#define frb_is_cclass(obj) (rb_ivar_get(CLASS_OF(obj), id_cclass) == Qtrue)
8 changes: 4 additions & 4 deletions ruby/ext/r_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -1148,10 +1148,10 @@ cwa_destroy_i(Analyzer *a)
}

static TokenStream *
cwa_get_ts(Analyzer *a, const char *field, char *text)
cwa_get_ts(Analyzer *a, Symbol field, char *text)
{
VALUE rts = rb_funcall(CWA(a)->ranalyzer, id_token_stream, 2,
ID2SYM(rb_intern(field)), rb_str_new2(text));
SYM2RSYM(field), rb_str_new2(text));
return frb_get_cwrapped_rts(rts);
}

Expand Down Expand Up @@ -1480,7 +1480,7 @@ static VALUE
frb_pfa_analyzer_token_stream(VALUE self, VALUE rfield, VALUE rstring)
{
Analyzer *pfa, *a;
char *field = frb_field(rfield);
Symbol field = frb_field(rfield);
GET_A(pfa, self);

StringValue(rstring);
Expand All @@ -1490,7 +1490,7 @@ frb_pfa_analyzer_token_stream(VALUE self, VALUE rfield, VALUE rstring)
}
if (a->get_ts == cwa_get_ts) {
return rb_funcall(CWA(a)->ranalyzer, id_token_stream, 2,
ID2SYM(rb_intern(field)), rb_str_new2(rs2s(rstring)));
SYM2RSYM(field), rb_str_new2(rs2s(rstring)));
}
else {
return get_rb_ts_from_a(a, rfield, rstring);
Expand Down
69 changes: 25 additions & 44 deletions ruby/ext/r_index.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ static VALUE sym_with_positions;
static VALUE sym_with_offsets;
static VALUE sym_with_positions_offsets;

static Symbol fsym_content;

static ID id_term;
static ID id_fields;
static ID id_fld_num_map;
Expand Down Expand Up @@ -206,7 +208,7 @@ static VALUE
frb_fi_name(VALUE self)
{
FieldInfo *fi = (FieldInfo *)DATA_PTR(self);
return ID2SYM(rb_intern(fi->name));
return ID2SYM(rb_intern(S(fi->name)));
}

/*
Expand Down Expand Up @@ -485,11 +487,14 @@ frb_fis_get(VALUE self, VALUE ridx)
break;
}
case T_SYMBOL:
case T_STRING:
rfi = frb_get_field_info(fis_get_field(fis, frb_field(ridx)));
break;
/*
case T_STRING:
rfi = frb_get_field_info(fis_get_field(fis, StringValuePtr(ridx)));
break;
*/
default:
rb_raise(rb_eArgError, "Can't index FieldInfos with %s",
rs2s(rb_obj_as_string(ridx)));
Expand Down Expand Up @@ -633,7 +638,7 @@ frb_fis_get_fields(VALUE self)
VALUE rfield_names = rb_ary_new();
int i;
for (i = 0; i < fis->size; i++) {
rb_ary_push(rfield_names, ID2SYM(rb_intern(fis->fields[i]->name)));
rb_ary_push(rfield_names, SYM2RSYM(fis->fields[i]->name));
}
return rfield_names;
}
Expand All @@ -653,7 +658,7 @@ frb_fis_get_tk_fields(VALUE self)
int i;
for (i = 0; i < fis->size; i++) {
if (!fi_is_tokenized(fis->fields[i])) continue;
rb_ary_push(rfield_names, ID2SYM(rb_intern(fis->fields[i]->name)));
rb_ary_push(rfield_names, SYM2RSYM(fis->fields[i]->name));
}
return rfield_names;
}
Expand Down Expand Up @@ -748,7 +753,7 @@ static VALUE
frb_te_skip_to(VALUE self, VALUE rterm)
{
TermEnum *te = (TermEnum *)DATA_PTR(self);
return frb_te_get_set_term(self, te->skip_to(te, frb_field(rterm)));
return frb_te_get_set_term(self, te->skip_to(te, rs2s(rterm)));
}

/*
Expand Down Expand Up @@ -1239,7 +1244,7 @@ frb_get_tv(TermVector *tv)
const int o_cnt = tv->offset_cnt;
VALUE rfield, rterms, *rts;
VALUE roffsets = Qnil;
rfield = ID2SYM(rb_intern(tv->field));
rfield = SYM2RSYM(tv->field);

rterms = rb_ary_new2(t_cnt);
rts = RARRAY(rterms)->ptr;
Expand Down Expand Up @@ -1431,22 +1436,9 @@ frb_hash_to_doc_i(VALUE key, VALUE value, VALUE arg)
return ST_CONTINUE;
} else {
Document *doc = (Document *)arg;
char *field;
Symbol field = frb_field(key);
VALUE val;
DocField *df;
switch (TYPE(key)) {
case T_STRING:
field = rs2s(key);
break;
case T_SYMBOL:
field = rb_id2name(SYM2ID(key));
break;
default:
rb_raise(rb_eArgError,
"%s cannot be a key to a field. Field keys must "
" be symbols.", rs2s(rb_obj_as_string(key)));
break;
}
if (NULL == (df = doc_get_field(doc, field))) {
df = df_new(field);
}
Expand Down Expand Up @@ -1496,7 +1488,7 @@ frb_get_doc(VALUE rdoc)
case T_ARRAY:
{
int i;
df = df_new("content");
df = df_new(fsym_content);
df->destroy_data = true;
for (i = 0; i < RARRAY(rdoc)->len; i++) {
val = rb_obj_as_string(RARRAY(rdoc)->ptr[i]);
Expand All @@ -1506,17 +1498,17 @@ frb_get_doc(VALUE rdoc)
}
break;
case T_SYMBOL:
df = df_add_data(df_new("content"), rb_id2name(SYM2ID(rdoc)));
df = df_add_data(df_new(fsym_content), rb_id2name(SYM2ID(rdoc)));
doc_add_field(doc, df);
break;
case T_STRING:
df = df_add_data_len(df_new("content"), rs2s(rdoc),
df = df_add_data_len(df_new(fsym_content), rs2s(rdoc),
RSTRING(rdoc)->len);
doc_add_field(doc, df);
break;
default:
val = rb_obj_as_string(rdoc);
df = df_add_data_len(df_new("content"), rstrdup(val),
df = df_add_data_len(df_new(fsym_content), rstrdup(val),
RSTRING(val)->len);
df->destroy_data = true;
doc_add_field(doc, df);
Expand Down Expand Up @@ -1987,22 +1979,10 @@ static VALUE
frb_lzd_default(VALUE self, VALUE rkey)
{
LazyDoc *lazy_doc = (LazyDoc *)DATA_PTR(rb_ivar_get(self, id_data));
char *field = NULL;
switch (TYPE(rkey)) {
case T_STRING:
field = rs2s(rkey);
rkey = ID2SYM(rb_intern(field));
break;
case T_SYMBOL:
field = frb_field(rkey);
break;
default:
rb_raise(rb_eArgError,
"%s cannot be a key to a field. Field keys must "
" be symbols.", rs2s(rb_obj_as_string(rkey)));
break;
}
return frb_lazy_df_load(self, rkey, h_get(lazy_doc->field_dict, field));
Symbol field = frb_field(rkey);
VALUE rfield = SYM2RSYM(field);

return frb_lazy_df_load(self, rfield, h_get(lazy_doc->field_dict, field));
}

/*
Expand Down Expand Up @@ -2032,7 +2012,7 @@ frb_lzd_load(VALUE self)
int i;
for (i = 0; i < lazy_doc->size; i++) {
LazyDocField *lazy_df = lazy_doc->fields[i];
frb_lazy_df_load(self, ID2SYM(rb_intern(lazy_df->name)), lazy_df);
frb_lazy_df_load(self, SYM2RSYM(lazy_df->name), lazy_df);
}
return self;
}
Expand All @@ -2051,7 +2031,7 @@ frb_get_lazy_doc(LazyDoc *lazy_doc)
rb_ivar_set(self, id_data, rdata);

for (i = 0; i < lazy_doc->size; i++) {
RARRAY(rfields)->ptr[i] = ID2SYM(rb_intern(lazy_doc->fields[i]->name));
RARRAY(rfields)->ptr[i] = SYM2RSYM(lazy_doc->fields[i]->name);
RARRAY(rfields)->len++;
}
rb_ivar_set(self, id_fields, rfields);
Expand Down Expand Up @@ -2190,7 +2170,7 @@ frb_ir_init(VALUE self, VALUE rdir)
for (i = 0; i < fis->size; i++) {
FieldInfo *fi = fis->fields[i];
rb_hash_aset(rfield_num_map,
ID2SYM(rb_intern(fi->name)),
SYM2RSYM(fi->name),
INT2FIX(fi->number));
}
rb_ivar_set(self, id_fld_num_map, rfield_num_map);
Expand Down Expand Up @@ -2659,7 +2639,7 @@ frb_ir_fields(VALUE self)
VALUE rfield_names = rb_ary_new();
int i;
for (i = 0; i < fis->size; i++) {
rb_ary_push(rfield_names, ID2SYM(rb_intern(fis->fields[i]->name)));
rb_ary_push(rfield_names, SYM2RSYM(fis->fields[i]->name));
}
return rfield_names;
}
Expand Down Expand Up @@ -2695,7 +2675,7 @@ frb_ir_tk_fields(VALUE self)
int i;
for (i = 0; i < fis->size; i++) {
if (!fi_is_tokenized(fis->fields[i])) continue;
rb_ary_push(rfield_names, ID2SYM(rb_intern(fis->fields[i]->name)));
rb_ary_push(rfield_names, SYM2RSYM(fis->fields[i]->name));
}
return rfield_names;
}
Expand Down Expand Up @@ -3485,6 +3465,7 @@ Init_Index(void)
sym_boost = ID2SYM(rb_intern("boost"));
sym_analyzer = ID2SYM(rb_intern("analyzer"));
sym_close_dir = ID2SYM(rb_intern("close_dir"));
fsym_content = I("content");

Init_TermVector();
Init_TermEnum();
Expand Down
2 changes: 1 addition & 1 deletion ruby/ext/r_qparser.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "intern.h"
#include "symbol.h"
#include "ferret.h"
#include "search.h"

Expand Down
Loading

0 comments on commit 0a55ed6

Please sign in to comment.