Skip to content

Commit

Permalink
added header size for dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-aladev committed Sep 30, 2021
1 parent 4b6b623 commit 448a3a7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions ext/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def require_library(name, functions)
"zstd",
%w[
ZDICT_getDictID
ZDICT_getDictHeaderSize
ZDICT_isError
ZDICT_trainFromBuffer
ZSTD_CCtx_loadDictionary
Expand Down
11 changes: 11 additions & 0 deletions ext/zstds_ext/dictionary.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,23 @@ VALUE zstds_ext_get_dictionary_buffer_id(VALUE ZSTDS_EXT_UNUSED(self), VALUE buf
return UINT2NUM(id);
}

VALUE zstds_ext_get_dictionary_header_size(VALUE ZSTDS_EXT_UNUSED(self), VALUE buffer)
{
zstds_result_t result = ZDICT_getDictHeaderSize(RSTRING_PTR(buffer), RSTRING_LEN(buffer));
if (ZDICT_isError(result)) {
zstds_ext_raise_error(zstds_ext_get_error(ZSTD_getErrorCode(result)));
}

return SIZET2NUM(result);
}

// -- exports --

void zstds_ext_dictionary_exports(VALUE root_module)
{
VALUE dictionary = rb_define_class_under(root_module, "Dictionary", rb_cObject);

rb_define_singleton_method(dictionary, "get_buffer_id", zstds_ext_get_dictionary_buffer_id, 1);
rb_define_singleton_method(dictionary, "get_header_size", zstds_ext_get_dictionary_header_size, 1);
rb_define_singleton_method(dictionary, "train_buffer", zstds_ext_train_dictionary_buffer, 2);
}
12 changes: 8 additions & 4 deletions lib/zstds/dictionary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ def initialize(buffer)
@buffer = buffer
end

def id
self.class.get_buffer_id @buffer
end

def self.train(samples, options = {})
Validation.validate_array samples

Expand All @@ -45,5 +41,13 @@ def self.train(samples, options = {})
buffer = train_buffer samples, options
new buffer
end

def id
self.class.get_buffer_id @buffer
end

def header_size
self.class.get_header_size @buffer
end
end
end
2 changes: 2 additions & 0 deletions test/dictionary.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ def test_basic
dictionary = Target.train SAMPLES, :capacity => capacity

assert dictionary.id.positive?
assert dictionary.header_size.positive?
refute_nil dictionary.buffer
refute_empty dictionary.buffer

dictionary_copy = Target.new dictionary.buffer

assert_equal dictionary.id, dictionary_copy.id
assert_equal dictionary.header_size, dictionary_copy.header_size
assert_equal dictionary.buffer, dictionary_copy.buffer

text = TEXTS.sample
Expand Down

0 comments on commit 448a3a7

Please sign in to comment.