Skip to content

Commit 9c98703

Browse files
authored
chore: remove latest epoch id and add some todo documentation comments (#26)
1 parent 592b283 commit 9c98703

File tree

4 files changed

+24
-32
lines changed

4 files changed

+24
-32
lines changed

optd-persistent/src/cost_model/catalog/mock_catalog.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pub struct MockTrigger {
5959
pub function: String,
6060
}
6161

62+
/// TODO: documentation
6263
#[derive(Default)]
6364
pub struct MockCatalog {
6465
pub databases: Vec<MockDatabaseMetadata>,

optd-persistent/src/cost_model/interface.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,44 +11,51 @@ use sea_orm_migration::prelude::*;
1111
use serde_json::json;
1212
use std::sync::Arc;
1313

14+
/// TODO: documentation
1415
pub enum CatalogSource {
1516
Iceberg(),
1617
Mock,
1718
}
1819

20+
/// TODO: documentation
1921
pub enum AttrType {
2022
Integer,
2123
Float,
2224
Varchar,
2325
Boolean,
2426
}
2527

28+
/// TODO: documentation
2629
pub enum IndexType {
2730
BTree,
2831
Hash,
2932
}
3033

34+
/// TODO: documentation
3135
pub enum ConstraintType {
3236
PrimaryKey,
3337
ForeignKey,
3438
Unique,
3539
Check,
3640
}
3741

42+
/// TODO: documentation
3843
pub enum StatType {
3944
Count,
4045
Cardinality,
4146
Min,
4247
Max,
4348
}
4449

50+
/// TODO: documentation
4551
#[derive(PartialEq)]
4652
pub enum EpochOption {
4753
// TODO(lanlou): Could I make i32 -> EpochId?
4854
Existed(i32),
4955
New(String, String),
5056
}
5157

58+
/// TODO: documentation
5259
#[derive(Clone)]
5360
pub struct Stat {
5461
pub stat_type: i32,
@@ -58,6 +65,7 @@ pub struct Stat {
5865
pub name: String,
5966
}
6067

68+
/// TODO: documentation
6169
#[trait_variant::make(Send)]
6270
pub trait CostModelStorageLayer {
6371
type GroupId;
@@ -68,11 +76,7 @@ pub trait CostModelStorageLayer {
6876
type StatId;
6977

7078
// TODO: Change EpochId to event::Model::epoch_id
71-
async fn create_new_epoch(
72-
&mut self,
73-
source: String,
74-
data: String,
75-
) -> StorageResult<Self::EpochId>;
79+
async fn create_new_epoch(&self, source: String, data: String) -> StorageResult<Self::EpochId>;
7680

7781
async fn update_stats_from_catalog(
7882
&self,
@@ -81,7 +85,7 @@ pub trait CostModelStorageLayer {
8185
) -> StorageResult<()>;
8286

8387
async fn update_stats(
84-
&mut self,
88+
&self,
8589
stat: Stat,
8690
epoch_option: EpochOption,
8791
) -> StorageResult<Option<Self::EpochId>>;

optd-persistent/src/cost_model/orm.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,19 @@ impl CostModelStorageLayer for BackendManager {
3838
type EpochId = i32;
3939
type StatId = i32;
4040

41-
async fn create_new_epoch(
42-
&mut self,
43-
source: String,
44-
data: String,
45-
) -> StorageResult<Self::EpochId> {
41+
/// TODO: documentation
42+
async fn create_new_epoch(&self, source: String, data: String) -> StorageResult<Self::EpochId> {
4643
let new_event = event::ActiveModel {
4744
source_variant: sea_orm::ActiveValue::Set(source),
4845
timestamp: sea_orm::ActiveValue::Set(Utc::now()),
4946
data: sea_orm::ActiveValue::Set(sea_orm::JsonValue::String(data)),
5047
..Default::default()
5148
};
5249
let insert_res = Event::insert(new_event).exec(&self.db).await?;
53-
self.latest_epoch_id.store(
54-
insert_res.last_insert_id as usize,
55-
std::sync::atomic::Ordering::Relaxed,
56-
);
5750
Ok(insert_res.last_insert_id)
5851
}
5952

53+
/// TODO: documentation
6054
async fn update_stats_from_catalog(
6155
&self,
6256
c: CatalogSource,
@@ -154,6 +148,7 @@ impl CostModelStorageLayer for BackendManager {
154148
}
155149
}
156150

151+
/// TODO: improve the documentation
157152
/* Update the statistics in the database.
158153
* The statistic can be newly inserted or updated. If the statistic value
159154
* is the same as the latest existing one, the update will be ignored, and
@@ -166,14 +161,14 @@ impl CostModelStorageLayer for BackendManager {
166161
* If the statistic value is the same as the latest existing one, this function
167162
* won't create a new epoch.
168163
*
169-
* For batch updates, if the caller can directly call this function with
164+
* For batch updates, the caller can directly call this function with
170165
* New epoch option at the first time, and if the epoch_id is returned, the
171166
* caller can use the returned epoch_id for the rest of the updates.
172167
* But if the epoch_id is not returned, the caller should continue using
173168
* the New epoch option for the next statistic update.
174169
*/
175170
async fn update_stats(
176-
&mut self,
171+
&self,
177172
stat: Stat,
178173
epoch_option: EpochOption,
179174
) -> StorageResult<Option<Self::EpochId>> {
@@ -270,11 +265,9 @@ impl CostModelStorageLayer for BackendManager {
270265
}
271266
};
272267
// 1. Insert into attr_stats and related junction tables.
273-
let mut insert_new_epoch = false;
274268
let epoch_id = match epoch_option {
275269
EpochOption::Existed(e) => e,
276270
EpochOption::New(source, data) => {
277-
insert_new_epoch = true;
278271
let new_event = event::ActiveModel {
279272
source_variant: sea_orm::ActiveValue::Set(source),
280273
timestamp: sea_orm::ActiveValue::Set(Utc::now()),
@@ -317,19 +310,11 @@ impl CostModelStorageLayer for BackendManager {
317310
.exec(&transaction)
318311
.await?;
319312

320-
// TODO(lanlou): consider the update conflict for latest_epoch_id in multiple threads
321-
// Assume the txn fails to commit, and the epoch_id is updated. But the epoch_id
322-
// is always increasing and won't be overwritten even if the record is deleted, it
323-
// might be fine.
324-
if insert_new_epoch {
325-
self.latest_epoch_id
326-
.store(epoch_id as usize, std::sync::atomic::Ordering::Relaxed);
327-
}
328-
329313
transaction.commit().await?;
330314
Ok(Some(epoch_id))
331315
}
332316

317+
/// TODO: documentation
333318
async fn store_expr_stats_mappings(
334319
&self,
335320
expr_id: Self::ExprId,
@@ -350,6 +335,7 @@ impl CostModelStorageLayer for BackendManager {
350335
Ok(())
351336
}
352337

338+
/// TODO: documentation
353339
async fn get_stats_for_table(
354340
&self,
355341
table_id: i32,
@@ -377,6 +363,7 @@ impl CostModelStorageLayer for BackendManager {
377363
}
378364
}
379365

366+
/// TODO: documentation
380367
async fn get_stats_for_attr(
381368
&self,
382369
mut attr_ids: Vec<Self::AttrId>,
@@ -414,6 +401,7 @@ impl CostModelStorageLayer for BackendManager {
414401
}
415402
}
416403

404+
/// TODO: documentation
417405
async fn get_cost_analysis(
418406
&self,
419407
expr_id: Self::ExprId,
@@ -440,6 +428,7 @@ impl CostModelStorageLayer for BackendManager {
440428
Ok(cost.map(|c| c.cost))
441429
}
442430

431+
/// TODO: documentation
443432
async fn store_cost(
444433
&self,
445434
physical_expression_id: Self::ExprId,

optd-persistent/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(dead_code)]
22

3-
use std::{cell::LazyCell, sync::atomic::AtomicUsize};
3+
use std::cell::LazyCell;
44

55
use sea_orm::*;
66
use sea_orm_migration::prelude::*;
@@ -47,7 +47,7 @@ pub enum CostModelError {
4747
pub enum BackendError {
4848
CostModel(CostModelError),
4949
Database(DbErr),
50-
// Add other variants as needed for different error types
50+
// TODO: Add other variants as needed for different error types
5151
}
5252

5353
impl From<CostModelError> for BackendError {
@@ -64,15 +64,13 @@ impl From<DbErr> for BackendError {
6464

6565
pub struct BackendManager {
6666
db: DatabaseConnection,
67-
latest_epoch_id: AtomicUsize,
6867
}
6968

7069
impl BackendManager {
7170
/// Creates a new `BackendManager`.
7271
pub async fn new(database_url: Option<&str>) -> StorageResult<Self> {
7372
Ok(Self {
7473
db: Database::connect(database_url.unwrap_or(DATABASE_URL)).await?,
75-
latest_epoch_id: AtomicUsize::new(0),
7674
})
7775
}
7876
}

0 commit comments

Comments
 (0)