Skip to content

Commit

Permalink
Merge pull request #20 from carvilsi/improv-doc-1
Browse files Browse the repository at this point in the history
Improv doc 1
  • Loading branch information
carvilsi authored Aug 14, 2024
2 parents 66b636b + 6da78d9 commit 13eb826
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 146 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gruphst"
version = "0.11.1"
version = "0.11.2"
edition = "2021"
description = "An in-memory graph database"
license = "MIT"
Expand Down
36 changes: 22 additions & 14 deletions src/edge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,6 @@ pub struct Edge {
}

impl Edge {
pub fn get_id(&self) -> String {
self.id.clone()
}

pub fn get_label(&self) -> String {
self.relation.clone()
}

pub fn set_label(&mut self, label: &str) {
self.relation = label.to_string()
}

/// Creates a new instance
/// # Examples
/// ```rust
Expand All @@ -57,6 +45,21 @@ impl Edge {
}
}

/// Retrieves the generated uuid for the edge
pub fn get_id(&self) -> String {
self.id.clone()
}

/// Retrieves the label for the edge
pub fn get_label(&self) -> String {
self.relation.clone()
}

/// Sets the label for the edge
pub fn set_label(&mut self, label: &str) {
self.relation = label.to_string()
}

/// Adds "From" and "To" vertices
/// to a previous created Edge
/// # Examples
Expand Down Expand Up @@ -84,7 +87,7 @@ impl Edge {
///
/// Edge::create(
/// &Vertex::new("Theoden"),
/// "kinf of",
/// "king of",
/// &Vertex::new("Rohan"));
/// ```
pub fn create(from: &Vertex, relation: &str, to: &Vertex) -> Self {
Expand All @@ -109,27 +112,31 @@ impl Edge {
self.to = Rc::clone(&to_vertex.vrtx);
}

/// Retrieves the "From" or source vertex of edge or the relation
pub fn get_from_vertex(&self) -> Vertex {
Vertex {
vrtx: self.from.clone(),
}
}

/// Retrieves the "To" or target vertex of the edge or relation
pub fn get_to_vertex(&self) -> Vertex {
Vertex {
vrtx: self.to.clone(),
}
}

/// Retrieves the name or label of the relation of the edge
pub fn get_relation(&self) -> String {
self.relation.clone()
}

/// Sets the name or label of the relation of the edge
pub fn set_relation(&mut self, relation_label: &str) {
self.relation = relation_label.to_string();
}

/// Set attributes for a edge
/// Set an attribute for a edge
pub fn set_attr<T>(&mut self, attr_k: &str, attr_v: T)
where
T: std::fmt::Display,
Expand All @@ -148,6 +155,7 @@ impl Edge {
}
}
}

/// Updates the value of an attribute
pub fn update_attr<T>(&mut self, attr_k: &str, attr_v: T) -> Result<(), &'static str>
where
Expand Down
16 changes: 14 additions & 2 deletions src/graphs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use log::error;
use serde::{Deserialize, Serialize};
use stats::GraphsStats;
use std::collections::HashMap;

use crate::{edge::Edge, util::graphs_memory_watcher, vertex::Vertex};
use crate::{edge::Edge, graphs_stats::GraphsStats, util::graphs_memory_watcher, vertex::Vertex};

mod persistence;
mod query;
Expand Down Expand Up @@ -77,19 +76,32 @@ impl Graphs {
graphs_memory_watcher(self);
}

/// Returns the label or name for the graphs
pub fn get_label(&self) -> String {
self.label.clone()
}

/// Sets the label or name for the graphs
pub fn set_label(&mut self, label: &str) {
self.label = label.to_string()
}

/// Returns the stats for a grpahs
/// the stats are generated
pub fn get_stats(&mut self) -> GraphsStats {
self.stats = GraphsStats::generate_stats(self);
self.stats.clone()
}

/// Returns the GraphsStats object
pub fn get_graphs_stats(&self) -> GraphsStats {
self.stats.clone()
}

pub fn get_vaults(&self) -> HashMap<String, Vec<Edge>> {
self.vault.clone()
}

/// Adds a Edge element to the Graphs' vault
/// for the provided graphs vault name
/// if does not exists it creates a new entry
Expand Down
92 changes: 0 additions & 92 deletions src/graphs/stats.rs
Original file line number Diff line number Diff line change
@@ -1,77 +1,7 @@
use crate::graphs::Graphs;
use log::error;
use serde::{Deserialize, Serialize};
use std::error::Error;

/// Represents stats data from the Graphs
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphsStats {
/// memory used by Graphs in bytes
mem: usize,
/// length of the Graph's vault
total_edges: usize,
/// total graphs
total_graphs: usize,
/// total attributes
total_attr: usize,
/// total edges
total_vertices: usize,
/// unique relations
uniq_rel: usize,
}

