Class: Sidetree::Model::ChunkFile

Inherits:
CASFileBase show all
Defined in:
lib/sidetree/model/chunk_file.rb

Overview

Constant Summary

Constants included from Util::Compressor

Util::Compressor::ESTIMATE_DECOMPRESSION_MULTIPLIER

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CASFileBase

decompress, #to_compress

Methods included from Util::Compressor

compress, decompress

Constructor Details

#initialize(deltas = []) ⇒ ChunkFile

Returns a new instance of ChunkFile.



7
8
9
10
11
12
13
14
15
# File 'lib/sidetree/model/chunk_file.rb', line 7

def initialize(deltas = [])
  deltas.each do |delta|
    unless delta.is_a?(Sidetree::Model::Delta)
      raise Sidetree::Error,
            "deltas contains data that is not Sidetree::Model::Delta object."
    end
  end
  @deltas = deltas
end

Instance Attribute Details

#deltasObject (readonly)

Array of Sidetree::Model::Delta



5
6
7
# File 'lib/sidetree/model/chunk_file.rb', line 5

def deltas
  @deltas
end

Class Method Details

.create_from_ops(create_ops: [], recover_ops: [], update_ops: []) ⇒ Object

Generate chunk file from operations.

Parameters:



21
22
23
24
# File 'lib/sidetree/model/chunk_file.rb', line 21

def self.create_from_ops(create_ops: [], recover_ops: [], update_ops: [])
  deltas = (create_ops + recover_ops + update_ops).map(&:delta)
  ChunkFile.new(deltas)
end

.parse(chunk_file, compressed: true) ⇒ Sidetree::Model::ChunkFile

Parse chunk file from compressed data.

Parameters:

  • chunk_file (String)

    compressed chunk file data.

  • compressed (Boolean) (defaults to: true)

    Whether the chunk_file is compressed or not, default: true.

Returns:

Raises:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sidetree/model/chunk_file.rb', line 31

def self.parse(chunk_file, compressed: true)
  decompressed =
    (
      if compressed
        decompress(chunk_file, Sidetree::Params::MAX_CHUNK_FILE_SIZE)
      else
        chunk_file
      end
    )
  json = JSON.parse(decompressed, symbolize_names: true)
  json.keys.each do |k|
    unless k == :deltas
      raise Sidetree::Error,
            "Unexpected property #{k.to_s} in chunk file."
    end
  end
  unless json[:deltas].is_a?(Array)
    raise Sidetree::Error,
          "Invalid chunk file, deltas property is not an array."
  end
  ChunkFile.new(
    json[:deltas].map do |delta|
      Sidetree::Model::Delta.from_object(delta)
    end
  )
end

Instance Method Details

#==(other) ⇒ Boolean

Check if the other object have the same chunk data.

Returns:

  • (Boolean)


66
67
68
69
# File 'lib/sidetree/model/chunk_file.rb', line 66

def ==(other)
  return false unless other.is_a?(ChunkFile)
  deltas == other.deltas
end

#to_jsonString

Build json string to be stored in CAS.

Returns:

  • (String)

    json string.



60
61
62
# File 'lib/sidetree/model/chunk_file.rb', line 60

def to_json
  { deltas: deltas.map(&:to_h) }.to_json
end