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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +51 -0
- data/GET_STARTED.md +5 -3
- data/README.md +2 -2
- data/THIRD_PARTY_NOTICES.md +3 -3
- data/ext/multi_compress/exports.darwin +1 -0
- data/ext/multi_compress/exports.map +4 -0
- data/ext/multi_compress/extconf.rb +20 -0
- data/ext/multi_compress/multi_compress.c +352 -149
- data/ext/multi_compress/vendor/.vendored +2 -2
- data/ext/multi_compress/vendor/brotli/c/common/version.h +2 -2
- data/ext/multi_compress/vendor/brotli/c/dec/decode.c +59 -30
- data/ext/multi_compress/vendor/brotli/c/include/brotli/decode.h +1 -3
- data/ext/multi_compress/vendor/zstd/lib/BUCK +10 -5
- data/ext/multi_compress/vendor/zstd/lib/README.md +25 -5
- data/ext/multi_compress/vendor/zstd/lib/common/allocations.h +6 -2
- data/ext/multi_compress/vendor/zstd/lib/common/bits.h +1 -1
- data/ext/multi_compress/vendor/zstd/lib/common/bitstream.h +3 -3
- data/ext/multi_compress/vendor/zstd/lib/common/bmi2.h +40 -0
- data/ext/multi_compress/vendor/zstd/lib/common/compiler.h +37 -11
- data/ext/multi_compress/vendor/zstd/lib/common/entropy_common.c +7 -6
- data/ext/multi_compress/vendor/zstd/lib/common/fse_decompress.c +4 -3
- data/ext/multi_compress/vendor/zstd/lib/common/pool.c +16 -19
- data/ext/multi_compress/vendor/zstd/lib/common/portability_macros.h +31 -0
- data/ext/multi_compress/vendor/zstd/lib/common/threading.c +16 -2
- data/ext/multi_compress/vendor/zstd/lib/common/zstd_common.c +9 -0
- data/ext/multi_compress/vendor/zstd/lib/common/zstd_internal.h +4 -2
- data/ext/multi_compress/vendor/zstd/lib/compress/hist.c +300 -3
- data/ext/multi_compress/vendor/zstd/lib/compress/hist.h +4 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/huf_compress.c +4 -1
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress.c +775 -214
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_internal.h +35 -6
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_sequences.c +9 -5
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_superblock.c +14 -5
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_double_fast.c +2 -2
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_fast.c +2 -2
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_lazy.c +49 -5
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_ldm.c +11 -11
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_ldm.h +11 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_opt.c +16 -18
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_preSplit.c +3 -3
- data/ext/multi_compress/vendor/zstd/lib/compress/zstdmt_compress.c +64 -39
- data/ext/multi_compress/vendor/zstd/lib/compress/zstdmt_compress.h +30 -2
- data/ext/multi_compress/vendor/zstd/lib/decompress/huf_decompress.c +78 -64
- data/ext/multi_compress/vendor/zstd/lib/decompress/huf_decompress_amd64.S +182 -18
- data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_ddict.c +59 -27
- data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_ddict.h +11 -0
- data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress.c +194 -93
- data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress_block.c +225 -85
- data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress_internal.h +19 -2
- data/ext/multi_compress/vendor/zstd/lib/dictBuilder/cover.c +136 -91
- data/ext/multi_compress/vendor/zstd/lib/dictBuilder/divsufsort.c +77 -77
- data/ext/multi_compress/vendor/zstd/lib/dictBuilder/fastcover.c +48 -42
- data/ext/multi_compress/vendor/zstd/lib/dictBuilder/zdict.c +18 -14
- data/ext/multi_compress/vendor/zstd/lib/dll/example/build_package.bat +66 -20
- data/ext/multi_compress/vendor/zstd/lib/dll/example/fullbench-dll.vcxproj +10 -6
- data/ext/multi_compress/vendor/zstd/lib/install_oses.mk +17 -0
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v01.c +10 -8
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v02.c +5 -364
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v03.c +4 -5
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v04.c +7 -3
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v05.c +8 -5
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v06.c +3 -3
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v07.c +1 -0
- data/ext/multi_compress/vendor/zstd/lib/libzstd.mk +8 -7
- data/ext/multi_compress/vendor/zstd/lib/libzstd.pc.in +1 -0
- data/ext/multi_compress/vendor/zstd/lib/module.modulemap +0 -21
- data/ext/multi_compress/vendor/zstd/lib/zstd.h +209 -112
- data/lib/multi_compress/active_record.rb +11 -5
- data/lib/multi_compress/codec.rb +164 -94
- data/lib/multi_compress/database/api.rb +42 -0
- data/lib/multi_compress/database/decoding.rb +42 -0
- data/lib/multi_compress/database/dictionary.rb +72 -0
- data/lib/multi_compress/database/encoding.rb +31 -0
- data/lib/multi_compress/database/header.rb +71 -0
- data/lib/multi_compress/database/operations.rb +73 -0
- data/lib/multi_compress/database/validation.rb +95 -0
- data/lib/multi_compress/database.rb +10 -282
- data/lib/multi_compress/db_deployment/bundle_archiver.rb +70 -0
- data/lib/multi_compress/db_deployment/bundle_metadata.rb +56 -0
- data/lib/multi_compress/db_deployment/bundle_stager.rb +62 -0
- data/lib/multi_compress/db_deployment/commands.rb +309 -0
- data/lib/multi_compress/db_deployment/deployment_bundle.rb +80 -0
- data/lib/multi_compress/db_deployment/deterministic_tar_writer.rb +76 -0
- data/lib/multi_compress/db_deployment/dictionary_registry.rb +244 -0
- data/lib/multi_compress/db_deployment/readable_view.rb +112 -0
- data/lib/multi_compress/db_deployment/readable_view_options.rb +51 -0
- data/lib/multi_compress/db_deployment/sql_identifiers.rb +45 -0
- data/lib/multi_compress/db_deployment.rb +10 -844
- data/lib/multi_compress/reader/buffer_operations.rb +57 -0
- data/lib/multi_compress/reader/read_operations.rb +69 -0
- data/lib/multi_compress/reader.rb +97 -0
- data/lib/multi_compress/stream_io.rb +13 -0
- data/lib/multi_compress/version.rb +1 -1
- data/lib/multi_compress/writer.rb +85 -0
- data/lib/multi_compress.rb +42 -333
- data/multi_compress.gemspec +2 -0
- data/mysql_udf/Makefile +12 -5
- data/mysql_udf/multi_compress_mysql.exports +6 -0
- data/postgres_extension/Makefile +2 -1
- data/postgres_extension/test/run_e2e.sh +28 -10
- metadata +30 -10
- data/ext/multi_compress/vendor/lz4/lib/Makefile +0 -244
- data/ext/multi_compress/vendor/lz4/lib/dll/example/Makefile +0 -63
- data/ext/multi_compress/vendor/zstd/lib/Makefile +0 -389
- data/ext/multi_compress/vendor/zstd/lib/dll/example/Makefile +0 -48
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module MultiCompress
|
|
4
|
+
module Database
|
|
5
|
+
class Engine
|
|
6
|
+
private
|
|
7
|
+
|
|
8
|
+
def validate_decoded!(_blob, header, out)
|
|
9
|
+
validate_decoded_size!(header, out)
|
|
10
|
+
validate_decoded_crc!(header, out)
|
|
11
|
+
validate_decoded_text(out)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def validate_decoded_size!(header, out)
|
|
15
|
+
expected_size = header.fetch(:original_size)
|
|
16
|
+
return if out.bytesize == expected_size
|
|
17
|
+
|
|
18
|
+
err("size mismatch: header says #{expected_size}, got #{out.bytesize}")
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def validate_decoded_crc!(header, out)
|
|
22
|
+
expected_crc = header.fetch(:expected_crc)
|
|
23
|
+
actual_crc = MultiCompress.crc32(out)
|
|
24
|
+
return if actual_crc == expected_crc
|
|
25
|
+
|
|
26
|
+
err("crc32 mismatch: header #{expected_crc}, computed #{actual_crc}")
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def validate_decoded_text(out)
|
|
30
|
+
text = out.force_encoding(Encoding::UTF_8)
|
|
31
|
+
err("decompressed bytes are not valid UTF-8") unless text.valid_encoding?
|
|
32
|
+
err("decompressed text contains a NUL byte") if text.include?("\0")
|
|
33
|
+
text
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def utf8_bytes(text)
|
|
37
|
+
s = text.encoding == Encoding::UTF_8 ? text : text.dup.force_encoding(Encoding::UTF_8)
|
|
38
|
+
raise ArgumentError, "MultiCompress::Database: input is not valid UTF-8" unless s.valid_encoding?
|
|
39
|
+
raise ArgumentError, "MultiCompress::Database: input contains a NUL byte" if s.include?("\0")
|
|
40
|
+
|
|
41
|
+
s.b
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def validate_canonical_zstd_frame!(frame, expected_size, expected_dictionary_id:)
|
|
45
|
+
_frame_size, content_size = MultiCompress.zstd_single_frame_info(frame)
|
|
46
|
+
if content_size != expected_size
|
|
47
|
+
err("zstd frame content size #{content_size} does not match header size #{expected_size}")
|
|
48
|
+
end
|
|
49
|
+
actual_dictionary_id = MultiCompress.zstd_frame_dictionary_id(frame)
|
|
50
|
+
if actual_dictionary_id != expected_dictionary_id
|
|
51
|
+
err("zstd frame dictionary id #{actual_dictionary_id} does not match expected #{expected_dictionary_id}")
|
|
52
|
+
end
|
|
53
|
+
rescue MultiCompress::DataError => e
|
|
54
|
+
err(e.message.sub(/\AMultiCompress::Database: /, ""))
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def database_dictionary!(value)
|
|
58
|
+
return value if value.is_a?(Dictionary)
|
|
59
|
+
|
|
60
|
+
raise TypeError, "MCDB2 requires MultiCompress::Database::Dictionary, got #{value.class}"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def check_envelope_size!(envelope, max, format)
|
|
64
|
+
return if envelope.bytesize <= max
|
|
65
|
+
|
|
66
|
+
raise MultiCompress::DataError,
|
|
67
|
+
"MultiCompress::Database: stored #{format} envelope is #{envelope.bytesize} bytes, " \
|
|
68
|
+
"over the #{max}-byte limit"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def header_v1(original_size, crc32)
|
|
72
|
+
encode_header(VERSION_V1, original_size, crc32)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def header_v2(original_size, crc32, dictionary_ref)
|
|
76
|
+
encode_header(VERSION_V2, original_size, crc32, dictionary_ref)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def encode_header(version, original_size, crc32, dictionary_ref = nil)
|
|
80
|
+
header = MAGIC + [version, CODEC_ZSTD, FLAGS_NONE].pack("C3") +
|
|
81
|
+
[original_size].pack("Q<") + [crc32].pack("L<")
|
|
82
|
+
header << [dictionary_ref].pack("Q<") unless dictionary_ref.nil?
|
|
83
|
+
header.b
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def format_name(version)
|
|
87
|
+
version == VERSION_V2 ? "MCDB2" : "MCDB1"
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def err(message)
|
|
91
|
+
raise MultiCompress::DataError, "MultiCompress::Database: #{message}"
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
@@ -20,293 +20,21 @@ module MultiCompress
|
|
|
20
20
|
ORIGINAL_SIZE_OFFSET = 7
|
|
21
21
|
CRC_OFFSET = 15
|
|
22
22
|
DICTIONARY_REF_OFFSET = 19
|
|
23
|
-
MAX_OUTPUT
|
|
23
|
+
MAX_OUTPUT = 16 * 1024 * 1024
|
|
24
24
|
MAX_DICTIONARY_BYTES = 256 * 1024
|
|
25
25
|
MAX_DICTIONARY_REF = (2**63) - 1
|
|
26
26
|
MAX_ENVELOPE_V1 = 16_842_771
|
|
27
27
|
MAX_ENVELOPE_V2 = 16_842_779
|
|
28
28
|
MAX_ENVELOPE = MAX_ENVELOPE_V2
|
|
29
29
|
ZSTD_LEVEL = 6
|
|
30
|
-
|
|
31
|
-
# A strict, immutable MCDB2 dictionary. It is intentionally distinct from
|
|
32
|
-
# MultiCompress::Dictionary: MCDB2 accepts only conformant zstd dictionaries
|
|
33
|
-
# with a non-zero zstd DictID and an application-level registry id.
|
|
34
|
-
class Dictionary
|
|
35
|
-
attr_reader :id, :bytes, :sha256, :zstd_id, :native
|
|
36
|
-
|
|
37
|
-
def self.train(samples, id:, size: 32 * 1024)
|
|
38
|
-
new(id: id, native: MultiCompress::Zstd.train_dictionary(samples, size: size))
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
def self.wrap(native, id:)
|
|
42
|
-
new(id: id, native: native)
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
def initialize(id:, native: nil, bytes: nil)
|
|
46
|
-
@id = normalize_id(id)
|
|
47
|
-
raise ArgumentError, "provide exactly one of native: or bytes:" if (!!native) == (!!bytes)
|
|
48
|
-
|
|
49
|
-
@native = native || MultiCompress::Dictionary.new(bytes, algo: :zstd)
|
|
50
|
-
unless @native.is_a?(MultiCompress::Dictionary) && @native.algo == :zstd
|
|
51
|
-
raise TypeError, "MCDB2 dictionary must be a MultiCompress::Dictionary with algo: :zstd"
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
@bytes = @native.bytes.b.freeze
|
|
55
|
-
if @bytes.empty? || @bytes.bytesize > MAX_DICTIONARY_BYTES
|
|
56
|
-
raise ArgumentError,
|
|
57
|
-
"MCDB2 dictionary is #{@bytes.bytesize} bytes; allowed range is 1..#{MAX_DICTIONARY_BYTES}"
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
@zstd_id = @native.zstd_id
|
|
61
|
-
raise ArgumentError, "MCDB2 dictionary must be a conformant zstd dictionary with a non-zero DictID" if @zstd_id.zero?
|
|
62
|
-
|
|
63
|
-
@sha256 = Digest::SHA256.digest(@bytes).freeze
|
|
64
|
-
freeze
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
def sha256_hex
|
|
68
|
-
@sha256.unpack1("H*")
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
def registry_attributes
|
|
72
|
-
{ id: @id, zstd_dict_id: @zstd_id, sha256: @sha256, bytes: @bytes }
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
private
|
|
76
|
-
|
|
77
|
-
def normalize_id(value)
|
|
78
|
-
raise TypeError, "MCDB2 dictionary id must be an Integer" unless value.respond_to?(:to_int)
|
|
79
|
-
|
|
80
|
-
integer = value.to_int
|
|
81
|
-
unless integer.between?(1, MAX_DICTIONARY_REF)
|
|
82
|
-
raise ArgumentError, "MCDB2 dictionary id must be in 1..#{MAX_DICTIONARY_REF}"
|
|
83
|
-
end
|
|
84
|
-
integer
|
|
85
|
-
end
|
|
86
|
-
end
|
|
87
|
-
|
|
88
|
-
module_function
|
|
89
|
-
|
|
90
|
-
# Without a dictionary this emits MCDB1. With Database::Dictionary it emits
|
|
91
|
-
# MCDB2. Choose one format per database column; mixed use is deliberately
|
|
92
|
-
# not what the generated dictionary view is optimized for.
|
|
93
|
-
def compress(text, dictionary: nil)
|
|
94
|
-
return compress_v1(text) if dictionary.nil?
|
|
95
|
-
|
|
96
|
-
compress_v2(text, dictionary: dictionary)
|
|
97
|
-
end
|
|
98
|
-
|
|
99
|
-
def compress_v1(text)
|
|
100
|
-
bytes = input_bytes!(text, format: "v1")
|
|
101
|
-
frame = MultiCompress.compress(bytes, algo: :zstd, level: ZSTD_LEVEL)
|
|
102
|
-
validate_canonical_zstd_frame!(frame, bytes.bytesize, expected_dictionary_id: 0)
|
|
103
|
-
envelope = header_v1(bytes.bytesize, MultiCompress.crc32(bytes)) << frame
|
|
104
|
-
check_envelope_size!(envelope, MAX_ENVELOPE_V1, "v1")
|
|
105
|
-
envelope
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
def compress_v2(text, dictionary:)
|
|
109
|
-
dictionary = database_dictionary!(dictionary)
|
|
110
|
-
bytes = input_bytes!(text, format: "v2")
|
|
111
|
-
frame = MultiCompress.compress(bytes, algo: :zstd, level: ZSTD_LEVEL, dictionary: dictionary.native)
|
|
112
|
-
validate_canonical_zstd_frame!(frame, bytes.bytesize, expected_dictionary_id: dictionary.zstd_id)
|
|
113
|
-
envelope = header_v2(bytes.bytesize, MultiCompress.crc32(bytes), dictionary.id) << frame
|
|
114
|
-
check_envelope_size!(envelope, MAX_ENVELOPE_V2, "v2")
|
|
115
|
-
envelope
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
def decompress(blob, dictionary: nil)
|
|
119
|
-
header = parse_header!(blob)
|
|
120
|
-
case header.fetch(:version)
|
|
121
|
-
when VERSION_V1
|
|
122
|
-
raise ArgumentError, "MCDB1 does not accept a dictionary" unless dictionary.nil?
|
|
123
|
-
|
|
124
|
-
decode_v1(blob.b, header)
|
|
125
|
-
when VERSION_V2
|
|
126
|
-
decode_v2(blob.b, header, dictionary)
|
|
127
|
-
else
|
|
128
|
-
err("unsupported version #{header.fetch(:version)}")
|
|
129
|
-
end
|
|
130
|
-
end
|
|
131
|
-
|
|
132
|
-
def valid?(blob, dictionary: nil)
|
|
133
|
-
decompress(blob, dictionary: dictionary)
|
|
134
|
-
true
|
|
135
|
-
rescue StandardError
|
|
136
|
-
false
|
|
137
|
-
end
|
|
138
|
-
|
|
139
|
-
# Parses only the envelope header. It is safe for registry/FK consistency
|
|
140
|
-
# checks and does not decompress the payload.
|
|
141
|
-
def dictionary_ref(blob)
|
|
142
|
-
header = parse_header!(blob)
|
|
143
|
-
return nil if header.fetch(:version) == VERSION_V1
|
|
144
|
-
|
|
145
|
-
header.fetch(:dictionary_ref)
|
|
146
|
-
end
|
|
147
|
-
|
|
148
|
-
def original_size(blob)
|
|
149
|
-
parse_header!(blob).fetch(:original_size)
|
|
150
|
-
end
|
|
151
|
-
|
|
152
|
-
def version_string
|
|
153
|
-
"MCDB1 + MCDB2 (zstd #{MultiCompress.version(:zstd)})"
|
|
154
|
-
end
|
|
155
|
-
|
|
156
|
-
def input_bytes!(text, format:)
|
|
157
|
-
raise TypeError, "MultiCompress::Database.compress expects a String, got #{text.class}" unless text.is_a?(String)
|
|
158
|
-
|
|
159
|
-
bytes = utf8_bytes(text)
|
|
160
|
-
n = bytes.bytesize
|
|
161
|
-
if n > MAX_OUTPUT
|
|
162
|
-
raise ArgumentError, "MultiCompress::Database: input is #{n} bytes, over the #{MAX_OUTPUT}-byte #{format} limit"
|
|
163
|
-
end
|
|
164
|
-
bytes
|
|
165
|
-
end
|
|
166
|
-
private_class_method :input_bytes!
|
|
167
|
-
|
|
168
|
-
def decode_v1(blob, header)
|
|
169
|
-
frame = blob.byteslice(V1_HEADER_SIZE, blob.bytesize - V1_HEADER_SIZE) || +"".b
|
|
170
|
-
validate_canonical_zstd_frame!(frame, header.fetch(:original_size), expected_dictionary_id: 0)
|
|
171
|
-
out = MultiCompress.decompress(frame, algo: :zstd, max_output_size: MAX_OUTPUT)
|
|
172
|
-
validate_decoded!(blob, header, out)
|
|
173
|
-
end
|
|
174
|
-
private_class_method :decode_v1
|
|
175
|
-
|
|
176
|
-
def decode_v2(blob, header, dictionary)
|
|
177
|
-
err("MCDB2 requires a dictionary") if dictionary.nil?
|
|
178
|
-
dictionary = database_dictionary!(dictionary)
|
|
179
|
-
if dictionary.id != header.fetch(:dictionary_ref)
|
|
180
|
-
err("dictionary reference #{header.fetch(:dictionary_ref)} does not match supplied dictionary #{dictionary.id}")
|
|
181
|
-
end
|
|
182
|
-
|
|
183
|
-
frame = blob.byteslice(V2_HEADER_SIZE, blob.bytesize - V2_HEADER_SIZE) || +"".b
|
|
184
|
-
validate_canonical_zstd_frame!(frame, header.fetch(:original_size), expected_dictionary_id: dictionary.zstd_id)
|
|
185
|
-
out = MultiCompress.decompress(
|
|
186
|
-
frame,
|
|
187
|
-
algo: :zstd,
|
|
188
|
-
dictionary: dictionary.native,
|
|
189
|
-
max_output_size: MAX_OUTPUT
|
|
190
|
-
)
|
|
191
|
-
validate_decoded!(blob, header, out)
|
|
192
|
-
end
|
|
193
|
-
private_class_method :decode_v2
|
|
194
|
-
|
|
195
|
-
def parse_header!(blob)
|
|
196
|
-
raise TypeError, "MultiCompress::Database.decompress expects a String, got #{blob.class}" unless blob.is_a?(String)
|
|
197
|
-
|
|
198
|
-
b = blob.b
|
|
199
|
-
err("truncated envelope (#{b.bytesize} < #{V1_HEADER_SIZE} bytes)") if b.bytesize < V1_HEADER_SIZE
|
|
200
|
-
err("bad magic") unless b.byteslice(0, 4) == MAGIC
|
|
201
|
-
|
|
202
|
-
version = b.getbyte(4)
|
|
203
|
-
case version
|
|
204
|
-
when VERSION_V1
|
|
205
|
-
header_size = V1_HEADER_SIZE
|
|
206
|
-
max_envelope = MAX_ENVELOPE_V1
|
|
207
|
-
when VERSION_V2
|
|
208
|
-
header_size = V2_HEADER_SIZE
|
|
209
|
-
max_envelope = MAX_ENVELOPE_V2
|
|
210
|
-
else
|
|
211
|
-
err("unsupported version #{version}")
|
|
212
|
-
end
|
|
213
|
-
|
|
214
|
-
err("truncated #{format_name(version)} envelope (#{b.bytesize} < #{header_size} bytes)") if b.bytesize < header_size
|
|
215
|
-
err("stored #{format_name(version)} envelope is #{b.bytesize} bytes, over the #{max_envelope}-byte limit") if b.bytesize > max_envelope
|
|
216
|
-
err("unsupported codec #{b.getbyte(5)}") unless b.getbyte(5) == CODEC_ZSTD
|
|
217
|
-
err("reserved flags must be 0, got #{b.getbyte(6)}") unless b.getbyte(6) == FLAGS_NONE
|
|
218
|
-
|
|
219
|
-
original_size = b.byteslice(ORIGINAL_SIZE_OFFSET, 8).unpack1("Q<")
|
|
220
|
-
err("declared size #{original_size} over the #{MAX_OUTPUT}-byte limit") if original_size > MAX_OUTPUT
|
|
221
|
-
|
|
222
|
-
header = {
|
|
223
|
-
version: version,
|
|
224
|
-
header_size: header_size,
|
|
225
|
-
original_size: original_size,
|
|
226
|
-
expected_crc: b.byteslice(CRC_OFFSET, 4).unpack1("L<")
|
|
227
|
-
}
|
|
228
|
-
if version == VERSION_V2
|
|
229
|
-
dictionary_ref = b.byteslice(DICTIONARY_REF_OFFSET, 8).unpack1("Q<")
|
|
230
|
-
unless dictionary_ref.between?(1, MAX_DICTIONARY_REF)
|
|
231
|
-
err("invalid dictionary reference #{dictionary_ref}")
|
|
232
|
-
end
|
|
233
|
-
header[:dictionary_ref] = dictionary_ref
|
|
234
|
-
end
|
|
235
|
-
header
|
|
236
|
-
end
|
|
237
|
-
private_class_method :parse_header!
|
|
238
|
-
|
|
239
|
-
def validate_decoded!(blob, header, out)
|
|
240
|
-
if out.bytesize != header.fetch(:original_size)
|
|
241
|
-
err("size mismatch: header says #{header.fetch(:original_size)}, got #{out.bytesize}")
|
|
242
|
-
end
|
|
243
|
-
actual_crc = MultiCompress.crc32(out)
|
|
244
|
-
if actual_crc != header.fetch(:expected_crc)
|
|
245
|
-
err("crc32 mismatch: header #{header.fetch(:expected_crc)}, computed #{actual_crc}")
|
|
246
|
-
end
|
|
247
|
-
s = out.force_encoding(Encoding::UTF_8)
|
|
248
|
-
err("decompressed bytes are not valid UTF-8") unless s.valid_encoding?
|
|
249
|
-
err("decompressed text contains a NUL byte") if s.include?("\0")
|
|
250
|
-
s
|
|
251
|
-
end
|
|
252
|
-
private_class_method :validate_decoded!
|
|
253
|
-
|
|
254
|
-
def utf8_bytes(text)
|
|
255
|
-
s = text.encoding == Encoding::UTF_8 ? text : text.dup.force_encoding(Encoding::UTF_8)
|
|
256
|
-
raise ArgumentError, "MultiCompress::Database: input is not valid UTF-8" unless s.valid_encoding?
|
|
257
|
-
raise ArgumentError, "MultiCompress::Database: input contains a NUL byte" if s.include?("\0")
|
|
258
|
-
|
|
259
|
-
s.b
|
|
260
|
-
end
|
|
261
|
-
private_class_method :utf8_bytes
|
|
262
|
-
|
|
263
|
-
def validate_canonical_zstd_frame!(frame, expected_size, expected_dictionary_id:)
|
|
264
|
-
_frame_size, content_size = MultiCompress.zstd_single_frame_info(frame)
|
|
265
|
-
err("zstd frame content size #{content_size} does not match header size #{expected_size}") if content_size != expected_size
|
|
266
|
-
actual_dictionary_id = MultiCompress.zstd_frame_dictionary_id(frame)
|
|
267
|
-
if actual_dictionary_id != expected_dictionary_id
|
|
268
|
-
err("zstd frame dictionary id #{actual_dictionary_id} does not match expected #{expected_dictionary_id}")
|
|
269
|
-
end
|
|
270
|
-
rescue MultiCompress::DataError => e
|
|
271
|
-
err(e.message.sub(/\AMultiCompress::Database: /, ""))
|
|
272
|
-
end
|
|
273
|
-
private_class_method :validate_canonical_zstd_frame!
|
|
274
|
-
|
|
275
|
-
def database_dictionary!(value)
|
|
276
|
-
return value if value.is_a?(Dictionary)
|
|
277
|
-
|
|
278
|
-
raise TypeError, "MCDB2 requires MultiCompress::Database::Dictionary, got #{value.class}"
|
|
279
|
-
end
|
|
280
|
-
private_class_method :database_dictionary!
|
|
281
|
-
|
|
282
|
-
def check_envelope_size!(envelope, max, format)
|
|
283
|
-
return if envelope.bytesize <= max
|
|
284
|
-
|
|
285
|
-
raise MultiCompress::DataError,
|
|
286
|
-
"MultiCompress::Database: stored #{format} envelope is #{envelope.bytesize} bytes, over the #{max}-byte limit"
|
|
287
|
-
end
|
|
288
|
-
private_class_method :check_envelope_size!
|
|
289
|
-
|
|
290
|
-
def header_v1(original_size, crc32)
|
|
291
|
-
(MAGIC + [VERSION_V1, CODEC_ZSTD, FLAGS_NONE].pack("C3") +
|
|
292
|
-
[original_size].pack("Q<") + [crc32].pack("L<")).b
|
|
293
|
-
end
|
|
294
|
-
private_class_method :header_v1
|
|
295
|
-
|
|
296
|
-
def header_v2(original_size, crc32, dictionary_ref)
|
|
297
|
-
(MAGIC + [VERSION_V2, CODEC_ZSTD, FLAGS_NONE].pack("C3") +
|
|
298
|
-
[original_size].pack("Q<") + [crc32].pack("L<") + [dictionary_ref].pack("Q<")).b
|
|
299
|
-
end
|
|
300
|
-
private_class_method :header_v2
|
|
301
|
-
|
|
302
|
-
def format_name(version)
|
|
303
|
-
version == VERSION_V2 ? "MCDB2" : "MCDB1"
|
|
304
|
-
end
|
|
305
|
-
private_class_method :format_name
|
|
306
|
-
|
|
307
|
-
def err(message)
|
|
308
|
-
raise MultiCompress::DataError, "MultiCompress::Database: #{message}"
|
|
309
|
-
end
|
|
310
|
-
private_class_method :err
|
|
311
30
|
end
|
|
312
31
|
end
|
|
32
|
+
|
|
33
|
+
require "multi_compress/database/dictionary"
|
|
34
|
+
require "multi_compress/database/operations"
|
|
35
|
+
require "multi_compress/database/encoding"
|
|
36
|
+
require "multi_compress/database/decoding"
|
|
37
|
+
require "multi_compress/database/header"
|
|
38
|
+
require "multi_compress/database/validation"
|
|
39
|
+
|
|
40
|
+
require "multi_compress/database/api"
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module MultiCompress
|
|
4
|
+
module DBDeployment
|
|
5
|
+
class BundleArchiver
|
|
6
|
+
DEFAULT_EPOCH = 315_619_200
|
|
7
|
+
attr_reader :output, :root_name
|
|
8
|
+
private :output, :root_name
|
|
9
|
+
|
|
10
|
+
def initialize(output, root_name)
|
|
11
|
+
@output = output
|
|
12
|
+
@root_name = root_name
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def write!(temporary_directory)
|
|
16
|
+
temporary_output = temporary_path
|
|
17
|
+
FileUtils.mkdir_p(File.dirname(output))
|
|
18
|
+
write_archive(temporary_output, temporary_directory)
|
|
19
|
+
File.rename(temporary_output, output)
|
|
20
|
+
ensure
|
|
21
|
+
File.delete(temporary_output) if defined?(temporary_output) && File.exist?(temporary_output)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def temporary_path
|
|
27
|
+
File.join(File.dirname(output), ".#{File.basename(output)}.#{Process.pid}.#{SecureRandom.hex(8)}.tmp")
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def write_archive(path, temporary_directory)
|
|
31
|
+
File.open(path, "wb", 0o644) do |file|
|
|
32
|
+
Zlib::GzipWriter.wrap(file) do |gzip|
|
|
33
|
+
gzip.mtime = archive_epoch
|
|
34
|
+
DeterministicTarWriter.new(gzip, archive_epoch) do |tar|
|
|
35
|
+
add_tree(tar, File.join(temporary_directory, root_name), root_name)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def archive_epoch
|
|
42
|
+
value = ENV.fetch("SOURCE_DATE_EPOCH", nil)
|
|
43
|
+
value&.match?(/\A\d+\z/) ? value.to_i : DEFAULT_EPOCH
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def add_tree(tar, directory, archive_path)
|
|
47
|
+
tar.mkdir(archive_path, mode(directory))
|
|
48
|
+
Dir.children(directory).sort.each { |entry| add_entry(tar, directory, archive_path, entry) }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def add_entry(tar, directory, archive_path, entry)
|
|
52
|
+
source = File.join(directory, entry)
|
|
53
|
+
destination = File.join(archive_path, entry)
|
|
54
|
+
return add_tree(tar, source, destination) if File.directory?(source)
|
|
55
|
+
|
|
56
|
+
add_file(tar, source, destination)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def add_file(tar, source, destination)
|
|
60
|
+
tar.add_file_simple(destination, mode(source), File.size(source)) do |destination_io|
|
|
61
|
+
File.open(source, "rb") { |source_io| IO.copy_stream(source_io, destination_io) }
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def mode(path)
|
|
66
|
+
File.stat(path).mode & 0o777
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module MultiCompress
|
|
4
|
+
module DBDeployment
|
|
5
|
+
class BundleMetadata
|
|
6
|
+
attr_reader :target
|
|
7
|
+
private :target
|
|
8
|
+
|
|
9
|
+
def initialize(target)
|
|
10
|
+
@target = target
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def write!(bundle_root)
|
|
14
|
+
write_manifest(bundle_root, manifest_files(bundle_root))
|
|
15
|
+
write_checksums(bundle_root)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def manifest_files(root)
|
|
21
|
+
regular_files(root).reject { |path| %w[manifest.json SHA256SUMS].include?(File.basename(path)) }
|
|
22
|
+
.sort.map { |path| file_record(root, path) }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def file_record(root, path)
|
|
26
|
+
{ "path" => relative_to(root, path), "sha256" => Digest::SHA256.file(path).hexdigest }
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def write_manifest(root, files)
|
|
30
|
+
manifest = {
|
|
31
|
+
"bundle_format_version" => BUNDLE_FORMAT_VERSION,
|
|
32
|
+
"gem" => { "name" => "multi_compress", "version" => MultiCompress::VERSION },
|
|
33
|
+
"database_target" => target,
|
|
34
|
+
"database_envelope" => FORMAT,
|
|
35
|
+
"files" => files
|
|
36
|
+
}
|
|
37
|
+
File.write(File.join(root, "manifest.json"), "#{JSON.pretty_generate(manifest)}\n")
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def write_checksums(root)
|
|
41
|
+
checksums = regular_files(root).reject { |path| File.basename(path) == "SHA256SUMS" }.sort.map do |path|
|
|
42
|
+
"#{Digest::SHA256.file(path).hexdigest} #{relative_to(root, path)}"
|
|
43
|
+
end
|
|
44
|
+
File.write(File.join(root, "SHA256SUMS"), "#{checksums.join("\n")}\n")
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def regular_files(root)
|
|
48
|
+
Dir.glob(File.join(root, "**", "*"), File::FNM_DOTMATCH).select { |path| File.file?(path) }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def relative_to(root, path)
|
|
52
|
+
Pathname.new(path).relative_path_from(Pathname.new(root)).to_s
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module MultiCompress
|
|
4
|
+
module DBDeployment
|
|
5
|
+
class BundleStager
|
|
6
|
+
attr_reader :root, :target
|
|
7
|
+
private :root, :target
|
|
8
|
+
|
|
9
|
+
IGNORED_ENTRIES = [".DS_Store", ".git", "build", "test"].freeze
|
|
10
|
+
|
|
11
|
+
def initialize(root, target)
|
|
12
|
+
@root = root
|
|
13
|
+
@target = target
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def stage!(bundle_root)
|
|
17
|
+
target.fetch(:source_paths).each { |relative| copy_path!(relative, bundle_root) }
|
|
18
|
+
stage_template(bundle_root)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def stage_template(bundle_root)
|
|
24
|
+
template = target.fetch(:template_path)
|
|
25
|
+
Dir.children(File.join(root, template)).sort.each do |entry|
|
|
26
|
+
next if ignored?(entry)
|
|
27
|
+
|
|
28
|
+
copy_path!(File.join(template, entry), bundle_root, destination: entry)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def copy_path!(relative, bundle_root, destination: relative)
|
|
33
|
+
source = File.join(root, relative)
|
|
34
|
+
raise Error, "package source is missing: #{relative}" unless File.exist?(source)
|
|
35
|
+
|
|
36
|
+
destination_path = File.join(bundle_root, destination)
|
|
37
|
+
return copy_directory(source, relative, destination_path, destination, bundle_root) if File.directory?(source)
|
|
38
|
+
|
|
39
|
+
copy_file(source, destination_path)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def copy_directory(source, relative, destination_path, destination, bundle_root)
|
|
43
|
+
FileUtils.mkdir_p(destination_path)
|
|
44
|
+
Dir.children(source).sort.each do |entry|
|
|
45
|
+
next if ignored?(entry)
|
|
46
|
+
|
|
47
|
+
copy_path!(File.join(relative, entry), bundle_root, destination: File.join(destination, entry))
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def copy_file(source, destination)
|
|
52
|
+
FileUtils.mkdir_p(File.dirname(destination))
|
|
53
|
+
FileUtils.copy_file(source, destination)
|
|
54
|
+
File.chmod(File.stat(source).mode & 0o777, destination)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def ignored?(entry)
|
|
58
|
+
IGNORED_ENTRIES.include?(entry)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|