ruby-zstds 1.3.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,195 +1,16 @@
1
1
  # Ruby bindings for zstd library.
2
2
  # Copyright (c) 2019 AUTHORS, MIT License.
3
3
 
4
- require_relative "abstract"
4
+ require "adsp/stream/reader"
5
+
5
6
  require_relative "raw/decompressor"
6
- require_relative "reader_helpers"
7
- require_relative "../validation"
8
7
 
9
8
  module ZSTDS
10
9
  module Stream
11
- class Reader < Abstract
12
- include ReaderHelpers
13
-
14
- attr_accessor :lineno
15
-
16
- def initialize(source_io, options = {}, *args)
17
- @options = options
18
-
19
- super source_io, *args
20
-
21
- initialize_source_buffer_length
22
- reset_io_remainder
23
- reset_need_to_flush
24
-
25
- @lineno = 0
26
- end
27
-
28
- protected def create_raw_stream
29
- Raw::Decompressor.new @options
30
- end
31
-
32
- protected def initialize_source_buffer_length
33
- source_buffer_length = @options[:source_buffer_length]
34
- Validation.validate_not_negative_integer source_buffer_length unless source_buffer_length.nil?
35
-
36
- if source_buffer_length.nil? || source_buffer_length.zero?
37
- source_buffer_length = Buffer::DEFAULT_SOURCE_BUFFER_LENGTH_FOR_DECOMPRESSOR
38
- end
39
-
40
- @source_buffer_length = source_buffer_length
41
- end
42
-
43
- protected def reset_io_remainder
44
- @io_remainder = ::String.new :encoding => ::Encoding::BINARY
45
- end
46
-
47
- protected def reset_need_to_flush
48
- @need_to_flush = false
49
- end
50
-
51
- # -- synchronous --
52
-
53
- def read(bytes_to_read = nil, out_buffer = nil)
54
- Validation.validate_not_negative_integer bytes_to_read unless bytes_to_read.nil?
55
- Validation.validate_string out_buffer unless out_buffer.nil?
56
-
57
- raise ValidateError, "io should be responsible to read and eof" unless
58
- @io.respond_to?(:read) && @io.respond_to?(:eof?)
59
-
60
- unless bytes_to_read.nil?
61
- return ::String.new :encoding => ::Encoding::BINARY if bytes_to_read.zero?
62
- return nil if eof?
63
-
64
- append_io_data @io.read(@source_buffer_length) while @buffer.bytesize < bytes_to_read && !@io.eof?
65
- flush_io_data if @buffer.bytesize < bytes_to_read
66
-
67
- return read_bytes_from_buffer bytes_to_read, out_buffer
68
- end
69
-
70
- append_io_data @io.read(@source_buffer_length) until @io.eof?
71
- flush_io_data
72
-
73
- read_buffer out_buffer
74
- end
75
-
76
- def rewind
77
- raw_wrapper :close
78
-
79
- reset_io_remainder
80
- reset_need_to_flush
81
-
82
- super
83
- end
84
-
85
- def close
86
- raw_wrapper :close
87
-
88
- super
89
- end
90
-
91
- def eof?
92
- raise ValidateError, "io should be responsible to eof" unless @io.respond_to? :eof?
93
-
94
- empty? && @io.eof?
95
- end
96
-
97
- # -- asynchronous --
98
-
99
- def readpartial(bytes_to_read, out_buffer = nil)
100
- raise ValidateError, "io should be responsible to readpartial" unless @io.respond_to? :readpartial
101
-
102
- read_more_nonblock(bytes_to_read, out_buffer) { @io.readpartial @source_buffer_length }
103
- end
104
-
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
-
108
- read_more_nonblock(bytes_to_read, out_buffer) { @io.read_nonblock(@source_buffer_length, *options) }
109
- end
110
-
111
- protected def read_more_nonblock(bytes_to_read, out_buffer, &_block)
112
- Validation.validate_not_negative_integer bytes_to_read
113
- Validation.validate_string out_buffer unless out_buffer.nil?
114
-
115
- return ::String.new :encoding => ::Encoding::BINARY if bytes_to_read.zero?
116
-
117
- io_provided_eof_error = false
118
-
119
- if @buffer.bytesize < bytes_to_read
120
- begin
121
- append_io_data yield
122
- rescue ::EOFError
123
- io_provided_eof_error = true
124
- end
125
- end
126
-
127
- flush_io_data if @buffer.bytesize < bytes_to_read
128
- raise ::EOFError if empty? && io_provided_eof_error
129
-
130
- read_bytes_from_buffer bytes_to_read, out_buffer
131
- end
132
-
133
- # -- common --
134
-
135
- protected def append_io_data(io_data)
136
- io_portion = @io_remainder + io_data
137
- bytes_read = raw_wrapper :read, io_portion
138
- @io_remainder = io_portion.byteslice bytes_read, io_portion.bytesize - bytes_read
139
-
140
- # Even empty io data may require flush.
141
- @need_to_flush = true
142
- end
143
-
144
- protected def flush_io_data
145
- raw_wrapper :flush
146
-
147
- @need_to_flush = false
148
- end
149
-
150
- protected def empty?
151
- !@need_to_flush && @buffer.bytesize.zero?
152
- end
153
-
154
- protected def read_bytes_from_buffer(bytes_to_read, out_buffer)
155
- bytes_read = [@buffer.bytesize, bytes_to_read].min
156
-
157
- # Result uses buffer binary encoding.
158
- result = @buffer.byteslice 0, bytes_read
159
- @buffer = @buffer.byteslice bytes_read, @buffer.bytesize - bytes_read
160
- @pos += bytes_read
161
-
162
- result = out_buffer.replace result unless out_buffer.nil?
163
- result
164
- end
165
-
166
- protected def read_buffer(out_buffer)
167
- result = @buffer
168
- reset_buffer
169
- @pos += result.bytesize
170
-
171
- result.force_encoding @external_encoding unless @external_encoding.nil?
172
- result = transcode_to_internal result
173
-
174
- result = out_buffer.replace result unless out_buffer.nil?
175
- result
176
- end
177
-
178
- protected def transcode_to_internal(data)
179
- data = data.encode @internal_encoding, **@transcode_options unless @internal_encoding.nil?
180
- data
181
- end
182
-
183
- # We should be able to return data back to buffer.
184
- # We won't use any transcode options because transcoded data should be backward compatible.
185
- protected def transcode_to_external(data)
186
- data = data.encode @external_encoding unless @external_encoding.nil?
187
- data
188
- end
189
-
190
- protected def raw_wrapper(method_name, *args)
191
- @raw_stream.send(method_name, *args) { |portion| @buffer << portion }
192
- end
10
+ # ZSTDS::Stream::Reader class.
11
+ class Reader < ADSP::Stream::Reader
12
+ # Current raw stream class.
13
+ RawDecompressor = Raw::Decompressor
193
14
  end
