Skip to content

Commit 9435a93

Browse files
committed
change to BackendManager
1 parent c6d3ee1 commit 9435a93

File tree

3 files changed

+46
-45
lines changed

3 files changed

+46
-45
lines changed

optd-persistent/src/lib.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::atomic::AtomicUsize;
2+
13
use sea_orm::*;
24
use sea_orm_migration::prelude::*;
35

@@ -10,11 +12,43 @@ mod expression;
1012

1113
mod memo;
1214
pub use memo::interface::Memo;
13-
pub use memo::orm::MemoTable;
1415

1516
pub const DATABASE_URL: &str = "sqlite:./sqlite.db?mode=rwc";
1617
pub const DATABASE_FILE: &str = "./sqlite.db";
1718

19+
/// The different kinds of errors that might occur while running operations on a memo table.
20+
pub enum MemoError {
21+
UnknownGroup,
22+
UnknownLogicalExpression,
23+
UnknownPhysicalExpression,
24+
InvalidExpression,
25+
Database(DbErr),
26+
}
27+
28+
impl From<DbErr> for MemoError {
29+
fn from(value: DbErr) -> Self {
30+
MemoError::Database(value)
31+
}
32+
}
33+
34+
/// A type alias for a result with [`MemoError`] as the error type.
35+
pub type Result<T> = std::result::Result<T, MemoError>;
36+
37+
pub struct BackendManager {
38+
db: DatabaseConnection,
39+
latest_epoch_id: AtomicUsize,
40+
}
41+
42+
impl BackendManager {
43+
/// Creates a new `BackendManager`.
44+
pub async fn new() -> Result<Self> {
45+
Ok(Self {
46+
db: Database::connect(DATABASE_URL).await?,
47+
latest_epoch_id: AtomicUsize::new(0),
48+
})
49+
}
50+
}
51+
1852
pub async fn migrate(db: &DatabaseConnection) -> std::result::Result<(), DbErr> {
1953
Migrator::refresh(db).await
2054
}

optd-persistent/src/memo/interface.rs

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,4 @@
1-
use sea_orm::*;
2-
3-
/// The different kinds of errors that might occur while running operations on a memo table.
4-
pub enum MemoError {
5-
UnknownGroup,
6-
UnknownLogicalExpression,
7-
UnknownPhysicalExpression,
8-
InvalidExpression,
9-
Database(DbErr),
10-
}
11-
12-
impl From<DbErr> for MemoError {
13-
fn from(value: DbErr) -> Self {
14-
MemoError::Database(value)
15-
}
16-
}
17-
18-
/// A type alias for a result with [`MemoError`] as the error type.
19-
pub type Result<T> = std::result::Result<T, MemoError>;
1+
use crate::Result;
202

213
/// A trait representing an implementation of a memoization table.
224
///
@@ -114,7 +96,7 @@ pub trait Memo {
11496
///
11597
/// If the group does not exist, returns a [`MemoError::UnknownGroup`] error.
11698
async fn update_group_winner(
117-
&mut self,
99+
&self,
118100
group_id: Self::GroupId,
119101
physical_expression_id: Self::PhysicalExpressionId,
120102
) -> Result<Option<Self::PhysicalExpressionId>>;
@@ -123,7 +105,7 @@ pub trait Memo {
123105
///
124106
/// If the group does not exist, returns a [`MemoError::UnknownGroup`] error.
125107
async fn add_logical_expression_to_group(
126-
&mut self,
108+
&self,
127109
group_id: Self::GroupId,
128110
logical_expression: Self::LogicalExpression,
129111
) -> Result<()>;
@@ -132,7 +114,7 @@ pub trait Memo {
132114
///
133115
/// If the group does not exist, returns a [`MemoError::UnknownGroup`] error.
134116
async fn add_physical_expression_to_group(
135-
&mut self,
117+
&self,
136118
group_id: Self::GroupId,
137119
physical_expression: Self::PhysicalExpression,
138120
) -> Result<()>;
@@ -150,7 +132,7 @@ pub trait Memo {
150132
/// If the expression does not exist, this function will create a new group and a new
151133
/// expression, returning brand new IDs for both.
152134
async fn add_logical_expression(
153-
&mut self,
135+
&self,
154136
expression: Self::LogicalExpression,
155137
) -> Result<(Self::GroupId, Self::LogicalExpressionId)>;
156138
}

optd-persistent/src/memo/orm.rs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,10 @@
11
use crate::{
22
entities::{prelude::*, *},
3-
DATABASE_URL,
3+
BackendManager, {Memo, MemoError, Result},
44
};
55
use sea_orm::*;
66

7-
use super::interface::{Memo, MemoError, Result};
8-
9-
pub struct MemoTable {
10-
db: DatabaseConnection,
11-
}
12-
13-
impl MemoTable {
14-
/// Creates a new `MemoTable`.
15-
pub async fn new() -> Result<Self> {
16-
Ok(Self {
17-
db: Database::connect(DATABASE_URL).await?,
18-
})
19-
}
20-
}
21-
22-
impl Memo for MemoTable {
7+
impl Memo for BackendManager {
238
type Group = cascades_group::Model;
249
type GroupId = i32;
2510
type LogicalExpression = logical_expression::Model;
@@ -113,7 +98,7 @@ impl Memo for MemoTable {
11398
}
11499

115100
async fn update_group_winner(
116-
&mut self,
101+
&self,
117102
group_id: Self::GroupId,
118103
physical_expression_id: Self::PhysicalExpressionId,
119104
) -> Result<Option<Self::PhysicalExpressionId>> {
@@ -130,7 +115,7 @@ impl Memo for MemoTable {
130115
}
131116

132117
async fn add_logical_expression_to_group(
133-
&mut self,
118+
&self,
134119
group_id: Self::GroupId,
135120
logical_expression: Self::LogicalExpression,
136121
) -> Result<()> {
@@ -151,7 +136,7 @@ impl Memo for MemoTable {
151136
}
152137

153138
async fn add_physical_expression_to_group(
154-
&mut self,
139+
&self,
155140
group_id: Self::GroupId,
156141
physical_expression: Self::PhysicalExpression,
157142
) -> Result<()> {
@@ -173,7 +158,7 @@ impl Memo for MemoTable {
173158

174159
/// Note that in this function, we ignore the group ID that the logical expression contains.
175160
async fn add_logical_expression(
176-
&mut self,
161+
&self,
177162
expression: Self::LogicalExpression,
178163
) -> Result<(Self::GroupId, Self::LogicalExpressionId)> {
179164
// Lookup all expressions that have the same fingerprint. There may be false positives, but

0 commit comments

Comments
 (0)