yeptris 0.6.78.0 → 0.6.79.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.
@@ -0,0 +1,309 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yeptris"
4
+
5
+ module Yeptris
6
+ # The plan face (engine plan.h; yeptris-ruby#411, the rs
7
+ # `pub mod plan` parity surface): compile a strict-JSON plan spec
8
+ # once, apply it to a document (the DOM leg) or a JSON tape (the
9
+ # tape leg), and read typed COLUMNS — no Ruby tree ever
10
+ # materializes on the hot path.
11
+ #
12
+ # plan = Yeptris::Plan.compile('{"kind":"seq","children":[
13
+ # {"name":"id","kind":"int"},{"name":"name","kind":"str"}]}')
14
+ # result = doc.plan_walk(plan)
15
+ # result.ints(0) # => [1, 2]
16
+ # result.strs(1) # => ["a", "b"]
17
+ #
18
+ # {Yeptris::JSON::Descriptor} stays the lutaml-vocabulary layer
19
+ # (build-time Ruby hashes, eager row hashes); this module is the
20
+ # cross-runtime face the "query" benchmark op rides — spec strings
21
+ # in, typed columns out, indexed by COLUMN NUMBER (rs parity).
22
+ module Plan
23
+ # Column kinds (plan.h's YEP_PLAN_*).
24
+ COLUMN_KINDS = { 0 => :int, 1 => :float, 2 => :str, 3 => :bool }.freeze
25
+ private_constant :COLUMN_KINDS
26
+
27
+ # Raised for a malformed spec (compile) and for a document/tape
28
+ # whose shape disagrees with the plan (walk).
29
+ class Error < ::Yeptris::Error; end
30
+
31
+ # A compiled plan. The engine handle rides an AutoPointer (freed
32
+ # at GC); #free releases it eagerly.
33
+ class Plan
34
+ # @api private — minted by .compile
35
+ def initialize(c_ptr)
36
+ @c_ptr = ::FFI::AutoPointer.new(
37
+ c_ptr, self.class.method(:release_handle)
38
+ )
39
+ @freed = false
40
+ end
41
+
42
+ def self.release_handle(ptr)
43
+ ::Yeptris::FFI.yeptris_plan_free(ptr)
44
+ end
45
+ private_class_method :release_handle
46
+
47
+ def column_count
48
+ ::Yeptris::FFI.yeptris_plan_column_count(@c_ptr)
49
+ end
50
+
51
+ def free
52
+ return if @freed
53
+
54
+ @freed = true
55
+ @c_ptr.free
56
+ nil
57
+ end
58
+
59
+ def freed?
60
+ @freed
61
+ end
62
+
63
+ # @api private — the raw handle for the walk calls
64
+ attr_reader :c_ptr
65
+ end
66
+
67
+ # The typed columnar result of one walk. Accessors read the C
68
+ # result lazily and CACHE the materialized Ruby arrays; the result
69
+ # borrows the walked source for the tape leg's string spans (the
70
+ # source is retained here, so the borrow cannot dangle while the
71
+ # result lives). The DOM leg's string columns are (ptr,len) views
72
+ # INTO the document — they MATERIALIZE EAGERLY at walk time (the
73
+ # document may be freed while the result lives; a view would
74
+ # dangle). #free releases the C handle; use after free raises, and
75
+ # a result left to GC still frees (the Document finalizer
76
+ # pattern).
77
+ class PlanColumns
78
+ # Shared between the instance and its GC finalizer (Procs close
79
+ # over variables by reference — the Document pattern).
80
+ Freed = Struct.new(:flag)
81
+
82
+ # @api private — minted by the walk faces (+plan+ only on the
83
+ # DOM leg, where the eager str pass needs the column count)
84
+ def initialize(c_ptr, src: nil, doc: nil, plan: nil)
85
+ @c_ptr = c_ptr
86
+ @src = src
87
+ @doc = doc
88
+ @freed = Freed.new(false)
89
+ @cache = {}
90
+ ObjectSpace.define_finalizer(self, self.class.finalize(c_ptr, @freed))
91
+ # the eager str pass (the DOM borrow law): read every str
92
+ # column NOW, while the document is certainly alive
93
+ return unless @doc && plan
94
+
95
+ plan.column_count.times do |col|
96
+ next unless column_kind(col) == :str
97
+
98
+ read_str_column(col)
99
+ end
100
+ end
101
+
102
+ def self.finalize(ptr, freed)
103
+ proc do
104
+ next if freed.flag
105
+
106
+ freed.flag = true
107
+ ::Yeptris::FFI.yeptris_plan_result_free(ptr)
108
+ end
109
+ end
110
+
111
+ def rows
112
+ ensure_alive!
113
+ ::Yeptris::FFI.yeptris_plan_result_rows(@c_ptr)
114
+ end
115
+ alias count rows
116
+
117
+ # The column's kind: :int, :float, :str, or :bool.
118
+ def column_kind(col)
119
+ ensure_alive!
120
+ COLUMN_KINDS.fetch(::Yeptris::FFI.yeptris_plan_result_kind(@c_ptr, col), col)
121
+ end
122
+
123
+ # INT columns as Integer; BOOL columns read through the same
124
+ # lane as 0/1 (the engine's columnar contract, rs parity).
125
+ def ints(col)
126
+ read_typed(col, :int)
127
+ end
128
+
129
+ # BOOL columns mapped to true/false.
130
+ def bools(col)
131
+ read_typed(col, :bool)
132
+ end
133
+
134
+ def floats(col)
135
+ read_typed(col, :float)
136
+ end
137
+
138
+ # STR columns: decoded UTF-8 strings (spans materialize at
139
+ # first read — nothing borrowed crosses).
140
+ def strs(col)
141
+ read_typed(col, :str)
142
+ end
143
+
144
+ # The column's null markers: true where the row's leaf was null
145
+ # or missing. One boolean per row.
146
+ def nulls(col)
147
+ read_nulls(col).map { |v| v == 1 }
148
+ end
149
+
150
+ def free
151
+ return if @freed.flag
152
+
153
+ @freed.flag = true
154
+ ObjectSpace.undefine_finalizer(self)
155
+ ::Yeptris::FFI.yeptris_plan_result_free(@c_ptr)
156
+ nil
157
+ end
158
+
159
+ def freed?
160
+ @freed.flag
161
+ end
162
+
163
+ private
164
+
165
+ def ensure_alive!
166
+ return unless @freed.flag
167
+
168
+ raise Error, "the plan result was freed"
169
+ end
170
+
171
+ def rows!
172
+ @rows ||= rows
173
+ end
174
+
175
+ def read_nulls(col)
176
+ @cache["nulls:#{col}"] ||= begin
177
+ n = rows!
178
+ n.zero? ? [] :
179
+ ::Yeptris::FFI.yeptris_plan_result_nulls(@c_ptr, col)
180
+ .read_bytes(n).unpack("C*")
181
+ end
182
+ end
183
+
184
+ def read_typed(col, want)
185
+ ensure_alive!
186
+ kind = COLUMN_KINDS.fetch(::Yeptris::FFI.yeptris_plan_result_kind(@c_ptr, col), col)
187
+ raise Error, "column #{col} is #{kind.inspect}, not #{want.inspect}" unless
188
+ kind == want || (want == :bool && kind == :bool) ||
189
+ (want == :int && kind == :bool)
190
+
191
+ # the shared int lane serves INT (raw) and BOOL columns; the
192
+ # want decides the mapping (ints reads bools as 0/1 — the rs
193
+ # parity contract — bools maps to true/false)
194
+ @cache["#{want}:#{col}"] ||= if kind == :str
195
+ read_str_column(col)
196
+ elsif kind == :float
197
+ read_float_lane(col)
198
+ elsif want == :bool
199
+ read_bool_lane(col)
200
+ else
201
+ read_int_lane(col)
202
+ end
203
+ end
204
+
205
+ def read_int_lane(col)
206
+ n = rows!
207
+ nulls = read_nulls(col)
208
+ return [] if n.zero?
209
+
210
+ vals = ::Yeptris::FFI.yeptris_plan_result_ints(@c_ptr, col)
211
+ .read_bytes(n * 8).unpack("q<*")
212
+ return vals unless nulls.include?(1)
213
+
214
+ Array.new(n) { |i| nulls[i] == 1 ? nil : vals[i] }
215
+ end
216
+
217
+ # BOOL rides the int lane (0/1); maps to true/false with the
218
+ # null slots preserved.
219
+ def read_bool_lane(col)
220
+ n = rows!
221
+ nulls = read_nulls(col)
222
+ return [] if n.zero?
223
+
224
+ vals = ::Yeptris::FFI.yeptris_plan_result_ints(@c_ptr, col)
225
+ .read_bytes(n * 8).unpack("q<*")
226
+ Array.new(n) { |i| nulls[i] == 1 ? nil : vals[i] == 1 }
227
+ end
228
+
229
+ def read_float_lane(col)
230
+ n = rows!
231
+ nulls = read_nulls(col)
232
+ return [] if n.zero?
233
+
234
+ vals = ::Yeptris::FFI.yeptris_plan_result_floats(@c_ptr, col)
235
+ .read_bytes(n * 8).unpack("E*")
236
+ return vals unless nulls.include?(1)
237
+
238
+ Array.new(n) { |i| nulls[i] == 1 ? nil : vals[i] }
239
+ end
240
+
241
+ # The tape leg: spans into the retained source; the DOM leg:
242
+ # (ptr,len) views into the document. Both materialize to Ruby
243
+ # strings.
244
+ def read_str_column(col)
245
+ n = rows!
246
+ nulls = read_nulls(col)
247
+ if @doc
248
+ base = ::FFI::Pointer.new(::Yeptris::FFI.yeptris_plan_result_strs(@c_ptr, col))
249
+ stride = ::Yeptris::FFI::PlanStr.size
250
+ Array.new(n) do |i|
251
+ next nil if nulls[i] == 1
252
+
253
+ v = ::Yeptris::FFI::PlanStr.new(base + i * stride)
254
+ v[:p].read_bytes(v[:len]).force_encoding(Encoding::UTF_8)
255
+ end
256
+ else
257
+ decode = ::Yeptris::JSON.method(:decode_span)
258
+ offs = ::Yeptris::FFI.yeptris_plan_result_str_offs(@c_ptr, col)
259
+ .read_bytes(n * 4).unpack("V*")
260
+ lens = ::Yeptris::FFI.yeptris_plan_result_str_lens(@c_ptr, col)
261
+ .read_bytes(n * 4).unpack("V*")
262
+ Array.new(n) { |i| nulls[i] == 1 ? nil : decode.call(@src, offs[i], lens[i]) }
263
+ end
264
+ end
265
+ end
266
+
267
+ module_function
268
+
269
+ # Compiles a plan from a strict-JSON spec STRING (plan.h's
270
+ # contract — kind/path/children; rs parity: the spec crosses as
271
+ # text, no Ruby-hash layer). Raises {Plan::Error} on a malformed
272
+ # spec.
273
+ def compile(spec)
274
+ spec = spec.to_s
275
+ st = ::FFI::MemoryPointer.new(:int)
276
+ raw = ::Yeptris::FFI.yeptris_plan_compile(spec, spec.bytesize, st)
277
+ raise Error,
278
+ "plan compile failed (status=#{st.read_int}): #{Yeptris::FFI.last_error_message}" if raw.null?
279
+
280
+ Plan.new(raw)
281
+ end
282
+
283
+ # @api private — the tape leg's reader (Yeptris::JSON::Tape#plan_walk)
284
+ def walk_tape(tape, plan)
285
+ st = ::FFI::MemoryPointer.new(:int)
286
+ raw = ::Yeptris::FFI.yeptris_tape_plan_walk(tape.c_ptr, plan.c_ptr, st)
287
+ if raw.null?
288
+ raise Error,
289
+ "plan walk failed (status=#{st.read_int}) — the document shape " \
290
+ "disagrees with the plan"
291
+ end
292
+
293
+ PlanColumns.new(raw, src: tape.source)
294
+ end
295
+
296
+ # @api private — the DOM leg's reader (Yeptris::Document#plan_walk)
297
+ def walk_document(doc, plan)
298
+ st = ::FFI::MemoryPointer.new(:int)
299
+ raw = ::Yeptris::FFI.yeptris_document_plan_walk(doc.c_ptr, plan.c_ptr, st)
300
+ if raw.null?
301
+ raise Error,
302
+ "plan walk failed (status=#{st.read_int}) — the document shape " \
303
+ "disagrees with the plan"
304
+ end
305
+
306
+ PlanColumns.new(raw, doc: doc, plan: plan)
307
+ end
308
+ end
309
+ end
data/lib/yeptris.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  module Yeptris
4
4
  # The gem's version lives in the parent namespace's file — the last