194
15
  end
195
16
  end
@@ -1,174 +1,16 @@
1
1
  # Ruby bindings for zstd library.
2
2
  # Copyright (c) 2019 AUTHORS, MIT License.
3
3
 
4
- require_relative "abstract"
4
+ require "adsp/stream/writer"
5
+
5
6
  require_relative "raw/compressor"
6
- require_relative "writer_helpers"
7
7
 
8
8
  module ZSTDS
9
9
  module Stream
10
- class Writer < Abstract
11
- include WriterHelpers
12
-
13
- def initialize(destination_io, options = {}, *args)
14
- @options = options
15
-
16
- super destination_io, *args
17
- end
18
-
19
- protected def create_raw_stream
20
- Raw::Compressor.new @options
21
- end
22
-
23
- # -- synchronous --
24
-
25
- def write(*objects)
26
- validate_write
27
-
28
- write_remaining_buffer
29
-
30
- bytes_written = 0
31
-
32
- objects.each do |object|
33
- source = transcode object.to_s
34
- bytes_written += raw_wrapper :write, source
35
- end
36
-
37
- @pos += bytes_written
38
-
39
- bytes_written
40
- end
41
-
42
- def flush
43
- validate_write
44
-
45
- finish :flush
46
-
47
- @io.flush if @io.respond_to? :flush
48
-
49
- self
50
- end
51
-
52
- def rewind
53
- validate_write
54
-
55
- finish :close
56
-
57
- super
58
- end
59
-
60
- def close
61
- validate_write
62
-
63
- finish :close
64
-
65
- super
66
- end
67
-
68
- protected def finish(method_name)
69
- write_remaining_buffer
70
-
71
- raw_wrapper method_name
72
- end
73
-
74
- protected def write_remaining_buffer
75
- return nil if @buffer.bytesize.zero?
76
-
77
- @io.write @buffer
78
-
79
- reset_buffer
80
- end
81
-
82
- protected def raw_wrapper(method_name, *args)
83
- @raw_stream.send(method_name, *args) { |portion| @io.write portion }
84
- end
85
-
86
- def validate_write
87
- raise ValidateError, "io should be responsible to write" unless @io.respond_to? :write
88
- end
89
-
90
- # -- asynchronous --
91
-
92
- # IO write nonblock can raise wait writable error.
93
- # After resolving this error user may provide same content again.
94
- # It is not possible to revert accepted content after error.
95
- # So we have to accept content after processing IO write nonblock.
96
- # It means that first write nonblock won't call IO write nonblock.
97
- def write_nonblock(object, *options)
98
- validate_write_nonblock
99
-
100
- return 0 unless write_remaining_buffer_nonblock(*options)
101
-
102
- source = transcode object.to_s
103
- bytes_written = raw_nonblock_wrapper :write, source
104
- @pos += bytes_written
105
-
106
- bytes_written
107
- end
108
-
109
- def flush_nonblock(*options)
110
- validate_write_nonblock
111
-
112
- return false unless finish_nonblock :flush, *options
113
-
114
- @io.flush if @io.respond_to? :flush
115
-
116
- true
117
- end
118
-
119
- def rewind_nonblock(*options)
120
- validate_write_nonblock
121
-
122
- return false unless finish_nonblock :close, *options
123
-
124
- method(:rewind).super_method.call
125
-
126
- true
127
- end
128
-
129
- def close_nonblock(*options)
130
- validate_write_nonblock
131
-
132
- return false unless finish_nonblock :close, *options
133
-
134
- method(:close).super_method.call
135
-
136
- true
137
- end
138
-
139
- protected def finish_nonblock(method_name, *options)
140
- return false unless write_remaining_buffer_nonblock(*options)
141
-
142
- raw_nonblock_wrapper method_name
143
-
144
- write_remaining_buffer_nonblock(*options)
145
- end
146
-
147
- protected def write_remaining_buffer_nonblock(*options)
148
- return true if @buffer.bytesize.zero?
149
-
150
- bytes_written = @io.write_nonblock @buffer, *options
151
- return false if bytes_written.zero?
152
-
153
- @buffer = @buffer.byteslice bytes_written, @buffer.bytesize - bytes_written
154
-
155
- @buffer.bytesize.zero?
156
- end
157
-
158
- protected def raw_nonblock_wrapper(method_name, *args)
159
- @raw_stream.send(method_name, *args) { |portion| @buffer << portion }
160
- end
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
-
166
- # -- common --
167
-
168
- protected def transcode(data)
169
- data = data.encode @external_encoding, **@transcode_options unless @external_encoding.nil?
170
- data
171
- end
10
+ # ZSTDS::Stream::Writer class.
11
+ class Writer < ADSP::Stream::Writer
12
+ # Current raw stream class.
13
+ RawCompressor = Raw::Compressor
172
14
  end
