Class: MTBL::Utils

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

Class Method Summary collapse

Class Method Details

.metadata(fname) ⇒ Object

Utils



480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
# File 'ext/mtbl/ruby-mtbl.c', line 480

static VALUE rbmtbl_utils_metadata(VALUE class, VALUE fname) {
    int fd, ret;
    struct stat ss;
    struct mtbl_reader *r;
    const struct mtbl_metadata *m;
    mtbl_compression_type compression_algorithm;
    uint64_t data_block_size, count_entries, count_data_blocks, bytes_data_blocks;
    uint64_t bytes_index_block, bytes_keys, bytes_values, index_block_offset;
    double p_data, p_index, compactness;
    VALUE metadata = rb_hash_new();

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

    fd = open(StringValueCStr(fname), O_RDONLY);
    if (fd < 0) {
        rb_raise(rb_eRuntimeError, "Unable to open file %s: %s", StringValueCStr(fname), strerror(errno));
        return Qnil;
    }

    ret = fstat(fd, &ss);
    if (ret < 0) {
        close(fd);
        rb_raise(rb_eRuntimeError, "Failed fstat on file %s: %s", StringValueCStr(fname), strerror(errno));
        return Qnil;
    }

    r = mtbl_reader_init_fd(fd, NULL);
    if (r == NULL) {
        close(fd);
        rb_raise(rb_eRuntimeError, "Unable to open file %s: mtbl_reader_init_fd()", StringValueCStr(fname));
        return Qnil;
    }

    m = mtbl_reader_metadata(r);

    data_block_size = mtbl_metadata_data_block_size(m);
    compression_algorithm = mtbl_metadata_compression_algorithm(m);
    count_entries = mtbl_metadata_count_entries(m);
    count_data_blocks = mtbl_metadata_count_data_blocks(m);
    bytes_data_blocks = mtbl_metadata_bytes_data_blocks(m);
    bytes_index_block = mtbl_metadata_bytes_index_block(m);
    bytes_keys = mtbl_metadata_bytes_keys(m);
    bytes_values = mtbl_metadata_bytes_values(m);
    index_block_offset = mtbl_metadata_index_block_offset(m);
    p_data = 100.0 * bytes_data_blocks / ss.st_size;
    p_index = 100.0 * bytes_index_block / ss.st_size;
    compactness = 100.0 * ss.st_size / (bytes_keys + bytes_values);

    rb_hash_aset(metadata, ID2SYM(rb_intern_const("filename")), fname);
    rb_hash_aset(metadata, ID2SYM(rb_intern_const("filesize")), LL2NUM((size_t) ss.st_size));
    rb_hash_aset(metadata, ID2SYM(rb_intern_const("index_block_offset")), LL2NUM(index_block_offset));
    rb_hash_aset(metadata, ID2SYM(rb_intern_const("index_bytes")), LL2NUM(bytes_index_block));
    rb_hash_aset(metadata, ID2SYM(rb_intern_const("index_bytes_pct")), DBL2NUM(p_index));
    rb_hash_aset(metadata, ID2SYM(rb_intern_const("data_block_bytes")), LL2NUM(bytes_data_blocks));
    rb_hash_aset(metadata, ID2SYM(rb_intern_const("data_block_bytes_pct")), DBL2NUM(p_data));
    rb_hash_aset(metadata, ID2SYM(rb_intern_const("data_block_size")), LL2NUM(data_block_size));
    rb_hash_aset(metadata, ID2SYM(rb_intern_const("data_block_count")), LL2NUM(count_data_blocks));
    rb_hash_aset(metadata, ID2SYM(rb_intern_const("entry_count")), LL2NUM(count_entries));
    rb_hash_aset(metadata, ID2SYM(rb_intern_const("key_bytes")), LL2NUM(bytes_keys));
    rb_hash_aset(metadata, ID2SYM(rb_intern_const("value_bytes")), LL2NUM(bytes_values));
    rb_hash_aset(metadata, ID2SYM(rb_intern_const("compression_algorithm")), rb_str_new2(mtbl_compression_type_to_str(compression_algorithm)));
    rb_hash_aset(metadata, ID2SYM(rb_intern_const("compactness")), DBL2NUM(compactness));

    mtbl_reader_destroy(&r);
    close(fd);

    return metadata;
}