Class: MTBL::Reader

Inherits:
Object
  • Object
show all
Defined in:
ext/mtbl/ruby-mtbl.c

Instance Method Summary collapse

Constructor Details

#initialize(fname) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'ext/mtbl/ruby-mtbl.c', line 129

static VALUE rbmtbl_reader_initialize(VALUE self, VALUE fname) {
    rbmtbl_reader_t *reader;
    Data_Get_Struct(self, rbmtbl_reader_t, reader);

    if (TYPE(fname) != T_STRING) {
        rb_raise(rb_eArgError, "File name must be a string");
        return Qnil;
    }

    reader->r = mtbl_reader_init(StringValueCStr(fname), NULL);
    if (reader->r == NULL) {
        rb_raise(rb_eRuntimeError, "Failed to open %s", StringValueCStr(fname));
        return (false);
    }
    return self;
}

Instance Method Details

#get(key) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'ext/mtbl/ruby-mtbl.c', line 152

static VALUE rbmtbl_reader_get(VALUE self, VALUE key) {
    VALUE iter = rb_obj_alloc(rb_cMTBLIterator);
    rbmtbl_reader_t *reader;
    rbmtbl_iterator_t *iterator;
    Data_Get_Struct(self, rbmtbl_reader_t, reader);
    Data_Get_Struct(iter, rbmtbl_iterator_t, iterator);

    if (TYPE(key) != T_STRING) {
        rb_raise(rb_eArgError, "Key must be a string");
        return Qnil;
    }

    iterator->it = mtbl_source_get(mtbl_reader_source(reader->r),
        (const uint8_t *) RSTRING_PTR(key), RSTRING_LEN(key));
    return iter;
}

#get_prefix(prefix) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'ext/mtbl/ruby-mtbl.c', line 169

static VALUE rbmtbl_reader_get_prefix(VALUE self, VALUE prefix) {
    VALUE iter = rb_obj_alloc(rb_cMTBLIterator);
    rbmtbl_reader_t *reader;
    rbmtbl_iterator_t *iterator;
    Data_Get_Struct(self, rbmtbl_reader_t, reader);
    Data_Get_Struct(iter, rbmtbl_iterator_t, iterator);

    if (TYPE(prefix) != T_STRING) {
        rb_raise(rb_eArgError, "Prefix must be a string");
        return Qnil;
    }

    iterator->it = mtbl_source_get_prefix(mtbl_reader_source(reader->r),
        (const uint8_t *) RSTRING_PTR(prefix), RSTRING_LEN(prefix));
    return iter;
}

#get_range(kstart, kend) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'ext/mtbl/ruby-mtbl.c', line 186

static VALUE rbmtbl_reader_get_range(VALUE self, VALUE kstart, VALUE kend) {
    VALUE iter = rb_obj_alloc(rb_cMTBLIterator);
    rbmtbl_reader_t *reader;
    rbmtbl_iterator_t *iterator;
    Data_Get_Struct(self, rbmtbl_reader_t, reader);
    Data_Get_Struct(iter, rbmtbl_iterator_t, iterator);

    if (TYPE(kstart) != T_STRING) {
        rb_raise(rb_eArgError, "Range start must be a string");
        return Qnil;
    }

    if (TYPE(kend) != T_STRING) {
        rb_raise(rb_eArgError, "Range stop must be a string");
        return Qnil;
    }

    iterator->it = mtbl_source_get_range(mtbl_reader_source(reader->r),
        (const uint8_t *) RSTRING_PTR(kstart), RSTRING_LEN(kstart),
        (const uint8_t *) RSTRING_PTR(kend), RSTRING_LEN(kend));
    return iter;
}

#iteratorObject



146
147
148
149
150
# File 'ext/mtbl/ruby-mtbl.c', line 146

static VALUE rbmtbl_reader_iterator(VALUE self) {
    VALUE argv[1];
    argv[0] = self;
    return rb_class_new_instance(1, argv, rb_cMTBLIterator);
}