Class: Omnizip::Algorithms::Zstandard::Dictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/algorithms/zstandard/dictionary.rb

Overview

Zstandard dictionary (port of the omnizip-rs dict.rs).

A dictionary lets the encoder preload a reference-content window so small inputs compress dramatically better: the dictionary content is used as a match-finder prefix, and the frame header carries the dictionary's ID.

Wire format (simplified form, as in the Rust reference): magic (4) + dictionary ID (4, LE) + raw content.

Constant Summary collapse

DICT_MAGIC =
0xEC30A437

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, content) ⇒ Dictionary

Returns a new instance of Dictionary.

Parameters:

  • id (Integer)

    dictionary ID carried in frame headers

  • content (String)

    corpus bytes used as the prefix



42
43
44
45
# File 'lib/omnizip/algorithms/zstandard/dictionary.rb', line 42

def initialize(id, content)
  @id = id
  @content = content.dup.force_encoding(Encoding::BINARY)
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



38
39
40
# File 'lib/omnizip/algorithms/zstandard/dictionary.rb', line 38

def content
  @content
end

#idObject (readonly)

Returns the value of attribute id.



38
39
40
# File 'lib/omnizip/algorithms/zstandard/dictionary.rb', line 38

def id
  @id
end

Class Method Details

.deserialize(data) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/omnizip/algorithms/zstandard/dictionary.rb', line 51

def self.deserialize(data)
  if data.bytesize < 8
    raise Omnizip::DecompressionError,
          "dictionary too short for magic + id"
  end
  if data.byteslice(0, 4).unpack1("V") != DICT_MAGIC
    raise Omnizip::DecompressionError, "bad dictionary magic"
  end

  new(data.byteslice(4, 4).unpack1("V"), data.byteslice(8..))
end

.from_raw(id, content) ⇒ Object



47
48
49
# File 'lib/omnizip/algorithms/zstandard/dictionary.rb', line 47

def self.from_raw(id, content)
  new(id, content)
end

Instance Method Details

#==(other) ⇒ Object



67
68
69
# File 'lib/omnizip/algorithms/zstandard/dictionary.rb', line 67

def ==(other)
  other.is_a?(Dictionary) && id == other.id && content == other.content
end

#serializeObject



63
64
65
# File 'lib/omnizip/algorithms/zstandard/dictionary.rb', line 63

def serialize
  [DICT_MAGIC].pack("V") + [@id].pack("V") + @content
end