xitdb 0.1.0 → 0.2.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/CHANGELOG.md +16 -0
- data/README.md +15 -3
- data/docs/benchmarks.md +4 -1
- data/docs/core.md +15 -2
- data/docs/learning.md +211 -0
- data/docs/native-profile-performance.md +254 -0
- data/docs/ruby-profile-v1.md +2 -0
- data/docs/ruby-profile-v2.md +279 -0
- data/docs/ruby-profile-v3.md +53 -0
- data/docs/ruby-profile-v4.md +67 -0
- data/docs/ruby.md +26 -3
- data/examples/indexed_users.rb +3 -0
- data/examples/portable.rb +3 -0
- data/examples/ruby.rb +2 -0
- data/lib/xitdb/collection_declaration.rb +39 -0
- data/lib/xitdb/context.rb +154 -8
- data/lib/xitdb/conversion/byte_source.rb +112 -0
- data/lib/xitdb/conversion.rb +35 -73
- data/lib/xitdb/core/array_builder.rb +74 -0
- data/lib/xitdb/core/array_list.rb +92 -8
- data/lib/xitdb/core/b_tree.rb +127 -19
- data/lib/xitdb/core/byte_value.rb +33 -0
- data/lib/xitdb/core/bytes/source.rb +59 -0
- data/lib/xitdb/core/bytes.rb +54 -58
- data/lib/xitdb/core/compaction.rb +49 -7
- data/lib/xitdb/core/crc64.rb +8 -1
- data/lib/xitdb/core/cursor.rb +108 -18
- data/lib/xitdb/core/database.rb +163 -29
- data/lib/xitdb/core/file_store.rb +43 -1
- data/lib/xitdb/core/format.rb +39 -98
- data/lib/xitdb/core/hash_map.rb +119 -17
- data/lib/xitdb/core/hasher.rb +33 -2
- data/lib/xitdb/core/header.rb +98 -0
- data/lib/xitdb/core/linked_array_list.rb +70 -1
- data/lib/xitdb/core/memory_store.rb +23 -224
- data/lib/xitdb/core/offset_map.rb +22 -1
- data/lib/xitdb/core/record.rb +64 -0
- data/lib/xitdb/core/replication.rb +108 -25
- data/lib/xitdb/core/slot.rb +123 -0
- data/lib/xitdb/core/sorted_map/tree.rb +317 -0
- data/lib/xitdb/core/sorted_map.rb +78 -256
- data/lib/xitdb/core/store.rb +286 -0
- data/lib/xitdb/core/transaction.rb +84 -6
- data/lib/xitdb/core.rb +12 -0
- data/lib/xitdb/database.rb +194 -15
- data/lib/xitdb/declarations.rb +33 -33
- data/lib/xitdb/errors.rb +15 -0
- data/lib/xitdb/history.rb +21 -0
- data/lib/xitdb/input_guard.rb +15 -1
- data/lib/xitdb/profiles/portable/adapter.rb +380 -0
- data/lib/xitdb/profiles/portable.rb +129 -243
- data/lib/xitdb/profiles/ruby.rb +206 -24
- data/lib/xitdb/revision.rb +17 -0
- data/lib/xitdb/ruby/codec.rb +61 -54
- data/lib/xitdb/ruby/codec_registry.rb +79 -0
- data/lib/xitdb/ruby/containers.rb +355 -82
- data/lib/xitdb/ruby/encoded_value.rb +38 -0
- data/lib/xitdb/ruby/indexed_page_decoder.rb +72 -0
- data/lib/xitdb/ruby/indexed_page_encoder.rb +58 -0
- data/lib/xitdb/ruby/input.rb +55 -0
- data/lib/xitdb/ruby/key_encoding.rb +43 -5
- data/lib/xitdb/ruby/ordered_keys.rb +46 -3
- data/lib/xitdb/ruby/packed_array.rb +179 -0
- data/lib/xitdb/ruby/packed_map.rb +272 -0
- data/lib/xitdb/ruby/packing_state.rb +84 -0
- data/lib/xitdb/ruby/page_array.rb +218 -0
- data/lib/xitdb/ruby/page_cell.rb +20 -0
- data/lib/xitdb/ruby/page_codec.rb +46 -0
- data/lib/xitdb/ruby/page_decoder.rb +172 -0
- data/lib/xitdb/ruby/page_draft.rb +24 -0
- data/lib/xitdb/ruby/page_encoder.rb +149 -0
- data/lib/xitdb/ruby/page_reference.rb +26 -0
- data/lib/xitdb/ruby/page_row.rb +108 -0
- data/lib/xitdb/ruby/pages.rb +94 -0
- data/lib/xitdb/ruby/scalars.rb +175 -33
- data/lib/xitdb/ruby/wire/reader.rb +138 -0
- data/lib/xitdb/ruby/wire.rb +36 -90
- data/lib/xitdb/ruby.rb +43 -3
- data/lib/xitdb/snapshot.rb +8 -0
- data/lib/xitdb/streams/handle.rb +46 -0
- data/lib/xitdb/streams/reader.rb +122 -0
- data/lib/xitdb/streams/writer.rb +43 -0
- data/lib/xitdb/streams.rb +8 -140
- data/lib/xitdb/uint.rb +17 -0
- data/lib/xitdb/value_walker.rb +86 -16
- data/lib/xitdb/version.rb +2 -1
- data/lib/xitdb/views/array.rb +94 -74
- data/lib/xitdb/views/base.rb +144 -12
- data/lib/xitdb/views/linked_array.rb +7 -1
- data/lib/xitdb/views/map.rb +71 -3
- data/lib/xitdb/views/set.rb +13 -1
- data/lib/xitdb/views/slice.rb +101 -0
- data/lib/xitdb/views/sorted.rb +25 -37
- data/lib/xitdb/views/sorted_map.rb +39 -0
- data/lib/xitdb/views/sorted_set.rb +10 -0
- data/lib/xitdb.rb +3 -0
- data/sig/core.rbs +65 -34
- data/sig/public.rbs +24 -19
- data/sig/ruby.rbs +260 -42
- metadata +42 -1
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
# Ruby profile version 2 wire contract
|
|
2
|
+
|
|
3
|
+
This contract defines the `rb02` profile and the Core record extension to upstream format version 1.
|
|
4
|
+
|
|
5
|
+
## File identity
|
|
6
|
+
|
|
7
|
+
The header has hash size 32 and hash identifier `rb02`. Hash indexes use SHA-256 over the canonical key bytes defined below.
|
|
8
|
+
|
|
9
|
+
The `rb01` profile is incompatible. Native handles reject it without modifying the file. There is no backward reader or migration.
|
|
10
|
+
|
|
11
|
+
The physical root is an upstream array list of history entries. Each entry references an encoded logical root map.
|
|
12
|
+
|
|
13
|
+
Portable files retain their upstream header and layout. Pinned Go, TypeScript, Java, and Zig implementations do not support the record extension or native `rb02` containers.
|
|
14
|
+
|
|
15
|
+
Ruby scalar payloads retain version `01` and their literal wire bytes. References can preserve these opaque payloads inside portable Core graphs.
|
|
16
|
+
|
|
17
|
+
## Scalar notation
|
|
18
|
+
|
|
19
|
+
- W1: `U(n)` is minimal unsigned LEB128 for a nonnegative integer.
|
|
20
|
+
- W2: `B(bytes)` is `U(bytes.bytesize)` followed by those bytes.
|
|
21
|
+
- W3: `T(text)` is `B(text.encode("UTF-8"))`, with valid UTF-8 required.
|
|
22
|
+
- W4: `Z(n)` is one sign byte, then `B(magnitude)`. The sign is 0 for nonnegative and 1 for negative.
|
|
23
|
+
- W5: `Q(r)` is `Z(r.numerator)` followed by `U(r.denominator)`, with a positive, reduced denominator.
|
|
24
|
+
|
|
25
|
+
Integer magnitudes use minimal big-endian bytes. Zero has an empty magnitude and sign 0. A zero integer therefore has `Z(0) = 00 00`.
|
|
26
|
+
|
|
27
|
+
`F64(f)` is the eight bytes from Ruby's `pack("G")`. Multi-byte fixed integers use big-endian order.
|
|
28
|
+
|
|
29
|
+
Every field length must fit inside its enclosing byte payload. Nonminimal integers, truncated fields, invalid flags, and trailing bytes raise `InvalidValueError`.
|
|
30
|
+
|
|
31
|
+
## Encoded scalar values
|
|
32
|
+
|
|
33
|
+
Upstream byte values carry format tag `rb`, hexadecimal `72 62`. Their payload starts with version byte `01` and a type byte.
|
|
34
|
+
|
|
35
|
+
| Type byte | Ruby value | Remaining payload |
|
|
36
|
+
| --- | --- | --- |
|
|
37
|
+
| `01` | String | `T(encoding.name)`, `B(bytes)` |
|
|
38
|
+
| `02` | Symbol | `T(name.encoding.name)`, `B(name.bytes)` |
|
|
39
|
+
| `03` | Boolean | One byte, `00` for false or `01` for true |
|
|
40
|
+
| `04` | Large Integer | `Z(value)` |
|
|
41
|
+
| `05` | Time | `Q(value.to_r)`, `Z(value.utc_offset)`, one byte for `utc?` |
|
|
42
|
+
| `06` | Date | `Z(value.jd)`, `F64(value.start)` |
|
|
43
|
+
| `07` | DateTime | `Q(value.ajd)`, `Q(value.offset)`, `F64(value.start)` |
|
|
44
|
+
| `08` | BigDecimal | Decimal encoding defined below |
|
|
45
|
+
| `09` | Rational | `Q(value)` |
|
|
46
|
+
| `0a` | Registered custom value | `T(codec_id)`, `B(codec_bytes)` |
|
|
47
|
+
|
|
48
|
+
Integers fitting a native upstream integer slot use that slot. Signed 64-bit values use `int`; remaining unsigned 64-bit values use `uint`.
|
|
49
|
+
|
|
50
|
+
Values outside those ranges use type `04`. Floats use upstream float slots. Stored null uses an occupied upstream `none` slot.
|
|
51
|
+
|
|
52
|
+
Time decoding uses the exact rational instant and recorded UTC offset. The UTC flag distinguishes UTC presentation from a fixed zero offset.
|
|
53
|
+
|
|
54
|
+
The instant retains subnanosecond precision. The offset field accepts integer seconds only; fractional-second UTC offsets raise `UnsupportedValueError`.
|
|
55
|
+
|
|
56
|
+
Date and DateTime decoding restores the recorded calendar start. Their date fields use the public `date` library constructors.
|
|
57
|
+
|
|
58
|
+
Untagged byte values represent explicit binary data. Ruby reads them as frozen ASCII-8BIT strings. Public byte streams address these untagged values.
|
|
59
|
+
|
|
60
|
+
Other format tags remain opaque tagged-byte values. An unknown custom codec retains its complete encoded payload and codec identifier.
|
|
61
|
+
|
|
62
|
+
An unsupported Ruby payload version or builtin type raises `UnsupportedEncodingError`. A registered custom loader never runs during file opening or unrelated traversal.
|
|
63
|
+
|
|
64
|
+
### Decimal encoding
|
|
65
|
+
|
|
66
|
+
The first decimal byte is 0 for finite, 1 for positive infinity, 2 for negative infinity, or 3 for NaN.
|
|
67
|
+
|
|
68
|
+
Infinite and NaN encodings end after that byte. A finite value adds one sign byte, `T(digits)`, and `Z(exponent)`.
|
|
69
|
+
|
|
70
|
+
Sign 0 is positive; sign 1 is negative. Digits and exponent come from `BigDecimal#split`, whose base must be 10.
|
|
71
|
+
|
|
72
|
+
Digits contain normalized ASCII decimal digits. Zero uses digits `0` and exponent 0. Its sign preserves positive or negative zero.
|
|
73
|
+
|
|
74
|
+
## Canonical hash keys
|
|
75
|
+
|
|
76
|
+
These bytes determine key equality and hashing. The stored entry separately retains the original encoded key for enumeration.
|
|
77
|
+
|
|
78
|
+
| Prefix | Accepted key | Remaining canonical bytes |
|
|
79
|
+
| --- | --- | --- |
|
|
80
|
+
| `00` | nil | None |
|
|
81
|
+
| `01` | false | None |
|
|
82
|
+
| `02` | true | None |
|
|
83
|
+
| `03` | Integer | `Z(value)` |
|
|
84
|
+
| `04` | Float | `F64(value)`, with signed zero normalized to positive zero |
|
|
85
|
+
| `05` | String | Canonical string body defined below |
|
|
86
|
+
| `06` | Symbol | Canonical string body for `value.to_s` |
|
|
87
|
+
|
|
88
|
+
NaN keys raise `UnsupportedValueError`. Infinite float keys are accepted in hash indexes.
|
|
89
|
+
|
|
90
|
+
A canonical string body is `T(encoding_label)` followed by `B(bytes)`. The encoding label is empty for every empty string.
|
|
91
|
+
|
|
92
|
+
For nonempty ASCII-only strings with an ASCII-compatible encoding, the label is also empty. Other strings use their canonical `Encoding#name`.
|
|
93
|
+
|
|
94
|
+
This preserves Ruby's empty-string equality across encodings. It also preserves the distinction between non-ASCII binary and UTF-8 strings.
|
|
95
|
+
|
|
96
|
+
String and Symbol prefixes remain distinct. Integer and Float prefixes remain distinct.
|
|
97
|
+
|
|
98
|
+
Golden vectors include:
|
|
99
|
+
|
|
100
|
+
| Input | Hexadecimal canonical bytes |
|
|
101
|
+
| --- | --- |
|
|
102
|
+
| nil | `00` |
|
|
103
|
+
| false | `01` |
|
|
104
|
+
| true | `02` |
|
|
105
|
+
| 0 | `03 00 00` |
|
|
106
|
+
| 1 | `03 00 01 01` |
|
|
107
|
+
| -1 | `03 01 01 01` |
|
|
108
|
+
| Empty String in any encoding | `05 00 00` |
|
|
109
|
+
| ASCII String `name` | `05 00 04 6e 61 6d 65` |
|
|
110
|
+
| Symbol `name` | `06 00 04 6e 61 6d 65` |
|
|
111
|
+
|
|
112
|
+
The decoder does not reconstruct keys from canonical bytes. Entry records retain their original Ruby representation.
|
|
113
|
+
|
|
114
|
+
## Compact containers
|
|
115
|
+
|
|
116
|
+
Core tag `17` (`0x11`) identifies an external record. Its slot value is the record offset, and native container descriptors require a clear full flag.
|
|
117
|
+
|
|
118
|
+
The record starts with two unsigned big-endian 32-bit words: metadata and field count. The count is at most 32.
|
|
119
|
+
|
|
120
|
+
Consecutive nine-byte slots follow the header. Readers validate the count before reading fields and enforce their captured read boundary.
|
|
121
|
+
|
|
122
|
+
| Metadata | Logical type | Fields |
|
|
123
|
+
| --- | --- | --- |
|
|
124
|
+
| `1` | Literal map | Alternating original-key and value slots, at most sixteen pairs |
|
|
125
|
+
| `2` | Small Array | At most sixteen element slots |
|
|
126
|
+
| `3` | Tree Array | Exactly one upstream array-list slot |
|
|
127
|
+
| `4` | Dictionary map | One dictionary record slot, followed by two to sixteen value slots |
|
|
128
|
+
| `5` | Key dictionary | Two to sixteen original-key slots |
|
|
129
|
+
| `6` | Set | At most sixteen original-key slots |
|
|
130
|
+
|
|
131
|
+
Metadata `5` is an internal dictionary, not a logical collection. A dictionary descriptor requires a clear full flag and metadata `5`.
|
|
132
|
+
|
|
133
|
+
A dictionary map has exactly one value per dictionary key. Keys appear in insertion order in literal maps, dictionaries, and sets.
|
|
134
|
+
|
|
135
|
+
Every key has a supported exact scalar class and a distinct canonical identity. NaN keys are invalid.
|
|
136
|
+
|
|
137
|
+
Packed keys have at most 128 canonical bytes and 256 encoded scalar bytes. Readers check byte payload bounds before decoding keys.
|
|
138
|
+
|
|
139
|
+
Replacing an equal key retains its original slot and position. Deletion removes its position; reinsertion appends it.
|
|
140
|
+
|
|
141
|
+
The seventeenth map or set member, or an oversized key, promotes the collection to indexed storage. Clearing retains indexed storage.
|
|
142
|
+
|
|
143
|
+
Small arrays retain element slots directly. Tree arrays use sixteen slots per branch, with selective copy-on-write updates through the affected path.
|
|
144
|
+
|
|
145
|
+
Scalar payloads remain separate byte records. A large value therefore does not require decoding or copying its containing collection.
|
|
146
|
+
|
|
147
|
+
Compaction follows all record fields, remaps offsets, and preserves shared references. Unknown native metadata or invalid shapes raise `InvalidValueError`.
|
|
148
|
+
|
|
149
|
+
### Bounded sharing
|
|
150
|
+
|
|
151
|
+
One encoding operation can share 256 String or Symbol scalar slots with at most 64 source bytes each.
|
|
152
|
+
|
|
153
|
+
Scalar identity includes exact type, String encoding, and binary bytes. A separate object-identity cache recognizes 256 validated frozen String keys.
|
|
154
|
+
|
|
155
|
+
The operation can share 64 ordered dictionaries identified by their original encoded key slots. Additional shapes use literal maps.
|
|
156
|
+
|
|
157
|
+
Value-only updates can retain their dictionary. Key changes can publish a literal map without allocating another dictionary.
|
|
158
|
+
|
|
159
|
+
Read-only materialization caches 64 decoded dictionaries within that call. Transaction reads use no decoded dictionary cache.
|
|
160
|
+
|
|
161
|
+
Sharing creates no Ruby object aliases during materialization. Tables do not survive an operation or transaction rollback.
|
|
162
|
+
|
|
163
|
+
## Indexed container envelopes
|
|
164
|
+
|
|
165
|
+
Each envelope is an upstream array list with the exact fields shown below. Marker fields are upstream unsigned integer slots.
|
|
166
|
+
|
|
167
|
+
Encoded children can be scalar slots, scalar bytes, compact records, or indexed envelopes. Internal indexes use upstream containers.
|
|
168
|
+
|
|
169
|
+
| Marker | Logical type | Envelope fields |
|
|
170
|
+
| --- | --- | --- |
|
|
171
|
+
| `0x52420101` | Array | `[marker, array_list]` |
|
|
172
|
+
| `0x52420102` | Linked array | `[marker, linked_array_list]` |
|
|
173
|
+
| `0x52420103` | Ordered map | `[marker, next_id, lookup, order]` |
|
|
174
|
+
| `0x52420104` | Ordered set | `[marker, next_id, lookup, order]` |
|
|
175
|
+
| `0x52420105` | Sorted map | `[marker, key_kind, sorted_map]` |
|
|
176
|
+
| `0x52420106` | Sorted set | `[marker, key_kind, sorted_map]` |
|
|
177
|
+
|
|
178
|
+
Arrays and linked arrays contain encoded children. Their envelopes do not require materializing those children.
|
|
179
|
+
|
|
180
|
+
Unexpected markers, field counts, or physical collection kinds raise `InvalidValueError`. The core remains able to inspect the physical values.
|
|
181
|
+
|
|
182
|
+
### Indexed maps and sets
|
|
183
|
+
|
|
184
|
+
`next_id` is an upstream unsigned integer starting at zero. `lookup` is a counted upstream hash map. `order` is an upstream sorted map.
|
|
185
|
+
|
|
186
|
+
A lookup entry uses `SHA256(canonical_key_bytes)` as its hash. Its physical key is the canonical byte string.
|
|
187
|
+
|
|
188
|
+
Its physical value is a three-element upstream array list: `[encoded_key, encoded_value, insertion_id]`. Sets store occupied null as `encoded_value`.
|
|
189
|
+
|
|
190
|
+
An order entry maps the eight-byte big-endian insertion identifier to the 32-byte lookup hash. Both indexes reference the same logical entry.
|
|
191
|
+
|
|
192
|
+
Insertion creates one entry and increments `next_id`. Updating an equal key retains its original encoded key and insertion identifier.
|
|
193
|
+
|
|
194
|
+
Deletion removes both index entries. Reinsertion receives a fresh identifier. Clearing resets both indexes and `next_id`.
|
|
195
|
+
|
|
196
|
+
When `next_id` equals `2**64 - 1`, another insertion raises `RangeError`. Updates and deletions remain valid.
|
|
197
|
+
|
|
198
|
+
The logical size comes from the counted lookup map. Iteration follows the order index and resolves its referenced entries.
|
|
199
|
+
|
|
200
|
+
### Sorted maps and sets
|
|
201
|
+
|
|
202
|
+
Key kind is 1 for String, 2 for Integer, 3 for Float, or 4 for Time.
|
|
203
|
+
|
|
204
|
+
A sorted map stores order-preserving key bytes and a two-element record: `[encoded_original_key, encoded_value]`.
|
|
205
|
+
|
|
206
|
+
A sorted set also uses an upstream sorted map. Its value is the encoded original member. This retains representation beyond the comparison key.
|
|
207
|
+
|
|
208
|
+
Updating an equal comparison key retains its original encoded key or member. Every operation uses the declared key kind.
|
|
209
|
+
|
|
210
|
+
## Ordered key encoding
|
|
211
|
+
|
|
212
|
+
Ordered encodings use unsigned byte comparison. They contain no locale-dependent data and do not use Ruby's randomized hash.
|
|
213
|
+
|
|
214
|
+
### Integers
|
|
215
|
+
|
|
216
|
+
For nonnegative `n`, define `O(n)` as an eight-byte magnitude length followed by minimal big-endian magnitude bytes.
|
|
217
|
+
|
|
218
|
+
`O(0)` has length zero and no magnitude bytes. The length field is an unsigned 64-bit integer.
|
|
219
|
+
|
|
220
|
+
Ordered signed integers use prefix `00` for negative, `01` for zero, and `02` for positive.
|
|
221
|
+
|
|
222
|
+
Negative values append the bytewise complement of `O(abs(n))`. Positive values append `O(n)`. Zero ends after its prefix.
|
|
223
|
+
|
|
224
|
+
These encodings are prefix-free and compare in numeric order. Magnitude lengths exceeding the physical file range raise `RangeError`.
|
|
225
|
+
|
|
226
|
+
### Floats
|
|
227
|
+
|
|
228
|
+
Reject NaN. Normalize either signed zero to positive zero. Read the IEEE-754 bits as an unsigned 64-bit integer.
|
|
229
|
+
|
|
230
|
+
For negative values, complement all 64 bits. For nonnegative values, toggle the sign bit. Write the resulting eight bytes big-endian.
|
|
231
|
+
|
|
232
|
+
This places negative infinity first and positive infinity last. It gives either zero representation one lookup key.
|
|
233
|
+
|
|
234
|
+
### Strings
|
|
235
|
+
|
|
236
|
+
String keys must convert to valid UTF-8. Their ordered bytes are the resulting UTF-8 bytes without a length prefix.
|
|
237
|
+
|
|
238
|
+
The entry record retains the original encoding and bytes. Invalid or unconvertible keys raise `UnsupportedEncodingError`.
|
|
239
|
+
|
|
240
|
+
This comparison deliberately uses UTF-8 text order. Ordinary Ruby hash-key encoding retains its separate equality contract.
|
|
241
|
+
|
|
242
|
+
### Exact time instants
|
|
243
|
+
|
|
244
|
+
Time keys compare `value.to_r`, including fractions smaller than a nanosecond. Their comparison ignores presentation offsets.
|
|
245
|
+
|
|
246
|
+
For nonnegative rational `x`, define a prefix-free encoding `R(x)`:
|
|
247
|
+
|
|
248
|
+
1. Compute `q, remainder = x.numerator.divmod(x.denominator)`.
|
|
249
|
+
2. Write `O(q)`.
|
|
250
|
+
3. For zero remainder, append byte `00` and stop.
|
|
251
|
+
4. Otherwise, append byte `01` and the complement of `R(x.denominator / remainder)`.
|
|
252
|
+
|
|
253
|
+
The division in step 4 denotes an exact Rational. An iterative continued-fraction implementation avoids Ruby stack growth.
|
|
254
|
+
|
|
255
|
+
For signed rational instants, use the signed prefixes from integer encoding. Negative instants append the complement of `R(abs(x))`.
|
|
256
|
+
|
|
257
|
+
Positive instants append `R(x)`. Zero ends after its prefix. Prefix-free child encodings make byte complementation reverse their order.
|
|
258
|
+
|
|
259
|
+
This requires work proportional to the continued-fraction representation. It does not emit one byte per denominator unit.
|
|
260
|
+
|
|
261
|
+
## Ownership and loader behavior
|
|
262
|
+
|
|
263
|
+
View reads return frozen scalar values. Materialization creates independent values and mutable containers and strings where Ruby permits mutation.
|
|
264
|
+
|
|
265
|
+
Custom loaders receive a frozen copy of their payload. They must return a fresh owned value, without shared mutable state between calls.
|
|
266
|
+
|
|
267
|
+
The profile freezes the returned object's outer state for view reads. A custom codec owns the immutability of references inside that value.
|
|
268
|
+
|
|
269
|
+
Materialization invokes the loader independently. No implicit constant lookup or arbitrary object loader interprets stored class names.
|
|
270
|
+
|
|
271
|
+
## Required fixtures
|
|
272
|
+
|
|
273
|
+
Golden scalar fixtures retain their version-one payload notation. Regression tests cover compact records, dictionaries, indexed envelopes, key equality, signed zero, and unknown codecs.
|
|
274
|
+
|
|
275
|
+
Ordered-key fixtures compare encoded byte order with numeric or temporal order. They include negative values, huge integers, infinities, and exact subnanosecond instants.
|
|
276
|
+
|
|
277
|
+
Malformed fixtures cover truncated lengths, nonminimal integers, invalid encodings, invalid fields, and conflicting index records.
|
|
278
|
+
|
|
279
|
+
Changes to canonical keys, payload interpretation, or container layouts require a different profile identifier. Readers never infer a profile version from data shape.
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# Ruby profile version 3 wire contract
|
|
2
|
+
|
|
3
|
+
The header has hash size 32 and hash identifier `rb03`. Hash indexes use SHA-256 over canonical typed keys.
|
|
4
|
+
|
|
5
|
+
The page encoder, decoder, and storage adapters are implemented in Ruby. The gem has no compiled extension or build step.
|
|
6
|
+
|
|
7
|
+
Scalar encodings, key identity, indexed envelopes, and record metadata 1 through 6 retain the layouts specified in [version 2](ruby-profile-v2.md). Version 3 adds the page layouts below. Native handles reject `rb01` and `rb02` files without changing their contents. No migration or backward reader is provided.
|
|
8
|
+
|
|
9
|
+
## Page arrays
|
|
10
|
+
|
|
11
|
+
A Core record with metadata `7` has two fields: `[row_count:uint, pages:array_list]`. The count is positive. The page list has exactly `ceil(row_count / 1024)` entries. All descriptor full flags are clear.
|
|
12
|
+
|
|
13
|
+
Each page contains 1,024 rows, except the final page, whose length is the remaining count. A typed page uses an untagged `bytes` or `short_bytes` slot. A promoted page uses a packed array record with metadata `2` or `3` and the expected length. Page arrays cannot contain other page arrays as physical pages.
|
|
14
|
+
|
|
15
|
+
The encoder attempts this layout for exact Arrays with at least 32 rows. Every row must be an exact Hash with identical ordered String key representations. Bound built-in methods read input contents without invoking per-object overrides. Hash defaults, identity comparison, unsupported cells, and oversized pages cause general encoding to resume. Application callback exceptions propagate. Partial page writes are rolled back before fallback.
|
|
16
|
+
|
|
17
|
+
## Row references
|
|
18
|
+
|
|
19
|
+
A Core record with metadata `8` has three fields: `[page:bytes, row_index:uint, expected_rows:uint]`. The page field also accepts `short_bytes`. All descriptor full flags are clear.
|
|
20
|
+
|
|
21
|
+
The expected row count is between 1 and 1,024. The row index is less than that count. The payload count must match the descriptor. Compaction follows the page field and preserves shared references.
|
|
22
|
+
|
|
23
|
+
Transient row references retain only a page descriptor, index, and count. They contain no decoded values and cannot be serialized directly. Assigning one persists a metadata `8` record.
|
|
24
|
+
|
|
25
|
+
## Typed page payload
|
|
26
|
+
|
|
27
|
+
The complete payload is at most 65,536 bytes. It starts with ASCII `XP01`, then unsigned variable-length row and field counts. The row count is between 1 and 1,024. The field count is between 0 and 16.
|
|
28
|
+
|
|
29
|
+
Unsigned integers use least-significant seven-bit groups first. Bit 7 marks continuation. Encodings must be minimal and fit within 64 bits. The schema contains one encoded String per field. Row cells follow in row-major order, using that schema order.
|
|
30
|
+
|
|
31
|
+
| Tag | Value | Following bytes |
|
|
32
|
+
| --- | --- | --- |
|
|
33
|
+
| 0 | nil | None |
|
|
34
|
+
| 1 | false | None |
|
|
35
|
+
| 2 | true | None |
|
|
36
|
+
| 3 | Signed Integer | Zigzag-encoded unsigned variable-length integer |
|
|
37
|
+
| 4 | Float | Eight IEEE 754 binary64 bytes, most significant first |
|
|
38
|
+
| 5 | UTF-8 String | Variable-length byte count, then bytes |
|
|
39
|
+
| 6 | US-ASCII String | Variable-length byte count, then bytes |
|
|
40
|
+
| 7 | ASCII-8BIT String | Variable-length byte count, then bytes |
|
|
41
|
+
| 8 | Other String encoding | Encoding-name byte count, name, value byte count, then value bytes |
|
|
42
|
+
|
|
43
|
+
Tag `8` requires a canonical Ruby encoding name of 1 through 127 bytes. It cannot name the three encodings with dedicated tags. String bytes need not be valid in their declared encoding. Float payloads preserve every bit, including negative zero and NaN payloads.
|
|
44
|
+
|
|
45
|
+
Schema keys must be distinct under Ruby String key equality. Decoded keys are frozen. Decoded String values are independent and mutable when materialized. Larger Integers and unsupported scalar types use ordinary Ruby-profile storage.
|
|
46
|
+
|
|
47
|
+
Decoding rejects unknown tags, invalid counts, duplicate keys, noncanonical integers or encoding names, truncated payloads, and trailing bytes. Selected-row reads validate skipped cells without allocating their values. Stored descriptor errors raise `InvalidValueError`.
|
|
48
|
+
|
|
49
|
+
## Mutation and compatibility
|
|
50
|
+
|
|
51
|
+
A compatible replacement rewrites only its typed page and the required ancestor records. An incompatible replacement promotes that page to a packed array of persisted row references. Structural array changes promote the logical array to ordinary packed storage. Raw byte-stream writes preserve their untagged byte representation through promotion.
|
|
52
|
+
|
|
53
|
+
Portable storage remains unchanged. Pinned reference implementations cannot maintain native graphs that use the Core record extension. Loading the portable entry point does not load the Ruby page codec.
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# Ruby profile version 4 wire contract
|
|
2
|
+
|
|
3
|
+
The header has hash size 32 and hash identifier `rb04`. Canonical typed keys use SHA-256.
|
|
4
|
+
Ruby scalar payloads, canonical keys, indexed envelopes, and record metadata 1 through 8 retain the [version 3 layouts](ruby-profile-v3.md).
|
|
5
|
+
Version 4 adds indexed compact pages with integrity checks.
|
|
6
|
+
The implementation uses Ruby and existing standard-library operations, with no custom compiled codec or build step.
|
|
7
|
+
|
|
8
|
+
## Compatibility
|
|
9
|
+
|
|
10
|
+
Ruby handles reject `rb01`, `rb02`, and `rb03` files without modifying their contents.
|
|
11
|
+
This pre-1.0 implementation provides no automatic migration or backward file reader.
|
|
12
|
+
Historical format documents describe those files; they remain distinct contracts.
|
|
13
|
+
Portable formats and entry points remain unchanged.
|
|
14
|
+
The `rb04` identifier is reserved for the Ruby profile, including files that contain only general collections.
|
|
15
|
+
|
|
16
|
+
## Indexed compact pages
|
|
17
|
+
|
|
18
|
+
An indexed page contains at most 65,536 bytes, including its checksum.
|
|
19
|
+
The row count is between 1 and 1,024; the field count is between 0 and 16.
|
|
20
|
+
The physical sequence is:
|
|
21
|
+
|
|
22
|
+
1. ASCII magic `XR01`.
|
|
23
|
+
2. Unsigned variable-length row and field counts, followed by one encoded String schema key per field.
|
|
24
|
+
3. Exactly `row_count + 1` unsigned 16-bit row offsets, least significant byte first.
|
|
25
|
+
4. Compact scalar cells in row-major schema order.
|
|
26
|
+
5. A 32-byte SHA-256 digest of every preceding payload byte.
|
|
27
|
+
|
|
28
|
+
Counts, schema keys, and scalar cells use the canonical encodings defined for [XP01 pages](ruby-profile-v3.md#typed-page-payload).
|
|
29
|
+
Offsets are relative to the first row cell, following the complete offset table.
|
|
30
|
+
The first offset is zero; the final offset equals the complete row payload length.
|
|
31
|
+
Each accessed pair must be ordered and within the row payload.
|
|
32
|
+
A row decoder must consume exactly the byte range between its adjacent offsets.
|
|
33
|
+
Zero-length rows are valid when the schema has no fields.
|
|
34
|
+
Full decoding validates every boundary and cell, including canonical scalar framing and exact payload exhaustion.
|
|
35
|
+
|
|
36
|
+
The schema preserves original key bytes, encodings, and order.
|
|
37
|
+
Schema keys must be distinct under Ruby String key equality and are frozen after decoding.
|
|
38
|
+
Materialized String values are independent and mutable.
|
|
39
|
+
Float cells preserve all IEEE binary64 bits, including NaN payloads and negative zero.
|
|
40
|
+
|
|
41
|
+
## Integrity and selected reads
|
|
42
|
+
|
|
43
|
+
Readers copy at most the page byte limit plus one byte before rejecting oversized input.
|
|
44
|
+
They verify the checksum before interpreting indexed schema or row payloads.
|
|
45
|
+
They validate the header, directory endpoints, selected offsets, and requested cells.
|
|
46
|
+
Selected reads allocate the schema and requested row without traversing unrelated row cells in Ruby.
|
|
47
|
+
They still obtain and hash the bounded page bytes; this is not constant work over an arbitrary byte length.
|
|
48
|
+
|
|
49
|
+
Ordinary byte damage anywhere in the page fails integrity validation, including damage outside the selected row.
|
|
50
|
+
The checksum provides integrity, not authenticity.
|
|
51
|
+
A malformed unselected cell with a deliberately recomputed checksum can remain deferred until access or full decoding.
|
|
52
|
+
Applications that require complete canonical validation must fully decode the page.
|
|
53
|
+
Invalid stored pages raise `InvalidValueError`; invalid argument types retain `TypeError`, and invalid row positions retain `IndexError`.
|
|
54
|
+
|
|
55
|
+
## Encoding, fallback, and mutation
|
|
56
|
+
|
|
57
|
+
The writer reserves the offset directory and checksum budget before appending row cells.
|
|
58
|
+
Capture retains bound built-in reads, unsupported-shape rejection, and bounded cell allocation from the compact encoder.
|
|
59
|
+
If indexed overhead exceeds the page budget, the writer can attempt an `XP01` compact sequential page.
|
|
60
|
+
Its decoder retains full validation of skipped cells during selected reads.
|
|
61
|
+
Thus a page array may contain both `XR01` and `XP01` byte pages, as well as promoted packed-array pages.
|
|
62
|
+
Unsupported schemas, values, or sizes retain general Ruby-profile storage with all supported value types.
|
|
63
|
+
Failed page attempts restore transaction checkpoints before fallback.
|
|
64
|
+
|
|
65
|
+
Compatible updates rewrite one page and its required ancestors, including fresh offsets and checksum.
|
|
66
|
+
Incompatible replacements, structural edits, persisted row references, snapshots, compaction, and view lifetimes retain version 3 semantics.
|
|
67
|
+
Decoded caches remain operation-local.
|
data/docs/ruby.md
CHANGED
|
@@ -1,7 +1,28 @@
|
|
|
1
1
|
# Ruby profile
|
|
2
2
|
|
|
3
3
|
`require "xitdb/ruby"` provides `XitDB::Ruby.open(path, readonly: false, lock_timeout: 5.0, codecs: [])` and `XitDB::Ruby.memory(codecs: [])`.
|
|
4
|
-
Configuration belongs to each handle. The file identifier `
|
|
4
|
+
Configuration belongs to each handle. The file identifier `rb04` selects SHA-256 over canonical typed keys, compact native containers, and indexed row pages.
|
|
5
|
+
Files using `rb01`, `rb02`, or `rb03` are incompatible and reject without modification.
|
|
6
|
+
This pre-1.0 format has no migration or backward reader.
|
|
7
|
+
|
|
8
|
+
Small maps and sets store up to sixteen original keys directly in insertion order in a Core record. Map metadata is `1`, followed by alternating original-key and value slots; set metadata is `6`, followed by original-key slots. Keys must have distinct canonical identities, at most 128 canonical bytes and at most 256 encoded scalar bytes. Larger collections and oversized keys use the indexed envelope representation. Promotion preserves order and the first original key representation, and clearing an indexed collection retains that representation.
|
|
9
|
+
|
|
10
|
+
The Core record extension has tag `17`, an unsigned big-endian 32-bit metadata word, an unsigned big-endian 32-bit field count, and up to 32 consecutive nine-byte slots. Compaction follows all fields and preserves shared references. Pinned reference implementations do not support this extension. Portable containers retain their existing wire representation.
|
|
11
|
+
|
|
12
|
+
Arrays with at most sixteen elements use record metadata `2` and consecutive element slots. Larger arrays use metadata `3` with one Core array-list slot. Bulk construction retains at most sixteen slots per tree level and appends completed blocks once. Selective updates use the existing Core copy-on-write path. Large values remain separate byte records referenced by their slots.
|
|
13
|
+
|
|
14
|
+
Within one bulk encoding, maps with two to sixteen fields can share an ordered dictionary. Metadata `4` contains a dictionary record slot followed by value slots. The dictionary uses metadata `5` and original-key slots. Dictionaries preserve exact key representations, including String encodings. Value-only updates retain the dictionary; key changes can return to literal records.
|
|
15
|
+
|
|
16
|
+
Arrays with at least 32 compatible flat Hash rows use typed pages. Each page holds at most 1,024 rows, sixteen String keys, and 65,536 encoded bytes. Keys retain their order, bytes, and encodings. Cells support nil, booleans, small Integers, Floats, and Strings. Other values use the general collection layouts.
|
|
17
|
+
|
|
18
|
+
A compatible row update rewrites one page. An incompatible update replaces that page with a packed array of row references. Structural array edits use the general array layout.
|
|
19
|
+
Indexed pages retain compact scalar cells and add 16-bit row offsets and a SHA-256 checksum.
|
|
20
|
+
Selected reads verify bounded page integrity, then decode the schema and requested row.
|
|
21
|
+
Full reads validate all cells and produce independent mutable values. Retained views never cache decoded rows.
|
|
22
|
+
Pages that exceed the indexed layout's byte budget can use the compact sequential layout, whose selected reads validate every cell.
|
|
23
|
+
Rechecksummed malformed cells in an unselected indexed row can remain undetected until access or full decoding.
|
|
24
|
+
|
|
25
|
+
Each encoding operation shares at most 256 short String or Symbol values and 64 dictionaries. Scalar sharing includes exact type, encoding, and bytes, and accepts at most 64 source bytes. A separate identity cache recognizes at most 256 validated frozen String key objects. Read-only materialization caches at most 64 decoded dictionaries within that call. Transaction reads use no decoded dictionary cache. Materialization returns independent mutable String values.
|
|
5
26
|
The [portable guide](portable.md) defines the shared transaction, snapshot, history, ownership, stream, and view contracts.
|
|
6
27
|
|
|
7
28
|
## Native values and keys
|
|
@@ -21,7 +42,9 @@ Ordinary keys never use custom codecs. Containers, temporal values, and custom o
|
|
|
21
42
|
|
|
22
43
|
Maps and sets preserve insertion order. Updating retains position; deleting and reinserting moves an entry to the end.
|
|
23
44
|
Native containers select the ordinary Ruby-profile representation. Counts are available without scanning members.
|
|
24
|
-
|
|
45
|
+
Exact Arrays, Hashes, and Strings are read through their built-in contents. Per-object overrides, extensions, and removed methods are ignored without invoking their hooks. Strings are copied before encoding or retaining a selector.
|
|
46
|
+
|
|
47
|
+
Hash defaults, default procs, identity comparison, executable objects, and cyclic inputs are unsupported. Extended scalars, application values, and codec registrations retain strict singleton-behavior validation.
|
|
25
48
|
Scalar and container subclasses need an explicit value codec. Unregistered subclasses reject before overridden methods execute.
|
|
26
49
|
|
|
27
50
|
## Sorted collections
|
|
@@ -58,4 +81,4 @@ Callbacks can read transaction views. Coercion, dump, load, and conversion callb
|
|
|
58
81
|
The guard applies across Context instances; nested writes raise `TransactionStateError` before changing state.
|
|
59
82
|
Callback failure aborts the affected assignment. Byte-stream callbacks retain their separate non-overlapping sibling-write behavior.
|
|
60
83
|
|
|
61
|
-
See [native workflows](../examples/ruby.rb) and the [wire contract](ruby-profile-
|
|
84
|
+
See [native workflows](../examples/ruby.rb) and the [wire contract](ruby-profile-v4.md).
|
data/examples/indexed_users.rb
CHANGED
data/examples/portable.rb
CHANGED
data/examples/ruby.rb
CHANGED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module XitDB
|
|
4
|
+
# Describes the storage layout of a new empty collection.
|
|
5
|
+
class CollectionDeclaration
|
|
6
|
+
# Physical collection layouts accepted by this declaration.
|
|
7
|
+
KINDS = %i[hash_map counted_hash_map hash_set counted_hash_set array_list linked_array_list sorted_map
|
|
8
|
+
sorted_set].freeze
|
|
9
|
+
|
|
10
|
+
# @!attribute [r] kind
|
|
11
|
+
# Physical layout to create when the declaration is assigned.
|
|
12
|
+
# @return [Symbol]
|
|
13
|
+
# @!attribute [r] key_kind
|
|
14
|
+
# Declared sorted key type, or nil when the profile chooses its default.
|
|
15
|
+
# @return [Symbol, nil]
|
|
16
|
+
attr_reader :kind, :key_kind
|
|
17
|
+
|
|
18
|
+
# Describe an empty collection to create when assigned in a transaction.
|
|
19
|
+
# @param kind [Symbol] supported physical collection kind
|
|
20
|
+
# @param key [Symbol, nil] sorted key kind: :string, :integer, :float, or :time
|
|
21
|
+
# @raise [ArgumentError] if the kind or key declaration is unsupported
|
|
22
|
+
def initialize(kind, key: nil)
|
|
23
|
+
raise ArgumentError, "unknown collection kind" unless KINDS.include?(kind)
|
|
24
|
+
if !nil.equal?(key) && (!%i[sorted_map sorted_set].include?(kind) || !%i[string integer float time].include?(key))
|
|
25
|
+
raise ArgumentError, "sorted key kind must be :string, :integer, :float, or :time"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
@kind = kind
|
|
29
|
+
@key_kind = key
|
|
30
|
+
freeze
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Report whether the hash layout stores its entry count.
|
|
34
|
+
# @return [Boolean]
|
|
35
|
+
def counted?
|
|
36
|
+
%i[counted_hash_map counted_hash_set].include?(kind)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|