Skip to content

Commit e5d9e91

Browse files
committed
change to BackendManager
1 parent c6d3ee1 commit e5d9e91

File tree

5 files changed

+48
-47
lines changed

5 files changed

+48
-47
lines changed

optd-persistent/src/lib.rs

Lines changed: 35 additions & 3 deletions
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

@@ -6,15 +8,45 @@ use migrator::Migrator;
68

79
mod entities;
810

9-
mod expression;
10-
1111
mod memo;
1212
pub use memo::interface::Memo;
13-
pub use memo::orm::MemoTable;
1413

1514
pub const DATABASE_URL: &str = "sqlite:./sqlite.db?mode=rwc";
1615
pub const DATABASE_FILE: &str = "./sqlite.db";
1716

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

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/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
mod expression;
2+
13
pub mod interface;
24
pub mod orm;

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)