Skip to content

Commit 758671f

Browse files
committed
add more docs to storage layer
Signed-off-by: Yuchen Liang <[email protected]>
1 parent b5150ca commit 758671f

File tree

3 files changed

+66
-36
lines changed

3 files changed

+66
-36
lines changed

optd-core/src/cascades/memo.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,45 +17,51 @@ use anyhow::Result;
1717

1818
#[trait_variant::make(Send)]
1919
pub trait Memoize: Send + Sync + 'static {
20+
/// Gets all logical expressions in a group.
2021
async fn get_all_logical_exprs_in_group(
2122
&self,
2223
group_id: RelationalGroupId,
2324
) -> Result<Vec<(LogicalExpressionId, Arc<LogicalExpression>)>>;
2425

25-
// Returns the group id of new group if merge happened.
26+
/// Adds a logical expression to an existing group.
27+
/// Returns the group id of new group if merge happened.
2628
async fn add_logical_expr_to_group(
2729
&self,
2830
logical_expr: &LogicalExpression,
2931
group_id: RelationalGroupId,
3032
) -> Result<RelationalGroupId>;
3133

32-
// Returns the group id of group if already exists, otherwise creates a new group.
34+
/// Adds a logical expression to the memo table.
35+
/// Returns the group id of group if already exists, otherwise creates a new group.
3336
async fn add_logical_expr(&self, logical_expr: &LogicalExpression)
3437
-> Result<RelationalGroupId>;
3538

39+
/// Gets all scalar expressions in a group.
3640
async fn get_all_scalar_exprs_in_group(
3741
&self,
3842
group_id: ScalarGroupId,
3943
) -> Result<Vec<(ScalarExpressionId, Arc<ScalarExpression>)>>;
4044

41-
// Returns the group id of new group if merge happened.
45+
/// Adds a scalar expression to an existing group.
46+
/// Returns the group id of new group if merge happened.
4247
async fn add_scalar_expr_to_group(
4348
&self,
4449
scalar_expr: &ScalarExpression,
4550
group_id: ScalarGroupId,
4651
) -> Result<ScalarGroupId>;
4752

48-
// Returns the group id of group if already exists, otherwise creates a new group.
53+
/// Adds a scalar expression to the memo table.
54+
/// Returns the group id of group if already exists, otherwise creates a new group.
4955
async fn add_scalar_expr(&self, scalar_expr: &ScalarExpression) -> Result<ScalarGroupId>;
5056

51-
// Merges two relational groups and returns the new group id.
57+
/// Merges two relational groups and returns the new group id.
5258
async fn merge_relation_group(
5359
&self,
5460
from: RelationalGroupId,
5561
to: RelationalGroupId,
5662
) -> Result<RelationalGroupId>;
5763

58-
// Merges two scalar groups and returns the new group id.
64+
/// Merges two scalar groups and returns the new group id.
5965
async fn merge_scalar_group(
6066
&self,
6167
from: ScalarGroupId,

optd-core/src/storage/memo.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! An implementation of the memo table using SQLite.
2+
13
use std::{str::FromStr, sync::Arc, time::Duration};
24

35
use super::transaction::Transaction;
@@ -184,8 +186,9 @@ impl Memoize for SqliteMemo {
184186
}
185187
}
186188