173
15
  end
174
16
  end
data/lib/zstds/string.rb CHANGED
@@ -1,15 +1,22 @@
1
1
  # Ruby bindings for zstd library.
2
2
  # Copyright (c) 2019 AUTHORS, MIT License.
3
3
 
4
+ require "adsp/string"
4
5
  require "zstds_ext"
5
6
 
6
7
  require_relative "option"
7
8
  require_relative "validation"
8
9
 
9
10
  module ZSTDS
10
- module String
11
- BUFFER_LENGTH_NAMES = %i[destination_buffer_length].freeze
12
-
11
+ # ZSTDS::String class.
12
+ class String < ADSP::String
13
+ # Current option class.
14
+ Option = ZSTDS::Option
15
+
16
+ # Compresses +source+ string using +options+.
17
+ # Option: +:destination_buffer_length+ destination buffer length.
18
+ # Option: +:pledged_size+ source bytesize.
19
+ # Returns compressed string.
13
20
  def self.compress(source, options = {})
14
21
  Validation.validate_string source
15
22
 
@@ -17,15 +24,17 @@ module ZSTDS
17
24
 
18
25
  options[:pledged_size] = source.bytesize
19
26
 
20
- ZSTDS._native_compress_string source, options
27
+ super source, options
21
28
  end
