Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[query] Consolidate alias query #463 #464

Merged
merged 7 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions src/agdb/commands_mut.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
pub mod insert_alias;
pub mod insert_alias_id;
pub mod insert_alias_id_result;
pub mod insert_alias_result;
pub mod insert_edge;
pub mod insert_index;
pub mod insert_index_id;
Expand All @@ -17,9 +14,6 @@ pub mod remove_node;
pub mod remove_node_index;

use self::insert_alias::InsertAlias;
use self::insert_alias_id::InsertAliasId;
use self::insert_alias_id_result::InsertAliasIdResult;
use self::insert_alias_result::InsertAliasResult;
use self::insert_edge::InsertEdge;
use self::insert_index::InsertIndex;
use self::insert_index_id::InsertIndexId;
Expand All @@ -38,9 +32,6 @@ use self::remove_node_index::RemoveNodeIndex;
pub enum CommandsMut {
None,
InsertAlias(InsertAlias),
InsertAliasResult(InsertAliasResult),
InsertAliasId(InsertAliasId),
InsertAliasIdResult(InsertAliasIdResult),
InsertEdge(InsertEdge),
InsertIndex(InsertIndex),
InsertIndexId(InsertIndexId),
Expand All @@ -62,14 +53,29 @@ mod tests {

#[test]
fn derived_from_debug() {
format!("{:?}", CommandsMut::InsertNode(InsertNode {}));
format!(
"{:?}",
CommandsMut::InsertAlias(InsertAlias {
id: None,
alias: String::new(),
result: false
})
);
}

#[test]
fn derived_from_partial_eq() {
assert_eq!(
CommandsMut::InsertNode(InsertNode {}),
CommandsMut::InsertNode(InsertNode {})
CommandsMut::InsertAlias(InsertAlias {
id: None,
alias: String::new(),
result: false
}),
CommandsMut::InsertAlias(InsertAlias {
id: None,
alias: String::new(),
result: false
})
);
}
}
39 changes: 30 additions & 9 deletions src/agdb/commands_mut/insert_alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,44 @@ use super::remove_alias::RemoveAlias;
use super::CommandsMut;
use crate::db::db_context::Context;
use crate::Db;
use crate::DbId;
use crate::QueryError;
use crate::QueryResult;

#[derive(Debug, PartialEq)]
pub struct InsertAlias {
pub(crate) id: Option<DbId>,
pub(crate) alias: String,
pub(crate) result: bool,
}

impl InsertAlias {
pub(crate) fn process(
&self,
db: &mut Db,
result: &mut QueryResult,
context: &Context,
) -> Result<CommandsMut, QueryError> {
if self.alias.is_empty() {
return Err(QueryError::from("Empty alias is not allowed"));
let undo = insert_alias(db, &self.id.unwrap_or(context.id), &self.alias)?;

if self.result {
result.result += 1;
}

db.aliases.insert(&self.alias, &context.id)?;
Ok(undo)
}
}

Ok(CommandsMut::RemoveAlias(RemoveAlias {
alias: self.alias.clone(),
}))
fn insert_alias(db: &mut Db, id: &DbId, alias: &String) -> Result<CommandsMut, QueryError> {
if alias.is_empty() {
return Err(QueryError::from("Empty alias is not allowed"));
}

db.aliases.insert(alias, id)?;

Ok(CommandsMut::RemoveAlias(RemoveAlias {
alias: alias.clone(),
}))
}

#[cfg(test)]
Expand All @@ -36,7 +51,9 @@ mod tests {
format!(
"{:?}",
InsertAlias {
alias: String::new()
id: None,
alias: String::new(),
result: false
}
);
}
Expand All @@ -45,10 +62,14 @@ mod tests {
fn derived_from_partial_eq() {
assert_eq!(
InsertAlias {
alias: String::new()
id: None,
alias: String::new(),
result: false
},
InsertAlias {
alias: String::new()
id: None,
alias: String::new(),
result: false
}
);
}
Expand Down
55 changes: 0 additions & 55 deletions src/agdb/commands_mut/insert_alias_id.rs

This file was deleted.

51 changes: 0 additions & 51 deletions src/agdb/commands_mut/insert_alias_id_result.rs

This file was deleted.

49 changes: 0 additions & 49 deletions src/agdb/commands_mut/insert_alias_result.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/agdb/commands_mut/insert_edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl InsertEdge {
) -> Result<CommandsMut, QueryError> {
let (from, to) = self.get_from_to(db)?;
context.graph_index = db.graph.insert_edge(&from, &to)?;
context.id = DbId { id: -db.next_index };
context.id = DbId(-db.next_index);

Ok(CommandsMut::RemoveEdgeIndex(RemoveEdgeIndex {
index: context.graph_index,
Expand Down
6 changes: 3 additions & 3 deletions src/agdb/commands_mut/insert_index_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mod tests {
format!(
"{:?}",
InsertIndexId {
id: DbId { id: 0 },
id: DbId(0),
graph_index: GraphIndex { index: 0 }
}
);
Expand All @@ -38,11 +38,11 @@ mod tests {
fn derived_from_partial_eq() {
assert_eq!(
InsertIndexId {
id: DbId { id: 0 },
id: DbId(0),
graph_index: GraphIndex { index: 0 }
},
InsertIndexId {
id: DbId { id: 0 },
id: DbId(0),
graph_index: GraphIndex { index: 0 }
}
);
Expand Down
2 changes: 1 addition & 1 deletion src/agdb/commands_mut/insert_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl InsertNode {
context: &mut Context,
) -> Result<CommandsMut, QueryError> {
context.graph_index = db.graph.insert_node()?;
context.id = DbId { id: db.next_index };
context.id = DbId(db.next_index);

Ok(CommandsMut::RemoveNodeIndex(RemoveNodeIndex {
index: context.graph_index,
Expand Down
7 changes: 4 additions & 3 deletions src/agdb/commands_mut/remove_alias.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::insert_alias_id::InsertAliasId;
use super::insert_alias::InsertAlias;
use super::CommandsMut;
use crate::db::db_context::Context;
use crate::Db;
Expand All @@ -18,9 +18,10 @@ impl RemoveAlias {
if let Some(id) = db.aliases.value(&self.alias)? {
context.id = id;
db.aliases.remove_key(&self.alias)?;
Ok(CommandsMut::InsertAliasId(InsertAliasId {
id: context.id,
Ok(CommandsMut::InsertAlias(InsertAlias {
id: Some(context.id),
alias: self.alias.clone(),
result: false,
}))
} else {
Ok(CommandsMut::None)
Expand Down
14 changes: 6 additions & 8 deletions src/agdb/commands_mut/remove_alias_id.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::insert_alias_id::InsertAliasId;
use super::insert_alias::InsertAlias;
use super::CommandsMut;
use crate::Db;
use crate::DbId;
Expand All @@ -13,9 +13,10 @@ impl RemoveAliasId {
pub(crate) fn process(&self, db: &mut Db) -> Result<CommandsMut, QueryError> {
if let Some(alias) = db.aliases.key(&self.id)? {
db.aliases.remove_value(&self.id)?;
Ok(CommandsMut::InsertAliasId(InsertAliasId {
id: self.id,
Ok(CommandsMut::InsertAlias(InsertAlias {
id: Some(self.id),
alias,
result: false,
}))
} else {
Ok(CommandsMut::None)
Expand All @@ -29,14 +30,11 @@ mod tests {

#[test]
fn derived_from_debug() {
format!("{:?}", RemoveAliasId { id: DbId { id: 0 } });
format!("{:?}", RemoveAliasId { id: DbId(0) });
}

#[test]
fn derived_from_partial_eq() {
assert_eq!(
RemoveAliasId { id: DbId { id: 0 } },
RemoveAliasId { id: DbId { id: 0 } }
);
assert_eq!(RemoveAliasId { id: DbId(0) }, RemoveAliasId { id: DbId(0) });
}
}
Loading