Skip to content

Commit

Permalink
[db] Make insert edge atomic #491 (#492)
Browse files Browse the repository at this point in the history
* Update commands_mut.rs

* Update insert_edge.rs

* Delete insert_index.rs

* Update insert_node.rs

* Update db.rs

* Update insert_edges_query.rs

* Update transaction_mut.rs
  • Loading branch information
michaelvlach authored May 1, 2023
1 parent f066940 commit 165deb3
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 108 deletions.
3 changes: 0 additions & 3 deletions src/agdb/commands_mut.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod insert_alias;
pub mod insert_edge;
pub mod insert_index;
pub mod insert_node;
pub mod remove_alias;
pub mod remove_edge;
Expand All @@ -9,7 +8,6 @@ pub mod remove_node;

use self::insert_alias::InsertAlias;
use self::insert_edge::InsertEdge;
use self::insert_index::InsertIndex;
use self::insert_node::InsertNode;
use self::remove_alias::RemoveAlias;
use self::remove_edge::RemoveEdge;
Expand All @@ -20,7 +18,6 @@ use self::remove_node::RemoveNode;
pub enum CommandsMut {
InsertAlias(InsertAlias),
InsertEdge(InsertEdge),
InsertIndex(InsertIndex),
InsertNode(InsertNode),
RemoveAlias(RemoveAlias),
RemoveEdge(RemoveEdge),
Expand Down
24 changes: 20 additions & 4 deletions src/agdb/commands_mut/insert_edge.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use crate::db::db_context::Context;
use crate::graph::graph_index::GraphIndex;
use crate::query::query_id::QueryId;
use crate::Db;
use crate::DbElement;
use crate::DbId;
use crate::QueryError;
use crate::QueryResult;

#[derive(Debug, PartialEq)]
pub struct InsertEdge {
id: DbId,
graph_index: GraphIndex,
from: QueryId,
to: QueryId,
Expand All @@ -14,22 +17,35 @@ pub struct InsertEdge {
impl InsertEdge {
pub(crate) fn new(from: QueryId, to: QueryId) -> Self {
Self {
id: DbId(0),
graph_index: GraphIndex { index: 0 },
from,
to,
}
}

pub(crate) fn redo(&mut self, db: &mut Db, context: &mut Context) -> Result<(), QueryError> {
pub(crate) fn redo(&mut self, db: &mut Db, result: &mut QueryResult) -> Result<(), QueryError> {
let from = db.graph_index_from_id(&self.from)?;
let to = db.graph_index_from_id(&self.to)?;
self.graph_index = db.graph.insert_edge(&from, &to)?;
context.graph_index = self.graph_index;
self.id = DbId(-db.next_id);
db.next_id += 1;
db.indexes.insert(&self.id, &self.graph_index)?;
result.result += 1;
result.elements.push(DbElement {
index: self.id,
values: vec![],
});

Ok(())
}

pub(crate) fn undo(self, db: &mut Db) -> Result<(), QueryError> {
Ok(db.graph.remove_edge(&self.graph_index)?)
db.graph.remove_edge(&self.graph_index)?;
db.indexes.remove_key(&self.id)?;
db.next_id -= 1;

Ok(())
}
}

Expand Down
60 changes: 0 additions & 60 deletions src/agdb/commands_mut/insert_index.rs

This file was deleted.

6 changes: 3 additions & 3 deletions src/agdb/commands_mut/insert_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ impl InsertNode {

pub(crate) fn redo(&mut self, db: &mut Db, result: &mut QueryResult) -> Result<(), QueryError> {
self.graph_index = db.graph.insert_node()?;
self.id = DbId(db.next_index);
db.next_index += 1;
self.id = DbId(db.next_id);
db.next_id += 1;
db.indexes.insert(&self.id, &self.graph_index)?;

if !self.alias.is_empty() {
Expand All @@ -44,7 +44,7 @@ impl InsertNode {
db.graph.remove_node(&self.graph_index)?;
db.indexes.remove_key(&self.id)?;
db.aliases.remove_key(&self.alias)?;
db.next_index -= 1;
db.next_id -= 1;

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions src/agdb/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct Db {
pub(crate) graph: Graph,
pub(crate) aliases: IndexedMap<String, DbId>,
pub(crate) indexes: IndexedMap<DbId, GraphIndex>,
pub(crate) next_index: i64,
pub(crate) next_id: i64,
}

impl Db {
Expand All @@ -34,7 +34,7 @@ impl Db {
graph: Graph::new(),
aliases: IndexedMap::<String, DbId>::new(),
indexes: IndexedMap::<DbId, GraphIndex>::new(),
next_index: 1,
next_id: 1,
})
}

Expand Down
49 changes: 16 additions & 33 deletions src/agdb/query/insert_edges_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use super::query_ids::QueryIds;
use super::query_values::QueryValues;
use super::QueryMut;
use crate::commands_mut::insert_edge::InsertEdge;
use crate::commands_mut::insert_index::InsertIndex;
use crate::commands_mut::CommandsMut;
use crate::QueryError;

Expand All @@ -30,20 +29,16 @@ impl QueryMut for InsertEdgesQuery {
}

impl InsertEdgesQuery {
fn insert_edge(from: &QueryId, to: &QueryId) -> Vec<CommandsMut> {
vec![
CommandsMut::InsertEdge(InsertEdge::new(from.clone(), to.clone())),
CommandsMut::InsertIndex(InsertIndex::new()),
]
}

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

match &self.to {
QueryIds::Ids(ids) => {
for (from, to) in from.iter().zip(ids.iter()) {
commands.extend(Self::insert_edge(from, to));
commands.push(CommandsMut::InsertEdge(InsertEdge::new(
from.clone(),
to.clone(),
)));
}
}
QueryIds::Search(_) => return Err(QueryError::from("Invalid insert edges query")),
Expand All @@ -59,7 +54,10 @@ impl InsertEdgesQuery {
QueryIds::Ids(ids) => {
for from in from {
for to in ids {
commands.extend(Self::insert_edge(from, to));
commands.push(CommandsMut::InsertEdge(InsertEdge::new(
from.clone(),
to.clone(),
)));
}
}
}
Expand All @@ -86,10 +84,10 @@ mod tests {

assert_eq!(
query.commands().unwrap(),
vec![
CommandsMut::InsertEdge(InsertEdge::new(QueryId::from(1), QueryId::from(2))),
CommandsMut::InsertIndex(InsertIndex::new())
]
vec![CommandsMut::InsertEdge(InsertEdge::new(
QueryId::from(1),
QueryId::from(2)
)),]
);
}

Expand All @@ -104,13 +102,10 @@ mod tests {

assert_eq!(
query.commands().unwrap(),
vec![
CommandsMut::InsertEdge(InsertEdge::new(
QueryId::Alias("alias".to_string()),
QueryId::Alias("alias2".to_string()),
)),
CommandsMut::InsertIndex(InsertIndex::new())
]
vec![CommandsMut::InsertEdge(InsertEdge::new(
QueryId::Alias("alias".to_string()),
QueryId::Alias("alias2".to_string()),
)),]
);
}

Expand All @@ -127,9 +122,7 @@ mod tests {
query.commands().unwrap(),
vec![
CommandsMut::InsertEdge(InsertEdge::new(QueryId::from(1), QueryId::from(2))),
CommandsMut::InsertIndex(InsertIndex::new()),
CommandsMut::InsertEdge(InsertEdge::new(QueryId::from(1), QueryId::from(3))),
CommandsMut::InsertIndex(InsertIndex::new()),
]
);
}
Expand All @@ -147,9 +140,7 @@ mod tests {
query.commands().unwrap(),
vec![
CommandsMut::InsertEdge(InsertEdge::new(QueryId::from(1), QueryId::from(3))),
CommandsMut::InsertIndex(InsertIndex::new()),
CommandsMut::InsertEdge(InsertEdge::new(QueryId::from(2), QueryId::from(3))),
CommandsMut::InsertIndex(InsertIndex::new()),
]
);
}
Expand All @@ -167,9 +158,7 @@ mod tests {
query.commands().unwrap(),
vec![
CommandsMut::InsertEdge(InsertEdge::new(QueryId::from(1), QueryId::from(3))),
CommandsMut::InsertIndex(InsertIndex::new()),
CommandsMut::InsertEdge(InsertEdge::new(QueryId::from(2), QueryId::from(3))),
CommandsMut::InsertIndex(InsertIndex::new()),
]
);
}
Expand All @@ -187,9 +176,7 @@ mod tests {
query.commands().unwrap(),
vec![
CommandsMut::InsertEdge(InsertEdge::new(QueryId::from(1), QueryId::from(3))),
CommandsMut::InsertIndex(InsertIndex::new()),
CommandsMut::InsertEdge(InsertEdge::new(QueryId::from(2), QueryId::from(4))),
CommandsMut::InsertIndex(InsertIndex::new()),
]
);
}
Expand All @@ -207,13 +194,9 @@ mod tests {
query.commands().unwrap(),
vec![
CommandsMut::InsertEdge(InsertEdge::new(QueryId::from(1), QueryId::from(3))),
CommandsMut::InsertIndex(InsertIndex::new()),
CommandsMut::InsertEdge(InsertEdge::new(QueryId::from(1), QueryId::from(4))),
CommandsMut::InsertIndex(InsertIndex::new()),
CommandsMut::InsertEdge(InsertEdge::new(QueryId::from(2), QueryId::from(3))),
CommandsMut::InsertIndex(InsertIndex::new()),
CommandsMut::InsertEdge(InsertEdge::new(QueryId::from(2), QueryId::from(4))),
CommandsMut::InsertIndex(InsertIndex::new()),
]
);
}
Expand Down
4 changes: 1 addition & 3 deletions src/agdb/transaction_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ impl<'a> TransactionMut<'a> {
) -> Result<(), QueryError> {
match command {
CommandsMut::InsertAlias(data) => data.redo(db, result, context),
CommandsMut::InsertEdge(data) => data.redo(db, context),
CommandsMut::InsertIndex(data) => data.redo(db, result, context),
CommandsMut::InsertEdge(data) => data.redo(db, result),
CommandsMut::InsertNode(data) => data.redo(db, result),
CommandsMut::RemoveAlias(data) => data.redo(db, result, context),
CommandsMut::RemoveEdge(data) => data.redo(db, context),
Expand All @@ -80,7 +79,6 @@ impl<'a> TransactionMut<'a> {
match command {
CommandsMut::InsertAlias(data) => data.undo(db),
CommandsMut::InsertEdge(data) => data.undo(db),
CommandsMut::InsertIndex(data) => data.undo(db),
CommandsMut::InsertNode(data) => data.undo(db),
CommandsMut::RemoveAlias(data) => data.undo(db),
CommandsMut::RemoveEdge(data) => data.undo(db),
Expand Down

0 comments on commit 165deb3

Please sign in to comment.