22
29
 
23
- def self.decompress(source, options = {})
24
- Validation.validate_string source
25
-
26
- options = Option.get_decompressor_options options, BUFFER_LENGTH_NAMES
30
+ # Bypasses native compress.
31
+ def self.native_compress_string(*args)
32
+ ZSTDS._native_compress_string(*args)
33
+ end
27
34
 
28
- ZSTDS._native_decompress_string source, options
35
+ # Bypasses native decompress.
36
+ def self.native_decompress_string(*args)
37
+ ZSTDS._native_decompress_string(*args)
29
38
  end
30
39
  end
31
40
  end
@@ -1,46 +1,19 @@
1
1
  # Ruby bindings for zstd library.
2
2
  # Copyright (c) 2019 AUTHORS, MIT License.
3
3
 
4
- require_relative "error"
4
+ require "adsp/validation"
5
5
 
6
6
  module ZSTDS
7
- module Validation
8
- def self.validate_array(value)
9
- raise ValidateError, "invalid array" unless value.is_a? ::Array
10
- end
11
-
7
+ # ZSTDS::Validation class.
8
+ class Validation < ADSP::Validation
9
+ # Raises error when +value+ is not boolean.
12
10
  def self.validate_bool(value)
13
11
  raise ValidateError, "invalid bool" unless value.is_a?(::TrueClass) || value.is_a?(::FalseClass)
14
12
  end
15
13
 
16
- def self.validate_hash(value)
17
- raise ValidateError, "invalid hash" unless value.is_a? ::Hash
18
- end
19
-
14
+ # Raises error when +value+ is not integer.
20
15
  def self.validate_integer(value)
21
16
  raise ValidateError, "invalid integer" unless value.is_a? ::Integer
22
17
  end
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
-
28
- def self.validate_positive_integer(value)
29
- raise ValidateError, "invalid positive integer" unless value.is_a?(::Integer) && value.positive?
30
- end
31
-
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
36
- end
37
-
38
- def self.validate_string(value)
39
- raise ValidateError, "invalid string" unless value.is_a? ::String
40
- end
41
-
42
- def self.validate_symbol(value)
43
- raise ValidateError, "invalid symbol" unless value.is_a? ::Symbol
44
- end
45
18
  end
46
19
  end
data/lib/zstds/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
  # Copyright (c) 2019 AUTHORS, MIT License.
3
3
 
4
4
  module ZSTDS
5
- VERSION = "1.3.0".freeze
5
+ VERSION = "1.3.1".freeze
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-zstds
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Aladjev
@@ -10,8 +10,22 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2022-03-18 00:00:00.000000000 Z
13
+ date: 2022-07-06 00:00:00.000000000 Z
14
14
  dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: adsp
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '1.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '1.0'
15
29
  - !ruby/object:Gem::Dependency
16
30
  name: codecov
17
31
  requirement: !ruby/object:Gem::Requirement
@@ -60,14 +74,14 @@ dependencies:
60
74
  requirements:
61
75
  - - "~>"
62
76
  - !ruby/object:Gem::Version
