multi_compress 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +51 -0
  3. data/GET_STARTED.md +5 -3
  4. data/README.md +2 -2
  5. data/THIRD_PARTY_NOTICES.md +3 -3
  6. data/ext/multi_compress/exports.darwin +1 -0
  7. data/ext/multi_compress/exports.map +4 -0
  8. data/ext/multi_compress/extconf.rb +20 -0
  9. data/ext/multi_compress/multi_compress.c +352 -149
  10. data/ext/multi_compress/vendor/.vendored +2 -2
  11. data/ext/multi_compress/vendor/brotli/c/common/version.h +2 -2
  12. data/ext/multi_compress/vendor/brotli/c/dec/decode.c +59 -30
  13. data/ext/multi_compress/vendor/brotli/c/include/brotli/decode.h +1 -3
  14. data/ext/multi_compress/vendor/zstd/lib/BUCK +10 -5
  15. data/ext/multi_compress/vendor/zstd/lib/README.md +25 -5
  16. data/ext/multi_compress/vendor/zstd/lib/common/allocations.h +6 -2
  17. data/ext/multi_compress/vendor/zstd/lib/common/bits.h +1 -1
  18. data/ext/multi_compress/vendor/zstd/lib/common/bitstream.h +3 -3
  19. data/ext/multi_compress/vendor/zstd/lib/common/bmi2.h +40 -0
  20. data/ext/multi_compress/vendor/zstd/lib/common/compiler.h +37 -11
  21. data/ext/multi_compress/vendor/zstd/lib/common/entropy_common.c +7 -6
  22. data/ext/multi_compress/vendor/zstd/lib/common/fse_decompress.c +4 -3
  23. data/ext/multi_compress/vendor/zstd/lib/common/pool.c +16 -19
  24. data/ext/multi_compress/vendor/zstd/lib/common/portability_macros.h +31 -0
  25. data/ext/multi_compress/vendor/zstd/lib/common/threading.c +16 -2
  26. data/ext/multi_compress/vendor/zstd/lib/common/zstd_common.c +9 -0
  27. data/ext/multi_compress/vendor/zstd/lib/common/zstd_internal.h +4 -2
  28. data/ext/multi_compress/vendor/zstd/lib/compress/hist.c +300 -3
  29. data/ext/multi_compress/vendor/zstd/lib/compress/hist.h +4 -0
  30. data/ext/multi_compress/vendor/zstd/lib/compress/huf_compress.c +4 -1
  31. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress.c +775 -214
  32. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_internal.h +35 -6
  33. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_sequences.c +9 -5
  34. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_superblock.c +14 -5
  35. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_double_fast.c +2 -2
  36. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_fast.c +2 -2
  37. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_lazy.c +49 -5
  38. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_ldm.c +11 -11
  39. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_ldm.h +11 -0
  40. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_opt.c +16 -18
  41. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_preSplit.c +3 -3
  42. data/ext/multi_compress/vendor/zstd/lib/compress/zstdmt_compress.c +64 -39
  43. data/ext/multi_compress/vendor/zstd/lib/compress/zstdmt_compress.h +30 -2
  44. data/ext/multi_compress/vendor/zstd/lib/decompress/huf_decompress.c +78 -64
  45. data/ext/multi_compress/vendor/zstd/lib/decompress/huf_decompress_amd64.S +182 -18
  46. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_ddict.c +59 -27
  47. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_ddict.h +11 -0
  48. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress.c +194 -93
  49. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress_block.c +225 -85
  50. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress_internal.h +19 -2
  51. data/ext/multi_compress/vendor/zstd/lib/dictBuilder/cover.c +136 -91
  52. data/ext/multi_compress/vendor/zstd/lib/dictBuilder/divsufsort.c +77 -77
  53. data/ext/multi_compress/vendor/zstd/lib/dictBuilder/fastcover.c +48 -42
  54. data/ext/multi_compress/vendor/zstd/lib/dictBuilder/zdict.c +18 -14
  55. data/ext/multi_compress/vendor/zstd/lib/dll/example/build_package.bat +66 -20
  56. data/ext/multi_compress/vendor/zstd/lib/dll/example/fullbench-dll.vcxproj +10 -6
  57. data/ext/multi_compress/vendor/zstd/lib/install_oses.mk +17 -0
  58. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v01.c +10 -8
  59. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v02.c +5 -364
  60. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v03.c +4 -5
  61. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v04.c +7 -3
  62. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v05.c +8 -5
  63. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v06.c +3 -3
  64. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v07.c +1 -0
  65. data/ext/multi_compress/vendor/zstd/lib/libzstd.mk +8 -7
  66. data/ext/multi_compress/vendor/zstd/lib/libzstd.pc.in +1 -0
  67. data/ext/multi_compress/vendor/zstd/lib/module.modulemap +0 -21
  68. data/ext/multi_compress/vendor/zstd/lib/zstd.h +209 -112
  69. data/lib/multi_compress/active_record.rb +11 -5
  70. data/lib/multi_compress/codec.rb +164 -94
  71. data/lib/multi_compress/database/api.rb +42 -0
  72. data/lib/multi_compress/database/decoding.rb +42 -0
  73. data/lib/multi_compress/database/dictionary.rb +72 -0
  74. data/lib/multi_compress/database/encoding.rb +31 -0
  75. data/lib/multi_compress/database/header.rb +71 -0
  76. data/lib/multi_compress/database/operations.rb +73 -0
  77. data/lib/multi_compress/database/validation.rb +95 -0
  78. data/lib/multi_compress/database.rb +10 -282
  79. data/lib/multi_compress/db_deployment/bundle_archiver.rb +70 -0
  80. data/lib/multi_compress/db_deployment/bundle_metadata.rb +56 -0
  81. data/lib/multi_compress/db_deployment/bundle_stager.rb +62 -0
  82. data/lib/multi_compress/db_deployment/commands.rb +309 -0
  83. data/lib/multi_compress/db_deployment/deployment_bundle.rb +80 -0
  84. data/lib/multi_compress/db_deployment/deterministic_tar_writer.rb +76 -0
  85. data/lib/multi_compress/db_deployment/dictionary_registry.rb +244 -0
  86. data/lib/multi_compress/db_deployment/readable_view.rb +112 -0
  87. data/lib/multi_compress/db_deployment/readable_view_options.rb +51 -0
  88. data/lib/multi_compress/db_deployment/sql_identifiers.rb +45 -0
  89. data/lib/multi_compress/db_deployment.rb +10 -844
  90. data/lib/multi_compress/reader/buffer_operations.rb +57 -0
  91. data/lib/multi_compress/reader/read_operations.rb +69 -0
  92. data/lib/multi_compress/reader.rb +97 -0
  93. data/lib/multi_compress/stream_io.rb +13 -0
  94. data/lib/multi_compress/version.rb +1 -1
  95. data/lib/multi_compress/writer.rb +85 -0
  96. data/lib/multi_compress.rb +42 -333
  97. data/multi_compress.gemspec +2 -0
  98. data/mysql_udf/Makefile +12 -5
  99. data/mysql_udf/multi_compress_mysql.exports +6 -0
  100. data/postgres_extension/Makefile +2 -1
  101. data/postgres_extension/test/run_e2e.sh +28 -10
  102. metadata +30 -10
  103. data/ext/multi_compress/vendor/lz4/lib/Makefile +0 -244
  104. data/ext/multi_compress/vendor/lz4/lib/dll/example/Makefile +0 -63
  105. data/ext/multi_compress/vendor/zstd/lib/Makefile +0 -389
  106. data/ext/multi_compress/vendor/zstd/lib/dll/example/Makefile +0 -48
