Class: C0::Stream::Reader

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/c0/stream.rb

Overview

Scans a buffer for commits. An unescaped ETB is always a commit, even inside open text or a level. A committed block that is not well-formed, cut by its ETB or holding any other error, is damaged: it delivers nothing and adds nothing to the parse state. By default the reader delivers the records before the first damaged block and then raises DamagedBlockError; with continue_past_damage: true it skips the block and reads on. The tail after the last commit is torn unless it holds only layout, notes, EOT or EM. Enumerates the committed blocks' bytes.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buf, continue_past_damage: false) ⇒ Reader

Returns a new instance of Reader.



36
37
38
39
40
# File 'lib/c0/stream.rb', line 36

def initialize(buf, continue_past_damage: false)
  @buf = C0.to_bin(buf)
  @continue_past_damage = continue_past_damage
  @committed_end, @torn, @blocks, @first_damaged, @resume_offset = Ext.stream(@buf)
end

Instance Attribute Details

#buf ⇒ Object (readonly)

Returns the value of attribute buf.



34
35
36
# File 'lib/c0/stream.rb', line 34

def buf
  @buf
end

#committed_end ⇒ Object (readonly)

Returns the value of attribute committed_end.



34
35
36
# File 'lib/c0/stream.rb', line 34

def committed_end
  @committed_end
end

#first_damaged ⇒ Object (readonly)

Returns the value of attribute first_damaged.



34
35
36
# File 'lib/c0/stream.rb', line 34

def first_damaged
  @first_damaged
end

#resume_offset ⇒ Object (readonly)

Returns the value of attribute resume_offset.



34
35
36
# File 'lib/c0/stream.rb', line 34

def resume_offset
  @resume_offset
end

Class Method Details

.read(path, continue_past_damage: false) ⇒ Object



42
43
44
# File 'lib/c0/stream.rb', line 42

def self.read(path, continue_past_damage: false)
  new(File.binread(path), continue_past_damage: continue_past_damage)
end

Instance Method Details

#block(index) ⇒ Object

Block i's bytes (previous commit to ETB, marker and payload excluded).



61
62
63
64
# File 'lib/c0/stream.rb', line 61

def block(index)
  s, e, = @blocks[index]
  s && @buf.byteslice(s, e - s)
end

#block_count ⇒ Object Also known as: size



56
# File 'lib/c0/stream.rb', line 56

def block_count = @blocks.size

#block_end(index) ⇒ Object

Offset after block i's ETB and payload: where the next block starts.



73
# File 'lib/c0/stream.rb', line 73

def block_end(index) = index + 1 < @blocks.size ? @blocks[index + 1][0] : @committed_end

#blocks ⇒ Object



66
# File 'lib/c0/stream.rb', line 66

def blocks = @blocks.map { |s, e, _| @buf.byteslice(s, e - s) }

#body ⇒ Object



93
# File 'lib/c0/stream.rb', line 93

def body = document.body

#committed ⇒ Object

The bytes up to and including the last commit.



51
# File 'lib/c0/stream.rb', line 51

def committed = @buf.byteslice(0, @committed_end)

#container ⇒ Object

The committed region as a Container of documents, damaged blocks left out. Raises DamagedBlockError when a block is damaged, unless continuing.



85
86
87
88
# File 'lib/c0/stream.rb', line 85

def container
  check_damage
  Container.new(Ext.stream_undamaged(@buf))
end

#continue_past_damage? ⇒ Boolean

Returns:

  • (Boolean)


48
# File 'lib/c0/stream.rb', line 48

def continue_past_damage? = @continue_past_damage

#damaged ⇒ Object

Indexes of the damaged blocks.



76
# File 'lib/c0/stream.rb', line 76

def damaged = @blocks.each_index.select { |i| @blocks[i][2] }

#damaged?(index) ⇒ Boolean

Returns:

  • (Boolean)


78
# File 'lib/c0/stream.rb', line 78

def damaged?(index) = @blocks[index] ? @blocks[index][2] : false

#document ⇒ Object

The first document of the committed region.



91
# File 'lib/c0/stream.rb', line 91

def document = container.first || Document.new("".b)

#each_block ⇒ Object Also known as: each



68
# File 'lib/c0/stream.rb', line 68

def each_block(&) = blocks.each(&)

#each_record ⇒ Object

Every committed record in order, across documents and groups, up to the first damaged block; then DamagedBlockError, unless continuing, in which case every record of every sound block.



104
105
106
107
108
109
110
111
112
113
# File 'lib/c0/stream.rb', line 104

def each_record
  return enum_for(:each_record) unless block_given?

  if has_damaged? && !@continue_past_damage
    yield_records(Container.new(Ext.stream_before_damage(@buf))) { |r| yield r }
    raise DamagedBlockError.new(@first_damaged, @resume_offset)
  end
  yield_records(container) { |r| yield r }
  nil
end

#has_damaged? ⇒ Boolean

Whether any committed block is damaged.

Returns:

  • (Boolean)


81
# File 'lib/c0/stream.rb', line 81

def has_damaged? = !@first_damaged.nil?

#records ⇒ Object



115
# File 'lib/c0/stream.rb', line 115

def records = each_record.to_a

#table ⇒ Object

The committed records as a group (the first group, or an empty one).



96
97
98
99
# File 'lib/c0/stream.rb', line 96

def table
  b = body
  b.groups.first || Group.new("".b, 0, 0, 0, 0, 0)
end

#tail ⇒ Object

The bytes after the last commit.



54
# File 'lib/c0/stream.rb', line 54

def tail = @buf.byteslice(@committed_end, @buf.bytesize - @committed_end)

#torn? ⇒ Boolean

Returns:

  • (Boolean)


46
# File 'lib/c0/stream.rb', line 46

def torn? = @torn