5
5
  # internal require (yeptris/version) retired with it.
6
- VERSION = "0.6.78.0".freeze
6
+ VERSION = "0.6.79.0".freeze
7
7
 
8
8
  # Informational notices are OPT-IN (yeptris-ruby#217): consumers
9
9
  # embed yeptris inside CLIs that assert clean stderr, and a
@@ -46,6 +46,16 @@ module Yeptris
46
46
  end
47
47
  end
48
48
 
49
+ # The per-minor native materializer's feature name (the org
50
+ # native-layer contract, leptris-ruby#671 clause 2): DOT-FREE
51
+ # yeptris/native_<major>_<minor> — MRI derives the init symbol from
52
+ # the feature basename cut at the first dot (native_3_4.so resolves
53
+ # Init_native_3_4). The naming SSOT is NativeLayout (the packaging
54
+ # script and CI gates consume the same mapping).
55
+ def self.native_bundle_feature
56
+ NativeLayout.bundle_feature
57
+ end
58
+
49
59
  autoload :Document, "yeptris/document"
50
60
  autoload :Node, "yeptris/node"
51
61
  autoload :YAML, "yeptris/yaml"
@@ -56,6 +66,9 @@ module Yeptris
56
66
  autoload :Schema, "yeptris/schema"
57
67
  autoload :Validation, "yeptris/validation"