@@ -4,51 +4,88 @@ require "multi_compress"
4
4
 
5
5
  module MultiCompress
6
6
  class Codec
7
- MAGIC = "\x89MCDB".b # reserved 5-byte namespace; 0x89 is an
8
- # invalid UTF-8 lead byte, so it won't
9
- # collide with valid text. For BINARY data
10
- # this prefix is reserved, not collision-free.
7
+ MAGIC = "\x89MCDB".b # reserved 5-byte namespace; 0x89 is an
8
+ # invalid UTF-8 lead byte, so it won't
9
+ # collide with valid text. For BINARY data
10
+ # this prefix is reserved, not collision-free.
11
11
  VERSION = 1
12
12
  HEADER_SIZE = MAGIC.bytesize + 2 # magic + version byte + algo byte
13
13
  B64_PREFIX = "mc1:"
14
- MAX_NATIVE_OUTPUT = (2**(0.size * 8 - 1)) - 1
14
+ MAX_NATIVE_OUTPUT = (2**((0.size * 8) - 1)) - 1
15
15
 
16
16
  ALGO_ID = { zstd: 1, lz4: 2, brotli: 3 }.freeze
17
17
  ID_ALGO = ALGO_ID.invert.freeze
18
18
 
19
- attr_reader :algo, :level, :max_output_size
19
+ Options = Struct.new(
20
+ :algo, :level, :encode, :serializer, :encoding, :max_output_size, :legacy, :dictionary,
21
+ keyword_init: true
22
+ )
20
23
 
