Module: VibeZstd

Defined in:
lib/vibe_zstd.rb,
lib/vibe_zstd/version.rb,
lib/vibe_zstd/constants.rb,
ext/vibe_zstd/vibe_zstd.c

Defined Under Namespace

Modules: DictAttachPref, Format, LiteralCompressionMode, ResetDirective, Strategy, ThreadLocal Classes: CCtx, CDict, CompressWriter, DCtx, DDict, DecompressReader, Error

Constant Summary collapse

VERSION =
"1.0.1"

Class Method Summary collapse

Class Method Details

.compress(data, **options) ⇒ Object

Convenience method for one-off compression Supports all CCtx#compress options: level, dict, pledged_size



12
13
14
15
# File 'lib/vibe_zstd.rb', line 12

def self.compress(data, **options)
  cctx = CCtx.new
  cctx.compress(data, **options)
end

.compress_bound(size) ⇒ Object

VibeZstd.compress_bound(size)



5
6
7
8
9
10
# File 'ext/vibe_zstd/frames.c', line 5

static VALUE
vibe_zstd_compress_bound(VALUE self, VALUE size) {
    size_t src_size = NUM2SIZET(size);
    size_t bound = ZSTD_compressBound(src_size);
    return SIZET2NUM(bound);
}

.decompress(data, **options) ⇒ Object

Convenience method for one-off decompression Supports all DCtx#decompress options: dict, initial_capacity



19
20
21
22
# File 'lib/vibe_zstd.rb', line 19

def self.decompress(data, **options)
  dctx = DCtx.new
  dctx.decompress(data, **options)
end

.default_compression_levelObject Also known as: default_level



243
244
245
246
247
# File 'ext/vibe_zstd/vibe_zstd.c', line 243

static VALUE
vibe_zstd_default_c_level(VALUE self) {
    (void)self;
    return INT2NUM(ZSTD_defaultCLevel());
}

.dict_header_size(dict_data) ⇒ Object

VibeZstd.dict_header_size(dict_data)



529
530
531
532
533
534
535
536
537
538
539
540
# File 'ext/vibe_zstd/dict.c', line 529

static VALUE
vibe_zstd_dict_header_size(VALUE self, VALUE dict_data) {
    StringValue(dict_data);
    size_t header_size = ZDICT_getDictHeaderSize(RSTRING_PTR(dict_data), RSTRING_LEN(dict_data));

    // Check for errors
    if (ZDICT_isError(header_size)) {
        rb_raise(rb_eRuntimeError, "Failed to get dictionary header size: %s", ZDICT_getErrorName(header_size));
    }

    return SIZET2NUM(header_size);
}

.each_skippable_frame(data) ⇒ Object

Iterate over all skippable frames in the data Yields [content, magic_variant, offset] for each skippable frame



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/vibe_zstd.rb', line 32

def self.each_skippable_frame(data)
  return enum_for(:each_skippable_frame, data) unless block_given?

  offset = 0
  while offset < data.bytesize
    frame_data = data.byteslice(offset..-1)
    frame_size = find_frame_compressed_size(frame_data)

    # Defense: Prevent infinite loop on malformed data
    # A valid frame must have non-zero size (at minimum: frame header)
    if frame_size <= 0
      raise Error, "Invalid frame: zero or negative size at offset #{offset}"
    end

    if skippable_frame?(frame_data)
      content, magic_variant = read_skippable_frame(frame_data)
      yield content, magic_variant, offset
    end

    offset += frame_size
  end
end

.finalize_dictionary(*args) ⇒ Object

For large datasets, consider using a representative subset of samples.



433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
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
# File 'ext/vibe_zstd/dict.c', line 433

