-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathindex.rs
More file actions
executable file
·340 lines (310 loc) · 12.1 KB
/
Copy pathindex.rs
File metadata and controls
executable file
·340 lines (310 loc) · 12.1 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright 2026 ExtendDB contributors
// SPDX-License-Identifier: Apache-2.0
//! GSI/LSI index operations for the `PostgreSQL` backend.
//!
//! Handles index metadata fetching, item projection for indexes, synchronous
//! index updates within transactions, and async index enqueue for deferred
//! propagation.
use extenddb_core::types::{
AttributeDefinition, Item, KeySchemaElement, Projection, ProjectionType, ScalarAttributeType,
};
use extenddb_storage::error::StorageError;
use extenddb_storage::util::SortKeyValue;
use extenddb_storage::util::{composite_pk_to_text, parse_sk, sk_column, sk_column_n};
use super::{all_sort_key_info, index_table_name};
use crate::gsi_queue::{GsiApplyContext, GsiIndexDef};
/// Map a `sqlx` error to `StorageError`, preserving the SQLSTATE code in the
/// message. The async GSI worker keys off the code (e.g. `42P01`,
/// undefined_table) to tell a dropped-index race apart from a real failure;
/// `sqlx`'s `Display` only carries the human text, so the code must be kept.
fn db_error(e: sqlx::Error) -> StorageError {
match e.as_database_error().and_then(|d| d.code()) {
Some(code) => StorageError::Internal(format!("SQLSTATE {code}: {e}")),
None => StorageError::Internal(e.to_string()),
}
}
/// Metadata for a single index, used during write-path GSI/LSI sync.
pub(crate) struct IndexMeta {
pub(super) index_id: String,
pub(super) index_type: String,
pub(super) key_schema: Vec<KeySchemaElement>,
pub(super) projection: Projection,
/// Per-GSI propagation delay in milliseconds. `None` means use system
/// default. `Some(0)` means synchronous.
pub(super) propagation_delay_ms: Option<i32>,
}
/// Fetch all index metadata for a table from the catalog.
pub(crate) async fn fetch_indexes_for_table(
table_id: &str,
pool: &sqlx::PgPool,
) -> Result<Vec<IndexMeta>, StorageError> {
let rows: Vec<(String, String, serde_json::Value, serde_json::Value, Option<i32>)> = sqlx::query_as(
"SELECT index_id, index_type, key_schema, projection, propagation_delay_ms FROM indexes WHERE table_id = $1",
)
.bind(table_id)
.fetch_all(pool)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
rows.into_iter()
.map(|(id, idx_type, ks_json, proj_json, delay)| {
let key_schema: Vec<KeySchemaElement> = serde_json::from_value(ks_json)
.map_err(|e| StorageError::Internal(e.to_string()))?;
let projection: Projection = serde_json::from_value(proj_json)
.map_err(|e| StorageError::Internal(e.to_string()))?;
Ok(IndexMeta {
index_id: id,
index_type: idx_type,
key_schema,
projection,
propagation_delay_ms: delay,
})
})
.collect()
}
/// Project an item according to an index's projection configuration.
///
/// Returns the projected item containing only the attributes that should be
/// stored in the index table's `item_data` column.
pub(crate) fn project_item_for_index(
item: &Item,
index_ks: &[KeySchemaElement],
base_ks: &[KeySchemaElement],
projection: &Projection,
) -> Item {
match projection.projection_type {
ProjectionType::All => item.clone(),
ProjectionType::KeysOnly => {
let mut projected = Item::new();
// Include base table keys + index keys
for ks in base_ks.iter().chain(index_ks.iter()) {
if let Some(v) = item.get(&ks.attribute_name) {
projected.insert(ks.attribute_name.clone(), v.clone());
}
}
projected
}
ProjectionType::Include => {
// Base keys + index keys + non-key attributes
let mut projected = Item::new();
for ks in base_ks.iter().chain(index_ks.iter()) {
if let Some(v) = item.get(&ks.attribute_name) {
projected.insert(ks.attribute_name.clone(), v.clone());
}
}
if let Some(ref attrs) = projection.non_key_attributes {
for attr in attrs {
if let Some(v) = item.get(attr) {
projected.insert(attr.clone(), v.clone());
}
}
}
projected
}
}
}
/// Check if an item has all the key attributes required by an index.
pub(crate) fn item_has_index_keys(item: &Item, index_ks: &[KeySchemaElement]) -> bool {
index_ks
.iter()
.all(|ks| item.contains_key(&ks.attribute_name))
}
/// Compute the effective propagation delay for an index.
///
/// Per-GSI setting overrides the system default. `Some(0)` = sync, `None` = use default.
pub(super) fn effective_delay(idx: &IndexMeta, system_default: u64) -> u64 {
match idx.propagation_delay_ms {
Some(0) => 0,
Some(ms) if ms > 0 => ms as u64,
Some(_) => system_default, // Negative values treated as "use system default".
None => system_default,
}
}
/// Synchronously update index tables for indexes with zero propagation delay.
///
/// Called within the same PG transaction as the base table write.
/// Only processes indexes where `effective_delay == 0`. Async indexes are
/// handled by the persistent `gsi_pending` queue after the transaction commits.
#[allow(clippy::too_many_arguments)]
pub(crate) async fn sync_indexes(
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
base_key_schema: &[KeySchemaElement],
attr_defs: &[AttributeDefinition],
indexes: &[IndexMeta],
old_item: Option<&Item>,
new_item: Option<&Item>,
system_default_delay: u64,
) -> Result<(), StorageError> {
for idx in indexes {
if idx.index_type != "LSI" && effective_delay(idx, system_default_delay) != 0 {
continue; // Async — handled after commit. LSIs are always synchronous.
}
let idx_table = index_table_name(&idx.index_id);
let idx_sks = all_sort_key_info(&idx.key_schema, attr_defs);
let base_sks = all_sort_key_info(base_key_schema, attr_defs);
// Delete old index row if the old item had index keys
if let Some(old) = old_item
&& item_has_index_keys(old, &idx.key_schema)
{
delete_index_row_multi(tx, &idx_table, old, base_key_schema, &base_sks).await?;
}
// Insert new index row if the new item has index keys
if let Some(new) = new_item
&& item_has_index_keys(new, &idx.key_schema)
{
let projected =
project_item_for_index(new, &idx.key_schema, base_key_schema, &idx.projection);
insert_index_row_multi(
tx,
&idx_table,
new,
&projected,
&idx.key_schema,
base_key_schema,
&idx_sks,
&base_sks,
)
.await?;
}
}
Ok(())
}
/// Check whether any indexes require async propagation.
///
/// Returns `true` if at least one GSI has a non-zero effective delay,
/// meaning the write should insert a row into `gsi_pending`.
pub(crate) fn has_async_indexes(indexes: &[IndexMeta], system_default_delay: u64) -> bool {
indexes
.iter()
.any(|idx| idx.index_type != "LSI" && effective_delay(idx, system_default_delay) != 0)
}
/// Build the self-describing apply context for a write's async GSIs.
///
/// Captures the base key schema, attribute definitions, and the definition of
/// each GSI that propagates asynchronously, so the queue worker can apply the
/// update without reading the catalog. Call only when [`has_async_indexes`]
/// returned `true`; the returned context then has at least one index.
pub(crate) fn async_index_context(
base_key_schema: &[KeySchemaElement],
attr_defs: &[AttributeDefinition],
indexes: &[IndexMeta],
system_default_delay: u64,
) -> GsiApplyContext {
let index_defs = indexes
.iter()
.filter(|idx| idx.index_type != "LSI" && effective_delay(idx, system_default_delay) != 0)
.map(|idx| GsiIndexDef {
index_id: idx.index_id.clone(),
key_schema: idx.key_schema.clone(),
projection: idx.projection.clone(),
})
.collect();
GsiApplyContext {
base_key_schema: base_key_schema.to_vec(),
attribute_definitions: attr_defs.to_vec(),
indexes: index_defs,
}
}
/// Delete a row from an index table using base table key columns.
pub(crate) async fn delete_index_row_multi(
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
idx_table: &str,
item: &Item,
base_ks: &[KeySchemaElement],
base_sks: &[(&str, ScalarAttributeType)],
) -> Result<(), StorageError> {
let base_pk_text = composite_pk_to_text(item, base_ks)?;
let mut where_parts = vec!["base_pk = $1".to_owned()];
for (i, &(_, sk_type)) in base_sks.iter().enumerate() {
let param_idx = (i as u32) + 2;
let col = if i == 0 {
format!("base_{}", sk_column(sk_type))
} else {
format!("base_{}", sk_column_n(i, sk_type))
};
where_parts.push(format!("{col} = ${param_idx}"));
}
let sql = format!(
"DELETE FROM {idx_table} WHERE {}",
where_parts.join(" AND ")
);
let mut query = sqlx::query(&sql).bind(base_pk_text);
for &(sk_name, sk_type) in base_sks {
if let Some(sk_val) = item.get(sk_name) {
let sk = parse_sk(sk_val, sk_type)?;
query = match sk {
SortKeyValue::S(s) => query.bind(s),
SortKeyValue::N(n) => query.bind(n),
SortKeyValue::B(b) => query.bind(b),
};
}
}
query.execute(&mut **tx).await.map_err(db_error)?;
Ok(())
}
/// Insert a row into an index table with multi-part key support.
#[allow(clippy::too_many_arguments)]
pub(crate) async fn insert_index_row_multi(
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
idx_table: &str,
item: &Item,
projected: &Item,
index_ks: &[KeySchemaElement],
base_ks: &[KeySchemaElement],
idx_sks: &[(&str, ScalarAttributeType)],
base_sks: &[(&str, ScalarAttributeType)],
) -> Result<(), StorageError> {
let idx_pk_text = composite_pk_to_text(item, index_ks)?;
let base_pk_text = composite_pk_to_text(item, base_ks)?;
let item_json =
serde_json::to_value(projected).map_err(|e| StorageError::Internal(e.to_string()))?;
// Build column list dynamically
let mut cols = vec!["pk".to_owned()];
for (i, &(_, sk_type)) in idx_sks.iter().enumerate() {
cols.push(sk_column_n(i, sk_type));
}
cols.push("base_pk".to_owned());
for (i, &(_, sk_type)) in base_sks.iter().enumerate() {
let col = if i == 0 {
format!("base_{}", sk_column(sk_type))
} else {
format!("base_{}", sk_column_n(i, sk_type))
};
cols.push(col);
}
cols.push("item_data".to_owned());
let placeholders: Vec<String> = (1..=cols.len()).map(|i| format!("${i}")).collect();
let sql = format!(
"INSERT INTO {idx_table} ({}) VALUES ({})",
cols.join(", "),
placeholders.join(", ")
);
let mut query = sqlx::query(&sql).bind(idx_pk_text);
// Bind index SK values
for &(sk_name, sk_type) in idx_sks {
if let Some(sk_val) = item.get(sk_name) {
let sk = parse_sk(sk_val, sk_type)?;
query = match sk {
SortKeyValue::S(s) => query.bind(s),
SortKeyValue::N(n) => query.bind(n),
SortKeyValue::B(b) => query.bind(b),
};
}
}
// Bind base PK
query = query.bind(base_pk_text);
// Bind base SK values
for &(sk_name, sk_type) in base_sks {
if let Some(sk_val) = item.get(sk_name) {
let sk = parse_sk(sk_val, sk_type)?;
query = match sk {
SortKeyValue::S(s) => query.bind(s),
SortKeyValue::N(n) => query.bind(n),
SortKeyValue::B(b) => query.bind(b),
};
}
}
// Bind item_data
query = query.bind(item_json);
query.execute(&mut **tx).await.map_err(db_error)?;
Ok(())
}