21
- def initialize(algo: :zstd, level: nil, encode: nil, serializer: nil,
22
- encoding: Encoding::UTF_8, max_output_size: nil,
23
- legacy: :reject, dictionary: nil)
24
+ DEFAULT_OPTIONS = {
25
+ algo: :zstd, level: nil, encode: nil, serializer: nil, encoding: Encoding::UTF_8,
26
+ max_output_size: nil, legacy: :reject, dictionary: nil
27
+ }.freeze
28
+
29
+ private_constant :DEFAULT_OPTIONS, :Options
30
+
31
+ def initialize(**values)
32
+ resolved_options = DEFAULT_OPTIONS.merge(values)
33
+ @options = Options.new(**resolved_options)
34
+ validate_options!
35
+ end
36
+
37
+ def algo
38
+ @options.algo
39
+ end
40
+
41
+ def level
42
+ @options.level
43
+ end
44
+
45
+ def max_output_size
46
+ @options.max_output_size
47
+ end
48
+
49
+ private
50
+
51
+ def validate_options!
52
+ validate_algorithm!
53
+ validate_encoding!
54
+ @options.max_output_size = normalized_output_limit
55
+ @options.legacy = normalize_legacy(@options.legacy)
56
+ end
57
+
58
+ def validate_algorithm!
24
59
  raise ArgumentError, "unknown algo #{algo.inspect}" unless ALGO_ID.key?(algo)
25
- raise ArgumentError, "encode must be nil or :base64" unless [nil, :base64].include?(encode)
26
-
27
- @algo = algo
28
- @level = level
29
- @encode = encode
30
- @serializer = serializer
31
- @encoding = encoding
32
- limit = Integer(max_output_size || MultiCompress.config.max_output_size)
60
+ end
61
+
62
+ def validate_encoding!
63
+ return if [nil, :base64].include?(@options.encode)
64
+
65
+ raise ArgumentError, "encode must be nil or :base64"
66
+ end
67
+
68
+ def normalized_output_limit
69
+ limit = Integer(@options.max_output_size || MultiCompress.config.max_output_size)
33
70
  raise ArgumentError, "max_output_size must be greater than 0" unless limit.positive?
34
71
  if limit > MAX_NATIVE_OUTPUT
35
- raise ArgumentError,
36
- "max_output_size must be at most #{MAX_NATIVE_OUTPUT} bytes on this platform"
72
+ raise ArgumentError, "max_output_size must be at most #{MAX_NATIVE_OUTPUT} bytes on this platform"
37
73
  end
38
74
 
39
- @max_output_size = limit
40
- @legacy = normalize_legacy(legacy)
41
- @dictionary = dictionary
75
+ limit
42
76
  end
43
77
 
78
+ public
79
+
44
80
  def dump(value)
45
81
  return nil if value.nil?
46
82
 
47
83
  payload = serialize_payload(value)
48
- body = MultiCompress.compress(payload, algo: @algo, level: @level, dictionary: @dictionary)
84
+ body = MultiCompress.compress(payload, algo: @options.algo, level: @options.level,
85
+ dictionary: @options.dictionary)
49
86
  framed = header + body
50
87
 
51
- @encode == :base64 ? B64_PREFIX + [framed].pack("m0") : framed
88
+ @options.encode == :base64 ? B64_PREFIX + [framed].pack("m0") : framed
52
89
  end
53
90
 
54
91
  def load(stored)
@@ -60,105 +97,138 @@ module MultiCompress
60
97
  read_legacy(stored)
61
98
  end
62
99
 
63
- private
100
+ module Envelope
101
+ private
64
102
 
