@@ -5,171 +5,16 @@ mod migrator;
55use migrator:: Migrator ;
66
77mod entities;
8- use entities:: { prelude:: * , * } ;
98
109mod expression;
1110
1211mod memo;
13- pub use memo:: MemoTable ;
12+ pub use memo:: interface:: Memo ;
13+ pub use memo:: orm:: MemoTable ;
1414
1515pub const DATABASE_URL : & str = "sqlite:./sqlite.db?mode=rwc" ;
1616pub const DATABASE_FILE : & str = "./sqlite.db" ;
1717
1818pub async fn migrate ( db : & DatabaseConnection ) -> std:: result:: Result < ( ) , DbErr > {
1919 Migrator :: refresh ( db) . await
2020}
21-
22- /// The different kinds of errors that might occur while running operations on a memo table.
23- pub enum MemoError {
24- UnknownGroup ,
25- UnknownLogicalExpression ,
26- UnknownPhysicalExpression ,
27- InvalidExpression ,
28- Database ( DbErr ) ,
29- }
30-
31- impl From < DbErr > for MemoError {
32- fn from ( value : DbErr ) -> Self {
33- MemoError :: Database ( value)
34- }
35- }
36-
37- /// A type alias for a result with [`MemoError`] as the error type.
38- type Result < T > = std:: result:: Result < T , MemoError > ;
39-
40- /// A trait representing an implementation of a memoization table.
41- ///
42- /// Note that we use [`trait_variant`] here in order to add bounds on every method.
43- /// See this [blog post](
44- /// https://blog.rust-lang.org/2023/12/21/async-fn-rpit-in-traits.html#async-fn-in-public-traits)
45- /// for more information.
46- ///
47- /// TODO Figure out for each when to get the ID of a record or the entire record itself.
48- #[ trait_variant:: make( Send ) ]
49- pub trait Memo {
50- /// A type representing a group in the Cascades framework.
51- type Group ;
52- /// A type representing a unique identifier for a group.
53- type GroupId ;
54- /// A type representing a logical expression.
55- type LogicalExpression ;
56- /// A type representing a unique identifier for a logical expression.
57- type LogicalExpressionId ;
58- /// A type representing a physical expression.
59- type PhysicalExpression ;
60- /// A type representing a unique identifier for a physical expression.
61- type PhysicalExpressionId ;
62-
63- /// Retrieves a [`Self::Group`] given a [`Self::GroupId`].
64- ///
65- /// If the group does not exist, returns a [`MemoError::UnknownGroup`] error.
66- async fn get_group ( & self , group_id : Self :: GroupId ) -> Result < Self :: Group > ;
67-
68- /// Retrieves all group IDs that are stored in the memo table.
69- async fn get_all_groups ( & self ) -> Result < Vec < Self :: Group > > ;
70-
71- /// Retrieves a [`Self::LogicalExpression`] given a [`Self::LogicalExpressionId`].
72- ///
73- /// If the logical expression does not exist, returns a [`MemoError::UnknownLogicalExpression`]
74- /// error.
75- async fn get_logical_expression (
76- & self ,
77- logical_expression_id : Self :: LogicalExpressionId ,
78- ) -> Result < Self :: LogicalExpression > ;
79-
80- /// Retrieves a [`Self::PhysicalExpression`] given a [`Self::PhysicalExpressionId`].
81- ///
82- /// If the physical expression does not exist, returns a
83- /// [`MemoError::UnknownPhysicalExpression`] error.
84- async fn get_physical_expression (
85- & self ,
86- physical_expression_id : Self :: PhysicalExpressionId ,
87- ) -> Result < Self :: PhysicalExpression > ;
88-
89- /// Retrieves the parent group ID of a logical expression given its expression ID.
90- ///
91- /// If the logical expression does not exist, returns a [`MemoError::UnknownLogicalExpression`]
92- /// error.
93- async fn get_group_from_logical_expression (
94- & self ,
95- logical_expression_id : Self :: LogicalExpressionId ,
96- ) -> Result < Self :: GroupId > ;
97-
98- /// Retrieves the parent group ID of a logical expression given its expression ID.
99- ///
100- /// If the physical expression does not exist, returns a
101- /// [`MemoError::UnknownPhysicalExpression`] error.
102- async fn get_group_from_physical_expression (
103- & self ,
104- physical_expression_id : Self :: PhysicalExpressionId ,
105- ) -> Result < Self :: GroupId > ;
106-
107- /// Retrieves all of the logical expression "children" of a group.
108- ///
109- /// If the group does not exist, returns a [`MemoError::UnknownGroup`] error.
110- async fn get_group_logical_expressions (
111- & self ,
112- group_id : Self :: GroupId ,
113- ) -> Result < Vec < Self :: LogicalExpression > > ;
114-
115- /// Retrieves all of the physical expression "children" of a group.
116- ///
117- /// If the group does not exist, returns a [`MemoError::UnknownGroup`] error.
118- async fn get_group_physical_expressions (
119- & self ,
120- group_id : Self :: GroupId ,
121- ) -> Result < Vec < Self :: PhysicalExpression > > ;
122-
123- /// Retrieves the best physical query plan (winner) for a given group.
124- ///
125- /// If the group does not exist, returns a [`MemoError::UnknownGroup`] error.
126- async fn get_winner (
127- & self ,
128- group_id : Self :: GroupId ,
129- ) -> Result < Option < Self :: PhysicalExpressionId > > ;
130-
131- /// Updates / replaces a group's best physical plan (winner). Optionally returns the previous
132- /// winner's physical expression ID.
133- ///
134- /// If the group does not exist, returns a [`MemoError::UnknownGroup`] error.
135- async fn update_group_winner (
136- & mut self ,
137- group_id : Self :: GroupId ,
138- physical_expression_id : Self :: PhysicalExpressionId ,
139- ) -> Result < Option < Self :: PhysicalExpressionId > > ;
140-
141- /// Adds a logical expression to an existing group via its [`Self::GroupId`].
142- ///
143- /// If the group does not exist, returns a [`MemoError::UnknownGroup`] error.
144- async fn add_logical_expression_to_group (
145- & mut self ,
146- group_id : Self :: GroupId ,
147- logical_expression : Self :: LogicalExpression ,
148- ) -> Result < ( ) > ;
149-
150- /// Adds a physical expression to an existing group via its [`Self::GroupId`].
151- ///
152- /// If the group does not exist, returns a [`MemoError::UnknownGroup`] error.
153- async fn add_physical_expression_to_group (
154- & mut self ,
155- group_id : Self :: GroupId ,
156- physical_expression : Self :: PhysicalExpression ,
157- ) -> Result < ( ) > ;
158-
159- /// Adds a new logical expression into the memo table, creating a new group if the expression
160- /// does not already exist.
161- ///
162- /// The [`Self::LogicalExpression`] type should have some sort of mechanism for checking if
163- /// the expression has been seen before, and if it has already been created, then the parent
164- /// group ID should also be retrievable.
165- ///
166- /// If the expression already exists, then this function will return the [`Self::GroupId`] of
167- /// the parent group and the corresponding (already existing) [`Self::LogicalExpressionId`].
168- ///
169- /// If the expression does not exist, this function will create a new group and a new
170- /// expression, returning brand new IDs for both.
171- async fn add_logical_expression (
172- & mut self ,
173- expression : Self :: LogicalExpression ,
174- ) -> Result < ( Self :: GroupId , Self :: LogicalExpressionId ) > ;
175- }
0 commit comments