Class: Sidetree::Model::ChunkFile
- Inherits:
-
CASFileBase
- Object
- CASFileBase
- Sidetree::Model::ChunkFile
- 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
-
#deltas ⇒ Object
readonly
Array of Sidetree::Model::Delta.
Class Method Summary collapse
-
.create_from_ops(create_ops: [], recover_ops: [], update_ops: []) ⇒ Object
Generate chunk file from operations.
-
.parse(chunk_file, compressed: true) ⇒ Sidetree::Model::ChunkFile
Parse chunk file from compressed data.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Check if the
other
object have the same chunk data. -
#initialize(deltas = []) ⇒ ChunkFile
constructor
A new instance of ChunkFile.
-
#to_json ⇒ String
Build json string to be stored in CAS.
Methods inherited from CASFileBase
Methods included from Util::Compressor
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
#deltas ⇒ Object (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.
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.
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.
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_json ⇒ String
Build json string to be stored in CAS.
60 61 62 |
# File 'lib/sidetree/model/chunk_file.rb', line 60 def to_json { deltas: deltas.map(&:to_h) }.to_json end |