65
- def header
66
- (MAGIC + [VERSION, ALGO_ID.fetch(@algo)].pack("C2")).b
67
- end
103
+ def header
104
+ (MAGIC + [VERSION, ALGO_ID.fetch(@options.algo)].pack("C2")).b
105
+ end
68
106
 
69
- def decode_transport(stored)
70
- if @encode == :base64
71
- s = stored.to_s
72
- return nil unless s.start_with?(B64_PREFIX)
107
+ def decode_transport(stored)
108
+ if @options.encode == :base64
109
+ s = stored.to_s
110
+ return nil unless s.start_with?(B64_PREFIX)
73
111
 
74
- decoded = optional_base64(s.byteslice(B64_PREFIX.bytesize, s.bytesize - B64_PREFIX.bytesize))
75
- decoded && decoded.start_with?(MAGIC) ? decoded : nil
76
- else
77
- b = stored.b
78
- b.start_with?(MAGIC) ? b : nil
112
+ decoded = optional_base64(s.byteslice(B64_PREFIX.bytesize, s.bytesize - B64_PREFIX.bytesize))
113
+ decoded&.start_with?(MAGIC) ? decoded : nil
114
+ else
115
+ b = stored.b
116
+ b.start_with?(MAGIC) ? b : nil
117
+ end
79
118
  end
80
- end
81
119
 
82
- def unwrap(framed)
83
- if framed.bytesize < HEADER_SIZE
84
- raise MultiCompress::DataError, "MultiCompress::Codec: truncated envelope"
120
+ def unwrap(framed)
121
+ raise MultiCompress::DataError, "MultiCompress::Codec: truncated envelope" if framed.bytesize < HEADER_SIZE
122
+
123
+ validate_envelope_version!(framed)
124
+ algo = envelope_algorithm(framed)
125
+ body = envelope_body(framed)
126
+ MultiCompress.decompress(body, algo: algo, dictionary: @options.dictionary,
127
+ max_output_size: @options.max_output_size)
85
128
  end
86
129
 
87
- version = framed.getbyte(MAGIC.bytesize)
88
- algo_id = framed.getbyte(MAGIC.bytesize + 1)
89
- unless version == VERSION
130
+ def validate_envelope_version!(framed)
131
+ version = framed.getbyte(MAGIC.bytesize)
132
+ return if version == VERSION
133
+
90
134
  raise MultiCompress::DataError, "MultiCompress::Codec: unsupported envelope version #{version}"
91
135
  end
92
- algo = ID_ALGO[algo_id]
93
- raise MultiCompress::DataError, "MultiCompress::Codec: unknown algorithm id #{algo_id}" unless algo
94
136
 
95
- body = framed.byteslice(HEADER_SIZE, framed.bytesize - HEADER_SIZE) || +"".b
96
- MultiCompress.decompress(body, algo: algo, dictionary: @dictionary, max_output_size: @max_output_size)
97
- end
137
+ def envelope_algorithm(framed)
138
+ algo_id = framed.getbyte(MAGIC.bytesize + 1)
139
+ ID_ALGO.fetch(algo_id) do
140
+ raise MultiCompress::DataError, "MultiCompress::Codec: unknown algorithm id #{algo_id}"
141
+ end
142
+ end
98
143
 
99
- def read_legacy(stored)
100
- case @legacy[:mode]
101
- when :reject
102
- raise MultiCompress::DataError,
103
- "MultiCompress::Codec: value has no envelope and legacy: :reject is set"
104
- when :plain
105
- decode_payload(stored.b.dup)
106
- when :compressed
144
+ def envelope_body(framed)
145
+ framed.byteslice(HEADER_SIZE, framed.bytesize - HEADER_SIZE) || +"".b
146
+ end
147
+
148
+ def read_legacy(stored)
149
+ case @options.legacy[:mode]
150
+ when :reject
151
+ raise MultiCompress::DataError,
152
+ "MultiCompress::Codec: value has no envelope and legacy: :reject is set"
153
+ when :plain
154
+ decode_payload(stored.b.dup)
155
+ when :compressed
156
+ decode_legacy_compressed(stored)
157
+ end
158
+ end
159
+
160
+ def decode_legacy_compressed(stored)
107
161
  payload = MultiCompress.decompress(
108
- stored.b, algo: @legacy[:algo], dictionary: @dictionary, max_output_size: @max_output_size
162
+ stored.b,
163
+ algo: @options.legacy[:algo],
164
+ dictionary: @options.dictionary,
165
+ max_output_size: @options.max_output_size
109
166
  )
