Skip to content

Commit

Permalink
Merge pull request #27 from carvilsi/refactor-1
Browse files Browse the repository at this point in the history
Refactor 1
  • Loading branch information
carvilsi authored Sep 12, 2024
2 parents b93bed2 + ef504a8 commit ef9ca56
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 239 deletions.
2 changes: 2 additions & 0 deletions src/errors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub enum GruPHstError {
EdgeNotFound,
#[error("No relations: \"{0}\" on Edges")]
EdgeNoRelations(String),
#[error("Vault is empy; no Edges")]
VaultEmpty,
#[error("Provided vault: \"{0}\" does not exists")]
VaultNotExists(String),
#[error("Persisted file excedes max memory usage, check GRUPHST_MAX_MEM_USAGE var")]
Expand Down
91 changes: 51 additions & 40 deletions src/graphs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,12 @@ impl Graphs {
/// Retrieves the collection of edges
/// the default one or by name
pub fn get_edges(&self, vault_name: Option<&str>) -> Result<Vec<Edge>, GruPHstError> {
let current_vault = self.select_vault_label(vault_name);
if let Some(edges) = self.vault.get(&current_vault) {
Ok(edges.clone())
} else {
warn!("Vault {} does not exists", current_vault);
Err(GruPHstError::VaultNotExists(current_vault))
}
Ok(self.select_vault(vault_name)?)
}

/// Returns a collection with the unique vertices on a vault
// FIXME: missing error returning
pub fn get_uniq_vertices(&self, vault_name: Option<&str>) -> Result<Vec<Vertex>, &'static str> {
let edges = self.get_edges(vault_name).unwrap();
pub fn get_uniq_vertices(&self, vault_name: Option<&str>) -> Result<Vec<Vertex>, GruPHstError> {
let edges = self.select_vault(vault_name)?;
let mut vertices_map: HashMap<String, Vertex> = HashMap::new();
for edge in edges {
vertices_map.insert(edge.get_from_vertex().get_id(), edge.get_from_vertex());
Expand All @@ -174,19 +167,14 @@ impl Graphs {
id: String,
vault_name: Option<&str>,
) -> Result<(), GruPHstError> {
let current_vault = self.select_vault_label(vault_name);
if let Some(edges) = self.vault.get_mut(&current_vault) {
if let Some(index) = edges.iter().position(|edge| edge.get_id() == id) {
edges.remove(index);
graphs_memory_watcher(self);
Ok(())
} else {
error!("Edge [{}] to delete not found", id);
Err(GruPHstError::EdgeNotFound)
}
let edges = self.select_vault_mut(vault_name)?;
if let Some(index) = edges.iter().position(|edge| edge.get_id() == id) {
edges.remove(index);
graphs_memory_watcher(self);
Ok(())
} else {
warn!("Vault {} does not exists", current_vault);
Err(GruPHstError::VaultNotExists(current_vault))
error!("Edge [{}] to delete not found", id);
Err(GruPHstError::EdgeNotFound)
}
}

Expand All @@ -196,34 +184,57 @@ impl Graphs {
edge_to_update: &Edge,
vault_name: Option<&str>,
) -> Result<(), GruPHstError> {
let current_vault = self.select_vault_label(vault_name);
if let Some(edges) = self.vault.get_mut(&current_vault) {
let index = edges
.iter()
.position(|vertex| vertex.get_id() == edge_to_update.get_id());
if index.is_some() {
let i = index.unwrap();
edges.remove(i);
edges.push(edge_to_update.clone());
graphs_memory_watcher(self);
Ok(())
} else {
error!("Edge to update with id: [{}] not found", edge_to_update.get_id());
Err(GruPHstError::EdgeNotFound)
}
let edges: &mut Vec<Edge> = self.select_vault_mut(vault_name)?;
let index = edges
.iter()
.position(|vertex| vertex.get_id() == edge_to_update.get_id());
if index.is_some() {
let i = index.unwrap();
edges.remove(i);
edges.push(edge_to_update.clone());
graphs_memory_watcher(self);
Ok(())
} else {
warn!("Vault {} does not exists", current_vault);
Err(GruPHstError::VaultNotExists(current_vault))
error!("Edge to update with id: [{}] not found", edge_to_update.get_id());
Err(GruPHstError::EdgeNotFound)
}
}

/// Retrieves the current vault or returns the current one
// XXX: Try to change this to return an error...
fn select_vault_label(&self, vault_label: Option<&str>) -> String {
let mut current_vault = self.label.clone();
if let Some(vlt) = vault_label {
current_vault = vlt.to_string();
}
current_vault.to_string()
}

fn select_vault_not_exists_error(vault: String) -> GruPHstError {
warn!("Vault {} does not exists", vault);
GruPHstError::VaultNotExists(vault)
}

fn select_vault_mut(&mut self, vault_label: Option<&str>) -> Result<&mut Vec<Edge>, GruPHstError> {
let vault = self.select_vault_label(vault_label);
if let Some(edges) = self.vault.get_mut(&vault) {
match edges.is_empty() {
false => Ok(edges),
true => Err(GruPHstError::VaultEmpty),
}
} else {
Err(Graphs::select_vault_not_exists_error(vault))
}
}

fn select_vault(&self, vault_label: Option<&str>) -> Result<Vec<Edge>, GruPHstError> {
let vault = self.select_vault_label(vault_label);
if let Some(edges) = self.vault.get(&vault) {
match edges.is_empty() {
false => Ok(edges.clone()),
true => Err(GruPHstError::VaultEmpty),
}
} else {
Err(Graphs::select_vault_not_exists_error(vault))
}
}
}
Loading

0 comments on commit ef9ca56

Please sign in to comment.