static VALUE
vibe_zstd_finalize_dictionary(int argc, VALUE* argv, VALUE self) {
    VALUE options;
    rb_scan_args(argc, argv, ":", &options);

    // Layer 1: Validate inputs BEFORE any allocation (fail-fast)
    if (NIL_P(options)) {
        rb_raise(rb_eArgError, "finalize_dictionary requires keyword arguments");
    }

    // Get required parameters
    VALUE content_val = rb_hash_aref(options, ID2SYM(rb_intern("content")));
    VALUE samples_val = rb_hash_aref(options, ID2SYM(rb_intern("samples")));
    VALUE max_size_val = rb_hash_aref(options, ID2SYM(rb_intern("max_size")));

    if (NIL_P(content_val)) {
        rb_raise(rb_eArgError, "content: parameter is required");
    }
    if (NIL_P(samples_val)) {
        rb_raise(rb_eArgError, "samples: parameter is required");
    }
    if (NIL_P(max_size_val)) {
        rb_raise(rb_eArgError, "max_size: parameter is required");
    }

    // Validate types early
    StringValue(content_val);
    Check_Type(samples_val, T_ARRAY);
    size_t max_size = NUM2SIZET(max_size_val);

    long num_samples = RARRAY_LEN(samples_val);
    if (num_samples == 0) {
        rb_raise(rb_eArgError, "samples array cannot be empty");
    }

    // Validate all samples are strings and calculate sizes BEFORE allocating
    size_t total_samples_size = 0;
    for (long i = 0; i < num_samples; i++) {
        VALUE sample = rb_ary_entry(samples_val, i);
        StringValue(sample);  // Validate type early - may raise TypeError
        total_samples_size += RSTRING_LEN(sample);
    }

    // Get optional parameters
    VALUE compression_level_val = rb_hash_aref(options, ID2SYM(rb_intern("compression_level")));
    VALUE dict_id_val = rb_hash_aref(options, ID2SYM(rb_intern("dict_id")));

    // Setup ZDICT_params_t
    ZDICT_params_t params;
    memset(&params, 0, sizeof(params));
    params.compressionLevel = NIL_P(compression_level_val) ? 0 : NUM2INT(compression_level_val);
    params.dictID = NIL_P(dict_id_val) ? 0 : NUM2UINT(dict_id_val);
    params.notificationLevel = 0;

    // Layer 2: Allocate late - only after validation passes
    dict_training_resources resources = {NULL, NULL, NULL};
    resources.sample_sizes = ALLOC_N(size_t, num_samples);
    resources.samples_buffer = ALLOC_N(char, total_samples_size);
    resources.dict_buffer = ALLOC_N(char, max_size);

    // Layer 3: Use rb_ensure for guaranteed cleanup (safety net)
    // Build samples buffer - we already validated, so just copy
    size_t offset = 0;
    for (long i = 0; i < num_samples; i++) {
        VALUE sample = rb_ary_entry(samples_val, i);
        size_t sample_len = RSTRING_LEN(sample);
        resources.sample_sizes[i] = sample_len;
        memcpy(resources.samples_buffer + offset, RSTRING_PTR(sample), sample_len);
        offset += sample_len;
    }

    // Finalize the dictionary
    size_t dict_size = ZDICT_finalizeDictionary(
        resources.dict_buffer, max_size,
        RSTRING_PTR(content_val), RSTRING_LEN(content_val),
        resources.samples_buffer, resources.sample_sizes, (unsigned)num_samples,
        params
    );

    // Check for errors
    if (ZDICT_isError(dict_size)) {
        dict_training_cleanup((VALUE)&resources);
        rb_raise(rb_eRuntimeError, "Dictionary finalization failed: %s", ZDICT_getErrorName(dict_size));
    }

    // Create Ruby string with the finalized dictionary
    VALUE dict_string = rb_str_new(resources.dict_buffer, dict_size);

    // Clean up all resources
    dict_training_cleanup((VALUE)&resources);

    return dict_string;
}

.find_frame_compressed_size(data) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'ext/vibe_zstd/frames.c', line 108

static VALUE
vibe_zstd_find_frame_compressed_size(VALUE self, VALUE data) {
    (void)self;
    StringValue(data);

    // Returns compressed size of first complete frame (including header/checksum)
    // Useful for splitting concatenated frames in multi-frame archives
    size_t frame_size = ZSTD_findFrameCompressedSize(RSTRING_PTR(data), RSTRING_LEN(data));

    if (ZSTD_isError(frame_size)) {
        rb_raise(rb_eRuntimeError, "Failed to find frame size: %s", ZSTD_getErrorName(frame_size));
    }

    return SIZET2NUM(frame_size);
}

.frame_content_size(data) ⇒ Object

Get the decompressed content size from a compressed frame Returns nil if size is unknown or data is invalid



26
27
28
# File 'lib/vibe_zstd.rb', line 26

def self.frame_content_size(data)
  DCtx.frame_content_size(data)
end

.get_dict_id(dict_data) ⇒ Object

VibeZstd.get_dict_id(dict_data)



412
413
414
415
416
417
# File 'ext/vibe_zstd/dict.c', line 412