110
167
  decode_payload(payload)
111
168
  end
112
- end
113
169
 
114
- def normalize_legacy(legacy)
115
- case legacy
116
- when :reject, :plain
117
- { mode: legacy }
118
- when Hash
119
- algo = legacy[:compressed]
120
- raise ArgumentError, "legacy: { compressed: <algo> } required" unless ALGO_ID.key?(algo)
121
-
122
- { mode: :compressed, algo: algo }
123
- else
124
- raise ArgumentError, "legacy: must be :reject, :plain, or { compressed: :algo }"
170
+ def normalize_legacy(legacy)
171
+ case legacy
172
+ when :reject, :plain
173
+ { mode: legacy }
174
+ when Hash
175
+ algo = legacy[:compressed]
176
+ raise ArgumentError, "legacy: { compressed: <algo> } required" unless ALGO_ID.key?(algo)
177
+
178
+ { mode: :compressed, algo: algo }
179
+ else
180
+ raise ArgumentError, "legacy: must be :reject, :plain, or { compressed: :algo }"
181
+ end
125
182
  end
126
183
  end
127
184
 
128
- def serialize_payload(value)
129
- if @serializer
130
- @serializer.dump(value).b
131
- else
185
+ module Payload
186
+ private
187
+
188
+ def serialize_payload(value)
189
+ if @options.serializer
190
+ @options.serializer.dump(value).b
191
+ else
192
+ serialize_string(value)
193
+ end
194
+ end
195
+
196
+ def serialize_string(value)
132
197
  unless value.is_a?(String)
133
198
  raise TypeError, "MultiCompress::Codec expects a String (or pass serializer:); got #{value.class}"
134
199
  end
135
-
136
- unless @encoding == Encoding::BINARY || value.dup.force_encoding(@encoding).valid_encoding?
137
- raise ArgumentError, "MultiCompress::Codec: value is not valid #{@encoding} " \
138
- "(use encoding: Encoding::BINARY for arbitrary bytes)"
200
+ if @options.encoding == Encoding::BINARY || value.dup.force_encoding(@options.encoding).valid_encoding?
201
+ return value.b
139
202
  end
140
- value.b
203
+
204
+ raise ArgumentError, "MultiCompress::Codec: value is not valid #{@options.encoding} " \
205
+ "(use encoding: Encoding::BINARY for arbitrary bytes)"
141
206
  end
142
- end
143
207
 
144
- def decode_payload(bytes)
145
- return @serializer.load(bytes) if @serializer
208
+ def decode_payload(bytes)
209
+ return @options.serializer.load(bytes) if @options.serializer
146
210
 
147
- finalize_string(bytes)
148
- end
211
+ finalize_string(bytes)
212
+ end
213
+
214
+ def finalize_string(bytes)
215
+ s = bytes.dup.force_encoding(@options.encoding)
216
+ unless @options.encoding == Encoding::BINARY || s.valid_encoding?
217
+ raise MultiCompress::DataError, "MultiCompress::Codec: decoded bytes are not valid #{@options.encoding}"
218
+ end
149
219
 
150
- def finalize_string(bytes)
151
- s = bytes.dup.force_encoding(@encoding)
152
- unless @encoding == Encoding::BINARY || s.valid_encoding?
153
- raise MultiCompress::DataError, "MultiCompress::Codec: decoded bytes are not valid #{@encoding}"
220
+ s
154
221
  end
155
- s
156
- end
157
222
 
158
- def optional_base64(str)
159
- str.unpack1("m0")&.b
160
- rescue ArgumentError
161
- nil
223
+ def optional_base64(str)
224
+ str.unpack1("m0")&.b
225
+ rescue ArgumentError
226
+ nil
227
+ end
162
228
  end
229
+
230
+ private_constant :Envelope, :Payload
231
+ include Envelope
232
+ include Payload
163
233
  end
164
234
  end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MultiCompress
