Class: LZ4::BlockEncoder

Inherits:
Object
  • Object
show all
Defined in:
ext/blockapi.c,
ext/blockapi.c

Overview

このクラスは LZ4 Block API を扱うためのものです。

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level = nil, predict = nil) ⇒ Object

RETURN

self

level

When given nil, encode normal compression.

When given 0 .. 15, encode high compression.

predict

Preset dictionary.



465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'ext/blockapi.c', line 465

static VALUE
blkenc_init(int argc, VALUE argv[], VALUE enc)
{
    struct blockencoder *p = getencoder(enc);
    if (p->context) {
        rb_raise(extlz4_eError,
                "already initialized - #<%s:%p>",
                rb_obj_classname(enc), (void *)enc);
    }

    blkenc_setup(argc, argv, p, Qnil);

    return enc;
}

Class Method Details

.compressbound(src) ⇒ Object

Calcuration maximum size of encoded data in worst case.



600
601
602
603
604
# File 'ext/blockapi.c', line 600

static VALUE
blkenc_s_compressbound(VALUE mod, VALUE src)
{
    return SIZET2NUM(LZ4_compressBound(NUM2UINT(src)));
}

.encode(src, dest = "") ⇒ Object .encode(src, max_dest_size, dest = "") ⇒ Object .encode(level, src, dest = "") ⇒ Object .encode(level, src, max_dest_size, dest = "") ⇒ Object

Encode to block LZ4 data.

level を指定した場合、より圧縮処理に時間を掛けて圧縮効率を高めることが出来ます。

実装の都合上、圧縮関数は LZ4_compress_fast / LZ4_compress_HC が使われます。

RETURN

圧縮されたデータが文字列として返ります。dest を指定した場合は、圧縮データを格納した dest を返します。

圧縮データには自身の終わりやデータ長が含まれていないため、伸張する際には余計なデータが付随していると正常に伸張できません。

src

圧縮対象となる文字列オブジェクトを指定します。

max_dest_size (optional)

出力バッファの最大バイト数を指定します。圧縮時にこれよりも多くのバッファ長が必要になった場合は例外が発生します。

省略時は src 長から最悪値が計算されます。dest が最初に確保できれば圧縮処理中に例外が発生することがありません。

dest (optional)

出力先とする文字列オブジェクトを指定します。

max_dest_size が同時に指定されない場合、出力バッファの最大バイト長は src 長から最悪値が求められて調整されます。

level (optional)

圧縮レベルとしての数値または nil を指定します。

0 を指定した場合、LZ4 の規定値による高効率圧縮処理が行われます。

0 を超えた数値を指定した場合、LZ4 の高効率圧縮処理が行われます。

nil を与えるか省略した場合、通常の圧縮処理が行われます。

0 に満たない数値を指定した場合、高速圧縮処理が行われます。 内部でこの値は絶対値に変換されて LZ4_compress_fast() の acceleration 引数として渡されます。



651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
# File 'ext/blockapi.c', line 651

static VALUE
blkenc_s_encode(int argc, VALUE argv[], VALUE lz4)
{
    VALUE src, dest;
    size_t maxsize;
    int level;
    blockprocess_args(argc, argv, &src, &dest, &maxsize, &level, aux_lz4_compressbound);

    aux_lz4_encoder_f *encoder;
    if (level < 0) {
        encoder = LZ4_compress_fast;
        level = -level;
    } else {
        encoder = LZ4_compress_HC;
    }

    size_t srcsize = RSTRING_LEN(src);
    if (srcsize > LZ4_MAX_INPUT_SIZE) {
        rb_raise(extlz4_eError,
                 "source size is too big for lz4 encode (given %"PRIuSIZE", but max %"PRIuSIZE" bytes)",
                 srcsize, (size_t)LZ4_MAX_INPUT_SIZE);
    }
    aux_str_reserve(dest, maxsize);
    rb_str_set_len(dest, 0);

    int size = encoder(RSTRING_PTR(src), RSTRING_PTR(dest), srcsize, maxsize, level);
    if (size <= 0) {
        rb_raise(extlz4_eError,
                 "failed LZ4 compress - maxsize is too small, or out of memory");
    }

    rb_str_set_len(dest, size);

    return dest;
}

Instance Method Details

