Skip to content

Commit

Permalink
[db] Remove alias #449 (#450)
Browse files Browse the repository at this point in the history
* insert aliases

* add remove alias
  • Loading branch information
michaelvlach authored Feb 26, 2023
1 parent a708779 commit a013e90
Show file tree
Hide file tree
Showing 16 changed files with 265 additions and 110 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
- uses: actions/checkout@v3
- uses: taiki-e/install-action@cargo-llvm-cov
- run: rustup component add llvm-tools-preview
- run: cargo llvm-cov --workspace --fail-uncovered-functions 0 --fail-uncovered-lines 12
- run: cargo llvm-cov --workspace --fail-uncovered-functions 0 --fail-uncovered-lines 10

format:
runs-on: ubuntu-latest
Expand Down
6 changes: 3 additions & 3 deletions src/agdb/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ mod tests {

#[test]
fn derived_from_debug() {
format!("{:?}", Commands::InsertNode(InsertNode {}));
format!("{:?}", Commands::InsertNode(InsertNode { alias: None }));
}

#[test]
fn derived_from_partial_eq() {
assert_eq!(
Commands::InsertNode(InsertNode {}),
Commands::InsertNode(InsertNode {})
Commands::InsertNode(InsertNode { alias: None }),
Commands::InsertNode(InsertNode { alias: None })
);
}
}
6 changes: 6 additions & 0 deletions src/agdb/commands/insert_alias.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::query::query_id::QueryId;

#[derive(Debug, PartialEq)]
pub struct InsertAlias {
pub id: QueryId,
pub alias: String,
}

