Class: Zrip::BlockCodec

Inherits:
Object
  • Object
show all
Defined in:
lib/zrip/block_codec.rb

Overview

Zstandard block-format codec.

BlockCodec keeps mutable native context state and is intended to be used per Ractor.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._native_new(dict, id, level) ⇒ BlockCodec

Native constructor used by .new.

Parameters:

  • dict (String, nil)

    dictionary bytes

  • id (Integer)

    dictionary ID, or 0 without a dictionary

  • level (Integer)

    compression level

Returns:

Raises:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/zrip/block_codec.rb', line 55

class BlockCodec
  def self.new(dict: nil, level: DEFAULT_LEVEL)
    case dict
    when nil
      _native_new(nil, 0, Integer(level))
    when Dictionary
      _native_new(dict.bytes, dict.id, Integer(level))
    when String
      d = Dictionary.new(bytes: dict)
      _native_new(d.bytes, d.id, Integer(level))
    else
      raise TypeError, "expected Zrip::Dictionary, String, or nil; got #{dict.class}"
    end
  end


  def decompress(bytes, max_output_size: nil)
    _native_decompress(bytes, Integer(max_output_size || 0))
  end
end

.new(dict: nil, level: DEFAULT_LEVEL) ⇒ BlockCodec

Create a block codec.

Parameters:

  • dict (Dictionary, String, nil) (defaults to: nil)

    optional Zstandard dictionary

  • level (Integer) (defaults to: DEFAULT_LEVEL)

    compression level

Returns:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/zrip/block_codec.rb', line 55

class BlockCodec
  def self.new(dict: nil, level: DEFAULT_LEVEL)
    case dict
    when nil
      _native_new(nil, 0, Integer(level))
    when Dictionary
      _native_new(dict.bytes, dict.id, Integer(level))
    when String
      d = Dictionary.new(bytes: dict)
      _native_new(d.bytes, d.id, Integer(level))
    else
      raise TypeError, "expected Zrip::Dictionary, String, or nil; got #{dict.class}"
    end
  end


  def decompress(bytes, max_output_size: nil)
    _native_decompress(bytes, Integer(max_output_size || 0))
  end
end

Instance Method Details

#_native_decompress(bytes, max_output_size) ⇒ String

Native decompression entry used by #decompress.

Parameters:

  • bytes (String)

    compressed Zstandard block

  • max_output_size (Integer)

    output byte limit, or 0 for unbounded

Returns:

  • (String)

Raises:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/zrip/block_codec.rb', line 55

class BlockCodec
  def self.new(dict: nil, level: DEFAULT_LEVEL)
    case dict
    when nil
      _native_new(nil, 0, Integer(level))
    when Dictionary
      _native_new(dict.bytes, dict.id, Integer(level))
    when String
      d = Dictionary.new(bytes: dict)
      _native_new(d.bytes, d.id, Integer(level))
    else
      raise TypeError, "expected Zrip::Dictionary, String, or nil; got #{dict.class}"
    end
  end


  def decompress(bytes, max_output_size: nil)
    _native_decompress(bytes, Integer(max_output_size || 0))
  end
end

#compress(bytes) ⇒ String

Compress bytes to a Zstandard block.

Parameters:

  • bytes (String)

    uncompressed bytes

Returns:

  • (String)

Raises:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/zrip/block_codec.rb', line 55

class BlockCodec
  def self.new(dict: nil, level: DEFAULT_LEVEL)
    case dict
    when nil
      _native_new(nil, 0, Integer(level))
    when Dictionary
      _native_new(dict.bytes, dict.id, Integer(level))
    when String
      d = Dictionary.new(bytes: dict)
      _native_new(d.bytes, d.id, Integer(level))
    else
      raise TypeError, "expected Zrip::Dictionary, String, or nil; got #{dict.class}"
    end
  end


  def decompress(bytes, max_output_size: nil)
    _native_decompress(bytes, Integer(max_output_size || 0))
  end
end

#decompress(bytes, max_output_size: nil) ⇒ String

Decompress a Zstandard block.

Parameters:

  • bytes (String)

    compressed Zstandard block

  • max_output_size (Integer, nil) (defaults to: nil)

    optional output byte limit

Returns:

  • (String)

Raises:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/zrip/block_codec.rb', line 55

class BlockCodec
  def self.new(dict: nil, level: DEFAULT_LEVEL)
    case dict
    when nil
      _native_new(nil, 0, Integer(level))
    when Dictionary
      _native_new(dict.bytes, dict.id, Integer(level))
    when String
      d = Dictionary.new(bytes: dict)
      _native_new(d.bytes, d.id, Integer(level))
    else
      raise TypeError, "expected Zrip::Dictionary, String, or nil; got #{dict.class}"
    end
  end


  def decompress(bytes, max_output_size: nil)
    _native_decompress(bytes, Integer(max_output_size || 0))
  end
end

#has_dict?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/zrip/block_codec.rb', line 55

class BlockCodec
  def self.new(dict: nil, level: DEFAULT_LEVEL)
    case dict
    when nil
      _native_new(nil, 0, Integer(level))
    when Dictionary
      _native_new(dict.bytes, dict.id, Integer(level))
    when String
      d = Dictionary.new(bytes: dict)
      _native_new(d.bytes, d.id, Integer(level))
    else
      raise TypeError, "expected Zrip::Dictionary, String, or nil; got #{dict.class}"
    end
  end


  def decompress(bytes, max_output_size: nil)
    _native_decompress(bytes, Integer(max_output_size || 0))
  end
end

#levelInteger

Returns compression level.

Returns:

  • (Integer)

    compression level



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/zrip/block_codec.rb', line 55

class BlockCodec
  def self.new(dict: nil, level: DEFAULT_LEVEL)
    case dict
    when nil
      _native_new(nil, 0, Integer(level))
    when Dictionary
      _native_new(dict.bytes, dict.id, Integer(level))
    when String
      d = Dictionary.new(bytes: dict)
      _native_new(d.bytes, d.id, Integer(level))
    else
      raise TypeError, "expected Zrip::Dictionary, String, or nil; got #{dict.class}"
    end
  end


  def decompress(bytes, max_output_size: nil)
    _native_decompress(bytes, Integer(max_output_size || 0))
  end
end

#sizeInteger

Returns dictionary size in bytes.

Returns:

  • (Integer)

    dictionary size in bytes



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/zrip/block_codec.rb', line 55

class BlockCodec
  def self.new(dict: nil, level: DEFAULT_LEVEL)
    case dict
    when nil
      _native_new(nil, 0, Integer(level))
    when Dictionary
      _native_new(dict.bytes, dict.id, Integer(level))
    when String
      d = Dictionary.new(bytes: dict)
      _native_new(d.bytes, d.id, Integer(level))
    else
      raise TypeError, "expected Zrip::Dictionary, String, or nil; got #{dict.class}"
    end
  end


  def decompress(bytes, max_output_size: nil)
    _native_decompress(bytes, Integer(max_output_size || 0))
  end
end