#inspectObject



571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
# File 'ext/blockapi.c', line 571

static VALUE
blkenc_inspect(VALUE enc)
{
    struct blockencoder *p = getencoderp(enc);
    if (p && p->context) {
        if (p->traits == &blockencoder_traits_std) {
            return rb_sprintf("#<%s:%p (fast compression %d)%s>",
                    rb_obj_classname(enc), (void *)enc, p->level,
                    (NIL_P(p->predict)) ? "" : " (with predict)");
        } else if (p->traits == &blockencoder_traits_hc) {
            return rb_sprintf("#<%s:%p (high compression %d)%s>",
                    rb_obj_classname(enc), (void *)enc, p->level,
                    (NIL_P(p->predict)) ? "" : " (with predict)");
        } else {
            return rb_sprintf("#<%s:%p **INVALID COMPRESSOR**>",
                    rb_obj_classname(enc), (void *)enc);
        }
    } else {
        return rb_sprintf("#<%s:%p **NOT INITIALIZED**>",
                rb_obj_classname(enc), (void *)enc);
    }
}

#predictObject



536
537
538
539
540
# File 'ext/blockapi.c', line 536

static VALUE
blkenc_predict(VALUE enc)
{
    return getencoder(enc)->predict;
}

#releaseObject Also known as: free



522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'ext/blockapi.c', line 522

static VALUE
blkenc_release(VALUE enc)
{
    struct blockencoder *p = getencoder(enc);
    if (p->traits && p->context) {
        p->traits->free(p->context);
    }
    p->context = NULL;
    p->traits = NULL;
    memset(p->prefix, 0, sizeof(p->prefix));
    p->prefixsize = 0;
    return Qnil;
}

#reset(level = nil) ⇒ self #reset(level, predict) ⇒ self

Reset block stream encoder.

Overloads:

  • #reset(level = nil) ⇒ self

    Returns:

    • (self)
  • #reset(level, predict) ⇒ self

    Returns:

    • (self)


513
514
515
516
517
518
519
520
# File 'ext/blockapi.c', line 513

static VALUE
blkenc_reset(int argc, VALUE argv[], VALUE enc)
{
    struct blockencoder *p = encoder_context(enc);
    blkenc_setup(argc, argv, p, p->predict);

    return enc;
}

#savedictnil #savedict(buf) ⇒ nil

Overloads:

  • #savedictnil

    Returns:

    • (nil)
  • #savedict(buf) ⇒ nil

    Returns:

    • (nil)


547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'ext/blockapi.c', line 547

static VALUE
blkenc_savedict(int argc, VALUE argv[], VALUE enc)
{
    struct blockencoder *p = encoder_context(enc);
    VALUE dict;

    if (argc == 0) {
        dict = rb_str_buf_new(p->prefixsize);
    } else if (argc == 1) {
        dict = argv[0];
        aux_str_reserve(dict, p->prefixsize);
    } else {
        rb_error_arity(argc, 0, 1);
    }

    memcpy(RSTRING_PTR(dict), p->prefix, p->prefixsize);
    if (p->prefixsize > 0) {
        rb_str_set_len(dict, p->prefixsize);
        return dict;
    } else {
        return Qnil;
    }
}

#update(src, dest = "") ⇒ Object #update(src, max_dest_size, dest = "") ⇒ Object Also known as: encode, compress



485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'ext/blockapi.c', line 485

static VALUE
blkenc_update(int argc, VALUE argv[], VALUE enc)
{
    struct blockencoder *p = encoder_context(enc);
    VALUE src, dest;
    size_t maxsize;
    blockprocess_args(argc, argv, &src, &dest, &maxsize, NULL, aux_lz4_compressbound);
    char *srcp;
    size_t srcsize;
    RSTRING_GETMEM(src, srcp, srcsize);
    int s = p->traits->update(p->context, srcp, RSTRING_PTR(dest), srcsize, maxsize, p->level);
    if (s <= 0) {
        rb_raise(extlz4_eError,
                "destsize too small (given destsize is %"PRIuSIZE")",
                rb_str_capacity(dest));
    }
    p->prefixsize = p->traits->savedict(p->context, p->prefix, sizeof(p->prefix));
    rb_str_set_len(dest, s);
    return dest;
}