pub struct Hdt {
pub dict: FourSectDict,
pub triples: TriplesBitmap,
}
Expand description
In-memory representation of an RDF graph loaded from an HDT file. Allows queries by triple patterns.
Fields§
§dict: FourSectDict
in-memory representation of dictionary
triples: TriplesBitmap
in-memory representation of triples
Implementations§
source§impl Hdt
impl Hdt
sourcepub fn new<R: BufRead>(reader: R) -> Result<Self, Box<dyn Error>>
pub fn new<R: BufRead>(reader: R) -> Result<Self, Box<dyn Error>>
Creates an immutable HDT instance containing the dictionary and triples from the given reader. The reader must point to the beginning of the data of an HDT file as produced by hdt-cpp. FourSectionDictionary with DictionarySectionPlainFrontCoding and SPO order is the only supported implementation. The format is specified at https://www.rdfhdt.org/hdt-binary-format/, however there are some deviations. The initial HDT specification at http://www.w3.org/Submission/2011/03/ is outdated and not supported.
§Example
let file = std::fs::File::open("tests/resources/snikmeta.hdt").expect("error opening file");
let hdt = hdt::Hdt::new(std::io::BufReader::new(file)).unwrap();
sourcepub fn size_in_bytes(&self) -> usize
pub fn size_in_bytes(&self) -> usize
Recursive size in bytes on the heap.
sourcepub fn triples(
&self
) -> impl Iterator<Item = (Arc<str>, Arc<str>, Arc<str>)> + '_
pub fn triples( &self ) -> impl Iterator<Item = (Arc<str>, Arc<str>, Arc<str>)> + '_
An iterator visiting all triples as strings in order.
Using this method with a filter can be inefficient for large graphs,
because the strings are stored in compressed form and must be decompressed and allocated.
Whenever possible, use Hdt::triples_with_pattern
instead.
§Example
fn print_first_triple(hdt: hdt::Hdt) {
println!("{:?}", hdt.triples().next().expect("no triple in the graph"));
}
sourcepub fn subjects_with_po(
&self,
p: &str,
o: &str
) -> Box<dyn Iterator<Item = String> + '_>
pub fn subjects_with_po( &self, p: &str, o: &str ) -> Box<dyn Iterator<Item = String> + '_>
Get all subjects with the given property and object (?PO pattern).
Use this over triples_with_pattern(None,Some(p),Some(o))
if you don’t need whole triples.
§Example
Who was born in Leipzig?
fn query(dbpedia: hdt::Hdt) {
for person in dbpedia.subjects_with_po(
"http://dbpedia.org/ontology/birthPlace", "http://dbpedia.org/resource/Leipzig") {
println!("{person:?}");
}
}
sourcepub fn triples_with_pattern<'a>(
&'a self,
sp: Option<&'a str>,
pp: Option<&'a str>,
op: Option<&'a str>
) -> Box<dyn Iterator<Item = (Arc<str>, Arc<str>, Arc<str>)> + '_>
pub fn triples_with_pattern<'a>( &'a self, sp: Option<&'a str>, pp: Option<&'a str>, op: Option<&'a str> ) -> Box<dyn Iterator<Item = (Arc<str>, Arc<str>, Arc<str>)> + '_>
Get all triples that fit the given triple patterns, where None
stands for a variable.
For example, triples_with_pattern(Some(s), Some(p), None)
answers an SP? pattern.
§Example
What is the capital of the United States of America?
fn query(dbpedia: hdt::Hdt) {
println!("{:?}", dbpedia.triples_with_pattern(
Some("http://dbpedia.org/resource/United_States"), Some("http://dbpedia.org/ontology/capital"), None)
.next().expect("no capital found").2);
}