Skip to main content

Database

Struct Database 

Source
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

Source

pub fn in_memory() -> Result<Self>

Create a new in-memory database.

Source

pub fn open(path: &Path) -> Result<Self>

Open or create a database at the given path.

Source

pub fn exists(path: &Path) -> bool

Check if a database file exists.

Source

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.

Source

pub fn commit_transaction(&self) -> Result<()>

Commit the current transaction.

Source

pub fn rollback_transaction(&self) -> Result<()>

Roll back the current transaction.

Source

pub fn insert_file(&self, file: &BidsFile) -> Result<()>

Insert a file into the database.

Source

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).

Source

pub fn insert_association(&self, src: &str, dst: &str, kind: &str) -> Result<()>

Insert a file association.

Source

pub fn set_layout_info(&self, root: &str, config: &str) -> Result<()>

Store layout info.

Source

pub fn get_layout_info(&self) -> Result<Option<(String, String)>>

Get layout info (root, config).

Source

pub fn all_file_paths(&self) -> Result<Vec<String>>

Query all file paths.

Source

pub fn get_tags( &self, file_path: &str, ) -> Result<Vec<(String, String, String, bool)>>

Get tags for a specific file.

Source

pub fn get_unique_entity_values(&self, entity_name: &str) -> Result<Vec<String>>

Get all unique values for a given entity.

Source

pub fn get_entity_names(&self) -> Result<Vec<String>>

Get all unique entity names.

Source

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)
Source

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.

Source

pub fn get_associations( &self, src: &str, kind: Option<&str>, ) -> Result<Vec<(String, String)>>

Get associated files for a given source file.

Source

pub fn file_count(&self) -> Result<usize>

Get the total number of indexed files.

Source

pub fn save_to(&self, path: &Path) -> Result<()>

Save the current in-memory database to a file.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.