hypertabular-wasm 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE-UNICODE +39 -0
- data/README.md +278 -34
- data/ext/hypertabular_native/3.4/hypertabular_native.a.gz +0 -0
- data/ext/hypertabular_native/4.0/hypertabular_native.a.gz +0 -0
- data/lib/hypertabular/batch.rb +219 -24
- data/lib/hypertabular/column.rb +166 -21
- data/lib/hypertabular/delimited_reader.rb +319 -94
- data/lib/hypertabular/dialect.rb +92 -10
- data/lib/hypertabular/guarded.rb +116 -0
- data/lib/hypertabular/header.rb +59 -22
- data/lib/hypertabular/row.rb +17 -4
- data/lib/hypertabular/runtime/columns.rb +78 -12
- data/lib/hypertabular/runtime/delimited.rb +63 -37
- data/lib/hypertabular/runtime/workbook.rb +39 -15
- data/lib/hypertabular/runtime.rb +255 -17
- data/lib/hypertabular/suggest.rb +283 -0
- data/lib/hypertabular/tabular_error.rb +9 -3
- data/lib/hypertabular/workbook.rb +185 -45
- data/lib/hypertabular.rb +14 -8
- metadata +7 -3
data/lib/hypertabular/dialect.rb
CHANGED
|
@@ -1,7 +1,37 @@
|
|
|
1
1
|
module HyperTabular
|
|
2
|
+
# What a declaration's fields are held to, shared by Dialect, SheetOptions, Range and Column.
|
|
3
|
+
module Declared
|
|
4
|
+
module_function
|
|
5
|
+
|
|
6
|
+
# +value+ if it is a count an unsigned 32-bit field holds; an ArgumentError naming +field+
|
|
7
|
+
# otherwise.
|
|
8
|
+
def count(field, value)
|
|
9
|
+
return value if value.is_a?(Integer) && value.between?(0, 0xFFFF_FFFF)
|
|
10
|
+
|
|
11
|
+
raise ArgumentError, "#{field} must be an Integer from 0 to 2^32 - 1; got #{value.inspect}"
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# +list+ as a frozen Array of frozen Strings, each the UTF-8 the core compares a cell's bytes
|
|
15
|
+
# with, or an ArgumentError: a lone String is too easy a mistake to take a letter at a time.
|
|
16
|
+
def tokens(list)
|
|
17
|
+
raise ArgumentError, "tokens must be an Array of Strings; got #{list.inspect}" unless list.is_a?(Array)
|
|
18
|
+
|
|
19
|
+
list.map do |token|
|
|
20
|
+
raise ArgumentError, "a token must be a String; got #{token.inspect}" unless token.is_a?(String)
|
|
21
|
+
|
|
22
|
+
token.encoding == Encoding::BINARY ? -token.dup.force_encoding(Encoding::UTF_8) : -token.encode(Encoding::UTF_8)
|
|
23
|
+
end.freeze
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# A token list as the core takes it: each a little-endian u32 length, then its bytes.
|
|
27
|
+
def packed_tokens(list)
|
|
28
|
+
list.map { |token| [token.bytesize].pack("L<") + token.b }.join.b
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
2
32
|
# How the text is delimited, declared by the caller. Nothing is sniffed: the separator is
|
|
3
|
-
# stated, quoting is stated, the header is stated — the same stance
|
|
4
|
-
# takes for numeric notation.
|
|
33
|
+
# stated, quoting is stated, the header is stated, the encoding is stated — the same stance
|
|
34
|
+
# HyperCast's NumFormat takes for numeric notation.
|
|
5
35
|
#
|
|
6
36
|
# +separator+ is one byte: tab, or any printable ASCII character except the double quote.
|
|
7
37
|
# +quoting+ says whether " quotes cells (RFC 4180, "" for a literal quote); off, a quote
|
|
@@ -9,28 +39,80 @@ module HyperTabular
|
|
|
9
39
|
# DelimitedReader#header and never delivered as a row. +skip_blank_lines+ says whether a
|
|
10
40
|
# completely empty line is skipped rather than read as a one-cell row.
|
|
11
41
|
#
|
|
42
|
+
# +skip+ is how many physical lines to pass over after a byte-order mark, before anything is
|
|
43
|
+
# read of them, so a quote in one means nothing. +header_row+ is how many records to discard
|
|
44
|
+
# after those, ahead of the header if there is one; a blank line this dialect skips is not a
|
|
45
|
+
# record. +short_rows+ says what a record with fewer cells than the width is: :reject, a
|
|
46
|
+
# TabularError, or :pad, a row whose missing cells are :empty Faults at Origin :padded.
|
|
47
|
+
# +long_rows+ says what a record with more cells is: :reject, :report (a row cut to the width,
|
|
48
|
+
# and listed by Batch#faults with the width it had) or :truncate (cut, and nothing said).
|
|
49
|
+
# +encoding+ is what the input's bytes are, never sniffed: :utf8, read in place; :windows_1252;
|
|
50
|
+
# :latin1; or :utf16le, with or without a byte-order mark. Anything but UTF-8 is decoded ahead
|
|
51
|
+
# of the read, so a batch's text and every span are the decoded text's, and it is the source's
|
|
52
|
+
# bytes that are decoded, whatever encoding a String source says it is in. +tokens+ is what
|
|
53
|
+
# stands for no value in every column without tokens of its own: a cell whose exact, unquoted
|
|
54
|
+
# bytes are one of these is an :empty Fault, whatever its door, at Origin :token.
|
|
55
|
+
#
|
|
12
56
|
# HyperTabular::Dialect::CSV
|
|
13
57
|
# HyperTabular::Dialect.new(separator: ";", has_header: false)
|
|
14
|
-
Dialect
|
|
58
|
+
# HyperTabular::Dialect.new(separator: ",", skip: 2, short_rows: :pad, tokens: ["N/A"])
|
|
59
|
+
Dialect = Data.define(:separator, :quoting, :has_header, :skip_blank_lines, :skip, :header_row, :short_rows,
|
|
60
|
+
:long_rows, :encoding, :tokens) do
|
|
15
61
|
# Everything but the separator defaults to what CSV means: quoted, with a header, blank
|
|
16
|
-
# lines skipped. A separator that is not a
|
|
17
|
-
#
|
|
18
|
-
#
|
|
19
|
-
|
|
62
|
+
# lines skipped, every record the same width, UTF-8, no tokens. A separator that is not a
|
|
63
|
+
# one-byte String, or any other field that is not what it says above, is a caller bug
|
|
64
|
+
# (ArgumentError); which bytes the scanner can honour is the core's to say, and it says so
|
|
65
|
+
# when a reader is built.
|
|
66
|
+
def initialize(separator:, quoting: true, has_header: true, skip_blank_lines: true, skip: 0, header_row: 0,
|
|
67
|
+
short_rows: :reject, long_rows: :reject, encoding: :utf8, tokens: [])
|
|
20
68
|
raise ArgumentError, "separator must be a one-byte String; got #{separator.inspect}" unless
|
|
21
69
|
separator.is_a?(String) && separator.bytesize == 1
|
|
22
70
|
|
|
71
|
+
{ short_rows: [short_rows, Dialect::SHORT_ROWS], long_rows: [long_rows, Dialect::LONG_ROWS],
|
|
72
|
+
encoding: [encoding, Dialect::ENCODINGS] }.each do |field, (value, known)|
|
|
73
|
+
raise ArgumentError, "#{field} must be one of #{known.keys.inspect}; got #{value.inspect}" unless
|
|
74
|
+
known.key?(value)
|
|
75
|
+
end
|
|
76
|
+
|
|
23
77
|
super(separator: -separator, quoting: quoting ? true : false,
|
|
24
|
-
has_header: has_header ? true : false, skip_blank_lines: skip_blank_lines ? true : false
|
|
78
|
+
has_header: has_header ? true : false, skip_blank_lines: skip_blank_lines ? true : false,
|
|
79
|
+
skip: Declared.count("skip", skip), header_row: Declared.count("header_row", header_row),
|
|
80
|
+
short_rows: short_rows, long_rows: long_rows, encoding: encoding, tokens: Declared.tokens(tokens))
|
|
25
81
|
end
|
|
26
82
|
|
|
27
|
-
# The four bytes of the core's RawDialect: separator, quoting, skip_blank_lines, and
|
|
28
|
-
# the scan engine left to the core's own choice.
|
|
83
|
+
# The four bytes of the core's 1.0 RawDialect: separator, quoting, skip_blank_lines, and
|
|
84
|
+
# the scan engine left to the core's own choice. No read takes it; it is what the 1.0 exports
|
|
85
|
+
# stay proved with.
|
|
29
86
|
def packed
|
|
30
87
|
[separator.getbyte(0), quoting ? 1 : 0, skip_blank_lines ? 1 : 0, 0].pack("C4")
|
|
31
88
|
end
|
|
89
|
+
|
|
90
|
+
# The 24 bytes of the core's RawDelimitedOptions, what every read starts from. The encoding
|
|
91
|
+
# is not in it: decoding happens ahead of the fill.
|
|
92
|
+
def packed_with
|
|
93
|
+
[Dialect::OPTIONS_BYTES, separator.getbyte(0), quoting ? 1 : 0, skip_blank_lines ? 1 : 0, 0, skip, header_row,
|
|
94
|
+
Dialect::SHORT_ROWS.fetch(short_rows), Dialect::LONG_ROWS.fetch(long_rows)].pack("L<C4L<4")
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# The tokens as the core takes them, or nil for none.
|
|
98
|
+
def packed_tokens
|
|
99
|
+
tokens.empty? ? nil : Declared.packed_tokens(tokens)
|
|
100
|
+
end
|
|
32
101
|
end
|
|
33
102
|
|
|
103
|
+
# The size of Dialect#packed_with: what RawDelimitedOptions' first word carries.
|
|
104
|
+
Dialect::OPTIONS_BYTES = 24
|
|
105
|
+
|
|
106
|
+
# What a record with fewer cells than the width can be, by the core's code.
|
|
107
|
+
Dialect::SHORT_ROWS = { reject: 0, pad: 1 }.freeze
|
|
108
|
+
|
|
109
|
+
# What a record with more cells than the width can be, by the core's code.
|
|
110
|
+
Dialect::LONG_ROWS = { reject: 0, report: 1, truncate: 2 }.freeze
|
|
111
|
+
|
|
112
|
+
# What a delimited file's bytes can be declared as, by the core's transcoding code: UTF-8 makes
|
|
113
|
+
# no call.
|
|
114
|
+
Dialect::ENCODINGS = { utf8: 0, windows_1252: 1, latin1: 2, utf16le: 3 }.freeze
|
|
115
|
+
|
|
34
116
|
# Comma-separated, quoted, with a header, blank lines skipped.
|
|
35
117
|
Dialect::CSV = Dialect.new(separator: ",")
|
|
36
118
|
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
module HyperTabular
|
|
2
|
+
# What a DelimitedReader and a Sheet share to stay whole while the caller's code runs in the middle
|
|
3
|
+
# of a call (an IO's read, a plan's #each) or an exception lands in one (Timeout.timeout,
|
|
4
|
+
# Thread#raise, Ctrl-C's Interrupt):
|
|
5
|
+
#
|
|
6
|
+
# - One call at a time. A call that the caller's code makes back into the same object, from inside
|
|
7
|
+
# one of its calls, is refused as busy, and the call it came from goes on as it was.
|
|
8
|
+
# - What the core did and what the object says of it move together. Asynchronous exceptions wait
|
|
9
|
+
# while they move (Thread.handle_interrupt), and land after. The caller's IO is read outside that
|
|
10
|
+
# wait, so a Timeout still breaks a read that blocks, and leaves the reader refusing every later
|
|
11
|
+
# read, since that read may have lost what it had taken from the IO.
|
|
12
|
+
# - A batch that is made is held until it is handed over. An exception that was waiting lands with
|
|
13
|
+
# the batch still held, and the next read hands it over, numbered the same; so does one that
|
|
14
|
+
# lands as the read returns it, anywhere short of the caller's own code. An exception raised
|
|
15
|
+
# inside the wait once the core has moved on, which nothing can undo, leaves the object refusing
|
|
16
|
+
# every later read, saying why.
|
|
17
|
+
module Guarded
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
# What #hand_over's wait answers when an exception is waiting to land.
|
|
21
|
+
NOT_YET = Object.new.freeze
|
|
22
|
+
private_constant :NOT_YET
|
|
23
|
+
|
|
24
|
+
# Runs the block as the one call into this object. A call it makes back in raises RuntimeError,
|
|
25
|
+
# naming the object busy; nothing it leaves behind is touched.
|
|
26
|
+
def exclusive
|
|
27
|
+
raise "the #{noun} is in the middle of a call; nothing that call runs can call back into it" if @busy
|
|
28
|
+
|
|
29
|
+
begin
|
|
30
|
+
@busy = true
|
|
31
|
+
yield
|
|
32
|
+
ensure
|
|
33
|
+
Thread.handle_interrupt(Object => :never) { @busy = false }
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Runs the block with every asynchronous exception held until it is done.
|
|
38
|
+
def atomically(&)
|
|
39
|
+
Thread.handle_interrupt(Object => :never, &)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Runs the block with every asynchronous exception held except while it blocks, as on a read of a
|
|
43
|
+
# pipe that has nothing yet: one lands there, and nowhere else until the block is done.
|
|
44
|
+
def while_blocked(&)
|
|
45
|
+
Thread.handle_interrupt(Object => :on_blocking, &)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Runs the block as a read, the one call into this object (#exclusive): if an exception unwinds
|
|
49
|
+
# it after #hand_over has let go of a batch, that batch is held again for the next read, since it
|
|
50
|
+
# never reached the caller.
|
|
51
|
+
def handing_out
|
|
52
|
+
owned = false
|
|
53
|
+
exclusive do
|
|
54
|
+
# In this order: the batch the last read handed out did reach its caller.
|
|
55
|
+
@in_flight = nil
|
|
56
|
+
owned = true
|
|
57
|
+
yield
|
|
58
|
+
end
|
|
59
|
+
rescue Exception
|
|
60
|
+
Thread.handle_interrupt(Object => :never) { give_back if owned }
|
|
61
|
+
raise
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Yields every remaining batch, as #each and its kin do. An exception that lands between a read
|
|
65
|
+
# and the yield gives that batch back for the next read; one from the block, which has had it,
|
|
66
|
+
# gives nothing back.
|
|
67
|
+
def batches
|
|
68
|
+
between = false
|
|
69
|
+
loop do
|
|
70
|
+
between = true
|
|
71
|
+
batch = read
|
|
72
|
+
between = false
|
|
73
|
+
break if batch.nil?
|
|
74
|
+
|
|
75
|
+
yield batch
|
|
76
|
+
end
|
|
77
|
+
self
|
|
78
|
+
rescue Exception
|
|
79
|
+
Thread.handle_interrupt(Object => :never) { give_back if between }
|
|
80
|
+
raise
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# The batch held for the caller, let go of. An asynchronous exception waiting to land lands first,
|
|
84
|
+
# with the batch still held for the next read; one the caller holds back itself lands in the
|
|
85
|
+
# caller's own code, after this.
|
|
86
|
+
def hand_over
|
|
87
|
+
handed = atomically { Thread.pending_interrupt? ? NOT_YET : let_go }
|
|
88
|
+
return handed unless handed.equal?(NOT_YET)
|
|
89
|
+
|
|
90
|
+
atomically { let_go }
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# The held batch, let go of and kept in flight until the read that hands it out is over.
|
|
94
|
+
def let_go
|
|
95
|
+
batch = @handing
|
|
96
|
+
@handing = nil
|
|
97
|
+
@in_flight = batch
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# The batch in flight, held again.
|
|
101
|
+
def give_back
|
|
102
|
+
atomically do
|
|
103
|
+
@handing ||= @in_flight
|
|
104
|
+
@in_flight = nil
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# The failure that stands for +error+, raised inside the wait once the core had moved on: what
|
|
109
|
+
# the core read is lost, so nothing after it can be read right.
|
|
110
|
+
def spoiled(error)
|
|
111
|
+
RuntimeError.new("the #{noun} can't read on: #{error.class} (#{error.message}) was raised after the core " \
|
|
112
|
+
"had read past a batch, and that batch is lost")
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
private_constant :Guarded
|
|
116
|
+
end
|
data/lib/hypertabular/header.rb
CHANGED
|
@@ -9,44 +9,81 @@ module HyperTabular
|
|
|
9
9
|
# reader.bind([HyperTabular::Column.text(header.ordinal("name")),
|
|
10
10
|
# HyperTabular::Column.f64(header.ordinal("score"))])
|
|
11
11
|
#
|
|
12
|
-
# A name matches exactly — byte for byte, case and spaces included
|
|
13
|
-
#
|
|
12
|
+
# A name matches exactly — byte for byte, case and spaces included. A name the header has twice
|
|
13
|
+
# is refused by #ordinal, because a plan built from names mustn't silently read the wrong
|
|
14
|
+
# column, and found where it is first by #find_ordinal.
|
|
14
15
|
class Header < Array
|
|
15
|
-
# The ordinal of the
|
|
16
|
-
#
|
|
17
|
-
#
|
|
16
|
+
# The ordinal of the column named +name+ (a String). A name the header does not have is a
|
|
17
|
+
# KeyError that says which — or, with a block, the block's value, the block handed the name,
|
|
18
|
+
# as Hash#fetch does. A name two columns share is an AmbiguousColumn, block or no block: the
|
|
19
|
+
# block answers a missing name, and a duplicate isn't one.
|
|
18
20
|
#
|
|
19
21
|
# header.ordinal("id") # => 0
|
|
20
22
|
# header.ordinal("missing") # KeyError: the header has no column named "missing"
|
|
21
23
|
# header.ordinal("missing") { nil } # => nil
|
|
24
|
+
# HyperTabular::Header["id", "id"].ordinal("id")
|
|
25
|
+
# # AmbiguousColumn: the header names two columns "id", at positions 0 and 1: say which by position
|
|
22
26
|
#
|
|
23
27
|
# A binary String is matched as the bytes it holds; a String in another encoding is
|
|
24
28
|
# matched as its UTF-8 transcoding. TypeError for anything that is not a String.
|
|
25
29
|
def ordinal(name)
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
30
|
+
wanted = wanted(name)
|
|
31
|
+
found = wanted && index(wanted)
|
|
32
|
+
if found.nil?
|
|
33
|
+
return yield(name) if block_given?
|
|
29
34
|
|
|
30
|
-
|
|
35
|
+
raise KeyError.new("the header has no column named #{name.inspect}", receiver: self, key: name)
|
|
36
|
+
end
|
|
37
|
+
(found + 1...size).each { |at| raise AmbiguousColumn.new(wanted, found, at) if self[at] == wanted }
|
|
38
|
+
found
|
|
31
39
|
end
|
|
32
40
|
|
|
33
|
-
# The ordinal of the first column named +name+, or nil when the header has none:
|
|
34
|
-
#
|
|
41
|
+
# The ordinal of the first column named +name+, or nil when the header has none: the optional
|
|
42
|
+
# lookup, matched as #ordinal matches, which keeps the first of a name two columns share.
|
|
35
43
|
def find_ordinal(name)
|
|
44
|
+
wanted = wanted(name)
|
|
45
|
+
wanted && index(wanted)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
private
|
|
49
|
+
|
|
50
|
+
# +name+ as the UTF-8 the names are in, or nil if it has no such form.
|
|
51
|
+
def wanted(name)
|
|
36
52
|
raise TypeError, "a column name is a String; got #{name.class}" unless name.is_a?(String)
|
|
37
53
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
return nil
|
|
47
|
-
end
|
|
54
|
+
case name.encoding
|
|
55
|
+
when Encoding::UTF_8 then name
|
|
56
|
+
when Encoding::BINARY then name.dup.force_encoding(Encoding::UTF_8)
|
|
57
|
+
else
|
|
58
|
+
begin
|
|
59
|
+
name.encode(Encoding::UTF_8)
|
|
60
|
+
rescue EncodingError
|
|
61
|
+
nil
|
|
48
62
|
end
|
|
49
|
-
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# A column name the header gives to more than one column, asked for by Header#ordinal: which
|
|
68
|
+
# column it means is the caller's to say, by position. An ArgumentError, so that a rescue of the
|
|
69
|
+
# KeyError a missing name raises doesn't take it for one.
|
|
70
|
+
class AmbiguousColumn < ArgumentError
|
|
71
|
+
# The name.
|
|
72
|
+
attr_reader :name
|
|
73
|
+
|
|
74
|
+
# The zero-based position of the first column so named, counted from a range's first column
|
|
75
|
+
# on a ranged sheet, as a plan's ordinals are.
|
|
76
|
+
attr_reader :first
|
|
77
|
+
|
|
78
|
+
# The zero-based position of the second column so named.
|
|
79
|
+
attr_reader :second
|
|
80
|
+
|
|
81
|
+
# Raised by Header#ordinal; nothing else builds one.
|
|
82
|
+
def initialize(name, first, second)
|
|
83
|
+
@name = name
|
|
84
|
+
@first = first
|
|
85
|
+
@second = second
|
|
86
|
+
super("the header names two columns #{name.inspect}, at positions #{first} and #{second}: say which by position")
|
|
50
87
|
end
|
|
51
88
|
end
|
|
52
89
|
end
|
data/lib/hypertabular/row.rb
CHANGED
|
@@ -13,7 +13,8 @@ module HyperTabular
|
|
|
13
13
|
# end
|
|
14
14
|
#
|
|
15
15
|
# It deconstructs as its verdicts, one per plan column, so a whole row can be matched at
|
|
16
|
-
# once: `in [HyperCast::Success(value: id), HyperCast::Success(value: name)]`.
|
|
16
|
+
# once: `in [HyperCast::Success(value: id), HyperCast::Success(value: name)]`. A column that
|
|
17
|
+
# declares constraints is there as #get_checked says it.
|
|
17
18
|
class Row
|
|
18
19
|
# The batch the row is in.
|
|
19
20
|
attr_reader :batch
|
|
@@ -35,13 +36,19 @@ module HyperTabular
|
|
|
35
36
|
end
|
|
36
37
|
|
|
37
38
|
# The cell in plan column +column+ as HyperCast judged it — Batch#get. IndexError for a
|
|
38
|
-
# column outside the plan.
|
|
39
|
+
# column outside the plan, and TypeError for one that declares constraints.
|
|
39
40
|
def get(column)
|
|
40
41
|
@batch.get(column, @index)
|
|
41
42
|
end
|
|
42
43
|
alias [] get
|
|
43
44
|
alias verdict get
|
|
44
45
|
|
|
46
|
+
# The cell in plan column +column+ as HyperCast's checked door judged it — Batch#get_checked.
|
|
47
|
+
# IndexError for a column outside the plan.
|
|
48
|
+
def get_checked(column)
|
|
49
|
+
@batch.get_checked(column, @index)
|
|
50
|
+
end
|
|
51
|
+
|
|
45
52
|
# The cell's value, or nil where it did not cast — Batch#values' entry for this row.
|
|
46
53
|
# IndexError for a column outside the plan.
|
|
47
54
|
def value(column)
|
|
@@ -53,6 +60,11 @@ module HyperTabular
|
|
|
53
60
|
@batch.raw(column, @index)
|
|
54
61
|
end
|
|
55
62
|
|
|
63
|
+
# Where the cell's verdict came from — Batch#origin.
|
|
64
|
+
def origin(column)
|
|
65
|
+
@batch.origin(column, @index)
|
|
66
|
+
end
|
|
67
|
+
|
|
56
68
|
# How many cells the row has: one per plan column.
|
|
57
69
|
def size
|
|
58
70
|
@batch.columns.size
|
|
@@ -60,9 +72,10 @@ module HyperTabular
|
|
|
60
72
|
alias length size
|
|
61
73
|
|
|
62
74
|
# Every cell's verdict, in plan order, as a frozen Array — the Array
|
|
63
|
-
# DelimitedReader#each_row yields for the same row
|
|
75
|
+
# DelimitedReader#each_row yields for the same row: #get_checked's for a column that declares
|
|
76
|
+
# constraints, #get's for one that doesn't.
|
|
64
77
|
def to_a
|
|
65
|
-
Array.new(size) { |column|
|
|
78
|
+
Array.new(size) { |column| @batch.send(:verdict_of, column, @index) }.freeze
|
|
66
79
|
end
|
|
67
80
|
alias deconstruct to_a
|
|
68
81
|
|
|
@@ -7,29 +7,79 @@ module HyperTabular
|
|
|
7
7
|
class Columns
|
|
8
8
|
# Bytes in one verdict (CellVerdict: offset, len, reason — three u32).
|
|
9
9
|
VERDICT_BYTES = 12
|
|
10
|
-
#
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
# Bytes in one CellNote: a detail and an origin, two u16.
|
|
11
|
+
NOTE_BYTES = 4
|
|
12
|
+
# Bytes in one row width.
|
|
13
|
+
WIDTH_BYTES = 4
|
|
14
|
+
# Bytes in one RawCheck: the keyword, adjusted and the enum index, three u32.
|
|
15
|
+
CHECK_BYTES = 12
|
|
16
|
+
# Bytes in a pointer.
|
|
17
|
+
WORD = [0].pack("J").bytesize
|
|
18
|
+
# A ColumnBufferWith: its size, then the values, the verdicts, the notes and the checks.
|
|
19
|
+
COLUMN_BUFFER_BYTES = 5 * WORD
|
|
20
|
+
# A BuffersWith: its size, then eight pointer and size pairs, then the row widths.
|
|
21
|
+
BUFFERS_WITH_BYTES = 18 * WORD
|
|
13
22
|
|
|
14
|
-
# The packed specs (nil for no columns), the
|
|
23
|
+
# The packed specs (nil for no columns), the ColumnBufferWith table (likewise), how many
|
|
15
24
|
# columns there are, and the most rows a batch holds.
|
|
16
25
|
attr_reader :specs, :table, :count, :batch_rows
|
|
17
26
|
|
|
18
|
-
#
|
|
19
|
-
|
|
20
|
-
|
|
27
|
+
# The size word of a size-tagged struct, in the pointer-wide slot it starts.
|
|
28
|
+
def self.word(size)
|
|
29
|
+
[size].pack("L<").ljust(WORD, "\0")
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# One ColumnBufferWith, for its arrays. A null values array is for a call that reads none,
|
|
33
|
+
# and null checks for a column that declares no constraints.
|
|
34
|
+
def self.column_buffer(values, verdicts, notes, checks = nil)
|
|
35
|
+
word(COLUMN_BUFFER_BYTES) + [values.to_i, verdicts.to_i, notes.to_i, checks.to_i].pack("J4")
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# The whole ColumnSpecWith for an entry of Column.specs. Each list goes in +lists+, which the
|
|
39
|
+
# caller keeps for as long as the bytes are used. Null tokens inherit, so an empty list still
|
|
40
|
+
# needs an address. No constraints: their pointer and the enum's are null.
|
|
41
|
+
def self.spec_with(spec, lists)
|
|
42
|
+
fixed, months, tokens, constraints, packed_enum = spec
|
|
43
|
+
held = [months, tokens, constraints, packed_enum].map do |list|
|
|
44
|
+
next [0, 0] if list.nil?
|
|
45
|
+
|
|
46
|
+
buffer = Runtime.buffer([list.bytesize, 1].max)
|
|
47
|
+
buffer[0, list.bytesize] = list unless list.empty?
|
|
48
|
+
lists << buffer
|
|
49
|
+
[buffer.to_i, list.bytesize]
|
|
50
|
+
end
|
|
51
|
+
fixed + held.values_at(0, 1).flatten.pack("J4") + [held[2][0], *held[3]].pack("J3")
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# +specs+ is one Column.specs entry per plan column and +sizes+ the bytes one value of each
|
|
55
|
+
# takes. +notes+ says whether each column keeps a CellNote per row, and +widths+ whether
|
|
56
|
+
# each row's width is kept.
|
|
57
|
+
def initialize(specs, sizes, batch_rows, notes, widths)
|
|
21
58
|
@sizes = sizes
|
|
22
59
|
@count = specs.size
|
|
23
60
|
@batch_rows = batch_rows
|
|
61
|
+
@row_widths = Runtime.buffer(WIDTH_BYTES * batch_rows) if widths
|
|
24
62
|
return if specs.empty?
|
|
25
63
|
|
|
26
|
-
@
|
|
27
|
-
|
|
64
|
+
@lists = []
|
|
65
|
+
packed = specs.map { |spec| Columns.spec_with(spec, @lists) }.join
|
|
66
|
+
@specs = Runtime.buffer(packed.bytesize)
|
|
67
|
+
@specs[0, packed.bytesize] = packed
|
|
28
68
|
@values = sizes.map { |size| Runtime.buffer(size * batch_rows) }
|
|
29
69
|
@verdicts = sizes.map { Runtime.buffer(VERDICT_BYTES * batch_rows) }
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
@
|
|
70
|
+
@notes = sizes.map { Runtime.buffer(NOTE_BYTES * batch_rows) } if notes
|
|
71
|
+
# As many checks as verdicts, for each column that declares constraints.
|
|
72
|
+
@checks = specs.map { |spec| spec[3] && Runtime.buffer(CHECK_BYTES * batch_rows) }
|
|
73
|
+
entries = @count.times.map do |at|
|
|
74
|
+
Columns.column_buffer(@values[at], @verdicts[at], @notes && @notes[at], @checks[at])
|
|
75
|
+
end.join
|
|
76
|
+
@table = Runtime.buffer(entries.bytesize)
|
|
77
|
+
@table[0, entries.bytesize] = entries
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# The row widths' address, or 0 for none.
|
|
81
|
+
def widths_address
|
|
82
|
+
@row_widths.to_i
|
|
33
83
|
end
|
|
34
84
|
|
|
35
85
|
# A column's value array for the first +rows+ rows, copied out as the core wrote it.
|
|
@@ -42,6 +92,22 @@ module HyperTabular
|
|
|
42
92
|
def verdicts(column, rows)
|
|
43
93
|
@verdicts[column][0, VERDICT_BYTES * rows]
|
|
44
94
|
end
|
|
95
|
+
|
|
96
|
+
# A column's CellNote array for the first +rows+ rows, copied out as the core wrote it.
|
|
97
|
+
def notes(column, rows)
|
|
98
|
+
@notes[column][0, NOTE_BYTES * rows]
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# A column's RawCheck array for the first +rows+ rows, copied out as the core wrote it, or nil
|
|
102
|
+
# for a column that declares no constraints.
|
|
103
|
+
def checks(column, rows)
|
|
104
|
+
@checks[column]&.[](0, CHECK_BYTES * rows)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# The widths of the first +rows+ rows, copied out as the core wrote them.
|
|
108
|
+
def widths(rows)
|
|
109
|
+
@row_widths[0, WIDTH_BYTES * rows]
|
|
110
|
+
end
|
|
45
111
|
end
|
|
46
112
|
end
|
|
47
113
|
end
|