187-
// Memoize helpers
189+
// Helper functions for implementing the `Memoize` trait.
188190
impl SqliteMemo {
191+
/// Gets the representative group id of a relational group.
189192
async fn get_representative_group_id(
190193
&self,
191194
db: &mut SqliteConnection,
@@ -199,6 +202,7 @@ impl SqliteMemo {
199202
Ok(representative_group_id)
200203
}
201204

205+
/// Sets the representative group id of a relational group.
202206
async fn set_representative_group_id(
203207
&self,
204208
db: &mut SqliteConnection,
@@ -213,6 +217,7 @@ impl SqliteMemo {
213217
Ok(())
214218
}
215219

220+
/// Gets the representative group id of a scalar group.
216221
async fn get_representative_scalar_group_id(
217222
&self,
218223
db: &mut SqliteConnection,
@@ -226,6 +231,7 @@ impl SqliteMemo {
226231
Ok(representative_group_id)
227232
}
228233

234+
/// Sets the representative group id of a scalar group.
229235
async fn set_representative_scalar_group_id(
230236
&self,
231237
db: &mut SqliteConnection,
@@ -240,6 +246,10 @@ impl SqliteMemo {
240246
Ok(())
241247
}
242248

249+
/// Inserts a scalar expression into the database. If the `add_to_group_id` is `Some`,
250+
/// we will attempt to add the scalar expression to the specified group.
251+
/// If the scalar expression already exists in the database, the existing group id will be returned.
252+
/// Otherwise, a new group id will be created.
243253
async fn add_scalar_expr_to_group_inner(
244254
&self,
245255
scalar_expr: &ScalarExpression,
@@ -304,9 +314,6 @@ impl SqliteMemo {
304314
ScalarOperatorKind::Add,
305315
)
306316
.await?;
307-
println!("add: {:?}", add);
308-
println!("scalar_expr_id: {:?}", scalar_expr_id);
309-
println!("group_id: {:?}", group_id);
310317

311318
sqlx::query_scalar("INSERT INTO scalar_adds (scalar_expression_id, group_id, left_group_id, right_group_id) VALUES ($1, $2, $3, $4) ON CONFLICT DO UPDATE SET group_id = group_id RETURNING group_id")
312319
.bind(scalar_expr_id)
@@ -351,7 +358,7 @@ impl SqliteMemo {
351358
Ok(inserted_group_id)
352359
}
353360

354-
/// Inserts a scalar expression into the database.
361+
/// Inserts an entry into the `scalar_expressions` table.
355362
async fn insert_into_scalar_expressions(
356363
db: &mut SqliteConnection,
357364
scalar_expr_id: ScalarExpressionId,
@@ -368,6 +375,7 @@ impl SqliteMemo {
368375
Ok(())
369376
}
370377

378+
/// Removes a dangling scalar expression from the `scalar_expressions` table.
371379
async fn remove_dangling_scalar_expr(
372380
&self,
373381
db: &mut SqliteConnection,
@@ -380,6 +388,10 @@ impl SqliteMemo {
380388
Ok(())
381389
}
382390

391+
/// Inserts a logical expression into the memo table. If the `add_to_group_id` is `Some`,
392+
/// we will attempt to add the logical expression to the specified group.
393+
/// If the logical expression already exists in the database, the existing group id will be returned.
394+
/// Otherwise, a new group id will be created.
383395
async fn add_logical_expr_to_group_inner(
384396
&self,
385397
logical_expr: &LogicalExpression,
@@ -477,6 +489,7 @@ impl SqliteMemo {
477489
Ok(inserted_group_id)
478490
}
479491

492+
/// Inserts an entry into the `logical_expressions` table.
480493
async fn insert_into_logical_expressions(
481494
txn: &mut SqliteConnection,
482495
logical_expr_id: LogicalExpressionId,
@@ -493,6 +506,7 @@ impl SqliteMemo {
493506
Ok(())
494507
}
495508

509+
/// Removes a dangling logical expression from the `logical_expressions` table.
496510
async fn remove_dangling_logical_expr(
497511
&self,
498512
db: &mut SqliteConnection,
@@ -507,6 +521,8 @@ impl SqliteMemo {
507521
}
508522

509523
/// The SQL query to get all logical expressions in a group.
524+
/// For each of the operators, the logical_expression_id is selected,
525+
/// as well as the data fields in json form.
510526
const fn get_all_logical_exprs_in_group_query() -> &'static str {
511527
concat!(
512528
"SELECT logical_expression_id, json_object('Scan', json_object('table_name', json(table_name), 'predicate', predicate_group_id)) as data FROM scans WHERE group_id = $1",
@@ -518,6 +534,8 @@ const fn get_all_logical_exprs_in_group_query() -> &'static str {
518534
}
519535

520536
/// The SQL query to get all scalar expressions in a group.
537+
/// For each of the operators, the scalar_expression_id is selected,
538+
/// as well as the data fields in json form.
521539
const fn get_all_scalar_exprs_in_group_query() -> &'static str {
522540
concat!(
523541
"SELECT scalar_expression_id, json_object('Constant', json(value)) as data FROM scalar_constants WHERE group_id = $1",

optd-core/src/storage/transaction.rs

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! A transaction that wraps a SQLite transaction while making it easy to generate new identifiers.
2+
13
use sqlx::SqliteConnection;
24
use std::ops::{Deref, DerefMut};
35

@@ -6,38 +8,18 @@ use crate::cascades::{
68
groups::{RelationalGroupId, ScalarGroupId},
79
};
810

9-
/// Sequence is a unique generator for the entities in the optd storage layer.
10-
struct Sequence;
11-
12-
impl Sequence {
13-
/// Returns the current value in the sequence.
14-
pub async fn value(db: &mut SqliteConnection) -> anyhow::Result<i64> {
15-
let value = sqlx::query_scalar!("SELECT current_value FROM id_sequences WHERE id = 0")
16-
.fetch_one(db)
17-
.await?;
18-
Ok(value)
19-
}
20-
21-
/// Sets the current value of the sequence to the given value.
22-
pub async fn set_value(db: &mut SqliteConnection, value: i64) -> anyhow::Result<()> {
23-
sqlx::query!(
24-
"UPDATE id_sequences SET current_value = ? WHERE id = 0",
25-
value
26-
)
27-
.execute(db)
28-
.await?;
29-
Ok(())
30-
}
31-
}
32-
3311
/// A transaction that wraps a SQLite transaction.
3412
pub struct Transaction<'c> {
3513
/// An active SQLite transaction.
3614
txn: sqlx::Transaction<'c, sqlx::Sqlite>,
15+
/// The current value of the sequence in the transaction.
16+
/// The value is read from the database on transaction start and
17+
/// persisted back to the database on commit.
3718
current_value: i64,
3819
}
3920

4021
impl Transaction<'_> {
22+
/// Creates a new transaction.
4123
pub async fn new(
4224
mut txn: sqlx::Transaction<'_, sqlx::Sqlite>,
4325
) -> anyhow::Result<Transaction<'_>> {
@@ -102,14 +84,38 @@ impl DerefMut for Transaction<'_> {
10284
}
10385
}
10486

105-
// TODO(alexis): This just checks the sequencing logic.
87+
/// Sequence is a unique generator for the entities in the optd storage layer.
88+
struct Sequence;
89+
90+
impl Sequence {
91+
/// Returns the current value in the sequence.
92+
pub async fn value(db: &mut SqliteConnection) -> anyhow::Result<i64> {
93+
let value = sqlx::query_scalar!("SELECT current_value FROM id_sequences WHERE id = 0")
94+
.fetch_one(db)
95+
.await?;
96+
Ok(value)
97+
}
98+
99+
/// Sets the current value of the sequence to the given value.
100+
pub async fn set_value(db: &mut SqliteConnection, value: i64) -> anyhow::Result<()> {
101+
sqlx::query!(
102+
"UPDATE id_sequences SET current_value = ? WHERE id = 0",
103+
value
104+
)
105+
.execute(db)
106+
.await?;
107+
Ok(())
108+
}
109+
}
110+
106111
#[cfg(test)]
107112
mod tests {
108113

109114
use crate::storage::memo::SqliteMemo;
110115

111116
use super::*;
112117

118+
/// Test if the sequence is working correctly with the transaction.
113119
#[tokio::test]
114120
async fn test_sequence() -> anyhow::Result<()> {
115121
let storage = SqliteMemo::new_in_memory().await?;

0 commit comments

Comments
 (0)