Skip to content

#529 add property index, speed up queries #531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 2, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Changes to JS assets are not included here, but in [`atomic-data-browser`'s CHAN

## UNRELEASED

- Improve query performance, refactor indexes. The `.tpf` API is deprecated in favor of the more powerful `.query`. #529
- Improved error handling for HTTPS initialization #530

## [v0.34.0] - 2022-10-31
Expand Down
2 changes: 1 addition & 1 deletion cli/src/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn print_resource(
Format::Json => resource.to_json(&context.store)?,
Format::JsonLd => resource.to_json_ld(&context.store)?,
Format::JsonAd => resource.to_json_ad()?,
Format::NTriples => serialize::atoms_to_ntriples(resource.to_atoms()?, &context.store)?,
Format::NTriples => serialize::atoms_to_ntriples(resource.to_atoms(), &context.store)?,
Format::Pretty => pretty_print_resource(resource, &context.store)?,
};
println!("{}", out);
Expand Down
41 changes: 37 additions & 4 deletions lib/src/atoms.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//! The smallest units of data, consiting of a Subject, a Property and a Value
//! The smallest units of data, consisting of a Subject, a Property and a Value

use crate::{errors::AtomicResult, values::Value};
use crate::{
errors::AtomicResult,
values::{ReferenceString, SortableValue, Value},
};

/// The Atom is the (non-validated) string representation of a piece of data.
/// It's RichAtom sibling provides some extra methods.
/// The Atom is the smallest meaningful piece of data.
/// It describes how one value relates to a subject.
/// A [Resource] can be converted into a bunch of Atoms.
#[derive(Clone, Debug)]
pub struct Atom {
/// The URL where the resource is located
Expand All @@ -27,6 +31,35 @@ impl Atom {
let base_path = format!("{} {}", self.subject, self.property);
self.value.to_subjects(Some(base_path))
}

/// Converts one Atom to a series of stringified values that can be indexed.
pub fn to_indexable_atoms(&self) -> Vec<IndexAtom> {
let sort_value = self.value.to_sortable_string();
let index_atoms = match &self.value.to_reference_index_strings() {
Some(v) => v,
None => return vec![],
}
.iter()
.map(|v| IndexAtom {
ref_value: v.into(),
sort_value: sort_value.clone(),
subject: self.subject.clone(),
property: self.property.clone(),
})
.collect();
index_atoms
}
}

/// Differs from a regular [Atom], since the value here is always a string,
/// and in the case of ResourceArrays, only a _single_ subject is used for each atom.
/// One IndexAtom for every member of the ResourceArray is created.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IndexAtom {
pub subject: String,
pub property: String,
pub ref_value: ReferenceString,
pub sort_value: SortableValue,
}

impl std::fmt::Display for Atom {
Expand Down
10 changes: 7 additions & 3 deletions lib/src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,18 +344,22 @@ impl Commit {
// Remove all atoms from index if destroy
if let Some(destroy) = self.destroy {
if destroy {
for atom in resource.to_atoms()?.into_iter() {
for atom in resource.to_atoms().into_iter() {
remove_atoms.push(atom);
}
}
}

if update_index {
for atom in remove_atoms {
store.remove_atom_from_index(&atom, &resource_unedited)?;
store
.remove_atom_from_index(&atom, &resource_unedited)
.map_err(|e| format!("Error removing atom from index: {e} Atom: {e}"))?
}
for atom in add_atoms {
store.add_atom_to_index(&atom, &resource)?;
store
.add_atom_to_index(&atom, &resource)
.map_err(|e| format!("Error adding atom to index: {e} Atom: {e}"))?;
}
}
Ok(resource)
Expand Down
Loading