Class: MTBL::Writer

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

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'ext/mtbl/ruby-mtbl.c', line 237

static VALUE rbmtbl_writer_initialize(int argc, VALUE *argv, VALUE self) {
    rbmtbl_writer_t *writer;
    struct stat ss;
    int scanc;
    VALUE fname, ctype, bsize, rinterval;

    Data_Get_Struct(self, rbmtbl_writer_t, writer);

    scanc = rb_scan_args(argc, argv, "13", &fname, &ctype, &bsize, &rinterval);

    if (scanc > 1) {
        if (TYPE(ctype) != T_FIXNUM) {
          rb_raise(rb_eArgError, "Compression type should be a constant (COMPRESSION_NONE, etc)");
          return Qnil;
        }
        if (FIX2INT(ctype) < MTBL_COMPRESSION_NONE || FIX2INT(ctype) > MTBL_COMPRESSION_LZ4HC) {
            rb_raise(rb_eArgError, "Invalid compression type: %d", FIX2INT(ctype));
            return Qnil;
        }
        mtbl_writer_options_set_compression(writer->o, FIX2INT(ctype));
    }

    if (scanc > 2) {
        if (TYPE(bsize) != T_FIXNUM) {
            rb_raise(rb_eArgError, "Block size should be an integer");
            return Qnil;
        }
      mtbl_writer_options_set_block_size(writer->o, FIX2INT(bsize));
    }

    if (scanc > 3) {
        if (TYPE(rinterval) != T_FIXNUM) {
            rb_raise(rb_eArgError, "Restart interval should be an integer");
            return Qnil;
        }
      mtbl_writer_options_set_block_restart_interval(writer->o, FIX2INT(rinterval));
    }

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

    if (! stat(StringValueCStr(fname), &ss)) {
        rb_raise(rb_eArgError, "File already exists: %s", StringValueCStr(fname));
        return Qnil;
    }

    writer->w = mtbl_writer_init(StringValueCStr(fname), writer->o);
    if (writer->w == NULL) {
        rb_raise(rb_eRuntimeError, "Failed to open %s", StringValueCStr(fname));
        return Qnil;
    }
    return self;
}

Instance Method Details

#add(key, val) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'ext/mtbl/ruby-mtbl.c', line 293

static VALUE rbmtbl_writer_add(VALUE self, VALUE key, VALUE val) {
    rbmtbl_writer_t *writer;
    Data_Get_Struct(self, rbmtbl_writer_t, writer);
    if (! writer->w) {
        rb_raise(rb_eRuntimeError, "Failed to write key %s: writer closed", StringValueCStr(key));
        return Qnil;
    }

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

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

    if( mtbl_res_success != mtbl_writer_add(writer->w,
        (const uint8_t *) RSTRING_PTR(key), RSTRING_LEN(key),
        (const uint8_t *) RSTRING_PTR(val), RSTRING_LEN(val))) {
        rb_raise(rb_eRuntimeError, "Failed to write key %s, input must be presorted", StringValueCStr(key));
        return Qnil;
    }
    return Qtrue;
}

#closeObject



320
321
322
323
324
325
326
327
328
329
330
# File 'ext/mtbl/ruby-mtbl.c', line 320

static VALUE rbmtbl_writer_close(VALUE self) {
    rbmtbl_writer_t *writer;
    Data_Get_Struct(self, rbmtbl_writer_t, writer);
    if (writer->w) {
        rbmtbl_writer_close_handles(writer);
        return Qtrue;
    } else {
        rb_raise(rb_eRuntimeError, "Writer is already closed");
        return Qfalse;
    }
}