1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//! ID generation helpers for the `tokio-postgres` stack.
//!
//! # What
//!
//! Thin async wrappers over the installed HeeRanjID SQL generation
//! functions — [`generate_heerid`], [`generate_ranjid`], and their
//! batch counterparts [`generate_heerids`] / [`generate_ranjids`].
//! Each helper takes a [`tokio_postgres::GenericClient`], a `node_id`,
//! and (for batch variants) a row count; it runs the appropriate
//! `SELECT generate_id(...)` / `SELECT id FROM generate_ids(...)` query
//! and deserialises the result into the matching [`crate::HeerId`] or
//! [`crate::RanjId`] type.
//!
//! All helpers return [`GenerateError`], which wraps the underlying
//! [`tokio_postgres::Error`] for transport failures and carries a
//! [`crate::Error`] source for the (unlikely but possible) case where
//! the database returns a bit pattern that fails HeerId / RanjId
//! validation.
//!
//! # Why here
//!
//! The `postgres_schema` module covers database-wide bootstrap
//! (installing DDL, seeding the default node). This module covers the
//! per-request generation path. Both are gated on the `postgres`
//! feature and require `tokio-postgres` at runtime.
//!
//! The sibling [`heeranjid-sqlx`](https://docs.rs/heeranjid-sqlx) crate
//! offers the same four helpers over `sqlx::Executor`; this module is
//! the `tokio-postgres` parity surface so applications on the
//! `postgres` feature don't have to hand-write `client.query_one(
//! "SELECT generate_id($1)", &[&node_id_i32])` calls. Public
//! signatures, naming, and error-handling behaviour match the sqlx
//! counterparts exactly (only the executor bound and error type
//! differ).
use GenericClient;
use crate::;
/// Error returned by the [`postgres_generate`](self) helpers.
///
/// Shape mirrors `heeranjid_sqlx::GenerateError` so callers can swap
/// between the two client stacks without restructuring their error
/// handling. Transport and SQL-level failures surface through
/// [`GenerateError::Database`]; the `Invalid*` variants fire only when
/// the database hands back a value that doesn't parse as a valid
/// HeerId / RanjId (a corruption or schema-mismatch signal, not a
/// normal operational error).
/// Generates a single [`HeerId`] for `node_id` by calling the installed
/// `generate_id(INTEGER)` SQL function.
///
/// The SQL function allocates a timestamp/sequence pair for the given
/// node and returns a `BIGINT`; this helper parses that integer through
/// [`HeerId::from_i64`] and returns the validated type.
///
/// # Errors
///
/// * [`GenerateError::Database`] on transport or SQL-level failures
/// (node not registered, sequence exhausted, etc.).
/// * [`GenerateError::InvalidHeerId`] if the returned integer fails
/// validation — indicates schema / client version drift.
pub async
/// Generates a single [`RanjId`] for `node_id` by calling the installed
/// `generate_ranjid(INTEGER)` SQL function.
///
/// The SQL function returns a `UUID` encoding the timestamp,
/// precision, node, and sequence fields; this helper validates the
/// version/variant bits through [`RanjId::from_uuid`] and returns the
/// typed value.
///
/// # Errors
///
/// * [`GenerateError::Database`] on transport or SQL-level failures.
/// * [`GenerateError::InvalidRanjId`] if the returned UUID is not a
/// valid RanjId (wrong version or variant bits) — indicates schema /
/// client version drift.
pub async
/// Generates `count` [`HeerId`]s for `node_id` in a single round-trip
/// by calling the installed `generate_ids(INTEGER, INTEGER)` SQL
/// function.
///
/// Returns the IDs in the order the SQL function emits them
/// (time-ordered, ascending). Each row is validated through
/// [`HeerId::from_i64`]; any invalid row short-circuits the result.
///
/// # Errors
///
/// * [`GenerateError::Database`] on transport or SQL-level failures.
/// * [`GenerateError::InvalidHeerId`] if any returned integer fails
/// validation.
pub async
/// Generates `count` [`RanjId`]s for `node_id` in a single round-trip
/// by calling the installed `generate_ranjids(INTEGER, INTEGER)` SQL
/// function.
///
/// Returns the IDs in the order the SQL function emits them
/// (time-ordered, ascending). Each row is validated through
/// [`RanjId::from_uuid`]; any invalid row short-circuits the result.
///
/// # Errors
///
/// * [`GenerateError::Database`] on transport or SQL-level failures.
/// * [`GenerateError::InvalidRanjId`] if any returned UUID fails
/// validation.
pub async