Skip to content

Commit

Permalink
perf(rdf): make id issuer return references
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Mar 26, 2024
1 parent afeb25c commit 64a227f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
8 changes: 4 additions & 4 deletions contracts/okp4-cognitarium/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,15 @@ pub mod query {
.map(|t| TripleConstructTemplate {
subject: match t.subject {
VarOrNode::Node(Node::BlankNode(n)) => {
VarOrNode::Node(Node::BlankNode(id_issuer.get_str_or_issue(n)))
VarOrNode::Node(Node::BlankNode(id_issuer.get_str_or_issue(n).to_string()))
}
_ => t.subject,
},
predicate: t.predicate,
object: match t.object {
VarOrNodeOrLiteral::Node(Node::BlankNode(n)) => {
VarOrNodeOrLiteral::Node(Node::BlankNode(id_issuer.get_str_or_issue(n)))
}
VarOrNodeOrLiteral::Node(Node::BlankNode(n)) => VarOrNodeOrLiteral::Node(
Node::BlankNode(id_issuer.get_str_or_issue(n).to_string()),
),
_ => t.object,
},
})
Expand Down
8 changes: 4 additions & 4 deletions contracts/okp4-cognitarium/src/querier/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,9 +902,9 @@ impl AtomTemplate {
&mut |value| {
Ok(match value {
Subject::Named(n) => rdf::Subject::NamedNode(n.as_iri(resolve_ns_fn)?),
Subject::Blank(n) => {
rdf::Subject::BlankNode(id_issuer.get_str_or_issue(n.to_string()))
}
Subject::Blank(n) => rdf::Subject::BlankNode(
id_issuer.get_str_or_issue(n.to_string()).to_string(),
),
})
},
"subject",
Expand Down Expand Up @@ -945,7 +945,7 @@ impl AtomTemplate {
Ok(match value {
Object::Named(n) => rdf::Value::NamedNode(n.as_iri(resolve_ns_fn)?),
Object::Blank(n) => {
rdf::Value::BlankNode(id_issuer.get_str_or_issue(n.to_string()))
rdf::Value::BlankNode(id_issuer.get_str_or_issue(n.to_string()).to_string())
}
Object::Literal(l) => match l {
state::Literal::Simple { value } => rdf::Value::LiteralSimple(value),
Expand Down
4 changes: 2 additions & 2 deletions contracts/okp4-cognitarium/src/querier/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl ResolvedVariable {
value: IRI::Full(iri),
})?,
Subject::Blank(blank) => Value::BlankNode {
value: id_issuer.get_str_or_issue(blank.to_string()),
value: id_issuer.get_str_or_issue(blank.to_string()).to_string(),
},
},
ResolvedVariable::Predicate(predicate) => {
Expand All @@ -72,7 +72,7 @@ impl ResolvedVariable {
value: IRI::Full(named.as_iri(ns_fn)?),
},
Object::Blank(blank) => Value::BlankNode {
value: id_issuer.get_str_or_issue(blank.to_string()),
value: id_issuer.get_str_or_issue(blank.to_string()).to_string(),
},
Object::Literal(literal) => match literal {
Literal::Simple { value } => Value::Literal {
Expand Down
17 changes: 9 additions & 8 deletions packages/okp4-rdf/src/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ impl<'a> Normalizer<'a> {
if !issuer.issued(&related) {
recursion_list.push(related.clone());
}
path.push_str(&issuer.get_str_or_issue(related));
path.push_str(issuer.get_str_or_issue(related));
}
}

Expand All @@ -245,7 +245,7 @@ impl<'a> Normalizer<'a> {
for related in recursion_list {
let (result, mut issuer) = self.compute_n_degree_hash(&mut issuer, &related)?;
path.push_str("_:");
path.push_str(&issuer.get_str_or_issue(related));
path.push_str(issuer.get_str_or_issue(related));
path.push('<');
path.push_str(&result);
path.push('>');
Expand Down Expand Up @@ -347,25 +347,26 @@ impl IdentifierIssuer {
}
}

pub fn get_or_issue(&mut self, identifier: String) -> (u128, String) {
match self.issued.entry(identifier.clone()) {
Entry::Occupied(e) => e.get().clone(),
pub fn get_or_issue(&mut self, identifier: String) -> (u128, &str) {
let res = match self.issued.entry(identifier.clone()) {
Entry::Occupied(e) => e.into_mut(),
Entry::Vacant(e) => {
let n = self.counter;
let str = format!("{}{}", self.prefix, n);
self.counter += 1;

self.issue_log.push(identifier);
e.insert((n, str)).clone()
e.insert((n, str))
}
}
};
(res.0, res.1.as_str())
}

pub fn get_n_or_issue(&mut self, identifier: String) -> u128 {
self.get_or_issue(identifier).0
}

pub fn get_str_or_issue(&mut self, identifier: String) -> String {
pub fn get_str_or_issue(&mut self, identifier: String) -> &str {
self.get_or_issue(identifier).1
}

Expand Down

0 comments on commit 64a227f

Please sign in to comment.