static VALUE
vibe_zstd_get_dict_id(VALUE self, VALUE dict_data) {
    StringValue(dict_data);
    unsigned dict_id = ZDICT_getDictID(RSTRING_PTR(dict_data), RSTRING_LEN(dict_data));
    return UINT2NUM(dict_id);
}

.get_dict_id_from_frame(data) ⇒ Object

VibeZstd.get_dict_id_from_frame(data)



421
422
423
424
425
426
# File 'ext/vibe_zstd/dict.c', line 421

static VALUE
vibe_zstd_get_dict_id_from_frame(VALUE self, VALUE data) {
    StringValue(data);
    unsigned dict_id = ZSTD_getDictID_fromFrame(RSTRING_PTR(data), RSTRING_LEN(data));
    return UINT2NUM(dict_id);
}

.max_compression_levelObject Also known as: max_level



237
238
239
240
241
# File 'ext/vibe_zstd/vibe_zstd.c', line 237

static VALUE
vibe_zstd_max_c_level(VALUE self) {
    (void)self;
    return INT2NUM(ZSTD_maxCLevel());
}

.min_compression_levelObject Also known as: min_level



231
232
233
234
235
# File 'ext/vibe_zstd/vibe_zstd.c', line 231

static VALUE
vibe_zstd_min_c_level(VALUE self) {
    (void)self;
    return INT2NUM(ZSTD_minCLevel());
}

.read_skippable_frame(data) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'ext/vibe_zstd/frames.c', line 64

static VALUE
vibe_zstd_read_skippable_frame(VALUE self, VALUE data) {
    (void)self;
    StringValue(data);

    if (!ZSTD_isSkippableFrame(RSTRING_PTR(data), RSTRING_LEN(data))) {
        rb_raise(rb_eArgError, "data is not a skippable frame (%zu bytes provided)", RSTRING_LEN(data));
    }

    const char* src = RSTRING_PTR(data);
    size_t src_size = RSTRING_LEN(data);

    // Content size is in bytes 4-7 (little-endian uint32)
    if (src_size < 8) {
        rb_raise(rb_eArgError, "skippable frame too small (%zu bytes, minimum 8 bytes required)", src_size);
    }

    uint32_t content_size;
    memcpy(&content_size, src + 4, 4);

    VALUE result = rb_str_buf_new(content_size);
    unsigned magic_variant;

    size_t bytes_read = ZSTD_readSkippableFrame(
        RSTRING_PTR(result),
        content_size,
        &magic_variant,
        src,
        src_size
    );

    if (ZSTD_isError(bytes_read)) {
        rb_raise(rb_eRuntimeError, "Failed to read skippable frame: %s", ZSTD_getErrorName(bytes_read));
    }

    rb_str_set_len(result, bytes_read);

    // Return [content, magic_variant]
    VALUE result_ary = rb_ary_new_capa(2);
    rb_ary_push(result_ary, result);
    rb_ary_push(result_ary, UINT2NUM(magic_variant));
    return result_ary;
}

.skippable_frame?(data) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
# File 'ext/vibe_zstd/frames.c', line 12

static VALUE
vibe_zstd_skippable_frame_p(VALUE self, VALUE data) {
    (void)self;
    StringValue(data);
    unsigned result = ZSTD_isSkippableFrame(RSTRING_PTR(data), RSTRING_LEN(data));
    return result ? Qtrue : Qfalse;
}

.train_dict(*args) ⇒ Object

For large datasets, consider training on a representative subset to reduce memory footprint.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'ext/vibe_zstd/dict.c', line 133

