Class: MultiCompress::Database::Dictionary
- Inherits:
-
Object
- Object
- MultiCompress::Database::Dictionary
- Defined in:
- lib/multi_compress/database.rb
Overview
A strict, immutable MCDB2 dictionary. It is intentionally distinct from MultiCompress::Dictionary: MCDB2 accepts only conformant zstd dictionaries with a non-zero zstd DictID and an application-level registry id.
Instance Attribute Summary collapse
-
#bytes ⇒ Object
readonly
Returns the value of attribute bytes.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#native ⇒ Object
readonly
Returns the value of attribute native.
-
#sha256 ⇒ Object
readonly
Returns the value of attribute sha256.
-
#zstd_id ⇒ Object
readonly
Returns the value of attribute zstd_id.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(id:, native: nil, bytes: nil) ⇒ Dictionary
constructor
A new instance of Dictionary.
- #registry_attributes ⇒ Object
- #sha256_hex ⇒ Object
Constructor Details
#initialize(id:, native: nil, bytes: nil) ⇒ Dictionary
Returns a new instance of Dictionary.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/multi_compress/database.rb', line 45 def initialize(id:, native: nil, bytes: nil) @id = normalize_id(id) raise ArgumentError, "provide exactly one of native: or bytes:" if (!!native) == (!!bytes) @native = native || MultiCompress::Dictionary.new(bytes, algo: :zstd) unless @native.is_a?(MultiCompress::Dictionary) && @native.algo == :zstd raise TypeError, "MCDB2 dictionary must be a MultiCompress::Dictionary with algo: :zstd" end @bytes = @native.bytes.b.freeze if @bytes.empty? || @bytes.bytesize > MAX_DICTIONARY_BYTES raise ArgumentError, "MCDB2 dictionary is #{@bytes.bytesize} bytes; allowed range is 1..#{MAX_DICTIONARY_BYTES}" end @zstd_id = @native.zstd_id raise ArgumentError, "MCDB2 dictionary must be a conformant zstd dictionary with a non-zero DictID" if @zstd_id.zero? @sha256 = Digest::SHA256.digest(@bytes).freeze freeze end |
Instance Attribute Details
#bytes ⇒ Object (readonly)
Returns the value of attribute bytes.
35 36 37 |
# File 'lib/multi_compress/database.rb', line 35 def bytes @bytes end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
35 36 37 |
# File 'lib/multi_compress/database.rb', line 35 def id @id end |
#native ⇒ Object (readonly)
Returns the value of attribute native.
35 36 37 |
# File 'lib/multi_compress/database.rb', line 35 def native @native end |
#sha256 ⇒ Object (readonly)
Returns the value of attribute sha256.
35 36 37 |
# File 'lib/multi_compress/database.rb', line 35 def sha256 @sha256 end |
#zstd_id ⇒ Object (readonly)
Returns the value of attribute zstd_id.
35 36 37 |
# File 'lib/multi_compress/database.rb', line 35 def zstd_id @zstd_id end |
Class Method Details
.train(samples, id:, size: 32 * 1024) ⇒ Object
37 38 39 |
# File 'lib/multi_compress/database.rb', line 37 def self.train(samples, id:, size: 32 * 1024) new(id: id, native: MultiCompress::Zstd.train_dictionary(samples, size: size)) end |
.wrap(native, id:) ⇒ Object
41 42 43 |
# File 'lib/multi_compress/database.rb', line 41 def self.wrap(native, id:) new(id: id, native: native) end |
Instance Method Details
#registry_attributes ⇒ Object
71 72 73 |
# File 'lib/multi_compress/database.rb', line 71 def registry_attributes { id: @id, zstd_dict_id: @zstd_id, sha256: @sha256, bytes: @bytes } end |
#sha256_hex ⇒ Object
67 68 69 |
# File 'lib/multi_compress/database.rb', line 67 def sha256_hex @sha256.unpack1("H*") end |