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

[db] Simplify insert alias #493 #494

Merged
merged 6 commits into from
May 1, 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
8 changes: 5 additions & 3 deletions src/agdb/commands_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,22 @@ pub enum CommandsMut {
#[cfg(test)]
mod tests {
use super::*;
use crate::query::query_id::QueryId;
use crate::DbId;

#[test]
fn derived_from_debug() {
format!(
"{:?}",
CommandsMut::InsertAlias(InsertAlias::new(String::new(), None, false))
CommandsMut::InsertAlias(InsertAlias::new(QueryId::Id(DbId(0)), String::new()))
);
}

#[test]
fn derived_from_partial_eq() {
assert_eq!(
CommandsMut::InsertAlias(InsertAlias::new(String::new(), None, false)),
CommandsMut::InsertAlias(InsertAlias::new(String::new(), None, false))
CommandsMut::InsertAlias(InsertAlias::new(QueryId::Id(DbId(0)), String::new())),
CommandsMut::InsertAlias(InsertAlias::new(QueryId::Id(DbId(0)), String::new()))
);
}
}
71 changes: 46 additions & 25 deletions src/agdb/commands_mut/insert_alias.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,67 @@
use crate::db::db_context::Context;
use crate::query::query_id::QueryId;
use crate::Db;
use crate::DbId;
use crate::QueryError;
use crate::QueryResult;

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

impl InsertAlias {
pub(crate) fn new(alias: String, id: Option<DbId>, result: bool) -> Self {
Self { id, alias, result }
pub(crate) fn new(id: QueryId, alias: String) -> Self {
Self {
id,
db_id: DbId(0),
alias,
old_alias: String::new(),
}
}

pub(crate) fn redo(
&mut self,
db: &mut Db,
result: &mut QueryResult,
context: &Context,
) -> Result<(), QueryError> {
pub(crate) fn redo(&mut self, db: &mut Db, result: &mut QueryResult) -> Result<(), QueryError> {
if self.alias.is_empty() {
return Err(QueryError::from("Empty alias is not allowed"));
}

let id = if let Some(id) = self.id {
id
} else {
self.id = Some(context.id);
context.id
self.db_id = match &self.id {
QueryId::Id(id) => {
if let Some(old_alias) = db.aliases.key(id)? {
self.old_alias = old_alias;
} else {
let _ = db
.indexes
.value(id)?
.ok_or(QueryError::from(format!("Id '{}' not found", id.0)))?;
}

*id
}
QueryId::Alias(old_alias) => {
self.old_alias = old_alias.clone();
db.aliases
.value(&self.old_alias)?
.ok_or(QueryError::from(format!("Alias '{}' not found", old_alias)))?
}
};

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

if self.result {
result.result += 1;
}
db.aliases.insert(&self.alias, &self.db_id)?;
result.result += 1;

Ok(())
}

pub(crate) fn undo(self, db: &mut Db) -> Result<(), QueryError> {
Ok(db.aliases.remove_key(&self.alias)?)
db.aliases.remove_key(&self.alias)?;

if !self.old_alias.is_empty() {
db.aliases.insert(&self.old_alias, &self.db_id)?;
}

Ok(())
}
}

Expand All @@ -53,14 +71,17 @@ mod tests {

#[test]
fn derived_from_debug() {
format!("{:?}", InsertAlias::new(String::new(), None, false));
format!(
"{:?}",
InsertAlias::new(QueryId::Id(DbId(0)), String::new())
);
}

#[test]
fn derived_from_partial_eq() {
assert_eq!(
InsertAlias::new(String::new(), None, false),
InsertAlias::new(String::new(), None, false)
InsertAlias::new(QueryId::Id(DbId(0)), String::new()),
InsertAlias::new(QueryId::Id(DbId(0)), String::new())
);
}
}
30 changes: 6 additions & 24 deletions src/agdb/query/insert_aliases_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use super::query_id::QueryId;
use super::query_ids::QueryIds;
use super::QueryMut;
use crate::commands_mut::insert_alias::InsertAlias;
use crate::commands_mut::remove_alias::RemoveAlias;
use crate::commands_mut::CommandsMut;
use crate::QueryError;

Expand All @@ -21,29 +20,14 @@ impl QueryMut for InsertAliasesQuery {
}

impl InsertAliasesQuery {
fn id(&self, id: &QueryId, new_alias: &str) -> Vec<CommandsMut> {
match id {
QueryId::Id(id) => {
vec![CommandsMut::InsertAlias(InsertAlias::new(
new_alias.to_string(),
Some(*id),
true,
))]
}
QueryId::Alias(alias) => {
vec![
CommandsMut::RemoveAlias(RemoveAlias::new(alias.clone(), None, false)),
CommandsMut::InsertAlias(InsertAlias::new(new_alias.to_string(), None, true)),
]
}
}
}

fn ids(&self, ids: &[QueryId]) -> Vec<CommandsMut> {
let mut commands = Vec::<CommandsMut>::new();

for (id, alias) in ids.iter().zip(&self.aliases) {
commands.extend(self.id(id, alias));
commands.push(CommandsMut::InsertAlias(InsertAlias::new(
id.clone(),
alias.to_string(),
)));
}

commands
Expand All @@ -66,9 +50,8 @@ mod tests {
assert_eq!(
query.commands(),
Ok(vec![CommandsMut::InsertAlias(InsertAlias::new(
QueryId::Id(DbId(0)),
"alias".to_string(),
Some(DbId(0)),
true
))])
)
}
Expand All @@ -83,9 +66,8 @@ mod tests {
assert_eq!(
query.commands(),
Ok(vec![CommandsMut::InsertAlias(InsertAlias::new(
QueryId::Id(DbId(0)),
"alias".to_string(),
Some(DbId(0)),
true
))])
)
}
Expand Down
5 changes: 5 additions & 0 deletions src/agdb/query/query_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ mod test {
assert!(!QueryId::from(0).is_node());
assert!(!QueryId::from(-1).is_node());
}

#[test]
fn from_db_id() {
let _ = QueryId::from(DbId(0));
}
}
2 changes: 1 addition & 1 deletion src/agdb/transaction_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl<'a> TransactionMut<'a> {
result: &mut QueryResult,
) -> Result<(), QueryError> {
match command {
CommandsMut::InsertAlias(data) => data.redo(db, result, context),
CommandsMut::InsertAlias(data) => data.redo(db, result),
CommandsMut::InsertEdge(data) => data.redo(db, result),
CommandsMut::InsertNode(data) => data.redo(db, result),
CommandsMut::RemoveAlias(data) => data.redo(db, result, context),
Expand Down
17 changes: 15 additions & 2 deletions tests/insert_aliases_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
mod test_file;

use agdb::Db;
use agdb::DbId;
use agdb::QueryBuilder;
use agdb::QueryError;
use test_file::TestFile;
Expand All @@ -13,7 +12,7 @@ fn insert_alias_of() {

let mut db = Db::new(test_file.file_name()).unwrap();
db.exec_mut(&QueryBuilder::insert().node().query()).unwrap();
let query = QueryBuilder::insert().alias("alias").of(DbId(1)).query();
let query = QueryBuilder::insert().alias("alias").of(1).query();
let result = db.exec_mut(&query).unwrap();

assert_eq!(result.result, 1);
Expand Down Expand Up @@ -104,3 +103,17 @@ fn insert_alias_empty() {

assert_eq!(error.description, "Empty alias is not allowed");
}

#[test]
fn insert_alias_old_alias_by_id() {
let test_file = TestFile::new();

let mut db = Db::new(test_file.file_name()).unwrap();
db.exec_mut(&QueryBuilder::insert().node().alias("old_alias").query())
.unwrap();
let query = QueryBuilder::insert().alias("alias").of(1).query();
let result = db.exec_mut(&query).unwrap();

assert_eq!(result.result, 1);
assert_eq!(result.elements, vec![]);
}