static VALUE
vibe_zstd_train_dict(int argc, VALUE* argv, VALUE self) {
    VALUE samples, options;
    rb_scan_args(argc, argv, "1:", &samples, &options);

    // Layer 1: Validate inputs BEFORE any allocation (fail-fast)
    Check_Type(samples, T_ARRAY);
    long num_samples = RARRAY_LEN(samples);

    if (num_samples == 0) {
        rb_raise(rb_eArgError, "samples array cannot be empty");
    }

    // Validate all samples are strings and calculate sizes BEFORE allocating
    size_t total_samples_size = 0;
    for (long i = 0; i < num_samples; i++) {
        VALUE sample = rb_ary_entry(samples, i);
        StringValue(sample);  // Validate type early - may raise TypeError
        total_samples_size += RSTRING_LEN(sample);
    }

    // Parse options
    VALUE max_dict_size_val = Qnil;
    if (!NIL_P(options)) {
        max_dict_size_val = rb_hash_aref(options, ID2SYM(rb_intern("max_dict_size")));
    }

    // Default max dictionary size is 112KB (zstd default)
    size_t max_dict_size = NIL_P(max_dict_size_val) ? (112 * 1024) : NUM2SIZET(max_dict_size_val);

    // Layer 2: Allocate late - only after validation passes
    dict_training_resources resources = {NULL, NULL, NULL};
    resources.sample_sizes = ALLOC_N(size_t, num_samples);
    resources.samples_buffer = ALLOC_N(char, total_samples_size);
    resources.dict_buffer = ALLOC_N(char, max_dict_size);

    // Layer 3: Use rb_ensure for guaranteed cleanup (safety net)
    // Build samples buffer - we already validated, so just copy
    size_t offset = 0;
    for (long i = 0; i < num_samples; i++) {
        VALUE sample = rb_ary_entry(samples, i);
        size_t sample_len = RSTRING_LEN(sample);
        resources.sample_sizes[i] = sample_len;
        memcpy(resources.samples_buffer + offset, RSTRING_PTR(sample), sample_len);
        offset += sample_len;
    }

    // Train the dictionary
    size_t dict_size = ZDICT_trainFromBuffer(
        resources.dict_buffer, max_dict_size,
        resources.samples_buffer, resources.sample_sizes, (unsigned)num_samples
    );

    // Check for errors
    if (ZDICT_isError(dict_size)) {
        dict_training_cleanup((VALUE)&resources);
        rb_raise(rb_eRuntimeError, "Dictionary training failed: %s", ZDICT_getErrorName(dict_size));
    }

    // Create Ruby string with the trained dictionary
    VALUE dict_string = rb_str_new(resources.dict_buffer, dict_size);

    // Clean up all resources
    dict_training_cleanup((VALUE)&resources);

    return dict_string;
}

.train_dict_cover(*args) ⇒ Object

For large datasets, consider training on a representative subset to reduce memory footprint.



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
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
292
293
294
295
296
297
298
299
300
301
302
# File 'ext/vibe_zstd/dict.c', line 205

static VALUE
vibe_zstd_train_dict_cover(int argc, VALUE* argv, VALUE self) {
    VALUE samples, options;
    rb_scan_args(argc, argv, "1:", &samples, &options);

    // Layer 1: Validate inputs BEFORE any allocation (fail-fast)
    Check_Type(samples, T_ARRAY);
    long num_samples = RARRAY_LEN(samples);

    if (num_samples == 0) {
        rb_raise(rb_eArgError, "samples array cannot be empty");
    }

    // Validate all samples are strings and calculate sizes BEFORE allocating
    size_t total_samples_size = 0;
    for (long i = 0; i < num_samples; i++) {
        VALUE sample = rb_ary_entry(samples, i);
        StringValue(sample);  // Validate type early - may raise TypeError
        total_samples_size += RSTRING_LEN(sample);
    }

    // Initialize COVER parameters with defaults
    ZDICT_cover_params_t params;
    memset(&params, 0, sizeof(params));
    params.splitPoint = 1.0;  // Default split point

    // Parse options
    if (!NIL_P(options)) {
        VALUE v;

        v = rb_hash_aref(options, ID2SYM(rb_intern("k")));
        if (!NIL_P(v)) params.k = NUM2UINT(v);

        v = rb_hash_aref(options, ID2SYM(rb_intern("d")));
        if (!NIL_P(v)) params.d = NUM2UINT(v);

        v = rb_hash_aref(options, ID2SYM(rb_intern("steps")));
        if (!NIL_P(v)) params.steps = NUM2UINT(v);

        v = rb_hash_aref(options, ID2SYM(rb_intern("split_point")));
        if (!NIL_P(v)) params.splitPoint = NUM2DBL(v);

        v = rb_hash_aref(options, ID2SYM(rb_intern("shrink_dict")));
        if (!NIL_P(v)) params.shrinkDict = RTEST(v) ? 1 : 0;

        v = rb_hash_aref(options, ID2SYM(rb_intern("shrink_dict_max_regression")));
        if (!NIL_P(v)) params.shrinkDictMaxRegression = NUM2UINT(v);

        v = rb_hash_aref(options, ID2SYM(rb_intern("nb_threads")));
        if (!NIL_P(v)) params.nbThreads = NUM2UINT(v);
    }

    // Get max_dict_size (default 112KB)
    VALUE max_dict_size_val = Qnil;
    if (!NIL_P(options)) {
        max_dict_size_val = rb_hash_aref(options, ID2SYM(rb_intern("max_dict_size")));
    }
    size_t max_dict_size = NIL_P(max_dict_size_val) ? (112 * 1024) : NUM2SIZET(max_dict_size_val);
    params.zParams.compressionLevel = 0;  // Use default compression level

    // Layer 2: Allocate late - only after validation passes
    dict_training_resources resources = {NULL, NULL, NULL};
    resources.sample_sizes = ALLOC_N(size_t, num_samples);
    resources.samples_buffer = ALLOC_N(char, total_samples_size);
    resources.dict_buffer = ALLOC_N(char, max_dict_size);

    // Layer 3: Use rb_ensure for guaranteed cleanup (safety net)
    // Build samples buffer - we already validated, so just copy
    size_t offset = 0;
    for (long i = 0; i < num_samples; i++) {
        VALUE sample = rb_ary_entry(samples, i);
        size_t sample_len = RSTRING_LEN(sample);
        resources.sample_sizes[i] = sample_len;
        memcpy(resources.samples_buffer + offset, RSTRING_PTR(sample), sample_len);
        offset += sample_len;
    }

    // Train the dictionary using COVER algorithm
    size_t dict_size = ZDICT_trainFromBuffer_cover(
        resources.dict_buffer, max_dict_size,
        resources.samples_buffer, resources.sample_sizes, (unsigned)num_samples,
        params
    );

    // Check for errors
    if (ZDICT_isError(dict_size)) {
        dict_training_cleanup((VALUE)&resources);
        rb_raise(rb_eRuntimeError, "Dictionary training failed: %s", ZDICT_getErrorName(dict_size));
    }

    // Create Ruby string with the trained dictionary
    VALUE dict_string = rb_str_new(resources.dict_buffer, dict_size);

    // Clean up all resources
    dict_training_cleanup((VALUE)&resources);

    return dict_string;
}

