Skip to content

Commit 51a6651

Browse files
committed
#529 add property index, speed up queries
Speed up empty queries,
1 parent 3b67266 commit 51a6651

17 files changed

+578
-486
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Changes to JS assets are not included here, but in [`atomic-data-browser`'s CHAN
66

77
## UNRELEASED
88

9+
- Improve query performance, refactor indexes #529
910
- Improved error handling for HTTPS initialization #530
1011

1112
## [v0.34.0] - 2022-10-31

cli/src/print.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub fn print_resource(
6464
Format::Json => resource.to_json(&context.store)?,
6565
Format::JsonLd => resource.to_json_ld(&context.store)?,
6666
Format::JsonAd => resource.to_json_ad()?,
67-
Format::NTriples => serialize::atoms_to_ntriples(resource.to_atoms()?, &context.store)?,
67+
Format::NTriples => serialize::atoms_to_ntriples(resource.to_atoms(), &context.store)?,
6868
Format::Pretty => pretty_print_resource(resource, &context.store)?,
6969
};
7070
println!("{}", out);

lib/src/atoms.rs

+37-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
//! The smallest units of data, consiting of a Subject, a Property and a Value
1+
//! The smallest units of data, consisting of a Subject, a Property and a Value
22
3-
use crate::{errors::AtomicResult, values::Value};
3+
use crate::{
4+
errors::AtomicResult,
5+
values::{ReferenceString, SortableValue, Value},
6+
};
47

5-
/// The Atom is the (non-validated) string representation of a piece of data.
6-
/// It's RichAtom sibling provides some extra methods.
8+
/// The Atom is the smallest meaningful piece of data.
9+
/// It describes how one value relates to a subject.
10+
/// A [Resource] can be converted into a bunch of Atoms.
711
#[derive(Clone, Debug)]
812
pub struct Atom {
913
/// The URL where the resource is located
@@ -27,6 +31,35 @@ impl Atom {
2731
let base_path = format!("{} {}", self.subject, self.property);
2832
self.value.to_subjects(Some(base_path))
2933
}
34+
35+
/// Converts one Atom to a series of stringified values that can be indexed.
36+
pub fn to_indexable_atoms(&self) -> Vec<IndexAtom> {
37+
let sort_value = self.value.to_sortable_string();
38+
let index_atoms = match &self.value.to_reference_index_strings() {
39+
Some(v) => v,
40+
None => return vec![],
41+
}
42+
.iter()
43+
.map(|v| IndexAtom {
44+
ref_value: v.into(),
45+
sort_value: sort_value.clone(),
46+
subject: self.subject.clone(),
47+
property: self.property.clone(),
48+
})
49+
.collect();
50+
index_atoms
51+
}
52+
}
53+
54+
/// Differs from a regular [Atom], since the value here is always a string,
55+
/// and in the case of ResourceArrays, only a _single_ subject is used for each atom.
56+
/// One IndexAtom for every member of the ResourceArray is created.
57+
#[derive(Debug, Clone, PartialEq, Eq)]
58+
pub struct IndexAtom {
59+
pub subject: String,
60+
pub property: String,
61+
pub ref_value: ReferenceString,
62+
pub sort_value: SortableValue,
3063
}
3164

3265
impl std::fmt::Display for Atom {

lib/src/commit.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -344,18 +344,22 @@ impl Commit {
344344
// Remove all atoms from index if destroy
345345
if let Some(destroy) = self.destroy {
346346
if destroy {
347-
for atom in resource.to_atoms()?.into_iter() {
347+
for atom in resource.to_atoms().into_iter() {
348348
remove_atoms.push(atom);
349349
}
350350
}
351351
}
352352

353353
if update_index {
354354
for atom in remove_atoms {
355-
store.remove_atom_from_index(&atom, &resource_unedited)?;
355+
store
356+
.remove_atom_from_index(&atom, &resource_unedited)
357+
.map_err(|e| format!("Error removing atom from index: {e} Atom: {e}"))?
356358
}
357359
for atom in add_atoms {
358-
store.add_atom_to_index(&atom, &resource)?;
360+
store
361+
.add_atom_to_index(&atom, &resource)
362+
.map_err(|e| format!("Error adding atom to index: {e} Atom: {e}"))?;
359363
}
360364
}
361365
Ok(resource)

0 commit comments

Comments
 (0)