Class: MultiCompress::Dictionary
- Inherits:
-
Object
- Object
- MultiCompress::Dictionary
- Defined in:
- ext/multi_compress/multi_compress.c
Class Method Summary collapse
Instance Method Summary collapse
Constructor Details
#initialize(*args) ⇒ Object
2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 |
# File 'ext/multi_compress/multi_compress.c', line 2324
static VALUE dict_initialize(int argc, VALUE *argv, VALUE self) {
VALUE raw, opts;
rb_scan_args(argc, argv, "1:", &raw, &opts);
StringValue(raw);
dictionary_t *d;
TypedData_Get_Struct(self, dictionary_t, &dictionary_type, d);
VALUE algo_sym = Qnil;
if (!NIL_P(opts)) {
algo_sym = rb_hash_aref(opts, ID2SYM(rb_intern("algo")));
}
d->algo = NIL_P(algo_sym) ? ALGO_ZSTD : sym_to_algo(algo_sym);
if (d->algo == ALGO_LZ4)
rb_raise(eUnsupportedError, "LZ4 does not support dictionaries");
d->size = RSTRING_LEN(raw);
d->data = ALLOC_N(uint8_t, d->size);
memcpy(d->data, RSTRING_PTR(raw), d->size);
return self;
}
|
Class Method Details
.load(*args) ⇒ Object
2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 |
# File 'ext/multi_compress/multi_compress.c', line 2432
static VALUE dict_load(int argc, VALUE *argv, VALUE self) {
VALUE path, opts;
rb_scan_args(argc, argv, "1:", &path, &opts);
StringValue(path);
VALUE algo_sym = Qnil;
if (!NIL_P(opts)) {
algo_sym = rb_hash_aref(opts, ID2SYM(rb_intern("algo")));
}
compress_algo_t algo = NIL_P(algo_sym) ? ALGO_ZSTD : sym_to_algo(algo_sym);
if (algo == ALGO_LZ4)
rb_raise(eUnsupportedError, "LZ4 does not support dictionaries");
const char *cpath = RSTRING_PTR(path);
FILE *f = fopen(cpath, "rb");
if (!f)
rb_sys_fail(cpath);
fseek(f, 0, SEEK_END);
long file_size = ftell(f);
fseek(f, 0, SEEK_SET);
if (file_size <= 0) {
fclose(f);
rb_raise(eDataError, "dictionary file is empty: %s", cpath);
}
uint8_t *buf = ALLOC_N(uint8_t, file_size);
size_t read_bytes = fread(buf, 1, file_size, f);
fclose(f);
if ((long)read_bytes != file_size) {
xfree(buf);
rb_raise(eDataError, "failed to read dictionary: %s", cpath);
}
VALUE dict_obj = rb_obj_alloc(cDictionary);
dictionary_t *d;
TypedData_Get_Struct(dict_obj, dictionary_t, &dictionary_type, d);
d->algo = algo;
d->data = buf;
d->size = (size_t)file_size;
return dict_obj;
}
|
Instance Method Details
#algo ⇒ Object
2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 |
# File 'ext/multi_compress/multi_compress.c', line 2495
static VALUE dict_algo(VALUE self) {
dictionary_t *d;
TypedData_Get_Struct(self, dictionary_t, &dictionary_type, d);
switch (d->algo) {
case ALGO_ZSTD:
return ID2SYM(rb_intern("zstd"));
case ALGO_LZ4:
return ID2SYM(rb_intern("lz4"));
case ALGO_BROTLI:
return ID2SYM(rb_intern("brotli"));
}
return Qnil;
}
|
#save(path) ⇒ Object
2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 |
# File 'ext/multi_compress/multi_compress.c', line 2478
static VALUE dict_save(VALUE self, VALUE path) {
dictionary_t *d;
TypedData_Get_Struct(self, dictionary_t, &dictionary_type, d);
const char *cpath = StringValueCStr(path);
FILE *f = fopen(cpath, "wb");
if (!f)
rb_sys_fail(cpath);
size_t written = fwrite(d->data, 1, d->size, f);
fclose(f);
if (written != d->size)
rb_raise(eError, "failed to write dictionary to %s", cpath);
return path;
}
|
#size ⇒ Object
2509 2510 2511 2512 2513 |
# File 'ext/multi_compress/multi_compress.c', line 2509
static VALUE dict_size(VALUE self) {
dictionary_t *d;
TypedData_Get_Struct(self, dictionary_t, &dictionary_type, d);
return SIZET2NUM(d->size);
}
|