Class: MTBL::Sorter

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

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'ext/mtbl/ruby-mtbl.c', line 374

static VALUE rbmtbl_sorter_initialize(int argc, VALUE *argv, VALUE self) {
    rbmtbl_sorter_t *sorter;
    struct stat ss;
    int scanc;
    VALUE mergef, tempd, maxm;

    Data_Get_Struct(self, rbmtbl_sorter_t, sorter);

    scanc = rb_scan_args(argc, argv, "03", &mergef, &tempd, &maxm);
    mtbl_sorter_options_set_merge_func(sorter->o, (mtbl_merge_func) rbmbtl_default_merge_func, (void *) sorter);

    if (scanc > 1 && mergef != Qnil) {
        // TODO: Implement the merge callback
    }

    if (scanc > 2 && tempd != Qnil) {
        if (TYPE(tempd) != T_STRING) {
            rb_raise(rb_eArgError, "Temporary directory should be a string");
            return Qnil;
        }
        if (stat(StringValueCStr(tempd), &ss)) {
            rb_raise(rb_eArgError, "Temporary directory does not exist: %s", StringValueCStr(tempd));
            return Qnil;
        }

        if (! S_ISDIR(ss.st_mode)) {
            rb_raise(rb_eArgError, "Path %s is not a directory", StringValueCStr(tempd));
            return Qnil;
        }
        mtbl_sorter_options_set_temp_dir(sorter->o, StringValueCStr(tempd));
    }

    if (scanc > 3 && maxm != Qnil) {
        mtbl_sorter_options_set_max_memory(sorter->o, NUM2ULONG(maxm));
    }

    // Verify that c_writer is a MTBL::Writer

    sorter->s = mtbl_sorter_init(sorter->o);
    if (sorter->s == NULL) {
        rb_raise(rb_eRuntimeError, "Failed to create sorter");
        return Qnil;
    }
    return self;
}

Instance Method Details

#add(key, val) ⇒ Object



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'ext/mtbl/ruby-mtbl.c', line 420

static VALUE rbmtbl_sorter_add(VALUE self, VALUE key, VALUE val) {
    rbmtbl_sorter_t *sorter;
    Data_Get_Struct(self, rbmtbl_sorter_t, sorter);
    if (! sorter->s) {
        rb_raise(rb_eRuntimeError, "Failed to write key %s: sorter 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_sorter_add(sorter->s,
        (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



447
448
449
450
451
452
453
454
455
456
457
# File 'ext/mtbl/ruby-mtbl.c', line 447

static VALUE rbmtbl_sorter_close(VALUE self) {
    rbmtbl_sorter_t *sorter;
    Data_Get_Struct(self, rbmtbl_sorter_t, sorter);
    if (sorter->s) {
        rbmtbl_sorter_close_handles(sorter);
        return Qtrue;
    } else {
        rb_raise(rb_eRuntimeError, "Sorter is already closed");
        return Qfalse;
    }
}

#write(c_writer) ⇒ Object



459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'ext/mtbl/ruby-mtbl.c', line 459

static VALUE rbmtbl_sorter_write(VALUE self, VALUE c_writer) {
    rbmtbl_sorter_t *sorter;
    rbmtbl_writer_t *writer;

    Data_Get_Struct(self, rbmtbl_sorter_t, sorter);

    // Verify MTBL::Writer is c_writer
    Data_Get_Struct(c_writer, rbmtbl_writer_t, writer);

    if (mtbl_res_success != mtbl_sorter_write(sorter->s, writer->w)) {
        rb_raise(rb_eRuntimeError, "Failed to write");
        return Qnil;
    }

    return Qtrue;
}