.train_dict_fast_cover(*args) ⇒ Object

For large datasets, consider training on a representative subset to reduce memory footprint.



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
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
# File 'ext/vibe_zstd/dict.c', line 308

static VALUE
vibe_zstd_train_dict_fast_cover(int argc, VALUE* argv, VALUE self) {
    VALUE samples, options;
    rb_scan_args(argc, argv, "1:", &samples, &options);

    // Layer 1: Validate inputs BEFORE any allocation (fail-fast)
    Check_Type(samples, T_ARRAY);
    long num_samples = RARRAY_LEN(samples);

    if (num_samples == 0) {
        rb_raise(rb_eArgError, "samples array cannot be empty");
    }

    // Validate all samples are strings and calculate sizes BEFORE allocating
    size_t total_samples_size = 0;
    for (long i = 0; i < num_samples; i++) {
        VALUE sample = rb_ary_entry(samples, i);
        StringValue(sample);  // Validate type early - may raise TypeError
        total_samples_size += RSTRING_LEN(sample);
    }

    // Initialize COVER parameters with defaults
    ZDICT_fastCover_params_t params;
    memset(&params, 0, sizeof(params));
    params.splitPoint = 1.0;  // Default split point

    // Parse options
    if (!NIL_P(options)) {
        VALUE v;

        v = rb_hash_aref(options, ID2SYM(rb_intern("k")));
        if (!NIL_P(v)) params.k = NUM2UINT(v);

        v = rb_hash_aref(options, ID2SYM(rb_intern("d")));
        if (!NIL_P(v)) params.d = NUM2UINT(v);

        v = rb_hash_aref(options, ID2SYM(rb_intern("f")));
        if (!NIL_P(v)) params.f = NUM2UINT(v);

        v = rb_hash_aref(options, ID2SYM(rb_intern("split_point")));
        if (!NIL_P(v)) params.splitPoint = NUM2DBL(v);

        v = rb_hash_aref(options, ID2SYM(rb_intern("accel")));
        if (!NIL_P(v)) params.accel = NUM2UINT(v);

        v = rb_hash_aref(options, ID2SYM(rb_intern("shrink_dict")));
        if (!NIL_P(v)) params.shrinkDict = RTEST(v) ? 1 : 0;

        v = rb_hash_aref(options, ID2SYM(rb_intern("shrink_dict_max_regression")));
        if (!NIL_P(v)) params.shrinkDictMaxRegression = NUM2UINT(v);

        v = rb_hash_aref(options, ID2SYM(rb_intern("nb_threads")));
        if (!NIL_P(v)) params.nbThreads = NUM2UINT(v);
    }

    // Get max_dict_size (default 112KB)
    VALUE max_dict_size_val = Qnil;
    if (!NIL_P(options)) {
        max_dict_size_val = rb_hash_aref(options, ID2SYM(rb_intern("max_dict_size")));
    }
    size_t max_dict_size = NIL_P(max_dict_size_val) ? (112 * 1024) : NUM2SIZET(max_dict_size_val);
    params.zParams.compressionLevel = 0;  // Use default compression level

    // Layer 2: Allocate late - only after validation passes
    dict_training_resources resources = {NULL, NULL, NULL};
    resources.sample_sizes = ALLOC_N(size_t, num_samples);
    resources.samples_buffer = ALLOC_N(char, total_samples_size);
    resources.dict_buffer = ALLOC_N(char, max_dict_size);

    // Layer 3: Use rb_ensure for guaranteed cleanup (safety net)
    // Build samples buffer - we already validated, so just copy
    size_t offset = 0;
    for (long i = 0; i < num_samples; i++) {
        VALUE sample = rb_ary_entry(samples, i);
        size_t sample_len = RSTRING_LEN(sample);
        resources.sample_sizes[i] = sample_len;
        memcpy(resources.samples_buffer + offset, RSTRING_PTR(sample), sample_len);
        offset += sample_len;
    }

    // Train the dictionary using fast COVER algorithm
    size_t dict_size = ZDICT_trainFromBuffer_fastCover(
        resources.dict_buffer, max_dict_size,
        resources.samples_buffer, resources.sample_sizes, (unsigned)num_samples,
        params
    );

    // Check for errors
    if (ZDICT_isError(dict_size)) {
        dict_training_cleanup((VALUE)&resources);
        rb_raise(rb_eRuntimeError, "Dictionary training failed: %s", ZDICT_getErrorName(dict_size));
    }

    // Create Ruby string with the trained dictionary
    VALUE dict_string = rb_str_new(resources.dict_buffer, dict_size);

    // Clean up all resources
    dict_training_cleanup((VALUE)&resources);

    return dict_string;
}

