Skip to content

Commit bc5ee59

Browse files
committed
test
1 parent 5d44890 commit bc5ee59

File tree

2 files changed

+90
-6
lines changed

2 files changed

+90
-6
lines changed

crates/core/src/db/relational_db.rs

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ impl RelationalDB {
823823

824824
let is_ephemeral_tables = |table_id: &TableId| -> bool {
825825
tx_data
826-
.ephermal_tables()
826+
.ephemeral_tables()
827827
.map(|etables| etables.contains(table_id))
828828
.unwrap_or(false)
829829
};
@@ -2507,6 +2507,90 @@ mod tests {
25072507
Ok(())
25082508
}
25092509

2510+
fn setup_view(stdb: &TestDB) -> ResultTest<(ViewId, TableId, ModuleDef, ViewDef)> {
2511+
let module_def = view_module_def();
2512+
let view_def = module_def.view("my_view").unwrap();
2513+
2514+
let mut tx = begin_mut_tx(stdb);
2515+
let (view_id, table_id) = stdb.create_view(&mut tx, &module_def, view_def)?;
2516+
stdb.commit_tx(tx)?;
2517+
2518+
Ok((view_id, table_id, module_def.clone(), view_def.clone()))
2519+
}
2520+
2521+
fn insert_view_row(
2522+
stdb: &TestDB,
2523+
view_id: ViewId,
2524+
table_id: TableId,
2525+
typespace: &Typespace,
2526+
row_type: AlgebraicTypeRef,
2527+
sender: Identity,
2528+
v: u8,
2529+
) -> ResultTest<()> {
2530+
let to_bstan = |pv: &ProductValue| {
2531+
Bytes::from(to_vec(&AlgebraicValue::Array([pv.clone()].into())).expect("bstan serialization failed"))
2532+
};
2533+
2534+
let row_pv = |v: u8| ProductValue::from_iter(vec![AlgebraicValue::U8(v)]);
2535+
2536+
let mut tx = begin_mut_tx(stdb);
2537+
tx.subscribe_view(view_id, ArgId::SENTINEL, sender)?;
2538+
stdb.materialize_view(&mut tx, table_id, sender, row_type, to_bstan(&row_pv(v)), typespace)?;
2539+
stdb.commit_tx(tx)?;
2540+
2541+
Ok(())
2542+
}
2543+
2544+
fn project_views(stdb: &TestDB, table_id: TableId, sender: Identity) -> Vec<ProductValue> {
2545+
let tx = begin_tx(stdb);
2546+
2547+
stdb.iter_by_col_eq(&tx, table_id, 0, &sender.into())
2548+
.unwrap()
2549+
.map(|row| {
2550+
let pv = row.to_product_value();
2551+
ProductValue {
2552+
elements: pv.elements.iter().skip(1).cloned().collect(),
2553+
}
2554+
})
2555+
.collect()
2556+
}
2557+
2558+
2559+
#[test]
2560+
fn test_view_tables_are_ephemeral() -> ResultTest<()> {
2561+
let stdb = TestDB::durable()?;
2562+
2563+
let (view_id, table_id, module_def, view_def) = setup_view(&stdb)?;
2564+
let row_type = view_def.product_type_ref;
2565+
let typespace = module_def.typespace();
2566+
2567+
// Write some rows (reusing the same helper)
2568+
insert_view_row(&stdb, view_id, table_id, typespace, row_type, Identity::ONE, 10)?;
2569+
insert_view_row(&stdb, view_id, table_id, typespace, row_type, Identity::ZERO, 20)?;
2570+
2571+
assert!(
2572+
!project_views(&stdb, table_id, Identity::ZERO).is_empty(),
2573+
"View table should NOT be empty after insert"
2574+
);
2575+
2576+
// Reopen the database — view tables must not persist
2577+
let stdb = stdb.reopen()?;
2578+
2579+
// Validate that the view's backing table has been removed
2580+
assert!(
2581+
project_views(&stdb, table_id, Identity::ZERO).is_empty(),
2582+
"View table should be empty after reopening the database"
2583+
);
2584+
2585+
let tx = begin_mut_tx(&stdb);
2586+
let subs_rows = tx.lookup_st_view_subs(view_id)?;
2587+
assert!(
2588+
subs_rows.is_empty(),
2589+
"st_view_subs should be empty after reopening the database"
2590+
);
2591+
Ok(())
2592+
}
2593+
25102594
#[test]
25112595
fn test_table_name() -> ResultTest<()> {
25122596
let stdb = TestDB::durable()?;

crates/datastore/src/traits.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ pub struct TxData {
197197
/// Set of ephemeral tables modified in this transaction (only populated when a view is executed).
198198
/// These tables do not need to be persisted to disk.
199199
/// Every table listed here must appear in either `inserts` or `deletes`.
200-
ephermal_tables: Option<EphemeralTables>,
200+
ephemeral_tables: Option<EphemeralTables>,
201201
}
202202

203203
impl TxData {
@@ -241,15 +241,15 @@ impl TxData {
241241
pub fn set_ephemeral_tables(&mut self, all_ephemeral_tables: &EphemeralTables) {
242242
for tid in self.tables.keys() {
243243
if all_ephemeral_tables.contains(tid) {
244-
self.ephermal_tables
244+
self.ephemeral_tables
245245
.get_or_insert_with(EphemeralTables::default)
246246
.insert(*tid);
247247
}
248248
}
249249
}
250250

251-
pub fn ephermal_tables(&self) -> Option<&EphemeralTables> {
252-
self.ephermal_tables.as_ref()
251+
pub fn ephemeral_tables(&self) -> Option<&EphemeralTables> {
252+
self.ephemeral_tables.as_ref()
253253
}
254254

255255
/// Obtain an iterator over the inserted rows per table.
@@ -286,7 +286,7 @@ impl TxData {
286286
pub fn durable_deletes(&self) -> impl Iterator<Item = (&TableId, &Arc<[ProductValue]>)> + '_ {
287287
self.deletes.iter().filter(move |(table_id, _)| {
288288
!self
289-
.ephermal_tables
289+
.ephemeral_tables
290290
.as_ref()
291291
.is_some_and(|etables| etables.contains(*table_id))
292292
})

0 commit comments

Comments
 (0)