impl GraphsStats {
pub fn init() -> Self {
GraphsStats {
mem: 64,
total_edges: 0,
total_graphs: 0,
total_attr: 0,
total_vertices: 0,
uniq_rel: 0,
}
}

pub fn get_mem(&self) -> usize {
self.mem
}

pub fn get_len_graphs(&self) -> usize {
self.total_edges
}

pub fn get_total_graphs(&self) -> usize {
self.total_graphs
}

pub fn get_total_attr(&self) -> usize {
self.total_attr
}

pub fn get_total_edges(&self) -> usize {
self.total_edges
}

pub fn get_uniq_rel(&self) -> usize {
self.uniq_rel
}

pub fn get_total_vertices(&self) -> usize {
self.total_vertices
}

pub fn generate_stats(graphs: &Graphs) -> Self {
get_stats(graphs).unwrap()
}
}

impl Graphs {
/// Returns stats from Graphs; size in bytes, amount of graph, name, total number of attributes
/// and total amount of edges
pub fn stats(&self) -> Result<GraphsStats, Box<dyn Error>> {
get_stats(self)
}

/// Returns an array with the unique relations in the current graph
/// or the one provided
pub fn uniq_graph_relations(
Expand Down Expand Up @@ -131,25 +61,3 @@ impl Graphs {
Ok(bytes.len())
}
}

/// private function to generate stats
fn get_stats(grphs: &Graphs) -> Result<GraphsStats, Box<dyn Error>> {
// lets count the amount of attributes in the graph
let mut attr_counter = 0;
for (_graph_name, edges) in grphs.vault.iter() {
for edge in edges {
attr_counter += edge.get_from_vertex().attr_len();
attr_counter += edge.get_to_vertex().attr_len();
}
}

let stats = GraphsStats {
mem: grphs.get_mem().unwrap(),
total_edges: grphs.len(),
total_attr: attr_counter,
total_vertices: grphs.len() * 2,
uniq_rel: grphs.uniq_relations().len(),
total_graphs: grphs.vault.len(),
};
Ok(stats)
}
102 changes: 102 additions & 0 deletions src/graphs_stats/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use serde::{Deserialize, Serialize};
use std::error::Error;
use crate::graphs::Graphs;
use crate::config::get_max_mem_usage;

/// Represents stats data from the Graphs
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphsStats {
/// memory used by Graphs in bytes
mem: usize,
/// length of the Graph's vault
total_edges: usize,
/// total graphs
total_graphs: usize,
/// total attributes
total_attr: usize,
/// total edges
total_vertices: usize,
/// unique relations
uniq_rel: usize,
/// maximum memory usage
max_mem: usize,
}

impl GraphsStats {
/// Initializes, creating a new instance of GraphsStats
pub(crate) fn init() -> Self {
GraphsStats {
mem: 64,
total_edges: 0,
total_graphs: 0,
total_attr: 0,
total_vertices: 0,
uniq_rel: 0,
max_mem: get_max_mem_usage(),
}
}

/// Retrieves the amount of memory of the Graphs
pub fn get_mem(&self) -> usize {
self.mem
}

/// Retrieves the maximum memory allow usage for the Graphs
pub fn get_max_mem(&self) -> usize {
self.max_mem
}
/// Retrieves the length of elements in the vault
pub fn get_total_graphs(&self) -> usize {
self.total_graphs
}

/// Retrieves the amount of attributes on edges and vertex
/// from graphs
pub fn get_total_attr(&self) -> usize {
self.total_attr
}

/// Retrieves the total amount of edges on Graphs
pub fn get_total_edges(&self) -> usize {
self.total_edges
}

/// Retrieves the amount of unique relations on Graphs
pub fn get_uniq_rel(&self) -> usize {
self.uniq_rel
}

/// Retrieves the total amount of vertices on Graphs
pub fn get_total_vertices(&self) -> usize {
self.total_vertices
}

/// Returns a GraphsStats object
pub(crate) fn generate_stats(graphs: &Graphs) -> Self {
get_stats(graphs).unwrap()
}
}

/// private function to generate stats
fn get_stats(grphs: &Graphs) -> Result<GraphsStats, Box<dyn Error>> {
// lets count the amount of attributes in the graph
let mut attr_counter = 0;
for (_name, edges) in grphs.get_vaults().iter() {
for edge in edges {
attr_counter += edge.get_from_vertex().attr_len();
attr_counter += edge.get_to_vertex().attr_len();
attr_counter += edge.attr_len();
}
}

let stats = GraphsStats {
mem: grphs.get_mem().unwrap(),
total_edges: grphs.len(),
total_attr: attr_counter,
total_vertices: grphs.len() * 2,
uniq_rel: grphs.uniq_relations().len(),
total_graphs: grphs.get_vaults().len(),
max_mem: get_max_mem_usage(),
};
Ok(stats)
}
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
pub mod config;
pub mod edge;
pub mod graphs;
pub mod graphs_stats;
pub mod logger;
pub mod util;
mod util;
pub mod vertex;
Loading

0 comments on commit 13eb826

Please sign in to comment.