Class: Blurrily::RawMap

Inherits:
Object
  • Object
show all
Defined in:
ext/blurrily/map_ext.c

Direct Known Subclasses

Map

Defined Under Namespace

Classes: ClosedError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject

***************************************************************************



75
76
77
# File 'ext/blurrily/map_ext.c', line 75

static VALUE blurrily_initialize(VALUE UNUSED(self)) {
  return Qtrue;
}

Class Method Details

.load(rb_path) ⇒ Object

***************************************************************************



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'ext/blurrily/map_ext.c', line 59

static VALUE blurrily_load(VALUE class, VALUE rb_path) {
  char*       path     = StringValuePtr(rb_path);
  VALUE       wrapper  = Qnil;
  trigram_map haystack = (trigram_map)NULL;
  int         res      = -1;

  res = blurrily_storage_load(&haystack, path);
  if (res < 0) { rb_sys_fail(NULL); return Qnil; }

  wrapper = Data_Wrap_Struct(class, blurrily_mark, blurrily_free, (void*)haystack);
  rb_obj_call_init(wrapper, 0, NULL);
  return wrapper;
}

.newObject

***************************************************************************



44
45
46
47
48
49
50
51
52
53
54
55
# File 'ext/blurrily/map_ext.c', line 44

static VALUE blurrily_new(VALUE class) {
  VALUE       wrapper  = Qnil;
  trigram_map haystack = (trigram_map)NULL;
  int         res      = -1;

  res = blurrily_storage_new(&haystack);
  if (res < 0) { rb_sys_fail(NULL); return Qnil; }

  wrapper = Data_Wrap_Struct(class, blurrily_mark, blurrily_free, (void*)haystack);
  rb_obj_call_init(wrapper, 0, NULL);
  return wrapper;
}

Instance Method Details

#closeObject

***************************************************************************



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'ext/blurrily/map_ext.c', line 188

static VALUE blurrily_close(VALUE self)
{
  trigram_map     haystack = (trigram_map)NULL;
  int             res      = -1;

  if (raise_if_closed(self)) return Qnil;
  Data_Get_Struct(self, struct trigram_map_t, haystack);

  res = blurrily_storage_close(&haystack);
  if (res < 0) rb_sys_fail(NULL);

  DATA_PTR(self) = NULL;
  mark_as_closed(self);
  return Qnil;
}

#delete(rb_reference) ⇒ Object

***************************************************************************



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'ext/blurrily/map_ext.c', line 99

static VALUE blurrily_delete(VALUE self, VALUE rb_reference) {
  trigram_map  haystack  = (trigram_map)NULL;
  uint32_t     reference = NUM2UINT(rb_reference);
  int          res       = -1;

  if (raise_if_closed(self)) return Qnil;
  Data_Get_Struct(self, struct trigram_map_t, haystack);

  res = blurrily_storage_delete(haystack, reference);
  assert(res >= 0);

  return INT2NUM(res);
}

#find(rb_needle, rb_limit) ⇒ Object

***************************************************************************



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'ext/blurrily/map_ext.c', line 131

static VALUE blurrily_find(VALUE self, VALUE rb_needle, VALUE rb_limit) {
  trigram_map   haystack   = (trigram_map)NULL;
  int           res        = -1;
  const char*   needle     = StringValuePtr(rb_needle);
  int           limit      = NUM2UINT(rb_limit);
  trigram_match matches    = NULL;
  VALUE         rb_matches = Qnil;

  if (raise_if_closed(self)) return Qnil;
  Data_Get_Struct(self, struct trigram_map_t, haystack);

  if (limit <= 0) {
    // rb_limit = rb_const_get(eBlurrilyModule, rb_intern('LIMIT_DEFAULT'));
    rb_limit = rb_const_get(eBlurrilyModule, rb_intern("LIMIT_DEFAULT"));
    limit = NUM2UINT(rb_limit);
  }
  matches = (trigram_match) malloc(limit * sizeof(trigram_match_t));

  res = blurrily_storage_find(haystack, needle, limit, matches);
  assert(res >= 0);

  /* wrap the matches into a Ruby array */
  rb_matches = rb_ary_new();
  for (int k = 0; k < res; ++k) {
    VALUE rb_match = rb_ary_new();
    rb_ary_push(rb_match, rb_uint_new(matches[k].reference));
    rb_ary_push(rb_match, rb_uint_new(matches[k].matches));
    rb_ary_push(rb_match, rb_uint_new(matches[k].weight));
    rb_ary_push(rb_matches, rb_match);
  }
  return rb_matches;
}

#put(rb_needle, rb_reference, rb_weight) ⇒ Object

***************************************************************************



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'ext/blurrily/map_ext.c', line 81

static VALUE blurrily_put(VALUE self, VALUE rb_needle, VALUE rb_reference, VALUE rb_weight) {
  trigram_map  haystack  = (trigram_map)NULL;
  int          res       = -1;
  char*        needle    = StringValuePtr(rb_needle);
  uint32_t     reference = NUM2UINT(rb_reference);
  uint32_t     weight    = NUM2UINT(rb_weight);

  if (raise_if_closed(self)) return Qnil;
  Data_Get_Struct(self, struct trigram_map_t, haystack);

  res = blurrily_storage_put(haystack, needle, reference, weight);
  assert(res >= 0);

  return INT2NUM(res);
}

#save(rb_path) ⇒ Object

***************************************************************************



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'ext/blurrily/map_ext.c', line 115

static VALUE blurrily_save(VALUE self, VALUE rb_path) {
  trigram_map  haystack  = (trigram_map)NULL;
  int          res       = -1;
  const char*  path      = StringValuePtr(rb_path);

  if (raise_if_closed(self)) return Qnil;
  Data_Get_Struct(self, struct trigram_map_t, haystack);

  res = blurrily_storage_save(haystack, path);
  if (res < 0) rb_sys_fail(NULL);

  return Qnil;
}

#statsObject

***************************************************************************



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'ext/blurrily/map_ext.c', line 167

static VALUE blurrily_stats(VALUE self)
{
  trigram_map     haystack = (trigram_map)NULL;
  trigram_stat_t  stats;
  VALUE           result   = rb_hash_new();
  int             res      = -1;

  if (raise_if_closed(self)) return Qnil;
  Data_Get_Struct(self, struct trigram_map_t, haystack);

  res = blurrily_storage_stats(haystack, &stats);
  assert(res >= 0);

  (void) rb_hash_aset(result, ID2SYM(rb_intern("references")), UINT2NUM(stats.references));
  (void) rb_hash_aset(result, ID2SYM(rb_intern("trigrams")),   UINT2NUM(stats.trigrams));

  return result;
}