58
68
  autoload :CBOR, "yeptris/cbor"
69
+ autoload :Plan, "yeptris/plan"
70
+ autoload :Events, "yeptris/events"
71
+ autoload :NativeLayout, "yeptris/native_layout"
59
72
  end
60
73
 
61
74
  # NOTE (the #318 poisoned-process class): the Psych error-name aliases
@@ -102,29 +115,28 @@ end
102
115
  # Optional C-API materializer (TODO.restructure/22): fused visit →
103
116
  # Ruby objects via the Ruby C API. Feature-detected — LoadError leaves
104
117
  # the FFI ladder (Marshal → columns → records) as the sole path.
105
- # One bundle per Ruby minor (the leptris-ruby #207/#227 lesson: a PE
106
- # DLL must bind its build Ruby's runtime; a unix bundle is also
107
- # minor-locked by the internal API it compiles against). The MINOR
108
- # lives in the DIRECTORY (lib/yeptris/<minor>/native.<ext>) and the
109
- # FILE name stays unversioned: ruby derives the init symbol from the
110
- # feature basename, so native-3.4.so asked for the untypeable
111
- # "Init_native-3" and the versioned-file scheme could never load
112
- # (#157 — every shipped bundle was dead code). Dev checkouts keep
113
- # the plain native.<ext> name (mkmf layout, built for the running
114
- # Ruby). A missing cell falls back LOUDLY to the ladder.
118
+ #
119
+ # The org native-layer contract (leptris-ruby#671 clause 2): ONE
120
+ # compiled bundle per Ruby minor, DOT-FREE, flat at
121
+ # lib/yeptris/native_<major>_<minor>.so (.bundle on darwin, one DLL
122
+ # per minor on windows — a PE DLL must bind its build Ruby's runtime,
123
+ # the #207/#227 lesson). MRI derives the init symbol from the feature
124
+ # basename cut at the first dot: native_3_4.so resolves Init_native_3_4
125
+ # (the old native-<minor>.so name asked for the untypeable
126
+ # "Init_native-3" — #157).
127
+ #
128
+ # NO unversioned fallback, ever: a bundle built by any other minor is
129
+ # a wrong-ABI load (the compiled-in RString layout changed in 3.2 —
130
+ # the 3.1 segfault class; Ruby 3.0 ran a 3.3-built ext and the JSON
131
+ # parse failed). An absent variant raises, and this top-level require
132
+ # falls back to the FFI ladder with a warning (opt-in display per the
133
+ # #217 informational-notices law — consumers assert clean stderr).
115
134
  begin