4
+ module Database
5
+ ENGINE = Engine.new
6
+ private_constant :ENGINE
7
+
8
+ module_function
9
+
10
+ def compress(text, dictionary: nil)
11
+ ENGINE.compress(text, dictionary: dictionary)
12
+ end
13
+
14
+ def compress_v1(text)
15
+ ENGINE.compress_v1(text)
16
+ end
17
+
18
+ def compress_v2(text, dictionary:)
19
+ ENGINE.compress_v2(text, dictionary: dictionary)
20
+ end
21
+
22
+ def decompress(blob, dictionary: nil)
23
+ ENGINE.decompress(blob, dictionary: dictionary)
24
+ end
25
+
26
+ def valid?(blob, dictionary: nil)
27
+ ENGINE.valid?(blob, dictionary: dictionary)
28
+ end
29
+
30
+ def dictionary_ref(blob)
31
+ ENGINE.dictionary_ref(blob)
32
+ end
33
+
34
+ def original_size(blob)
35
+ ENGINE.original_size(blob)
36
+ end
37
+
38
+ def version_string
39
+ ENGINE.version_string
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MultiCompress
4
+ module Database
5
+ class Engine
6
+ private
7
+
8
+ def decode_v1(blob, header)
9
+ frame = blob.byteslice(V1_HEADER_SIZE, blob.bytesize - V1_HEADER_SIZE) || +"".b
10
+ validate_canonical_zstd_frame!(frame, header.fetch(:original_size), expected_dictionary_id: 0)
11
+ out = MultiCompress.decompress(frame, algo: :zstd, max_output_size: MAX_OUTPUT)
12
+ validate_decoded!(blob, header, out)
13
+ end
14
+
15
+ def decode_v2(blob, header, dictionary)
16
+ dictionary = match_dictionary!(dictionary, header)
17
+ frame = blob.byteslice(V2_HEADER_SIZE, blob.bytesize - V2_HEADER_SIZE) || +"".b
18
+ validate_canonical_zstd_frame!(frame, header.fetch(:original_size), expected_dictionary_id: dictionary.zstd_id)
19
+ out = decompress_dictionary_frame(frame, dictionary)
20
+ validate_decoded!(blob, header, out)
21
+ end
22
+
23
+ def match_dictionary!(dictionary, header)
24
+ err("MCDB2 requires a dictionary") if dictionary.nil?
25
+ dictionary = database_dictionary!(dictionary)
26
+ expected_ref = header.fetch(:dictionary_ref)
27
+ return dictionary if dictionary.id == expected_ref
28
+
29
+ err("dictionary reference #{expected_ref} does not match supplied dictionary #{dictionary.id}")
30
+ end
31
+
32
+ def decompress_dictionary_frame(frame, dictionary)
33
+ MultiCompress.decompress(
34
+ frame,
35
+ algo: :zstd,
36
+ dictionary: dictionary.native,
37
+ max_output_size: MAX_OUTPUT
38
+ )
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MultiCompress
4
+ module Database
5
+ # A strict, immutable MCDB2 dictionary. It is intentionally distinct from
6
+ # MultiCompress::Dictionary: MCDB2 accepts only conformant zstd dictionaries
7
+ # with a non-zero zstd DictID and an application-level registry id.
8
+ class Dictionary
9
+ attr_reader :id, :bytes, :sha256, :zstd_id, :native
10
+
11
+ def self.train(samples, id:, size: 32 * 1024)
12
+ new(id: id, native: MultiCompress::Zstd.train_dictionary(samples, size: size))
13
+ end
14
+
15
+ def self.wrap(native, id:)
16
+ new(id: id, native: native)
17
+ end
18
+
19
+ def initialize(id:, native: nil, bytes: nil)
20
+ @id = normalize_id(id)
21
+ @native = build_native(native, bytes)
22
+ @bytes = validate_bytes(@native.bytes.b.freeze)
23
+ @zstd_id = validate_zstd_id(@native.zstd_id)
24
+ @sha256 = Digest::SHA256.digest(@bytes).freeze
25
+ freeze
26
+ end
27
+
28
+ def sha256_hex
29
+ sha256.unpack1("H*")
30
+ end
31
+
32
+ def registry_attributes
33
+ { id: id, zstd_dict_id: zstd_id, sha256: sha256, bytes: bytes }
34
+ end
35
+
36
+ private
37
+
38
+ def build_native(native, bytes)
39
+ raise ArgumentError, "provide exactly one of native: or bytes:" if (!!native) == (!!bytes)
40
+
41
+ value = native || MultiCompress::Dictionary.new(bytes, algo: :zstd)
42
+ return value if value.is_a?(MultiCompress::Dictionary) && value.algo == :zstd
43
+
44
+ raise TypeError, "MCDB2 dictionary must be a MultiCompress::Dictionary with algo: :zstd"
45
+ end
46
+
47
+ def validate_bytes(value)
48
+ return value unless value.empty? || value.bytesize > MAX_DICTIONARY_BYTES
49
+
50
+ raise ArgumentError,
51
+ "MCDB2 dictionary is #{value.bytesize} bytes; allowed range is 1..#{MAX_DICTIONARY_BYTES}"
52
+ end
53
+
54
+ def validate_zstd_id(value)
55
+ return value unless value.zero?
56
+
57
+ raise ArgumentError, "MCDB2 dictionary must be a conformant zstd dictionary with a non-zero DictID"
58
+ end
59
+
60
+ def normalize_id(value)
61
+ raise TypeError, "MCDB2 dictionary id must be an Integer" unless value.respond_to?(:to_int)
62
+
63
+ integer = value.to_int
64
+ unless integer.between?(1, MAX_DICTIONARY_REF)
65
+ raise ArgumentError, "MCDB2 dictionary id must be in 1..#{MAX_DICTIONARY_REF}"
66
+ end
67
+
68
+ integer
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MultiCompress
4
+ module Database
5
+ class Engine
6
+ private
7
+
8
+ def input_bytes!(text, format:)
9
+ validate_input_type!(text)
10
+
11
+ bytes = utf8_bytes(text)
12
+ validate_input_size!(bytes, format)
13
+ bytes
14
+ end
15
+
16
+ def validate_input_type!(text)
17
+ return if text.is_a?(String)
18
+
19
+ raise TypeError, "MultiCompress::Database.compress expects a String, got #{text.class}"
20
+ end
21
+
22
+ def validate_input_size!(bytes, format)
23
+ size = bytes.bytesize
24
+ return unless size > MAX_OUTPUT
25
+
26
+ raise ArgumentError,
27
+ "MultiCompress::Database: input is #{size} bytes, over the #{MAX_OUTPUT}-byte #{format} limit"
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MultiCompress
4
+ module Database
5
+ class Engine
6
+ private
7
+
8
+ def parse_header!(blob)
9
+ bytes = header_bytes!(blob)
10
+ version = bytes.getbyte(4)
11
+ validate_header_size!(bytes, version)
12
+ header_metadata(bytes, version)
13
+ end
14
+
15
+ def header_bytes!(blob)
16
+ unless blob.is_a?(String)
17
+ raise TypeError, "MultiCompress::Database.decompress expects a String, got #{blob.class}"
18
+ end
19
+
20
+ bytes = blob.b
21
+ err("truncated envelope (#{bytes.bytesize} < #{V1_HEADER_SIZE} bytes)") if bytes.bytesize < V1_HEADER_SIZE
22
+ err("bad magic") unless bytes.byteslice(0, 4) == MAGIC
23
+ bytes
24
+ end
25
+
26
+ def validate_header_size!(bytes, version)
27
+ header_size, max_envelope = envelope_limits(version)
28
+ if bytes.bytesize < header_size
29
+ err("truncated #{format_name(version)} envelope (#{bytes.bytesize} < #{header_size} bytes)")
30
+ end
31
+ if bytes.bytesize > max_envelope
32
+ err("stored #{format_name(version)} envelope is #{bytes.bytesize} bytes, over the #{max_envelope}-byte limit")
33
+ end
34
+ validate_fixed_header!(bytes)
35
+ end
36
+
37
+ def envelope_limits(version)
38
+ case version
39
+ when VERSION_V1 then [V1_HEADER_SIZE, MAX_ENVELOPE_V1]
40
+ when VERSION_V2 then [V2_HEADER_SIZE, MAX_ENVELOPE_V2]
41
+ else err("unsupported version #{version}")
42
+ end
43
+ end
44
+
45
+ def validate_fixed_header!(bytes)
46
+ err("unsupported codec #{bytes.getbyte(5)}") unless bytes.getbyte(5) == CODEC_ZSTD
47
+ err("reserved flags must be 0, got #{bytes.getbyte(6)}") unless bytes.getbyte(6) == FLAGS_NONE
48
+
49
+ original_size = bytes.byteslice(ORIGINAL_SIZE_OFFSET, 8).unpack1("Q<")
50
+ err("declared size #{original_size} over the #{MAX_OUTPUT}-byte limit") if original_size > MAX_OUTPUT
51
+ end
52
+
53
+ def header_metadata(bytes, version)
54
+ header = {
55
+ version: version,
56
+ header_size: version == VERSION_V2 ? V2_HEADER_SIZE : V1_HEADER_SIZE,
57
+ original_size: bytes.byteslice(ORIGINAL_SIZE_OFFSET, 8).unpack1("Q<"),
58
+ expected_crc: bytes.byteslice(CRC_OFFSET, 4).unpack1("L<")
59
+ }
60
+ header[:dictionary_ref] = dictionary_ref_from_header(bytes) if version == VERSION_V2
61
+ header
62
+ end
63
+
64
+ def dictionary_ref_from_header(bytes)
65
+ dictionary_ref = bytes.byteslice(DICTIONARY_REF_OFFSET, 8).unpack1("Q<")
66
+ err("invalid dictionary reference #{dictionary_ref}") unless dictionary_ref.between?(1, MAX_DICTIONARY_REF)
67
+ dictionary_ref
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MultiCompress
4
+ module Database
5
+ class Engine
6
+ # Without a dictionary this emits MCDB1. With Database::Dictionary it emits
7
+ # MCDB2. Choose one format per database column; mixed use is deliberately
8
+ # not what the generated dictionary view is optimized for.
9
+ def compress(text, dictionary: nil)
10
+ return compress_v1(text) if dictionary.nil?
11
+
12
+ compress_v2(text, dictionary: dictionary)
13
+ end
14
+
15
+ def compress_v1(text)
16
+ bytes = input_bytes!(text, format: "v1")
17
+ frame = MultiCompress.compress(bytes, algo: :zstd, level: ZSTD_LEVEL)
18
+ validate_canonical_zstd_frame!(frame, bytes.bytesize, expected_dictionary_id: 0)
19
+ envelope = header_v1(bytes.bytesize, MultiCompress.crc32(bytes)) << frame
20
+ check_envelope_size!(envelope, MAX_ENVELOPE_V1, "v1")
21
+ envelope
22
+ end
23
+
24
+ def compress_v2(text, dictionary:)
25
+ dictionary = database_dictionary!(dictionary)
26
+ bytes = input_bytes!(text, format: "v2")
27
+ frame = MultiCompress.compress(bytes, algo: :zstd, level: ZSTD_LEVEL, dictionary: dictionary.native)
28
+ validate_canonical_zstd_frame!(frame, bytes.bytesize, expected_dictionary_id: dictionary.zstd_id)
29
+ envelope = header_v2(bytes.bytesize, MultiCompress.crc32(bytes), dictionary.id) << frame
30
+ check_envelope_size!(envelope, MAX_ENVELOPE_V2, "v2")
31
+ envelope
32
+ end
33
+
34
+ def decompress(blob, dictionary: nil)
35
+ header = parse_header!(blob)
36
+ case header.fetch(:version)
37
+ when VERSION_V1
38
+ raise ArgumentError, "MCDB1 does not accept a dictionary" unless dictionary.nil?
39
+
40
+ decode_v1(blob.b, header)
41
+ when VERSION_V2
42
+ decode_v2(blob.b, header, dictionary)
43
+ else
44
+ err("unsupported version #{header.fetch(:version)}")
45
+ end
46
+ end
47
+
48
+ def valid?(blob, dictionary: nil)
49
+ decompress(blob, dictionary: dictionary)
50
+ true
51
+ rescue StandardError
52
+ false
53
+ end
54
+
55
+ # Parses only the envelope header. It is safe for registry/FK consistency
56
+ # checks and does not decompress the payload.
57
+ def dictionary_ref(blob)
58
+ header = parse_header!(blob)
59
+ return nil if header.fetch(:version) == VERSION_V1
60
+
61
+ header.fetch(:dictionary_ref)
62
+ end
63
+
64
+ def original_size(blob)
65
+ parse_header!(blob).fetch(:original_size)
66
+ end
67
+
68
+ def version_string
69
+ "MCDB1 + MCDB2 (zstd #{MultiCompress.version(:zstd)})"
70
+ end
71
+ end
72
+ end
73
+ end