Expand All @@ -12,6 +15,7 @@ mod tests {
format!(
"{:?}",
InsertAlias {
id: QueryId::Id(0),
alias: String::new()
}
);
Expand All @@ -21,9 +25,11 @@ mod tests {
fn derived_from_partial_eq() {
assert_eq!(
InsertAlias {
id: QueryId::Id(0),
alias: String::new()
},
InsertAlias {
id: QueryId::Id(0),
alias: String::new()
}
);
Expand Down
8 changes: 5 additions & 3 deletions src/agdb/commands/insert_node.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#[derive(Debug, PartialEq)]
pub struct InsertNode {}
pub struct InsertNode {
pub alias: Option<String>,
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn derived_from_debug() {
format!("{:?}", InsertNode {});
format!("{:?}", InsertNode { alias: None });
}

#[test]
fn derived_from_partial_eq() {
assert_eq!(InsertNode {}, InsertNode {});
assert_eq!(InsertNode { alias: None }, InsertNode { alias: None });
}
}
181 changes: 95 additions & 86 deletions src/agdb/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ mod db_float;

use self::db_context::Context;
use self::db_data::DbData;
use crate::collections::indexed_map::IndexedMap;
use crate::commands::insert_alias::InsertAlias;
use crate::commands::insert_edge::InsertEdge;
use crate::commands::insert_node::InsertNode;
use crate::commands::remove_alias::RemoveAlias;
use crate::commands::remove_edge::RemoveEdge;
use crate::commands::Commands;
use crate::graph::graph_index::GraphIndex;
use crate::query::query_id::QueryId;
Expand Down Expand Up @@ -54,85 +56,54 @@ impl Db {
Transaction::default()
}

fn process_command(
fn get_from_to(
&self,
command: Commands,
data: InsertEdge,
context: &mut Context,
result: &mut QueryResult,
) -> Result<(), QueryError> {
match command {
Commands::InsertAlias(data) => self.insert_alias(data, context),
Commands::InsertEdge(data) => self.insert_edge(data, context, result),
Commands::InsertNode(_) => self.insert_node(context, result),
Commands::RemoveAlias(_) => todo!(),
Commands::RemoveEdge(data) => self.remove_edge(data, result),
Commands::RemoveNode(data) => self.remove_node(data, result),
}
}
) -> Result<(GraphIndex, GraphIndex), QueryError> {
let db_data = self.data.read()?;
let from = Self::graph_index_from_query_id(&data.from, &*self.data.read()?)?;
let to = Self::graph_index_from_query_id(&data.to, &*self.data.read()?)?;
context.index = db_data.next_edge;

fn remove_node(
&self,
data: crate::commands::remove_node::RemoveNode,
result: &mut QueryResult,
) -> Result<(), QueryError> {
let mut db_data = self.data.write()?;
let index = Self::graph_index_from_query_id(&data.id, &db_data.aliases, &db_data.indexes)?;
db_data.graph.remove_node(&index)?;
result.result += 1;
Ok(())
Ok((from, to))
}

fn remove_edge(
&self,
data: crate::commands::remove_edge::RemoveEdge,
result: &mut QueryResult,
) -> Result<(), QueryError> {
let mut db_data = self.data.write()?;
let index = Self::graph_index_from_query_id(&data.id, &db_data.aliases, &db_data.indexes)?;
db_data.graph.remove_edge(&index)?;
result.result += 1;
Ok(())
fn get_index_from_id(id: &QueryId, db_data: &DbData) -> Result<i64, QueryError> {
Ok(match id {
QueryId::Id(id) => {
let _ = db_data
.indexes
.value(id)?
.ok_or(QueryError::from(format!("Id '{id}' not found")))?;
*id
}
QueryId::Alias(alias) => db_data
.aliases
.value(alias)?
.ok_or(QueryError::from(format!("Alias '{alias}' not found")))?,
})
}

fn insert_alias(&self, data: InsertAlias, context: &mut Context) -> Result<(), QueryError> {
Ok(self
.data
.write()?
.aliases
.insert(&data.alias, &context.index)?)
fn graph_index_from_query_id(id: &QueryId, data: &DbData) -> Result<GraphIndex, QueryError> {
Ok(GraphIndex::from(Self::get_index_from_id(id, data)?))
}

fn insert_node(
&self,
context: &mut Context,
result: &mut QueryResult,
) -> Result<(), QueryError> {
self.insert_node_write_data(context)?;

fn insert_alias(&self, data: InsertAlias, result: &mut QueryResult) -> Result<(), QueryError> {
let mut mut_data = self.data.write()?;
let index = Self::get_index_from_id(&data.id, &mut_data)?;
mut_data.aliases.insert(&data.alias, &index)?;
result.result += 1;
result.elements.push(crate::DbElement {
index: context.index,
values: vec![],
});

Ok(())
}

fn insert_node_write_data(&self, context: &mut Context) -> Result<(), QueryError> {
let mut mut_data = self.data.write()?;
context.index = mut_data.next_node;
let graph_index = mut_data.graph.insert_node()?.index;
mut_data.next_node += 1;
Ok(mut_data.indexes.insert(&context.index, &graph_index)?)
}

fn insert_edge(
&self,
data: InsertEdge,
context: &mut Context,
result: &mut QueryResult,
) -> Result<(), QueryError> {
let (from, to) = self.validate_from_to(data, context)?;
let (from, to) = self.get_from_to(data, context)?;
self.insert_edge_write_data(from, to, context)?;

result.result += 1;
Expand All @@ -156,35 +127,73 @@ impl Db {
Ok(mut_data.indexes.insert(&context.index, &graph_index)?)
}

fn validate_from_to(
fn insert_node(&self, data: InsertNode, result: &mut QueryResult) -> Result<(), QueryError> {
let index = self.insert_node_write_data(data)?;

result.result += 1;
result.elements.push(crate::DbElement {
index,
values: vec![],
});

Ok(())
}

fn insert_node_write_data(&self, data: InsertNode) -> Result<i64, QueryError> {
let mut mut_data = self.data.write()?;
let index = mut_data.next_node;
let graph_index = mut_data.graph.insert_node()?.index;
mut_data.next_node += 1;

if let Some(alias) = data.alias {
mut_data.aliases.insert(&alias, &index)?
}

mut_data.indexes.insert(&index, &graph_index)?;

Ok(index)
}

fn process_command(
&self,
data: InsertEdge,
command: Commands,
context: &mut Context,
) -> Result<(GraphIndex, GraphIndex), QueryError> {
let db_data = self.data.read()?;
let from = Self::graph_index_from_query_id(&data.from, &db_data.aliases, &db_data.indexes)?;
let to = Self::graph_index_from_query_id(&data.to, &db_data.aliases, &db_data.indexes)?;
context.index = db_data.next_edge;
result: &mut QueryResult,
) -> Result<(), QueryError> {
match command {
Commands::InsertAlias(data) => self.insert_alias(data, result),
Commands::InsertEdge(data) => self.insert_edge(data, context, result),
Commands::InsertNode(data) => self.insert_node(data, result),
Commands::RemoveAlias(data) => self.remove_alias(data, result),
Commands::RemoveEdge(data) => self.remove_edge(data, result),
Commands::RemoveNode(data) => self.remove_node(data, result),
}
}

Ok((from, to))
fn remove_alias(&self, data: RemoveAlias, result: &mut QueryResult) -> Result<(), QueryError> {
let mut db_data = self.data.write()?;
db_data.aliases.remove_key(&data.alias)?;
result.result += 1;
Ok(())
}

fn graph_index_from_query_id(
id: &QueryId,
aliases: &IndexedMap<String, i64>,
indexes: &IndexedMap<i64, i64>,
) -> Result<GraphIndex, QueryError> {
Ok(match id {
QueryId::Id(id) => GraphIndex::from(
indexes
.value(id)?
.ok_or(QueryError::from(format!("Id '{id}' not found")))?,
),
QueryId::Alias(alias) => GraphIndex::from(
aliases
.value(alias)?
.ok_or(QueryError::from(format!("Alias '{alias}' not found")))?,
),
})
fn remove_edge(&self, data: RemoveEdge, result: &mut QueryResult) -> Result<(), QueryError> {
let mut db_data = self.data.write()?;
let index = Self::graph_index_from_query_id(&data.id, &db_data)?;
db_data.graph.remove_edge(&index)?;
result.result += 1;
Ok(())
}

fn remove_node(
&self,
data: crate::commands::remove_node::RemoveNode,
result: &mut QueryResult,
) -> Result<(), QueryError> {
let mut db_data = self.data.write()?;
let index = Self::graph_index_from_query_id(&data.id, &db_data)?;
db_data.graph.remove_node(&index)?;
result.result += 1;
Ok(())
}
}
4 changes: 2 additions & 2 deletions src/agdb/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ pub enum Query {
impl Query {
pub fn commands(&self) -> Vec<Commands> {
match self {
Query::InsertAliases(_) => todo!(),
Query::InsertAliases(query) => query.commands(),
Query::InsertEdges(query) => query.commands(),
Query::InsertNodes(query) => query.commands(),
Query::InsertValues(_) => todo!(),
Query::RemoveAliases(_) => todo!(),
Query::RemoveAliases(query) => query.commands(),
Query::Remove(query) => query.commands(),
Query::RemoveValues(_) => todo!(),
Query::Search(_) => todo!(),
Expand Down
56 changes: 56 additions & 0 deletions src/agdb/query/insert_aliases_query.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,62 @@
use super::query_id::QueryId;
use super::query_ids::QueryIds;
use crate::commands::insert_alias::InsertAlias;
use crate::commands::Commands;

pub struct InsertAliasesQuery {
pub ids: QueryIds,
pub aliases: Vec<String>,
}

impl InsertAliasesQuery {
pub(crate) fn commands(&self) -> Vec<Commands> {
match &self.ids {
QueryIds::All | QueryIds::Search(_) => panic!("Invalid query"),
QueryIds::Id(id) => self.id(id),
QueryIds::Ids(ids) => self.ids(ids),
}
}

fn id(&self, id: &QueryId) -> Vec<Commands> {
vec![Commands::InsertAlias(InsertAlias {
id: id.clone(),
alias: self.aliases[0].clone(),
})]
}

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

for (id, alias) in ids.iter().zip(self.aliases.iter()) {
commands.push(Commands::InsertAlias(InsertAlias {
id: id.clone(),
alias: alias.clone(),
}));
}

commands
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::test_utilities::catch_unwind_silent::catch_unwind_silent;

#[test]
fn invalid_query() {
let result = catch_unwind_silent(|| {
let query = InsertAliasesQuery {
ids: QueryIds::All,
aliases: vec![],
};

query.commands();
});

assert_eq!(
*result.unwrap_err().downcast_ref::<&str>().unwrap(),
"Invalid query"
);
}
}
2 changes: 1 addition & 1 deletion src/agdb/query/insert_edges_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl InsertEdgesQuery {
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utilities::catch_unwidn_silent::catch_unwind_silent;
use crate::test_utilities::catch_unwind_silent::catch_unwind_silent;

#[test]
fn one_to_one() {
Expand Down
Loading

0 comments on commit a013e90

Please sign in to comment.