Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Cargo.lock

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

32 changes: 25 additions & 7 deletions lib/maplib/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,17 @@ impl Model {
path: &Path,
graph: &NamedGraph,
transient: bool,
uuid_namespace: Option<String>,
) -> Result<(), MaplibError> {
let mut u8s =
fs::read(path).map_err(|x| TriplestoreError::ReadJSONFileError(x.to_string()))?;

let use_uuid_namespace = if let Some(uuid_namespace) = uuid_namespace {
uuid_namespace
} else {
String::from(path.to_string_lossy())
};
self.triplestore
.map_json(&mut u8s, graph, transient)
.map_json(&mut u8s, Some(use_uuid_namespace), graph, transient)
.map_err(MaplibError::TriplestoreError)
}

Expand All @@ -216,18 +221,24 @@ impl Model {
mut p: String,
graph: &NamedGraph,
transient: bool,
uuid_namespace: Option<String>,
) -> Result<(), MaplibError> {
//Safety: we are never reading this vec back to a string
let u8s = unsafe { p.as_mut_vec() };
self.triplestore
.map_json(u8s, graph, transient)
.map_json(u8s, uuid_namespace, graph, transient)
.map_err(MaplibError::TriplestoreError)
}

#[instrument(skip_all)]
pub fn map_df(&mut self, df: &DataFrame, graph: &NamedGraph) -> Result<(), MaplibError> {
pub fn map_df(
&mut self,
df: &DataFrame,
graph: &NamedGraph,
uuid_namespace: Option<String>,
) -> Result<(), MaplibError> {
self.triplestore
.map_df(df, graph)
.map_df(df, graph, uuid_namespace)
.map_err(MaplibError::TriplestoreError)
}

Expand All @@ -237,10 +248,16 @@ impl Model {
path: &Path,
graph: &NamedGraph,
transient: bool,
uuid_namespace: Option<String>,
) -> Result<(), MaplibError> {
let mut u8s = fs::read(path).map_err(|x| TriplestoreError::XMLError(x.to_string()))?;
let use_uuid_namespace = if let Some(uuid_namespace) = uuid_namespace {
uuid_namespace
} else {
String::from(path.to_string_lossy())
};
self.triplestore
.map_xml(&mut u8s, graph, transient)
.map_xml(&mut u8s, graph, transient, Some(use_uuid_namespace))
.map_err(MaplibError::TriplestoreError)
}

Expand All @@ -250,11 +267,12 @@ impl Model {
mut p: String,
graph: &NamedGraph,
transient: bool,
uuid_namespace: Option<String>,
) -> Result<(), MaplibError> {
//Safety: we are never reading this vec back to a string
let u8s = unsafe { p.as_mut_vec() };
self.triplestore
.map_xml(u8s, graph, transient)
.map_xml(u8s, graph, transient, uuid_namespace)
.map_err(MaplibError::TriplestoreError)
}

Expand Down
20 changes: 14 additions & 6 deletions lib/triplestore/src/map_df.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,38 @@ use oxrdf::NamedNode;
use polars::prelude::{col, lit, DataFrame, IntoLazy};
use polars_core::prelude::{Column, IntoColumn, NamedFrom, Series};
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use representation::constants::{
FX_CHILD, FX_CHILD_NUMBER, FX_ROOT, MAPLIB_PREFIX_IRI, XYZ_PREFIX_IRI,
};
use representation::constants::{FX_CHILD, FX_CHILD_NUMBER, FX_ROOT, XYZ_PREFIX_IRI};
use representation::dataset::NamedGraph;
use representation::polars_to_rdf::polars_type_to_literal_type;
use representation::{BaseRDFNodeType, OBJECT_COL_NAME, SUBJECT_COL_NAME};
use std::collections::HashMap;
use uuid::Uuid;

impl Triplestore {
pub fn map_df(
&mut self,
df: &DataFrame,
named_graph: &NamedGraph,
uuid_namespace: Option<String>,
) -> Result<(), TriplestoreError> {
let use_uuid_namespace = if let Some(uuid_namespace) = uuid_namespace {
Uuid::new_v5(&Uuid::NAMESPACE_DNS, uuid_namespace.as_bytes())
} else {
Uuid::new_v4()
};
let root_node_uuri = new_iri_subject(&use_uuid_namespace, "".as_bytes());
let mut column_types = HashMap::new();
let col_names: Vec<_> = df.columns().iter().map(|x| x.name().to_string()).collect();
for c in df.columns() {
//Todo handle
let dt = polars_type_to_literal_type(c.dtype()).unwrap();
column_types.insert(c.name().to_string(), dt);
}
let id_col = uuid::Uuid::new_v4().to_string();
let id_col = Uuid::new_v5(&use_uuid_namespace, "id".as_bytes()).to_string();
let mut df = df.clone();
let root_node_uuri = format!("{}{}", MAPLIB_PREFIX_IRI, uuid::Uuid::new_v4());
let uuids: Vec<_> = (0..df.height())
.into_par_iter()
.map(|_| format!("{}{}", MAPLIB_PREFIX_IRI, uuid::Uuid::new_v4()))
.map(|i| new_iri_subject(&use_uuid_namespace, &i.to_string().into_bytes()))
.collect();
df.with_column(Series::new(id_col.as_str().into(), uuids).into_column())
.unwrap();
Expand Down Expand Up @@ -129,3 +134,6 @@ impl Triplestore {
Ok(())
}
}
fn new_iri_subject(namespace: &Uuid, name: &[u8]) -> String {
format!("urn:maplib:{}", Uuid::new_v5(namespace, name))
}
47 changes: 39 additions & 8 deletions lib/triplestore/src/map_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use representation::dataset::NamedGraph;
use representation::{BaseRDFNodeType, OBJECT_COL_NAME, SUBJECT_COL_NAME};
use serde_json::Value;
use std::collections::HashMap;
use uuid::Uuid;

const BOOLEAN: u8 = 0;
const INTEGER: u8 = 1;
Expand Down Expand Up @@ -131,6 +132,7 @@ impl Triplestore {
pub fn map_json(
&mut self,
u8s: &mut [u8],
uuid_namespace: Option<String>,
named_graph: &NamedGraph,
transient: bool,
) -> Result<(), TriplestoreError> {
Expand All @@ -140,8 +142,21 @@ impl Triplestore {
let mut pred_map = HashMap::new();
let rdf_type = rdf::TYPE.into_owned();
pred_map.insert(rdf_type.clone(), TripleTableBuilder::new());
let mut path: Vec<String> = Vec::new();

let doc_subject = new_iri_typed_subject(FX_ROOT, &rdf_type, &mut pred_map);
let use_uuid_namespace = if let Some(uuid_namespace) = uuid_namespace {
Uuid::new_v5(&Uuid::NAMESPACE_DNS, uuid_namespace.as_bytes())
} else {
Uuid::new_v4()
};

let doc_subject = new_iri_typed_subject(
FX_ROOT,
&rdf_type,
&use_uuid_namespace,
"".as_bytes(),
&mut pred_map,
);

let root_elem_property = NamedNode::new_unchecked(ROOT_ELEMENT_PROPERTY);
pred_map.insert(root_elem_property.clone(), TripleTableBuilder::new());
Expand All @@ -153,6 +168,8 @@ impl Triplestore {
v,
&rdf_type,
&root_elem_property,
&mut path,
&use_uuid_namespace,
&mut pred_map,
);
let mut triples_to_add = Vec::new();
Expand Down Expand Up @@ -194,6 +211,8 @@ fn process_value(
value: Value,
rdf_type: &NamedNode,
root_elem_property: &NamedNode,
path: &mut Vec<String>,
uuid_namespace: &Uuid,
map: &mut HashMap<NamedNode, TripleTableBuilder>,
) {
match value {
Expand All @@ -220,8 +239,9 @@ fn process_value(
builder.push_iri_iri(subject, FX_NULL);
}
Value::Object(obj) => {
let name: String = path.join(".");
let new_subject = if let Some(property) = property {
let new_subject = new_iri_subject();
let new_subject = new_iri_subject(uuid_namespace, name.as_bytes());
let builder = map.get_mut(property).unwrap();
builder.push_iri_iri(subject, &new_subject);
new_subject
Expand All @@ -230,39 +250,48 @@ fn process_value(
};

for (key, val) in obj {
let property = new_key_property(key, prefix, map);
let property = new_key_property(key.clone(), prefix, map);
path.push(key.to_string());
process_value(
&new_subject,
Some(&property),
prefix,
val,
rdf_type,
root_elem_property,
path,
uuid_namespace,
map,
);
path.pop();
}
}
Value::Array(arr) => {
let name: String = path.join(".");
let array_subject = if let Some(property) = property {
let array_subject = new_iri_subject();
let array_subject = new_iri_subject(uuid_namespace, name.as_bytes());
let builder = map.get_mut(property).unwrap();
builder.push_iri_iri(subject, &array_subject);
array_subject
} else {
subject.to_string()
};
let ch = NamedNode::new_unchecked(FX_CHILD);
for v in arr.into_iter() {
for (i, v) in arr.into_iter().enumerate() {
add_new_property(&ch, map);
path.push(i.to_string());
process_value(
&array_subject,
Some(&ch),
prefix,
v,
rdf_type,
root_elem_property,
path,
uuid_namespace,
map,
);
path.pop();
}
}
}
Expand All @@ -271,15 +300,17 @@ fn process_value(
fn new_iri_typed_subject(
t: &str,
rdf_type: &NamedNode,
namespace: &Uuid,
name: &[u8],
map: &mut HashMap<NamedNode, TripleTableBuilder>,
) -> String {
let bstr = new_iri_subject();
let bstr = new_iri_subject(namespace, name);
map.get_mut(rdf_type).unwrap().push_iri_iri(&bstr, t);
bstr
}

fn new_iri_subject() -> String {
let bstr = format!("urn:maplib:{}", uuid::Uuid::new_v4());
fn new_iri_subject(namespace: &Uuid, name: &[u8]) -> String {
let bstr = format!("urn:maplib:{}", Uuid::new_v5(namespace, name));
bstr
}

Expand Down
Loading
Loading