Age/Storage/Async.pm untested

filter: Statementstmt 0.0% 0 / 31 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 51 CC 3 SCAR 13.3
CC
3
Cov
0%
cypher_async
6.0
drop_graph_async
6.0
required_transport_capabilities
2.0
CRAP 3.8
1 package DBIO::PostgreSQL::Age::Storage::Async;
2 # ABSTRACT: Floating async storage layer for Apache AGE (cypher_async)
3  
4 use strict;
5 use warnings;
6  
7 use DBIO::PostgreSQL::Age::Storage ();
8  
9  
10 sub required_transport_capabilities { qw(on_connect_replay) }
11  
12  
13 sub cypher_async {
14 my ($self, $graph, $query, $columns, $params, $opts) = @_;
15  
16 # Reuse the sync layer's PURE builder by composition: it only needs
17 # $self->throw_exception (resolved through the composed transport MRO) and the
18 # sync file's lexical JSON encoder, so calling it fully-qualified is DRY and
19 # stays in lock-step with sync cypher().
20 my ($sql, $bind) = DBIO::PostgreSQL::Age::Storage::_cypher_sql_bind(
21 $self, $graph, $query, $columns, $params,
22 );
23  
24 my $decode = $opts && $opts->{auto_decode};
25  
26 # Pass the '?'-placeholder SQL straight to the transport: shaping '?' to the
27 # wire dialect (e.g. DBD::Pg's $N) is the transport's concern, applied inside
28 # its _query_async (core karr #70 / ADR 0032 '?'-seam). The '$$...$$' cypher
29 # body and any '$name' reference are not placeholders and survive untouched.
30 return $self->_query_async($sql, $bind)->then(sub {
31 my @rows = @_; # raw row arrayrefs, in the declared column order
32  
33 my @out;
34 for my $raw (@rows) {
35 my %row;
36 # AGE returns columns named exactly as declared in "AS ($col agtype, ...)",
37 # so zipping @$columns reproduces the sync {Slice => {}} hashref shape.
38 @row{ @$columns } = @$raw;
39  
40 if ($decode) {
41 $row{$_} = DBIO::PostgreSQL::Age::Storage::decode_agtype($self, $row{$_})
42 for keys %row;
43 }
44  
45 push @out, \%row;
46 }
47  
48 # Resolve to the arrayref sync cypher() returns, so cypher_async(...)->get
49 # equals cypher(...). done(\@out) keeps the arrayref intact through ->then.
50 return $self->future_class->done(\@out);
51 });
52 }
53  
54  
55 sub create_graph_async {
56 my ($self, $name) = @_;
57 return $self->_query_async(
58 'SELECT * FROM ag_catalog.create_graph(?)', [ $name ],
59 );
60 }
61  
62  
63 sub drop_graph_async {
64 my ($self, $name, $cascade) = @_;
65 return $self->_query_async(
66 'SELECT * FROM ag_catalog.drop_graph(?, ?)', [ $name, $cascade ? 1 : 0 ],
67 );
68 }
69  
70  
71 1;
72  
73 __END__
74  
75 =pod
76  
77 =encoding UTF-8
78  
79 =head1 NAME
80  
81 DBIO::PostgreSQL::Age::Storage::Async - Floating async storage layer for Apache AGE (cypher_async)
82  
83 =head1 VERSION
84  
85 version 0.900001
86  
87 =head1 SYNOPSIS
88  
89 # Composed automatically for an Age schema opened in ANY async mode whose
90 # transport can replay LOAD 'age' -- future_io, ev, ... (core karr #70).
91  
92 my $schema = MyApp::Schema->connect(
93 $dsn, $user, $pass,
94 { on_connect_call => 'load_age', async => 'future_io' }, # or 'ev'
95 );
96  
97 my $async = $schema->storage->async; # the composed async backend
98  
99 $async->create_graph_async('social')->then(sub {
100 $async->cypher_async(
101 'social',
102 $$ MATCH (n:Person {name: $name}) RETURN n $$,
103 ['node'],
104 { name => 'Alice' },
105 { auto_decode => 1 },
106 );
107 })->then(sub {
108 my ($rows) = @_; # arrayref of hashrefs, one key per column, already decoded
109 ...
110 });
111  
112 =head1 DESCRIPTION
113  
114 The B<floating async storage layer> for L<Apache AGE|https://age.apache.org/>.
115 It is a plain method package -- B<not> a transport and B<not> a subclass of any
116 transport. Core's storage-layer composition (karr #70) mirrors the registered
117 sync Age layer onto its async sibling by convention
118 (C<< DBIO::PostgreSQL::Age::Storage >> -> C<< ...::Async >>) and composes B<this>
119 package (C3) OVER whatever transport the connection's async mode resolves:
120 L<DBIO::PostgreSQL::Storage::Async> for C<future_io>,
121 L<DBIO::PostgreSQL::EV::Storage> for C<ev>. The composed backend isa both this
122 layer and that transport, so C<cypher_async> rides every capable transport
123 without a per-transport class.
124  
125 =head2 Required transport capability
126  
127 AGE mandates C<LOAD 'age'> (and the C<ag_catalog> search_path SET) on B<every>
128 pooled connection, replayed through core's pool C<on_connect> seam. This layer
129 therefore declares C<on_connect_replay> as a
130 L</required_transport_capabilities>; core's capability gate croaks loudly if a
131 schema tries to compose AGE over a transport that cannot replay connect actions,
132 rather than silently losing the session setup. Both shipped PostgreSQL async
133 transports advertise C<on_connect_replay>.
134  
135 =head2 Separate sync and async entry points
136  
137 Graph queries have two entry points, on the two composed storages:
138  
139 =over 4
140  
141 =item * B<sync> -- C<< $schema->storage->cypher(...) >>, on the composed sync
142 storage (the L<DBIO::PostgreSQL::Age::Storage> layer over
143 L<DBIO::PostgreSQL::Storage>, a L<DBIO::Storage::DBI>), returns an arrayref of
144 hashrefs.
145  
146 =item * B<async> -- C<< $schema->storage->async->cypher_async(...) >>, on the
147 composed async backend (this layer over a L<DBIO::Storage::Async> transport),
148 returns a L<Future> resolving to the same arrayref of hashrefs. As a convenience
149 the composed sync storage also carries a C<cypher_async> dispatcher (see
150 L<DBIO::PostgreSQL::Age::Storage/cypher_async>): C<< $schema->storage->cypher_async(...) >>
151 routes to this backend on an async connection and degrades in-process under
152 C<< { async => 'immediate' } >>, exactly like the core CRUD C<*_async> methods.
153  
154 =back
155  
156 The sync and async surfaces stay B<separate by design>: this layer is I<not> a
157 subclass of L<DBIO::PostgreSQL::Age::Storage>, and it never C<use base>s a
158 storage. Welding the blocking DBI machinery (C<dbh>, C<dbh_do>, C<txn_do>, the
159 driver registry) onto an async transport is exactly what the layer model avoids
160 -- the reason is unchanged, but the B<mechanism> is now composition-by-core, not
161 a parallel-inheritance adapter bolted to one transport. What crosses between the
162 two surfaces is only the two pure, DB-free helpers of the sync layer -- the SQL
163 builder C<_cypher_sql_bind> and C<decode_agtype> -- reused by B<composition>,
164 called as class-level helpers so both entry points build identical SQL and
165 decode identically. C<auto_decode> therefore behaves identically on both.
166  
167 =head2 Session setup (LOAD 'age')
168  
169 This layer does B<nothing> locally for C<LOAD 'age'> -- it defines no
170 C<connect_call_load_age>. The setup rides core's pool C<on_connect> seam (karr
171 #68): a schema connected with C<< { on_connect_call => 'load_age' } >> gives the
172 pool an owning sync storage (the composed sync Age storage, which defines
173 C<connect_call_load_age> on its layer), and core replays that connect action
174 synchronously against each freshly-spawned pool connection -- on whichever
175 transport was composed under this layer. Installing any LOAD logic on this layer
176 would be the karr #66 anti-pattern.
177  
178 =head2 The '?' placeholder seam
179  
180 C<cypher_async> / C<create_graph_async> / C<drop_graph_async> hand the transport
181 plain C<?>-placeholder SQL. Shaping C<?> to the transport's wire dialect (e.g.
182 DBD::Pg's positional C<$N>) is the B<transport's> concern, applied inside its
183 C<_query_async> (core karr #70 / ADR 0032 '?'-seam). The C<$$...$$> Cypher body
184 and any C<$name> Cypher parameter reference are not SQL placeholders and survive
185 untouched; only a real C<?> is rewritten, and only by the transport.
186  
187 Everything else -- the transport, the connection pool, the CRUD runner,
188 transactions and the SQLMaker -- comes from whichever transport this layer is
189 composed over.
190  
191 =head1 METHODS
192  
193 =head2 required_transport_capabilities
194  
195 my @caps = DBIO::PostgreSQL::Age::Storage::Async->required_transport_capabilities;
196 # ('on_connect_replay')
197  
198 Class method. The transport capabilities this async layer requires. Returns
199 C<on_connect_replay> because C<LOAD 'age'> must replay on every pooled
200 connection. Core's composition capability gate (see
201 L<DBIO::Storage::Async/transport_capabilities>) croaks naming this layer, the
202 missing capability and the transport if AGE is composed over a transport that
203 does not advertise it.
204  
205 =head2 cypher_async
206  
207 my $future = $async->cypher_async(
208 'social',
209 $$ MATCH (a:Person)-[:KNOWS]->(b:Person) RETURN a.name, b.name $$,
210 [qw( person friend )],
211 );
212  
213 # With Cypher parameters:
214 my $future = $async->cypher_async(
215 'social',
216 $$ MATCH (n:Person {name: $name}) RETURN n $$,
217 ['node'],
218 { name => 'Alice' },
219 );
220  
221 # Auto-decode each cell into native Perl data (identical to sync cypher):
222 my $future = $async->cypher_async(
223 'social',
224 $$ MATCH (n:Person {name: $name}) RETURN n $$,
225 ['node'],
226 { name => 'Alice' },
227 { auto_decode => 1 },
228 );
229  
230 The async counterpart of L<DBIO::PostgreSQL::Age::Storage/cypher>. Builds the
231 same SQL and binds via the shared C<_cypher_sql_bind> and executes them over the
232 composed transport, returning a L<Future> that resolves to an arrayref of
233 hashrefs (one key per C<$columns> entry) -- exactly the shape sync C<cypher()>
234 returns.
235  
236 C<$params>, if given, is JSON-encoded and passed as AGE's third C<cypher()>
237 argument. With C<< { auto_decode => 1 } >> every cell is passed through
238 L<DBIO::PostgreSQL::Age::Storage/decode_agtype> B<inside the Future chain>, so
239 the resolved arrayref is already decoded -- identical semantics to sync
240 C<auto_decode>, just async. Without it every cell is a raw agtype string and
241 decoding is the caller's responsibility.
242  
243 =head2 create_graph_async
244  
245 my $future = $async->create_graph_async('social');
246  
247 Async counterpart of L<DBIO::PostgreSQL::Age::Storage/create_graph>: a thin
248 wrapper that runs C<ag_catalog.create_graph(?)> over the composed transport.
249 Returns a L<Future>.
250  
251 =head2 drop_graph_async
252  
253 my $future = $async->drop_graph_async('social');
254 my $future = $async->drop_graph_async('social', 1); # cascade
255  
256 Async counterpart of L<DBIO::PostgreSQL::Age::Storage/drop_graph>: a thin
257 wrapper that runs C<ag_catalog.drop_graph(?, ?)> over the composed transport.
258 Pass a true second argument to cascade the drop. Returns a L<Future>.
259  
260 =seealso
261  
262 =over 4
263  
264 =item * L<DBIO::PostgreSQL::Age::Storage> - the sync storage layer with C<cypher()>
265  
266 =item * L<DBIO::PostgreSQL::Storage::Async> - the C<future_io> transport this layer composes over
267  
268 =item * L<DBIO::PostgreSQL::EV::Storage> - the C<ev> transport this layer composes over
269  
270 =item * L<DBIO::PostgreSQL::Age> - Schema component that activates AGE
271  
272 =back
273  
274 =head1 AUTHOR
275  
276 DBIO Authors
277  
278 =head1 COPYRIGHT AND LICENSE
279  
280 Copyright (C) 2026 DBIO Authors
281  
282 This is free software; you can redistribute it and/or modify it under
283 the same terms as the Perl 5 programming language system itself.
284  
285 =cut