pub struct Database { /* private fields */ }Expand description
Manages the SQLite database backing a BidsLayout index.
Stores the complete file index for a BIDS dataset, including:
- Files — Path, filename, directory, file type for every indexed file
- Tags — Entity-value pairs (subject=01, task=rest, etc.) and metadata key-value pairs from JSON sidecars
- Associations — Relationships between files (IntendedFor, Metadata inheritance parent/child, InformedBy)
- Layout info — Root path and config names for database reloading
The database supports both in-memory operation (fast, ephemeral) and
on-disk persistence (for caching large dataset indices). It registers
a custom REGEXP function for SQLite to support regex-based queries.
§Schema
CREATE TABLE files (path TEXT PRIMARY KEY, filename TEXT, dirname TEXT, ...);
CREATE TABLE tags (file_path TEXT, entity_name TEXT, value TEXT, ...);
CREATE TABLE associations (src TEXT, dst TEXT, kind TEXT, ...);
CREATE TABLE layout_info (root TEXT PRIMARY KEY, config TEXT, ...);Implementations§
Source§impl Database
impl Database
Sourcepub fn begin_transaction(&self) -> Result<()>
pub fn begin_transaction(&self) -> Result<()>
Begin an explicit transaction for bulk operations.
Wrapping many inserts in a single transaction avoids per-statement fsyncs, giving ~100× better insert throughput on large datasets.
Sourcepub fn commit_transaction(&self) -> Result<()>
pub fn commit_transaction(&self) -> Result<()>
Commit the current transaction.
Sourcepub fn rollback_transaction(&self) -> Result<()>
pub fn rollback_transaction(&self) -> Result<()>
Roll back the current transaction.
Sourcepub fn insert_file(&self, file: &BidsFile) -> Result<()>
pub fn insert_file(&self, file: &BidsFile) -> Result<()>
Insert a file into the database.
Sourcepub fn insert_tag(
&self,
file_path: &str,
entity_name: &str,
value: &str,
dtype: &str,
is_metadata: bool,
) -> Result<()>
pub fn insert_tag( &self, file_path: &str, entity_name: &str, value: &str, dtype: &str, is_metadata: bool, ) -> Result<()>
Insert a tag (entity-value pair for a file).
Sourcepub fn insert_association(&self, src: &str, dst: &str, kind: &str) -> Result<()>
pub fn insert_association(&self, src: &str, dst: &str, kind: &str) -> Result<()>
Insert a file association.
Sourcepub fn get_layout_info(&self) -> Result<Option<(String, String)>>
pub fn get_layout_info(&self) -> Result<Option<(String, String)>>
Get layout info (root, config).
Sourcepub fn all_file_paths(&self) -> Result<Vec<String>>
pub fn all_file_paths(&self) -> Result<Vec<String>>
Query all file paths.
Get tags for a specific file.
Sourcepub fn get_unique_entity_values(&self, entity_name: &str) -> Result<Vec<String>>
pub fn get_unique_entity_values(&self, entity_name: &str) -> Result<Vec<String>>
Get all unique values for a given entity.
Sourcepub fn get_entity_names(&self) -> Result<Vec<String>>
pub fn get_entity_names(&self) -> Result<Vec<String>>
Get all unique entity names.
Sourcepub fn query_files(
&self,
filters: &[(String, Vec<String>, bool)],
) -> Result<Vec<String>>
pub fn query_files( &self, filters: &[(String, Vec<String>, bool)], ) -> Result<Vec<String>>
Query files with advanced filtering including Query::None/Any and regex.
Filter types:
- Normal: (entity, values, false) — entity must match one of values
- Regex:
(entity, [pattern], true)— entity must match regex - Query::None: (entity, [“NONE”], false) — entity must NOT exist
- Query::Any: (entity, [“ANY”], false) — entity must exist (any value)
Sourcepub fn query_directories(
&self,
target_entity: &str,
filters: &[(String, Vec<String>, bool)],
) -> Result<Vec<String>>
pub fn query_directories( &self, target_entity: &str, filters: &[(String, Vec<String>, bool)], ) -> Result<Vec<String>>
Get distinct directories for files matching filters and having a target entity.
Sourcepub fn get_associations(
&self,
src: &str,
kind: Option<&str>,
) -> Result<Vec<(String, String)>>
pub fn get_associations( &self, src: &str, kind: Option<&str>, ) -> Result<Vec<(String, String)>>
Get associated files for a given source file.
Sourcepub fn file_count(&self) -> Result<usize>
pub fn file_count(&self) -> Result<usize>
Get the total number of indexed files.