.version_numberObject

Module-level version and compression level functions



219
220
221
222
223
# File 'ext/vibe_zstd/vibe_zstd.c', line 219

static VALUE
vibe_zstd_version_number(VALUE self) {
    (void)self;
    return UINT2NUM(ZSTD_versionNumber());
}

.version_stringObject



225
226
227
228
229
# File 'ext/vibe_zstd/vibe_zstd.c', line 225

static VALUE
vibe_zstd_version_string(VALUE self) {
    (void)self;
    return rb_str_new_cstr(ZSTD_versionString());
}

.write_skippable_frame(*args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'ext/vibe_zstd/frames.c', line 20

static VALUE
vibe_zstd_write_skippable_frame(int argc, VALUE *argv, VALUE self) {
    (void)self;
    VALUE data, options;
    rb_scan_args(argc, argv, "11", &data, &options);

    StringValue(data);

    unsigned magic_variant = 0;  // Default to 0
    if (!NIL_P(options)) {
        Check_Type(options, T_HASH);
        VALUE magic_num = rb_hash_aref(options, ID2SYM(rb_intern("magic_number")));
        if (!NIL_P(magic_num)) {
            magic_variant = NUM2UINT(magic_num);
            if (magic_variant > 15) {
                rb_raise(rb_eArgError, "magic_number %u out of bounds (valid: 0-15)", magic_variant);
            }
        }
    }

    const char* src = RSTRING_PTR(data);
    size_t src_size = RSTRING_LEN(data);

    // Skippable frame structure: 4-byte magic (0x184D2A5X) + 4-byte size + content
    // Decoders skip these frames, allowing custom metadata/padding
    size_t frame_size = 8 + src_size;
    VALUE result = rb_str_buf_new(frame_size);

    size_t written = ZSTD_writeSkippableFrame(
        RSTRING_PTR(result),
        frame_size,
        src,
        src_size,
        magic_variant
    );

    if (ZSTD_isError(written)) {
        rb_raise(rb_eRuntimeError, "Failed to write skippable frame: %s", ZSTD_getErrorName(written));
    }

    rb_str_set_len(result, written);
    return result;
}