63
- version: '5.15'
77
+ version: '5.16'
64
78
  type: :development
65
79
  prerelease: false
66
80
  version_requirements: !ruby/object:Gem::Requirement
67
81
  requirements:
68
82
  - - "~>"
69
83
  - !ruby/object:Gem::Version
70
- version: '5.15'
84
+ version: '5.16'
71
85
  - !ruby/object:Gem::Dependency
72
86
  name: ocg
73
87
  requirement: !ruby/object:Gem::Requirement
@@ -124,48 +138,62 @@ dependencies:
124
138
  - - ">="
125
139
  - !ruby/object:Gem::Version
126
140
  version: '0'
141
+ - !ruby/object:Gem::Dependency
142
+ name: rdoc
143
+ requirement: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ type: :development
149
+ prerelease: false
150
+ version_requirements: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
127
155
  - !ruby/object:Gem::Dependency
128
156
  name: rubocop
129
157
  requirement: !ruby/object:Gem::Requirement
130
158
  requirements:
131
159
  - - "~>"
132
160
  - !ruby/object:Gem::Version
133
- version: '1.26'
161
+ version: '1.31'
134
162
  type: :development
135
163
  prerelease: false
136
164
  version_requirements: !ruby/object:Gem::Requirement
137
165
  requirements:
138
166
  - - "~>"
139
167
  - !ruby/object:Gem::Version
140
- version: '1.26'
168
+ version: '1.31'
141
169
  - !ruby/object:Gem::Dependency
142
170
  name: rubocop-minitest
143
171
  requirement: !ruby/object:Gem::Requirement
144
172
  requirements:
145
173
  - - "~>"
146
174
  - !ruby/object:Gem::Version
147
- version: '0.17'
175
+ version: '0.20'
148
176
  type: :development
149
177
  prerelease: false
150
178
  version_requirements: !ruby/object:Gem::Requirement
151
179
  requirements:
152
180
  - - "~>"
153
181
  - !ruby/object:Gem::Version
154
- version: '0.17'
182
+ version: '0.20'
155
183
  - !ruby/object:Gem::Dependency
156
184
  name: rubocop-performance
157
185
  requirement: !ruby/object:Gem::Requirement
158
186
  requirements:
159
187
  - - "~>"
160
188
  - !ruby/object:Gem::Version
161
- version: '1.13'
189
+ version: '1.14'
162
190
  type: :development
163
191
  prerelease: false
164
192
  version_requirements: !ruby/object:Gem::Requirement
165
193
  requirements:
166
194
  - - "~>"
167
195
  - !ruby/object:Gem::Version
168
- version: '1.13'
196
+ version: '1.14'
169
197
  - !ruby/object:Gem::Dependency
170
198
  name: rubocop-rake
171
199
  requirement: !ruby/object:Gem::Requirement
@@ -230,16 +258,10 @@ files:
230
258
  - lib/zstds/error.rb
231
259
  - lib/zstds/file.rb
232
260
  - lib/zstds/option.rb
233
- - lib/zstds/stream/abstract.rb
234
- - lib/zstds/stream/delegates.rb
235
- - lib/zstds/stream/raw/abstract.rb
236
261
  - lib/zstds/stream/raw/compressor.rb
237
262
  - lib/zstds/stream/raw/decompressor.rb
238
263
  - lib/zstds/stream/reader.rb
239
- - lib/zstds/stream/reader_helpers.rb
240
- - lib/zstds/stream/stat.rb
241
264
  - lib/zstds/stream/writer.rb
242
- - lib/zstds/stream/writer_helpers.rb
243
265
  - lib/zstds/string.rb
244
266
  - lib/zstds/validation.rb
245
267
  - lib/zstds/version.rb
@@ -256,7 +278,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
256
278
  requirements:
257
279
  - - ">="
258
280
  - !ruby/object:Gem::Version
259
- version: '2.5'
281
+ version: '2.6'
260
282
  required_rubygems_version: !ruby/object:Gem::Requirement
261
283
  requirements:
262
284
  - - ">="