Class: HmSearch::Postgres

Inherits:
Object
  • Object
show all
Defined in:
ext/hmsearch/postgres_ext.cc

Instance Method Summary collapse

Instance Method Details

#closeObject



64
65
66
67
68
# File 'ext/hmsearch/postgres_ext.cc', line 64

static VALUE HmSearch_close(VALUE self)
{
  HmSearch_ptr(self)->close();
  return Qnil;
}

#insert(_hash) ⇒ Object



70
71
72
73
74
# File 'ext/hmsearch/postgres_ext.cc', line 70

static VALUE HmSearch_insert(VALUE self, VALUE _hash)
{
  HmSearch_ptr(self)->insert(HmSearch::parse_hexhash(StringValueCStr(_hash)));
  return Qnil;
}

#lookup(*args) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'ext/hmsearch/postgres_ext.cc', line 76

static VALUE HmSearch_lookup(int argc, VALUE *argv, VALUE self)
{
  VALUE _hash, _reduced_error;
  rb_scan_args(argc, argv, "11", &_hash, &_reduced_error);

  const char *hash = StringValueCStr(_hash);

  int reduced_error = NIL_P(_reduced_error) ? -1 : FIX2INT(_reduced_error);

  HmSearch::LookupResultList matches;
  std::string error_msg;
  if (!HmSearch_ptr(self)->lookup(HmSearch::parse_hexhash(hash), matches, reduced_error, &error_msg)) {
    rb_raise(eHmSearchError, "%s", error_msg.c_str());
  }

  VALUE results = rb_ary_new2(matches.size());

  for (HmSearch::LookupResultList::const_iterator it=matches.begin(); it != matches.end(); ++it) {
    VALUE result = rb_hash_new();

    rb_hash_aset(result, ID2SYM(id_hash), rb_str_new2(HmSearch::format_hexhash(it->hash).c_str()));
    rb_hash_aset(result, ID2SYM(id_distance), INT2NUM(it->distance));

    rb_ary_push(results, result);
  }

  return results;
}