ruby-zstds 1.1.1 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/AUTHORS +2 -0
- data/README.md +27 -5
- data/ext/extconf.rb +111 -25
- data/ext/zstds_ext/buffer.c +0 -2
- data/ext/zstds_ext/dictionary.c +244 -46
- data/ext/zstds_ext/dictionary.h +22 -0
- data/ext/zstds_ext/error.c +3 -4
- data/ext/zstds_ext/error.h +1 -0
- data/ext/zstds_ext/gvl.h +2 -2
- data/ext/zstds_ext/io.c +0 -1
- data/ext/zstds_ext/macro.h +7 -1
- data/ext/zstds_ext/main.c +3 -1
- data/ext/zstds_ext/option.c +0 -3
- data/ext/zstds_ext/option.h +14 -0
- data/ext/zstds_ext/stream/compressor.c +0 -3
- data/ext/zstds_ext/stream/decompressor.c +0 -3
- data/ext/zstds_ext/string.c +0 -1
- data/lib/zstds/dictionary.rb +60 -10
- data/lib/zstds/error.rb +2 -1
- data/lib/zstds/stream/abstract.rb +9 -5
- data/lib/zstds/stream/reader.rb +9 -0
- data/lib/zstds/stream/writer.rb +26 -2
- data/lib/zstds/validation.rb +15 -32
- data/lib/zstds/version.rb +1 -1
- metadata +19 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ec16246ba1102583f6f309e555e4ac4dbbf49135f5adee4e0332b86be7388de4
|
4
|
+
data.tar.gz: 7d8d40b406c9da3ed50c5929f565c51fc8cf46558bee937fdeaa0d942d0fe84f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd92bd835bf7eb14db13ecc1761a89467e3f38bbcb9d373573fa7c3a8839f4050a478091ada05fec97bb8e2824e9913d23ef9593f3388e317c5b9df456e7e7cd
|
7
|
+
data.tar.gz: 00d8bd37067fd788a7033abd8cf4c6d590b4f672f8986e7da7b80e47ed9ffa4a91886db7442a2a118a6ba45c8f9b2b232665455a5fc85c6cf4c8fcbe119c4e9c
|
data/AUTHORS
CHANGED
data/README.md
CHANGED
@@ -8,7 +8,17 @@ See [zstd library](https://github.com/facebook/zstd).
|
|
8
8
|
|
9
9
|
## Installation
|
10
10
|
|
11
|
-
|
11
|
+
Operating systems: GNU/Linux, FreeBSD, OSX, Windows (MinGW).
|
12
|
+
|
13
|
+
Dependencies: [zstd](https://github.com/facebook/zstd) 1.4.0+ version.
|
14
|
+
|
15
|
+
| Popular OS | Dependencies |
|
16
|
+
|------------|---------------------------|
|
17
|
+
| Ubuntu | `libzstd-dev` |
|
18
|
+
| CentOS | `libzstd-devel` |
|
19
|
+
| ArchLinux | `zstd` |
|
20
|
+
| OSX | `zstd` |
|
21
|
+
| Windows | `mingw-w64-x86_64-zstd` |
|
12
22
|
|
13
23
|
```sh
|
14
24
|
gem install ruby-zstds
|
@@ -23,6 +33,22 @@ gem install pkg/ruby-zstds-*.gem
|
|
23
33
|
|
24
34
|
You can also use [overlay](https://github.com/andrew-aladev/overlay) for gentoo.
|
25
35
|
|
36
|
+
### Installation in macOS on Apple Silicon
|
37
|
+
|
38
|
+
On M1 Macs, Homebrew installs to /opt/homebrew, so you'll need to specify its
|
39
|
+
include and lib paths when building the native extension for zstd.
|
40
|
+
|
41
|
+
```sh
|
42
|
+
brew install zstd
|
43
|
+
gem install ruby-zstds -- --with-opt-include=/opt/homebrew/include --with-opt-lib=/opt/homebrew/lib
|
44
|
+
```
|
45
|
+
|
46
|
+
You can also configure Bundler to use those options when installing:
|
47
|
+
|
48
|
+
```sh
|
49
|
+
bundle config set build.ruby-zstds "--with-opt-include=/opt/homebrew/include --with-opt-lib=/opt/homebrew/lib"
|
50
|
+
```
|
51
|
+
|
26
52
|
## Usage
|
27
53
|
|
28
54
|
There are simple APIs: `String` and `File`. Also you can use generic streaming API: `Stream::Writer` and `Stream::Reader`.
|
@@ -449,10 +475,6 @@ You should lock all shared data between threads.
|
|
449
475
|
For example: you should not use same compressor/decompressor inside multiple threads.
|
450
476
|
Please verify that you are using each processor inside single thread at the same time.
|
451
477
|
|
452
|
-
## Operating systems
|
453
|
-
|
454
|
-
GNU/Linux, FreeBSD, OSX, Windows (MinGW).
|
455
|
-
|
456
478
|
## CI
|
457
479
|
|
458
480
|
Please visit [scripts/test-images](scripts/test-images).
|
data/ext/extconf.rb
CHANGED
@@ -9,26 +9,97 @@ have_func "rb_thread_call_without_gvl", "ruby/thread.h"
|
|
9
9
|
# https://bugs.gentoo.org/713940
|
10
10
|
$LDFLAGS << " -pthread" # rubocop:disable Style/GlobalVars
|
11
11
|
|
12
|
-
def require_header(name,
|
12
|
+
def require_header(name, constants: [], macroses: [], types: [])
|
13
13
|
abort "Can't find #{name} header" unless find_header name
|
14
14
|
|
15
|
+
constants.each do |constant|
|
16
|
+
abort "Can't find #{constant} constant in #{name} header" unless have_const constant, name
|
17
|
+
end
|
18
|
+
|
19
|
+
macroses.each do |macro|
|
20
|
+
abort "Can't find #{macro} macro in #{name} header" unless have_macro macro, name
|
21
|
+
end
|
22
|
+
|
15
23
|
types.each do |type|
|
16
24
|
abort "Can't find #{type} type in #{name} header" unless find_type type, nil, name
|
17
25
|
end
|
18
26
|
end
|
19
27
|
|
20
|
-
require_header "zstd_errors.h", %w[
|
21
|
-
ZSTD_ErrorCode
|
22
|
-
]
|
23
|
-
require_header "zstd.h", [
|
24
|
-
"ZSTD_CCtx *",
|
25
|
-
"ZSTD_DCtx *",
|
26
|
-
"ZSTD_strategy",
|
27
|
-
"ZSTD_bounds",
|
28
|
-
"ZSTD_inBuffer",
|
29
|
-
"ZSTD_outBuffer"
|
30
|
-
]
|
31
28
|
require_header "zdict.h"
|
29
|
+
require_header(
|
30
|
+
"zstd.h",
|
31
|
+
:constants => %w[
|
32
|
+
ZSTD_btlazy2
|
33
|
+
ZSTD_btopt
|
34
|
+
ZSTD_btultra
|
35
|
+
ZSTD_btultra2
|
36
|
+
ZSTD_c_chainLog
|
37
|
+
ZSTD_c_checksumFlag
|
38
|
+
ZSTD_c_compressionLevel
|
39
|
+
ZSTD_c_contentSizeFlag
|
40
|
+
ZSTD_c_dictIDFlag
|
41
|
+
ZSTD_c_enableLongDistanceMatching
|
42
|
+
ZSTD_c_hashLog
|
43
|
+
ZSTD_c_jobSize
|
44
|
+
ZSTD_c_ldmBucketSizeLog
|
45
|
+
ZSTD_c_ldmHashLog
|
46
|
+
ZSTD_c_ldmHashRateLog
|
47
|
+
ZSTD_c_ldmMinMatch
|
48
|
+
ZSTD_c_minMatch
|
49
|
+
ZSTD_c_nbWorkers
|
50
|
+
ZSTD_c_overlapLog
|
51
|
+
ZSTD_c_searchLog
|
52
|
+
ZSTD_c_strategy
|
53
|
+
ZSTD_c_targetLength
|
54
|
+
ZSTD_c_windowLog
|
55
|
+
ZSTD_dfast
|
56
|
+
ZSTD_d_windowLogMax
|
57
|
+
ZSTD_e_continue
|
58
|
+
ZSTD_e_end
|
59
|
+
ZSTD_e_flush
|
60
|
+
ZSTD_fast
|
61
|
+
ZSTD_greedy
|
62
|
+
ZSTD_lazy
|
63
|
+
ZSTD_lazy2
|
64
|
+
],
|
65
|
+
:macroses => %w[ZSTD_VERSION_STRING],
|
66
|
+
:types => [
|
67
|
+
"ZSTD_bounds",
|
68
|
+
"ZSTD_CCtx *",
|
69
|
+
"ZSTD_DCtx *",
|
70
|
+
"ZSTD_inBuffer",
|
71
|
+
"ZSTD_outBuffer",
|
72
|
+
"ZSTD_strategy"
|
73
|
+
]
|
74
|
+
)
|
75
|
+
|
76
|
+
require_header(
|
77
|
+
"zstd_errors.h",
|
78
|
+
:constants => %w[
|
79
|
+
ZSTD_error_checksum_wrong
|
80
|
+
ZSTD_error_corruption_detected
|
81
|
+
ZSTD_error_dictionaryCreation_failed
|
82
|
+
ZSTD_error_dictionary_corrupted
|
83
|
+
ZSTD_error_dictionary_wrong
|
84
|
+
ZSTD_error_dstBuffer_null
|
85
|
+
ZSTD_error_dstSize_tooSmall
|
86
|
+
ZSTD_error_frameParameter_unsupported
|
87
|
+
ZSTD_error_frameParameter_windowTooLarge
|
88
|
+
ZSTD_error_init_missing
|
89
|
+
ZSTD_error_maxSymbolValue_tooLarge
|
90
|
+
ZSTD_error_maxSymbolValue_tooSmall
|
91
|
+
ZSTD_error_memory_allocation
|
92
|
+
ZSTD_error_parameter_outOfBound
|
93
|
+
ZSTD_error_parameter_unsupported
|
94
|
+
ZSTD_error_prefix_unknown
|
95
|
+
ZSTD_error_srcSize_wrong
|
96
|
+
ZSTD_error_stage_wrong
|
97
|
+
ZSTD_error_tableLog_tooLarge
|
98
|
+
ZSTD_error_version_unsupported
|
99
|
+
ZSTD_error_workSpace_tooSmall
|
100
|
+
],
|
101
|
+
:types => %w[ZSTD_ErrorCode]
|
102
|
+
)
|
32
103
|
|
33
104
|
def require_library(name, functions)
|
34
105
|
functions.each do |function|
|
@@ -36,29 +107,44 @@ def require_library(name, functions)
|
|
36
107
|
end
|
37
108
|
end
|
38
109
|
|
110
|
+
# rubocop:disable Style/GlobalVars
|
111
|
+
if find_library "zstd", "ZDICT_getDictHeaderSize"
|
112
|
+
$defs.push "-DHAVE_ZDICT_HEADER_SIZE"
|
113
|
+
end
|
114
|
+
|
115
|
+
zdict_has_params = find_type "ZDICT_params_t", nil, "zdict.h"
|
116
|
+
zdict_has_finalize = find_library "zstd", "ZDICT_finalizeDictionary"
|
117
|
+
|
118
|
+
if zdict_has_params && zdict_has_finalize
|
119
|
+
$defs.push "-DHAVE_ZDICT_FINALIZE"
|
120
|
+
end
|
121
|
+
# rubocop:enable Style/GlobalVars
|
122
|
+
|
39
123
|
require_library(
|
40
124
|
"zstd",
|
41
125
|
%w[
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
ZSTD_freeCCtx
|
47
|
-
ZSTD_freeDCtx
|
126
|
+
ZDICT_getDictID
|
127
|
+
ZDICT_isError
|
128
|
+
ZDICT_trainFromBuffer
|
129
|
+
ZSTD_CCtx_loadDictionary
|
48
130
|
ZSTD_CCtx_setParameter
|
49
|
-
ZSTD_DCtx_setParameter
|
50
131
|
ZSTD_CCtx_setPledgedSrcSize
|
51
|
-
ZSTD_cParam_getBounds
|
52
|
-
ZSTD_dParam_getBounds
|
53
132
|
ZSTD_CStreamInSize
|
54
133
|
ZSTD_CStreamOutSize
|
134
|
+
ZSTD_compressStream2
|
135
|
+
ZSTD_cParam_getBounds
|
136
|
+
ZSTD_createCCtx
|
137
|
+
ZSTD_createDCtx
|
138
|
+
ZSTD_DCtx_setParameter
|
139
|
+
ZSTD_DCtx_loadDictionary
|
55
140
|
ZSTD_DStreamInSize
|
56
141
|
ZSTD_DStreamOutSize
|
57
|
-
ZSTD_compressStream2
|
58
142
|
ZSTD_decompressStream
|
59
|
-
|
60
|
-
|
61
|
-
|
143
|
+
ZSTD_dParam_getBounds
|
144
|
+
ZSTD_freeCCtx
|
145
|
+
ZSTD_freeDCtx
|
146
|
+
ZSTD_getErrorCode
|
147
|
+
ZSTD_isError
|
62
148
|
]
|
63
149
|
)
|
64
150
|
|
data/ext/zstds_ext/buffer.c
CHANGED
data/ext/zstds_ext/dictionary.c
CHANGED
@@ -6,14 +6,12 @@
|
|
6
6
|
#include <string.h>
|
7
7
|
#include <zdict.h>
|
8
8
|
|
9
|
-
#include "ruby.h"
|
10
9
|
#include "zstds_ext/buffer.h"
|
11
10
|
#include "zstds_ext/error.h"
|
12
11
|
#include "zstds_ext/gvl.h"
|
13
|
-
#include "zstds_ext/macro.h"
|
14
12
|
#include "zstds_ext/option.h"
|
15
13
|
|
16
|
-
// --
|
14
|
+
// -- common --
|
17
15
|
|
18
16
|
typedef struct
|
19
17
|
{
|
@@ -21,43 +19,64 @@ typedef struct
|
|
21
19
|
size_t size;
|
22
20
|
} sample_t;
|
23
21
|
|
24
|
-
|
22
|
+
static inline void check_raw_samples(VALUE raw_samples)
|
25
23
|
{
|
26
|
-
|
27
|
-
size_t length;
|
28
|
-
char* buffer;
|
29
|
-
size_t capacity;
|
30
|
-
zstds_result_t result;
|
31
|
-
zstds_ext_result_t ext_result;
|
32
|
-
} train_args_t;
|
24
|
+
Check_Type(raw_samples, T_ARRAY);
|
33
25
|
|
34
|
-
|
26
|
+
size_t samples_length = RARRAY_LEN(raw_samples);
|
27
|
+
|
28
|
+
for (size_t index = 0; index < samples_length; index++) {
|
29
|
+
Check_Type(rb_ary_entry(raw_samples, index), T_STRING);
|
30
|
+
}
|
31
|
+
}
|
32
|
+
|
33
|
+
static inline sample_t* prepare_samples(VALUE raw_samples, size_t* samples_length_ptr)
|
35
34
|
{
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
35
|
+
size_t samples_length = RARRAY_LEN(raw_samples);
|
36
|
+
sample_t* samples = malloc(sizeof(sample_t) * samples_length);
|
37
|
+
if (samples == NULL) {
|
38
|
+
zstds_ext_raise_error(ZSTDS_EXT_ERROR_ALLOCATE_FAILED);
|
39
|
+
}
|
40
40
|
|
41
|
-
for (size_t index = 0; index <
|
41
|
+
for (size_t index = 0; index < samples_length; index++) {
|
42
|
+
VALUE raw_sample = rb_ary_entry(raw_samples, index);
|
43
|
+
sample_t* sample = &samples[index];
|
44
|
+
|
45
|
+
sample->data = RSTRING_PTR(raw_sample);
|
46
|
+
sample->size = RSTRING_LEN(raw_sample);
|
47
|
+
}
|
48
|
+
|
49
|
+
*samples_length_ptr = samples_length;
|
50
|
+
|
51
|
+
return samples;
|
52
|
+
}
|
53
|
+
|
54
|
+
static inline zstds_ext_result_t prepare_samples_group(
|
55
|
+
const sample_t* samples,
|
56
|
+
size_t samples_length,
|
57
|
+
zstds_ext_byte_t** group_ptr,
|
58
|
+
size_t** sizes_ptr)
|
59
|
+
{
|
60
|
+
size_t size = 0;
|
61
|
+
|
62
|
+
for (size_t index = 0; index < samples_length; index++) {
|
42
63
|
size += samples[index].size;
|
43
64
|
}
|
44
65
|
|
45
66
|
zstds_ext_byte_t* group = malloc(size);
|
46
67
|
if (group == NULL) {
|
47
|
-
|
48
|
-
return NULL;
|
68
|
+
return ZSTDS_EXT_ERROR_ALLOCATE_FAILED;
|
49
69
|
}
|
50
70
|
|
51
|
-
size_t* sizes = malloc(
|
71
|
+
size_t* sizes = malloc(samples_length * sizeof(size_t));
|
52
72
|
if (sizes == NULL) {
|
53
73
|
free(group);
|
54
|
-
|
55
|
-
return NULL;
|
74
|
+
return ZSTDS_EXT_ERROR_ALLOCATE_FAILED;
|
56
75
|
}
|
57
76
|
|
58
77
|
size_t offset = 0;
|
59
78
|
|
60
|
-
for (size_t index = 0; index <
|
79
|
+
for (size_t index = 0; index < samples_length; index++) {
|
61
80
|
const sample_t* sample_ptr = &samples[index];
|
62
81
|
size_t sample_size = sample_ptr->size;
|
63
82
|
|
@@ -67,7 +86,38 @@ static inline void* train_wrapper(void* data)
|
|
67
86
|
sizes[index] = sample_size;
|
68
87
|
}
|
69
88
|
|
70
|
-
|
89
|
+
*group_ptr = group;
|
90
|
+
*sizes_ptr = sizes;
|
91
|
+
|
92
|
+
return 0;
|
93
|
+
}
|
94
|
+
|
95
|
+
// -- training --
|
96
|
+
|
97
|
+
typedef struct
|
98
|
+
{
|
99
|
+
const sample_t* samples;
|
100
|
+
size_t samples_length;
|
101
|
+
char* buffer;
|
102
|
+
size_t capacity;
|
103
|
+
zstds_result_t result;
|
104
|
+
zstds_ext_result_t ext_result;
|
105
|
+
} train_args_t;
|
106
|
+
|
107
|
+
static inline void* train_wrapper(void* data)
|
108
|
+
{
|
109
|
+
train_args_t* args = data;
|
110
|
+
|
111
|
+
zstds_ext_byte_t* group;
|
112
|
+
size_t* sizes;
|
113
|
+
zstds_ext_result_t result = prepare_samples_group(args->samples, args->samples_length, &group, &sizes);
|
114
|
+
if (result != 0) {
|
115
|
+
args->ext_result = result;
|
116
|
+
return NULL;
|
117
|
+
}
|
118
|
+
|
119
|
+
args->result =
|
120
|
+
ZDICT_trainFromBuffer((void*) args->buffer, args->capacity, group, sizes, (unsigned int) args->samples_length);
|
71
121
|
|
72
122
|
free(group);
|
73
123
|
free(sizes);
|
@@ -84,17 +134,13 @@ static inline void* train_wrapper(void* data)
|
|
84
134
|
|
85
135
|
VALUE zstds_ext_train_dictionary_buffer(VALUE ZSTDS_EXT_UNUSED(self), VALUE raw_samples, VALUE options)
|
86
136
|
{
|
87
|
-
|
88
|
-
|
89
|
-
size_t length = RARRAY_LEN(raw_samples);
|
90
|
-
|
91
|
-
for (size_t index = 0; index < length; index++) {
|
92
|
-
Check_Type(rb_ary_entry(raw_samples, index), T_STRING);
|
93
|
-
}
|
94
|
-
|
137
|
+
check_raw_samples(raw_samples);
|
95
138
|
Check_Type(options, T_HASH);
|
96
|
-
ZSTDS_EXT_GET_BOOL_OPTION(options, gvl);
|
97
139
|
ZSTDS_EXT_GET_SIZE_OPTION(options, capacity);
|
140
|
+
ZSTDS_EXT_GET_BOOL_OPTION(options, gvl);
|
141
|
+
|
142
|
+
size_t samples_length;
|
143
|
+
sample_t* samples = prepare_samples(raw_samples, &samples_length);
|
98
144
|
|
99
145
|
if (capacity == 0) {
|
100
146
|
capacity = ZSTDS_EXT_DEFAULT_DICTIONARY_CAPACITY;
|
@@ -107,27 +153,147 @@ VALUE zstds_ext_train_dictionary_buffer(VALUE ZSTDS_EXT_UNUSED(self), VALUE raw_
|
|
107
153
|
zstds_ext_raise_error(ZSTDS_EXT_ERROR_ALLOCATE_FAILED);
|
108
154
|
}
|
109
155
|
|
110
|
-
|
111
|
-
|
156
|
+
train_args_t args = {
|
157
|
+
.samples = samples,
|
158
|
+
.samples_length = samples_length,
|
159
|
+
.buffer = RSTRING_PTR(buffer),
|
160
|
+
.capacity = capacity,
|
161
|
+
};
|
162
|
+
|
163
|
+
ZSTDS_EXT_GVL_WRAP(gvl, train_wrapper, &args);
|
164
|
+
free(samples);
|
165
|
+
|
166
|
+
if (args.ext_result != 0) {
|
167
|
+
zstds_ext_raise_error(args.ext_result);
|
168
|
+
}
|
169
|
+
|
170
|
+
ZSTDS_EXT_RESIZE_STRING_BUFFER(buffer, args.result, exception);
|
171
|
+
if (exception != 0) {
|
112
172
|
zstds_ext_raise_error(ZSTDS_EXT_ERROR_ALLOCATE_FAILED);
|
113
173
|
}
|
114
174
|
|
115
|
-
|
116
|
-
|
117
|
-
sample_t* sample = &samples[index];
|
175
|
+
return buffer;
|
176
|
+
}
|
118
177
|
|
119
|
-
|
120
|
-
|
178
|
+
// -- finalizing --
|
179
|
+
|
180
|
+
#if defined(HAVE_ZDICT_FINALIZE)
|
181
|
+
typedef struct
|
182
|
+
{
|
183
|
+
const sample_t* samples;
|
184
|
+
size_t samples_length;
|
185
|
+
char* buffer;
|
186
|
+
size_t max_size;
|
187
|
+
char* content;
|
188
|
+
size_t content_length;
|
189
|
+
zstds_ext_dictionary_options_t dictionary_options;
|
190
|
+
zstds_result_t result;
|
191
|
+
zstds_ext_result_t ext_result;
|
192
|
+
} finalize_args_t;
|
193
|
+
|
194
|
+
static inline void* finalize_wrapper(void* data)
|
195
|
+
{
|
196
|
+
finalize_args_t* args = data;
|
197
|
+
|
198
|
+
zstds_ext_byte_t* group;
|
199
|
+
size_t* sizes;
|
200
|
+
zstds_ext_result_t result = prepare_samples_group(args->samples, args->samples_length, &group, &sizes);
|
201
|
+
if (result != 0) {
|
202
|
+
args->ext_result = result;
|
203
|
+
return NULL;
|
121
204
|
}
|
122
205
|
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
206
|
+
int compressionLevel;
|
207
|
+
zstds_ext_option_t compression_level = args->dictionary_options.compression_level;
|
208
|
+
if (compression_level.has_value) {
|
209
|
+
compressionLevel = compression_level.value;
|
210
|
+
} else {
|
211
|
+
compressionLevel = 0;
|
212
|
+
}
|
213
|
+
|
214
|
+
unsigned int notificationLevel;
|
215
|
+
zstds_ext_option_t notification_level = args->dictionary_options.notification_level;
|
216
|
+
if (notification_level.has_value) {
|
217
|
+
notificationLevel = notification_level.value;
|
218
|
+
} else {
|
219
|
+
notificationLevel = 0;
|
220
|
+
}
|
221
|
+
|
222
|
+
unsigned int dictID;
|
223
|
+
zstds_ext_option_t dictionary_id = args->dictionary_options.dictionary_id;
|
224
|
+
if (dictionary_id.has_value) {
|
225
|
+
dictID = dictionary_id.value;
|
226
|
+
} else {
|
227
|
+
dictID = 0;
|
228
|
+
}
|
229
|
+
|
230
|
+
ZDICT_params_t dictionary_params = {
|
231
|
+
.compressionLevel = compressionLevel,
|
232
|
+
.notificationLevel = notificationLevel,
|
233
|
+
.dictID = dictID,
|
128
234
|
};
|
129
235
|
|
130
|
-
|
236
|
+
args->result = ZDICT_finalizeDictionary(
|
237
|
+
(void*) args->buffer,
|
238
|
+
args->max_size,
|
239
|
+
(void*) args->content,
|
240
|
+
args->content_length,
|
241
|
+
group,
|
242
|
+
sizes,
|
243
|
+
(unsigned int) args->samples_length,
|
244
|
+
dictionary_params);
|
245
|
+
|
246
|
+
free(group);
|
247
|
+
free(sizes);
|
248
|
+
|
249
|
+
if (ZDICT_isError(args->result)) {
|
250
|
+
args->ext_result = zstds_ext_get_error(ZSTD_getErrorCode(args->result));
|
251
|
+
return NULL;
|
252
|
+
}
|
253
|
+
|
254
|
+
args->ext_result = 0;
|
255
|
+
|
256
|
+
return NULL;
|
257
|
+
}
|
258
|
+
|
259
|
+
VALUE zstds_ext_finalize_dictionary_buffer(
|
260
|
+
VALUE ZSTDS_EXT_UNUSED(self),
|
261
|
+
VALUE content,
|
262
|
+
VALUE raw_samples,
|
263
|
+
VALUE options)
|
264
|
+
{
|
265
|
+
Check_Type(content, T_STRING);
|
266
|
+
check_raw_samples(raw_samples);
|
267
|
+
Check_Type(options, T_HASH);
|
268
|
+
ZSTDS_EXT_GET_SIZE_OPTION(options, max_size);
|
269
|
+
ZSTDS_EXT_GET_BOOL_OPTION(options, gvl);
|
270
|
+
ZSTDS_EXT_GET_DICTIONARY_OPTIONS(options);
|
271
|
+
|
272
|
+
size_t samples_length;
|
273
|
+
sample_t* samples = prepare_samples(raw_samples, &samples_length);
|
274
|
+
|
275
|
+
if (max_size == 0) {
|
276
|
+
max_size = ZSTDS_EXT_DEFAULT_DICTIONARY_MAX_SIZE;
|
277
|
+
}
|
278
|
+
|
279
|
+
int exception;
|
280
|
+
|
281
|
+
ZSTDS_EXT_CREATE_STRING_BUFFER(buffer, max_size, exception);
|
282
|
+
if (exception != 0) {
|
283
|
+
zstds_ext_raise_error(ZSTDS_EXT_ERROR_ALLOCATE_FAILED);
|
284
|
+
}
|
285
|
+
|
286
|
+
finalize_args_t args = {
|
287
|
+
.samples = samples,
|
288
|
+
.samples_length = samples_length,
|
289
|
+
.buffer = RSTRING_PTR(buffer),
|
290
|
+
.max_size = max_size,
|
291
|
+
.content = RSTRING_PTR(content),
|
292
|
+
.content_length = RSTRING_LEN(content),
|
293
|
+
.dictionary_options = dictionary_options,
|
294
|
+
};
|
295
|
+
|
296
|
+
ZSTDS_EXT_GVL_WRAP(gvl, finalize_wrapper, &args);
|
131
297
|
free(samples);
|
132
298
|
|
133
299
|
if (args.ext_result != 0) {
|
@@ -142,6 +308,17 @@ VALUE zstds_ext_train_dictionary_buffer(VALUE ZSTDS_EXT_UNUSED(self), VALUE raw_
|
|
142
308
|
return buffer;
|
143
309
|
}
|
144
310
|
|
311
|
+
#else
|
312
|
+
ZSTDS_EXT_NORETURN VALUE zstds_ext_finalize_dictionary_buffer(
|
313
|
+
VALUE ZSTDS_EXT_UNUSED(self),
|
314
|
+
VALUE ZSTDS_EXT_UNUSED(content),
|
315
|
+
VALUE ZSTDS_EXT_UNUSED(raw_samples),
|
316
|
+
VALUE ZSTDS_EXT_UNUSED(options))
|
317
|
+
{
|
318
|
+
zstds_ext_raise_error(ZSTDS_EXT_ERROR_NOT_IMPLEMENTED);
|
319
|
+
}
|
320
|
+
#endif // HAVE_ZDICT_FINALIZE
|
321
|
+
|
145
322
|
// -- other --
|
146
323
|
|
147
324
|
VALUE zstds_ext_get_dictionary_buffer_id(VALUE ZSTDS_EXT_UNUSED(self), VALUE buffer)
|
@@ -154,12 +331,33 @@ VALUE zstds_ext_get_dictionary_buffer_id(VALUE ZSTDS_EXT_UNUSED(self), VALUE buf
|
|
154
331
|
return UINT2NUM(id);
|
155
332
|
}
|
156
333
|
|
334
|
+
#if defined(HAVE_ZDICT_HEADER_SIZE)
|
335
|
+
VALUE zstds_ext_get_dictionary_header_size(VALUE ZSTDS_EXT_UNUSED(self), VALUE buffer)
|
336
|
+
{
|
337
|
+
zstds_result_t result = ZDICT_getDictHeaderSize(RSTRING_PTR(buffer), RSTRING_LEN(buffer));
|
338
|
+
if (ZDICT_isError(result)) {
|
339
|
+
zstds_ext_raise_error(zstds_ext_get_error(ZSTD_getErrorCode(result)));
|
340
|
+
}
|
341
|
+
|
342
|
+
return SIZET2NUM(result);
|
343
|
+
}
|
344
|
+
|
345
|
+
#else
|
346
|
+
ZSTDS_EXT_NORETURN VALUE
|
347
|
+
zstds_ext_get_dictionary_header_size(VALUE ZSTDS_EXT_UNUSED(self), VALUE ZSTDS_EXT_UNUSED(buffer))
|
348
|
+
{
|
349
|
+
zstds_ext_raise_error(ZSTDS_EXT_ERROR_NOT_IMPLEMENTED);
|
350
|
+
};
|
351
|
+
#endif // HAVE_ZDICT_HEADER_SIZE
|
352
|
+
|
157
353
|
// -- exports --
|
158
354
|
|
159
355
|
void zstds_ext_dictionary_exports(VALUE root_module)
|
160
356
|
{
|
161
357
|
VALUE dictionary = rb_define_class_under(root_module, "Dictionary", rb_cObject);
|
162
358
|
|
359
|
+
rb_define_singleton_method(dictionary, "finalize_buffer", zstds_ext_finalize_dictionary_buffer, 3);
|
163
360
|
rb_define_singleton_method(dictionary, "get_buffer_id", zstds_ext_get_dictionary_buffer_id, 1);
|
361
|
+
rb_define_singleton_method(dictionary, "get_header_size", zstds_ext_get_dictionary_header_size, 1);
|
164
362
|
rb_define_singleton_method(dictionary, "train_buffer", zstds_ext_train_dictionary_buffer, 2);
|
165
363
|
}
|
data/ext/zstds_ext/dictionary.h
CHANGED
@@ -6,11 +6,33 @@
|
|
6
6
|
|
7
7
|
#include "ruby.h"
|
8
8
|
|
9
|
+
#include "zstds_ext/macro.h"
|
10
|
+
|
9
11
|
#define ZSTDS_EXT_DEFAULT_DICTIONARY_CAPACITY (1 << 17); // 128 KB
|
12
|
+
#define ZSTDS_EXT_DEFAULT_DICTIONARY_MAX_SIZE ZSTDS_EXT_DEFAULT_DICTIONARY_CAPACITY
|
13
|
+
|
14
|
+
// -- training --
|
10
15
|
|
11
16
|
VALUE zstds_ext_train_dictionary_buffer(VALUE self, VALUE samples, VALUE options);
|
17
|
+
|
18
|
+
// -- finalizing --
|
19
|
+
|
20
|
+
#if defined(HAVE_ZDICT_FINALIZE)
|
21
|
+
VALUE zstds_ext_finalize_dictionary_buffer(VALUE self, VALUE content, VALUE samples, VALUE options);
|
22
|
+
#else
|
23
|
+
ZSTDS_EXT_NORETURN VALUE zstds_ext_finalize_dictionary_buffer(VALUE self, VALUE content, VALUE samples, VALUE options);
|
24
|
+
#endif // HAVE_ZDICT_FINALIZE
|
25
|
+
|
26
|
+
// -- other --
|
27
|
+
|
12
28
|
VALUE zstds_ext_get_dictionary_buffer_id(VALUE self, VALUE buffer);
|
13
29
|
|
30
|
+
#if defined(HAVE_ZDICT_HEADER_SIZE)
|
31
|
+
VALUE zstds_ext_get_dictionary_header_size(VALUE self, VALUE buffer);
|
32
|
+
#else
|
33
|
+
ZSTDS_EXT_NORETURN VALUE zstds_ext_get_dictionary_header_size(VALUE self, VALUE buffer);
|
34
|
+
#endif // HAVE_ZDICT_HEADER_SIZE
|
35
|
+
|
14
36
|
void zstds_ext_dictionary_exports(VALUE root_module);
|
15
37
|
|
16
38
|
#endif // ZSTDS_EXT_DICTIONARY_H
|
data/ext/zstds_ext/error.c
CHANGED
@@ -3,10 +3,6 @@
|
|
3
3
|
|
4
4
|
#include "zstds_ext/error.h"
|
5
5
|
|
6
|
-
#include <zstd_errors.h>
|
7
|
-
|
8
|
-
#include "ruby.h"
|
9
|
-
|
10
6
|
zstds_ext_result_t zstds_ext_get_error(ZSTD_ErrorCode error_code)
|
11
7
|
{
|
12
8
|
switch (error_code) {
|
@@ -73,6 +69,9 @@ void zstds_ext_raise_error(zstds_ext_result_t ext_result)
|
|
73
69
|
case ZSTDS_EXT_ERROR_WRITE_IO:
|
74
70
|
raise_error("WriteIOError", "failed to write IO");
|
75
71
|
|
72
|
+
case ZSTDS_EXT_ERROR_NOT_IMPLEMENTED:
|
73
|
+
raise_error("NotImplementedError", "not implemented error");
|
74
|
+
|
76
75
|
default:
|
77
76
|
// ZSTDS_EXT_ERROR_UNEXPECTED
|
78
77
|
raise_error("UnexpectedError", "unexpected error");
|
data/ext/zstds_ext/error.h
CHANGED
data/ext/zstds_ext/gvl.h
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
#if !defined(ZSTDS_EXT_GVL_H)
|
5
5
|
#define ZSTDS_EXT_GVL_H
|
6
6
|
|
7
|
-
#
|
7
|
+
#if defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL)
|
8
8
|
|
9
9
|
#include "ruby/thread.h"
|
10
10
|
|
@@ -19,6 +19,6 @@
|
|
19
19
|
|
20
20
|
#define ZSTDS_EXT_GVL_WRAP(_gvl, function, data) function((void*) data);
|
21
21
|
|
22
|
-
#endif
|
22
|
+
#endif // HAVE_RB_THREAD_CALL_WITHOUT_GVL
|
23
23
|
|
24
24
|
#endif // ZSTDS_EXT_GVL_H
|
data/ext/zstds_ext/io.c
CHANGED
data/ext/zstds_ext/macro.h
CHANGED
@@ -8,6 +8,12 @@
|
|
8
8
|
#define ZSTDS_EXT_UNUSED(x) x __attribute__((__unused__))
|
9
9
|
#else
|
10
10
|
#define ZSTDS_EXT_UNUSED(x) x
|
11
|
-
#endif
|
11
|
+
#endif // __GNUC__
|
12
|
+
|
13
|
+
#if defined(__GNUC__)
|
14
|
+
#define ZSTDS_EXT_NORETURN __attribute__((__noreturn__))
|
15
|
+
#else
|
16
|
+
#define ZSTDS_EXT_NORETURN
|
17
|
+
#endif // __GNUC__
|
12
18
|
|
13
19
|
#endif // ZSTDS_EXT_MACRO_H
|
data/ext/zstds_ext/main.c
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
// Ruby bindings for zstd library.
|
2
2
|
// Copyright (c) 2019 AUTHORS, MIT License.
|
3
3
|
|
4
|
-
#include "ruby.h"
|
5
4
|
#include "zstds_ext/buffer.h"
|
6
5
|
#include "zstds_ext/dictionary.h"
|
7
6
|
#include "zstds_ext/io.h"
|
@@ -21,4 +20,7 @@ void Init_zstds_ext()
|
|
21
20
|
zstds_ext_compressor_exports(root_module);
|
22
21
|
zstds_ext_decompressor_exports(root_module);
|
23
22
|
zstds_ext_string_exports(root_module);
|
23
|
+
|
24
|
+
VALUE version = rb_str_new2(ZSTD_VERSION_STRING);
|
25
|
+
rb_define_const(root_module, "LIBRARY_VERSION", rb_obj_freeze(version));
|
24
26
|
}
|
data/ext/zstds_ext/option.c
CHANGED
data/ext/zstds_ext/option.h
CHANGED
@@ -68,6 +68,13 @@ typedef struct
|
|
68
68
|
VALUE dictionary;
|
69
69
|
} zstds_ext_decompressor_options_t;
|
70
70
|
|
71
|
+
typedef struct
|
72
|
+
{
|
73
|
+
zstds_ext_option_t compression_level;
|
74
|
+
zstds_ext_option_t notification_level;
|
75
|
+
zstds_ext_option_t dictionary_id;
|
76
|
+
} zstds_ext_dictionary_options_t;
|
77
|
+
|
71
78
|
void zstds_ext_resolve_option(
|
72
79
|
VALUE options,
|
73
80
|
zstds_ext_option_t* option,
|
@@ -117,6 +124,13 @@ void zstds_ext_resolve_dictionary_option(VALUE options, VALUE* option, const cha
|
|
117
124
|
ZSTDS_EXT_RESOLVE_OPTION(options, decompressor_options, ZSTDS_EXT_OPTION_TYPE_UINT, window_log_max); \
|
118
125
|
ZSTDS_EXT_RESOLVE_DICTIONARY_OPTION(options, decompressor_options, dictionary);
|
119
126
|
|
127
|
+
#define ZSTDS_EXT_GET_DICTIONARY_OPTIONS(options) \
|
128
|
+
zstds_ext_dictionary_options_t dictionary_options; \
|
129
|
+
\
|
130
|
+
ZSTDS_EXT_RESOLVE_OPTION(options, dictionary_options, ZSTDS_EXT_OPTION_TYPE_INT, compression_level); \
|
131
|
+
ZSTDS_EXT_RESOLVE_OPTION(options, dictionary_options, ZSTDS_EXT_OPTION_TYPE_UINT, notification_level); \
|
132
|
+
ZSTDS_EXT_RESOLVE_OPTION(options, dictionary_options, ZSTDS_EXT_OPTION_TYPE_UINT, dictionary_id);
|
133
|
+
|
120
134
|
bool zstds_ext_get_bool_option_value(VALUE options, const char* name);
|
121
135
|
size_t zstds_ext_get_size_option_value(VALUE options, const char* name);
|
122
136
|
|
data/ext/zstds_ext/string.c
CHANGED
data/lib/zstds/dictionary.rb
CHANGED
@@ -14,6 +14,20 @@ module ZSTDS
|
|
14
14
|
}
|
15
15
|
.freeze
|
16
16
|
|
17
|
+
FINALIZE_DEFAULTS = {
|
18
|
+
:gvl => false,
|
19
|
+
:max_size => 0,
|
20
|
+
:dictionary_options => {}
|
21
|
+
}
|
22
|
+
.freeze
|
23
|
+
|
24
|
+
FINALIZE_DICTIONARY_DEFAULTS = {
|
25
|
+
:compression_level => 0,
|
26
|
+
:notification_level => 0,
|
27
|
+
:dictionary_id => 0
|
28
|
+
}
|
29
|
+
.freeze
|
30
|
+
|
17
31
|
attr_reader :buffer
|
18
32
|
|
19
33
|
def initialize(buffer)
|
@@ -23,17 +37,8 @@ module ZSTDS
|
|
23
37
|
@buffer = buffer
|
24
38
|
end
|
25
39
|
|
26
|
-
def id
|
27
|
-
self.class.get_buffer_id @buffer
|
28
|
-
end
|
29
|
-
|
30
40
|
def self.train(samples, options = {})
|
31
|
-
|
32
|
-
|
33
|
-
samples.each do |sample|
|
34
|
-
Validation.validate_string sample
|
35
|
-
raise ValidateError, "dictionary sample should not be empty" if sample.empty?
|
36
|
-
end
|
41
|
+
validate_samples samples
|
37
42
|
|
38
43
|
Validation.validate_hash options
|
39
44
|
|
@@ -45,5 +50,50 @@ module ZSTDS
|
|
45
50
|
buffer = train_buffer samples, options
|
46
51
|
new buffer
|
47
52
|
end
|
53
|
+
|
54
|
+
def self.finalize(content, samples, options = {})
|
55
|
+
Validation.validate_string content
|
56
|
+
raise ValidateError, "content should not be empty" if content.empty?
|
57
|
+
|
58
|
+
validate_samples samples
|
59
|
+
|
60
|
+
Validation.validate_hash options
|
61
|
+
|
62
|
+
options = FINALIZE_DEFAULTS.merge options
|
63
|
+
|
64
|
+
Validation.validate_bool options[:gvl]
|
65
|
+
Validation.validate_not_negative_integer options[:max_size]
|
66
|
+
Validation.validate_hash options[:dictionary_options]
|
67
|
+
|
68
|
+
dictionary_options = FINALIZE_DICTIONARY_DEFAULTS.merge options[:dictionary_options]
|
69
|
+
|
70
|
+
compression_level = dictionary_options[:compression_level]
|
71
|
+
Validation.validate_integer compression_level
|
72
|
+
raise ValidateError, "invalid compression level" if
|
73
|
+
compression_level < Option::MIN_COMPRESSION_LEVEL || compression_level > Option::MAX_COMPRESSION_LEVEL
|
74
|
+
|
75
|
+
Validation.validate_not_negative_integer dictionary_options[:notification_level]
|
76
|
+
Validation.validate_not_negative_integer dictionary_options[:dictionary_id]
|
77
|
+
|
78
|
+
buffer = finalize_buffer content, samples, options
|
79
|
+
new buffer
|
80
|
+
end
|
81
|
+
|
82
|
+
def self.validate_samples(samples)
|
83
|
+
Validation.validate_array samples
|
84
|
+
|
85
|
+
samples.each do |sample|
|
86
|
+
Validation.validate_string sample
|
87
|
+
raise ValidateError, "dictionary sample should not be empty" if sample.empty?
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def id
|
92
|
+
self.class.get_buffer_id @buffer
|
93
|
+
end
|
94
|
+
|
95
|
+
def header_size
|
96
|
+
self.class.get_header_size @buffer
|
97
|
+
end
|
48
98
|
end
|
49
99
|
end
|
data/lib/zstds/error.rb
CHANGED
@@ -24,9 +24,7 @@ module ZSTDS
|
|
24
24
|
|
25
25
|
def initialize(io, options = {})
|
26
26
|
@raw_stream = create_raw_stream
|
27
|
-
|
28
|
-
Validation.validate_io io
|
29
|
-
@io = io
|
27
|
+
@io = io
|
30
28
|
|
31
29
|
@stat = Stat.new @io.stat if @io.respond_to? :stat
|
32
30
|
|
@@ -135,13 +133,19 @@ module ZSTDS
|
|
135
133
|
end
|
136
134
|
|
137
135
|
def close
|
138
|
-
@io.close
|
136
|
+
@io.close if @io.respond_to? :close
|
139
137
|
|
140
138
|
nil
|
141
139
|
end
|
142
140
|
|
143
141
|
def closed?
|
144
|
-
|
142
|
+
return false unless @raw_stream.closed?
|
143
|
+
|
144
|
+
if @io.respond_to? :closed
|
145
|
+
@io.closed?
|
146
|
+
else
|
147
|
+
true
|
148
|
+
end
|
145
149
|
end
|
146
150
|
|
147
151
|
def to_io
|
data/lib/zstds/stream/reader.rb
CHANGED
@@ -54,6 +54,9 @@ module ZSTDS
|
|
54
54
|
Validation.validate_not_negative_integer bytes_to_read unless bytes_to_read.nil?
|
55
55
|
Validation.validate_string out_buffer unless out_buffer.nil?
|
56
56
|
|
57
|
+
raise ValidateError, "io should be responsible to read and eof" unless
|
58
|
+
@io.respond_to?(:read) && @io.respond_to?(:eof?)
|
59
|
+
|
57
60
|
unless bytes_to_read.nil?
|
58
61
|
return ::String.new :encoding => ::Encoding::BINARY if bytes_to_read.zero?
|
59
62
|
return nil if eof?
|
@@ -86,16 +89,22 @@ module ZSTDS
|
|
86
89
|
end
|
87
90
|
|
88
91
|
def eof?
|
92
|
+
raise ValidateError, "io should be responsible to eof" unless @io.respond_to? :eof?
|
93
|
+
|
89
94
|
empty? && @io.eof?
|
90
95
|
end
|
91
96
|
|
92
97
|
# -- asynchronous --
|
93
98
|
|
94
99
|
def readpartial(bytes_to_read, out_buffer = nil)
|
100
|
+
raise ValidateError, "io should be responsible to readpartial" unless @io.respond_to? :readpartial
|
101
|
+
|
95
102
|
read_more_nonblock(bytes_to_read, out_buffer) { @io.readpartial @source_buffer_length }
|
96
103
|
end
|
97
104
|
|
98
105
|
def read_nonblock(bytes_to_read, out_buffer = nil, *options)
|
106
|
+
raise ValidateError, "io should be responsible to read nonblock" unless @io.respond_to? :read_nonblock
|
107
|
+
|
99
108
|
read_more_nonblock(bytes_to_read, out_buffer) { @io.read_nonblock(@source_buffer_length, *options) }
|
100
109
|
end
|
101
110
|
|
data/lib/zstds/stream/writer.rb
CHANGED
@@ -23,6 +23,8 @@ module ZSTDS
|
|
23
23
|
# -- synchronous --
|
24
24
|
|
25
25
|
def write(*objects)
|
26
|
+
validate_write
|
27
|
+
|
26
28
|
write_remaining_buffer
|
27
29
|
|
28
30
|
bytes_written = 0
|
@@ -38,20 +40,26 @@ module ZSTDS
|
|
38
40
|
end
|
39
41
|
|
40
42
|
def flush
|
43
|
+
validate_write
|
44
|
+
|
41
45
|
finish :flush
|
42
46
|
|
43
|
-
@io.flush
|
47
|
+
@io.flush if @io.respond_to? :flush
|
44
48
|
|
45
49
|
self
|
46
50
|
end
|
47
51
|
|
48
52
|
def rewind
|
53
|
+
validate_write
|
54
|
+
|
49
55
|
finish :close
|
50
56
|
|
51
57
|
super
|
52
58
|
end
|
53
59
|
|
54
60
|
def close
|
61
|
+
validate_write
|
62
|
+
|
55
63
|
finish :close
|
56
64
|
|
57
65
|
super
|
@@ -75,6 +83,10 @@ module ZSTDS
|
|
75
83
|
@raw_stream.send(method_name, *args) { |portion| @io.write portion }
|
76
84
|
end
|
77
85
|
|
86
|
+
def validate_write
|
87
|
+
raise ValidateError, "io should be responsible to write" unless @io.respond_to? :write
|
88
|
+
end
|
89
|
+
|
78
90
|
# -- asynchronous --
|
79
91
|
|
80
92
|
# IO write nonblock can raise wait writable error.
|
@@ -83,6 +95,8 @@ module ZSTDS
|
|
83
95
|
# So we have to accept content after processing IO write nonblock.
|
84
96
|
# It means that first write nonblock won't call IO write nonblock.
|
85
97
|
def write_nonblock(object, *options)
|
98
|
+
validate_write_nonblock
|
99
|
+
|
86
100
|
return 0 unless write_remaining_buffer_nonblock(*options)
|
87
101
|
|
88
102
|
source = transcode object.to_s
|
@@ -93,14 +107,18 @@ module ZSTDS
|
|
93
107
|
end
|
94
108
|
|
95
109
|
def flush_nonblock(*options)
|
110
|
+
validate_write_nonblock
|
111
|
+
|
96
112
|
return false unless finish_nonblock :flush, *options
|
97
113
|
|
98
|
-
@io.flush
|
114
|
+
@io.flush if @io.respond_to? :flush
|
99
115
|
|
100
116
|
true
|
101
117
|
end
|
102
118
|
|
103
119
|
def rewind_nonblock(*options)
|
120
|
+
validate_write_nonblock
|
121
|
+
|
104
122
|
return false unless finish_nonblock :close, *options
|
105
123
|
|
106
124
|
method(:rewind).super_method.call
|
@@ -109,6 +127,8 @@ module ZSTDS
|
|
109
127
|
end
|
110
128
|
|
111
129
|
def close_nonblock(*options)
|
130
|
+
validate_write_nonblock
|
131
|
+
|
112
132
|
return false unless finish_nonblock :close, *options
|
113
133
|
|
114
134
|
method(:close).super_method.call
|
@@ -139,6 +159,10 @@ module ZSTDS
|
|
139
159
|
@raw_stream.send(method_name, *args) { |portion| @buffer << portion }
|
140
160
|
end
|
141
161
|
|
162
|
+
def validate_write_nonblock
|
163
|
+
raise ValidateError, "io should be responsible to write nonblock" unless @io.respond_to? :write_nonblock
|
164
|
+
end
|
165
|
+
|
142
166
|
# -- common --
|
143
167
|
|
144
168
|
protected def transcode(data)
|
data/lib/zstds/validation.rb
CHANGED
@@ -5,33 +5,34 @@ require_relative "error"
|
|
5
5
|
|
6
6
|
module ZSTDS
|
7
7
|
module Validation
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
readpartial
|
12
|
-
read_nonblock
|
13
|
-
write_nonblock
|
14
|
-
eof?
|
15
|
-
flush
|
16
|
-
close
|
17
|
-
closed?
|
18
|
-
]
|
19
|
-
.freeze
|
8
|
+
def self.validate_array(value)
|
9
|
+
raise ValidateError, "invalid array" unless value.is_a? ::Array
|
10
|
+
end
|
20
11
|
|
21
12
|
def self.validate_bool(value)
|
22
13
|
raise ValidateError, "invalid bool" unless value.is_a?(::TrueClass) || value.is_a?(::FalseClass)
|
23
14
|
end
|
24
15
|
|
16
|
+
def self.validate_hash(value)
|
17
|
+
raise ValidateError, "invalid hash" unless value.is_a? ::Hash
|
18
|
+
end
|
19
|
+
|
25
20
|
def self.validate_integer(value)
|
26
21
|
raise ValidateError, "invalid integer" unless value.is_a? ::Integer
|
27
22
|
end
|
28
23
|
|
24
|
+
def self.validate_not_negative_integer(value)
|
25
|
+
raise ValidateError, "invalid not negative integer" unless value.is_a?(::Integer) && value >= 0
|
26
|
+
end
|
27
|
+
|
29
28
|
def self.validate_positive_integer(value)
|
30
29
|
raise ValidateError, "invalid positive integer" unless value.is_a?(::Integer) && value.positive?
|
31
30
|
end
|
32
31
|
|
33
|
-
def self.
|
34
|
-
|
32
|
+
def self.validate_proc(value)
|
33
|
+
unless value.is_a?(::Proc) || value.is_a?(::Method) || value.is_a?(::UnboundMethod)
|
34
|
+
raise ValidateError, "invalid proc"
|
35
|
+
end
|
35
36
|
end
|
36
37
|
|
37
38
|
def self.validate_string(value)
|
@@ -41,23 +42,5 @@ module ZSTDS
|
|
41
42
|
def self.validate_symbol(value)
|
42
43
|
raise ValidateError, "invalid symbol" unless value.is_a? ::Symbol
|
43
44
|
end
|
44
|
-
|
45
|
-
def self.validate_io(value)
|
46
|
-
raise ValidateError, "invalid io" unless IO_METHODS.all? { |method| value.respond_to? method }
|
47
|
-
end
|
48
|
-
|
49
|
-
def self.validate_array(value)
|
50
|
-
raise ValidateError, "invalid array" unless value.is_a? ::Array
|
51
|
-
end
|
52
|
-
|
53
|
-
def self.validate_hash(value)
|
54
|
-
raise ValidateError, "invalid hash" unless value.is_a? ::Hash
|
55
|
-
end
|
56
|
-
|
57
|
-
def self.validate_proc(value)
|
58
|
-
unless value.is_a?(::Proc) || value.is_a?(::Method) || value.is_a?(::UnboundMethod)
|
59
|
-
raise ValidateError, "invalid proc"
|
60
|
-
end
|
61
|
-
end
|
62
45
|
end
|
63
46
|
end
|
data/lib/zstds/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-zstds
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Aladjev
|
8
|
+
- Ivan Takarlikov
|
9
|
+
- Jenner La Fave
|
8
10
|
autorequire:
|
9
11
|
bindir: bin
|
10
12
|
cert_chain: []
|
11
|
-
date:
|
13
|
+
date: 2022-03-18 00:00:00.000000000 Z
|
12
14
|
dependencies:
|
13
15
|
- !ruby/object:Gem::Dependency
|
14
16
|
name: codecov
|
@@ -58,28 +60,28 @@ dependencies:
|
|
58
60
|
requirements:
|
59
61
|
- - "~>"
|
60
62
|
- !ruby/object:Gem::Version
|
61
|
-
version: '5.
|
63
|
+
version: '5.15'
|
62
64
|
type: :development
|
63
65
|
prerelease: false
|
64
66
|
version_requirements: !ruby/object:Gem::Requirement
|
65
67
|
requirements:
|
66
68
|
- - "~>"
|
67
69
|
- !ruby/object:Gem::Version
|
68
|
-
version: '5.
|
70
|
+
version: '5.15'
|
69
71
|
- !ruby/object:Gem::Dependency
|
70
72
|
name: ocg
|
71
73
|
requirement: !ruby/object:Gem::Requirement
|
72
74
|
requirements:
|
73
75
|
- - "~>"
|
74
76
|
- !ruby/object:Gem::Version
|
75
|
-
version: '1.
|
77
|
+
version: '1.4'
|
76
78
|
type: :development
|
77
79
|
prerelease: false
|
78
80
|
version_requirements: !ruby/object:Gem::Requirement
|
79
81
|
requirements:
|
80
82
|
- - "~>"
|
81
83
|
- !ruby/object:Gem::Version
|
82
|
-
version: '1.
|
84
|
+
version: '1.4'
|
83
85
|
- !ruby/object:Gem::Dependency
|
84
86
|
name: parallel
|
85
87
|
requirement: !ruby/object:Gem::Requirement
|
@@ -128,56 +130,56 @@ dependencies:
|
|
128
130
|
requirements:
|
129
131
|
- - "~>"
|
130
132
|
- !ruby/object:Gem::Version
|
131
|
-
version: '1.
|
133
|
+
version: '1.26'
|
132
134
|
type: :development
|
133
135
|
prerelease: false
|
134
136
|
version_requirements: !ruby/object:Gem::Requirement
|
135
137
|
requirements:
|
136
138
|
- - "~>"
|
137
139
|
- !ruby/object:Gem::Version
|
138
|
-
version: '1.
|
140
|
+
version: '1.26'
|
139
141
|
- !ruby/object:Gem::Dependency
|
140
142
|
name: rubocop-minitest
|
141
143
|
requirement: !ruby/object:Gem::Requirement
|
142
144
|
requirements:
|
143
145
|
- - "~>"
|
144
146
|
- !ruby/object:Gem::Version
|
145
|
-
version: '0.
|
147
|
+
version: '0.17'
|
146
148
|
type: :development
|
147
149
|
prerelease: false
|
148
150
|
version_requirements: !ruby/object:Gem::Requirement
|
149
151
|
requirements:
|
150
152
|
- - "~>"
|
151
153
|
- !ruby/object:Gem::Version
|
152
|
-
version: '0.
|
154
|
+
version: '0.17'
|
153
155
|
- !ruby/object:Gem::Dependency
|
154
156
|
name: rubocop-performance
|
155
157
|
requirement: !ruby/object:Gem::Requirement
|
156
158
|
requirements:
|
157
159
|
- - "~>"
|
158
160
|
- !ruby/object:Gem::Version
|
159
|
-
version: '1.
|
161
|
+
version: '1.13'
|
160
162
|
type: :development
|
161
163
|
prerelease: false
|
162
164
|
version_requirements: !ruby/object:Gem::Requirement
|
163
165
|
requirements:
|
164
166
|
- - "~>"
|
165
167
|
- !ruby/object:Gem::Version
|
166
|
-
version: '1.
|
168
|
+
version: '1.13'
|
167
169
|
- !ruby/object:Gem::Dependency
|
168
170
|
name: rubocop-rake
|
169
171
|
requirement: !ruby/object:Gem::Requirement
|
170
172
|
requirements:
|
171
173
|
- - "~>"
|
172
174
|
- !ruby/object:Gem::Version
|
173
|
-
version: '0.
|
175
|
+
version: '0.6'
|
174
176
|
type: :development
|
175
177
|
prerelease: false
|
176
178
|
version_requirements: !ruby/object:Gem::Requirement
|
177
179
|
requirements:
|
178
180
|
- - "~>"
|
179
181
|
- !ruby/object:Gem::Version
|
180
|
-
version: '0.
|
182
|
+
version: '0.6'
|
181
183
|
- !ruby/object:Gem::Dependency
|
182
184
|
name: simplecov
|
183
185
|
requirement: !ruby/object:Gem::Requirement
|
@@ -244,7 +246,8 @@ files:
|
|
244
246
|
homepage: https://github.com/andrew-aladev/ruby-zstds
|
245
247
|
licenses:
|
246
248
|
- MIT
|
247
|
-
metadata:
|
249
|
+
metadata:
|
250
|
+
rubygems_mfa_required: 'true'
|
248
251
|
post_install_message:
|
249
252
|
rdoc_options: []
|
250
253
|
require_paths:
|
@@ -260,7 +263,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
260
263
|
- !ruby/object:Gem::Version
|
261
264
|
version: '0'
|
262
265
|
requirements: []
|
263
|
-
rubygems_version: 3.
|
266
|
+
rubygems_version: 3.3.7
|
264
267
|
signing_key:
|
265
268
|
specification_version: 4
|
266
269
|
summary: Ruby bindings for zstd library.
|