116
- # per-minor FIRST: a single version-agnostic native.so loads under
117
- # ANY Ruby and is an ABI gamble across minors — Ruby 3.0 ran a
118
- # 3.3-built ext and the JSON parse failed.
119
- require "yeptris/#{RUBY_VERSION[/\A\d+\.\d+/]}/native"
120
- rescue LoadError
121
- begin
122
- require "yeptris/native"
123
- rescue LoadError => e
124
- if Yeptris.debug? # the require chain is top-level (self = main)
125
- warn "yeptris: no precompiled native materializer for Ruby " \
126
- "#{RUBY_VERSION[/\A\d+\.\d+/]} on this platform " \
127
- "(#{e.message}); the FFI ladder carries the load"
128
- end
135
+ require Yeptris.native_bundle_feature
136
+ rescue LoadError => e
137
+ if Yeptris.debug? # the require chain is top-level (self = main)
138
+ warn "yeptris: no precompiled native materializer for Ruby " \
139
+ "#{RUBY_VERSION[/\A\d+\.\d+/]} on this platform " \
140
+ "(#{e.message}); the FFI ladder carries the load"
129
141
  end
130
142
  end
@@ -3,7 +3,7 @@
3
3
  cmake_minimum_required(VERSION 3.20)
4
4
 
5
5
  project(yeptris
6
- VERSION 0.6.78
6
+ VERSION 0.6.79
7
7
  DESCRIPTION "Ultra-fast YAML 1.2 parser, emitter and streamer in C"
8
8
  LANGUAGES C CXX
9
9
  )
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yeptris
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.78.0
4
+ version: 0.6.79.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-23 00:00:00.000000000 Z
11
+ date: 2026-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi
@@ -36,7 +36,7 @@ extensions:
36
36
  extra_rdoc_files: []
37
37
  files:
38
38
  - README.adoc
39
- - ext/build_windows_native.rb
39
+ - ext/build_native_per_minor.rb
40
40
  - ext/libyeptris/extconf.rb
41
41
  - ext/yeptris_native/cbor_ruby.c
42
42
  - ext/yeptris_native/extconf.rb
@@ -45,11 +45,15 @@ files:
45
45
  - lib/yeptris.rb
46
46
  - lib/yeptris/cbor.rb
47
47
  - lib/yeptris/document.rb
48
+ - lib/yeptris/events.rb
48
49
  - lib/yeptris/ffi.rb
49
50
  - lib/yeptris/json.rb
50
51
  - lib/yeptris/json/descriptor.rb
52
+ - lib/yeptris/json/tape.rb
51
53
  - lib/yeptris/materializer.rb
54
+ - lib/yeptris/native_layout.rb
52
55
  - lib/yeptris/node.rb
56
+ - lib/yeptris/plan.rb
53
57
  - lib/yeptris/psych.rb
54
58
  - lib/yeptris/psych/class_loader.rb
55
59
  - lib/yeptris/psych/coder_shim.rb