Skip to content

Commit

Permalink
Merge pull request #8 from weso/vector-to-matrix
Browse files Browse the repository at this point in the history
Vector to matrix implementation and changes in the tests accordingly
  • Loading branch information
DiegoMfer authored Dec 22, 2023
2 parents 981d032 + 6160032 commit 84b5871
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 34 deletions.
15 changes: 8 additions & 7 deletions examples/serialize_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ static ALLOCATOR: jemallocator::Jemalloc = jemallocator::Jemalloc;

fn main() {
let args: Vec<String> = env::args().collect();
if args.len() <= 1 {
panic!("Usage: cargo run --example serialize_bench <number_of_universities>");
if args.len() <= 3 {
panic!("Usage: cargo run --example serialize_bench <rdf_path> <zarr_path> <shard_size>");
}
let number_of_universities: &String = &args[1];
let zarr_path = format!("{}-lubm", number_of_universities);
let rdf_path: &String = &args[1];
let zarr_path: &String = &args[2];
let shard_size: &String = &args[3];

let before = Instant::now();

LocalStorage::new(MatrixLayout)
.serialize(
format!("{}.zarr", zarr_path).as_str(),
format!("../lubm-uba-improved/out/{}.ttl", zarr_path).as_str(),
ChunkingStrategy::Sharding(10240),
&zarr_path.as_str(),
&rdf_path.as_str(),
ChunkingStrategy::Sharding(shard_size.parse::<u64>().unwrap()),
)
.unwrap();

Expand Down
5 changes: 1 addition & 4 deletions src/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@ impl Dictionary {

pub fn get_predicate_idx(&self, predicate: &str) -> Option<usize> {
let mut locator = self.predicates.locator();
match locator.run(predicate) {
Some(value) => Some(value + 1),
None => None,
}
locator.run(predicate).map(|value| value + 1)
}

pub fn get_predicate_idx_unchecked(&self, predicate: &str) -> usize {
Expand Down
22 changes: 13 additions & 9 deletions src/engine/array.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
use sprs::CsVec;
use sprs::{CsMat, TriMat};

use crate::storage::ZarrArray;

use super::{EngineResult, EngineStrategy};

impl EngineStrategy<CsVec<u8>> for ZarrArray {
fn get_subject(&self, index: usize) -> EngineResult<CsVec<u8>> {
let selection = CsVec::new(self.rows(), vec![index], vec![1]);
Ok(&self.transpose_view() * &selection)
impl EngineStrategy<CsMat<u8>> for ZarrArray {
fn get_subject(&self, index: usize) -> EngineResult<CsMat<u8>> {
let mut matrix = TriMat::new((self.rows(), self.rows()));
matrix.add_triplet(index, index, 1);
let matrix = matrix.to_csc();
Ok(&matrix * self)
}

fn get_predicate(&self, index: usize) -> EngineResult<CsVec<u8>> {
fn get_predicate(&self, _value: u8) -> EngineResult<CsMat<u8>> {
unimplemented!()
}

fn get_object(&self, index: usize) -> EngineResult<CsVec<u8>> {
let selection = CsVec::new(self.cols(), vec![index], vec![1]);
Ok(self * &selection)
fn get_object(&self, index: usize) -> EngineResult<CsMat<u8>> {
let mut matrix = TriMat::new((self.cols(), self.cols()));
matrix.add_triplet(index, index, 1);
let matrix = matrix.to_csc();
Ok(self * &matrix)
}
}
2 changes: 1 addition & 1 deletion src/engine/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl<T: ReadableStorageTraits> EngineStrategy<Vec<u8>> for Array<T> {
}
}

fn get_predicate(&self, index: usize) -> EngineResult<Vec<u8>> {
fn get_predicate(&self, _index: u8) -> EngineResult<Vec<u8>> {
unimplemented!()
}

Expand Down
2 changes: 1 addition & 1 deletion src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ pub type EngineResult<T> = Result<T, EngineError>;

pub trait EngineStrategy<T> {
fn get_subject(&self, index: usize) -> EngineResult<T>;
fn get_predicate(&self, index: usize) -> EngineResult<T>;
fn get_predicate(&self, index: u8) -> EngineResult<T>;
fn get_object(&self, index: usize) -> EngineResult<T>;
}
3 changes: 1 addition & 2 deletions src/storage/tabular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,12 @@ where
graph
.iter()
.enumerate()
.map(|(subject, triples)| {
.flat_map(|(subject, triples)| {
triples
.iter()
.map(|&(predicate, object)| (subject as u32, predicate, object))
.collect::<Vec<Chunk>>()
})
.flatten()
.collect::<Vec<Chunk>>()
}

Expand Down
1 change: 0 additions & 1 deletion tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ impl Graph {
Object::GCHQ.get_idx(dictionary),
Predicate::Manufacturer.get_idx(dictionary),
);

ans.to_csc()
}
}
8 changes: 5 additions & 3 deletions tests/get_object_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use remote_hdt::{
engine::EngineStrategy,
storage::{matrix::MatrixLayout, tabular::TabularLayout, ChunkingStrategy, LocalStorage},
};
use sprs::CsVec;

use sprs::TriMat;
mod common;

#[test]
Expand Down Expand Up @@ -49,5 +48,8 @@ fn get_object_tabular_test() {
.get_object(common::Object::Alan.get_idx(&storage.get_dictionary()))
.unwrap();

assert_eq!(actual, CsVec::new(4, vec![1], vec![3]))
let mut expected = TriMat::new((4, 9));
expected.add_triplet(1, 3, 3);
let expected = expected.to_csc();
assert_eq!(actual, expected)
}
15 changes: 9 additions & 6 deletions tests/get_subject_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use remote_hdt::{
engine::EngineStrategy,
storage::{matrix::MatrixLayout, tabular::TabularLayout, ChunkingStrategy, LocalStorage},
};
use sprs::CsVec;

use sprs::TriMat;
mod common;

#[test]
Expand Down Expand Up @@ -49,8 +48,12 @@ fn get_subject_tabular_test() {
.get_subject(common::Subject::Alan.get_idx(&storage.get_dictionary()))
.unwrap();

assert_eq!(
actual,
CsVec::new(9, vec![0, 1, 2, 7, 8], vec![2, 4, 5, 7, 8])
)
let mut result = TriMat::new((4, 9));
result.add_triplet(0, 0, 2);
result.add_triplet(0, 1, 4);
result.add_triplet(0, 2, 5);
result.add_triplet(0, 7, 7);
result.add_triplet(0, 8, 8);
let result = result.to_csc();
assert_eq!(actual, result)
}

0 comments on commit 84b5871

Please sign in to comment.