Skip to main content

AsyncSqliteIo

Struct AsyncSqliteIo 

Source
pub struct AsyncSqliteIo<S: AsyncConnectionState> { /* private fields */ }
Expand description

Async interface for page-level SQLite database access.

This struct wraps the blocking SqliteIo and executes all blocking SQLite operations via tokio::task::spawn_blocking.

§Important Notes

  • The connection is opened in-memory, and the actual database file is attached
  • All page operations use the ‘target’ schema name
  • The connection is configured with special permissions for page-level access

§Typestate Pattern

This connection uses a typestate pattern. When you start a transaction with transaction, the connection moves to the AsyncInTransaction state. You must call commit() or rollback() to return to the normal state.

Implementations§

Source§

impl AsyncSqliteIo<AsyncNormal>

Source

pub async fn open(uri: &str) -> Result<Self, Error>

Opens a database for page-level access asynchronously.

This creates an in-memory SQLite connection and attaches the database using the provided URI. The URI can be a simple path or include query parameters like ?vfs=reverb.

§Arguments
  • uri - URI string for the database. Can be:
    • A simple path: /path/to/db.sqlite
    • A file URI: file:/path/to/db.sqlite
    • A URI with parameters: file:/path/to/db.sqlite?vfs=reverb
§Errors

Returns an error if:

  • The database path is invalid
  • The database cannot be opened or attached
  • The specified VFS is not registered
  • Required database configurations cannot be set
Source

pub async fn transaction( self, transaction_type: TransactionType, ) -> Result<AsyncSqliteIo<AsyncInTransaction>, Error>

Starts a new async transaction for page modifications.

This method consumes the connection and returns it in transaction mode. You must call commit() or rollback() on the returned connection to get back to normal mode.

§Arguments
  • transaction_type - The type of transaction to begin. Use TransactionType::Immediate (the default) for write operations to avoid SQLITE_BUSY errors.
§Errors

Returns an error if the transaction cannot be started.

§Example
use sqlite_pages::{AsyncSqliteIo, TransactionType};

// Create a test database
let tempdir = tempfile::tempdir()?;
let db_path = tempdir.path().join("test.db");
rusqlite::Connection::open(&db_path)?.execute(
    "CREATE TABLE test (id INTEGER PRIMARY KEY)",
    [],
)?;

let db = AsyncSqliteIo::open(&db_path.to_string_lossy()).await?;
let tx = db.transaction(TransactionType::Immediate).await?;

// Can await between operations
let page = tx.get_page_data(1).await?;
tx.set_page_data(1, page.data()).await?;
let db = tx.commit().await?;  // Returns connection back to normal mode
Source

pub async fn page_map<R, F>(&self, range: R, fun: F) -> Result<(), Error>
where R: RangeBounds<usize> + Send + 'static, F: FnMut(usize, &[u8]) + Send + 'static,

Maps a function over database pages in the specified range asynchronously.

Page numbers are 1-based (the first page is page 1, not page 0).

§Arguments
  • range - A range of page numbers (1-based) to process
  • fun - A function that receives the page number (1-based) and page data for each page
§Errors

Returns an error if:

  • The range is invalid (start > end)
  • The database cannot be queried
  • Page data cannot be read
Source

pub async fn page_count(&self) -> Result<usize, Error>

Returns the number of pages in the database.

§Errors

Returns an error if the page count cannot be retrieved.

Source

pub async fn with_connection<F, R>(&self, f: F) -> Result<R, Error>
where F: FnOnce(&Connection) -> R + Send + 'static, R: Send + 'static,

Executes a closure with access to the underlying SqliteIo connection.

This method provides synchronous access to the rusqlite::Connection for operations that require direct SQL access, such as diagnostic queries, test setup, or arbitrary SQL execution.

The closure runs inside spawn_blocking to avoid blocking the async runtime.

§Arguments
  • f - A closure that receives a reference to the SqliteIo and returns a value.
§Errors

Returns an error if the inner connection cannot be accessed or if the spawned task fails.

§Example
use sqlite_pages::AsyncSqliteIo;

let db = AsyncSqliteIo::open(&db_path.to_string_lossy()).await?;

// Execute arbitrary SQL via the connection
let count: i64 = db.with_connection(|conn| {
    conn.query_row("SELECT COUNT(*) FROM sqlite_master", [], |row| row.get(0))
        .unwrap()
}).await?;
Source§

impl AsyncSqliteIo<AsyncInTransaction>

Source

pub async fn set_page_data( &self, page_number: usize, data: &[u8], ) -> Result<(), Error>

Sets the data for a specific page in the database.

§Arguments
  • page_number - The page number (1-based: first page is 1)
  • data - The raw page data
§Errors

Returns an error if the page data cannot be written to the database.

Source

pub async fn get_page_data(&self, page_number: usize) -> Result<Page, Error>

Retrieves the data for a specific page from the database.

§Arguments
  • page_number - The page number (1-based: first page is 1)
§Errors

Returns an error if the page cannot be read from the database.

Source

pub async fn page_map<R, F>(&self, range: R, fun: F) -> Result<(), Error>
where R: RangeBounds<usize> + Send + 'static, F: FnMut(usize, &[u8]) + Send + 'static,

Maps a function over database pages in the specified range asynchronously.

Page numbers are 1-based (the first page is page 1, not page 0).

§Arguments
  • range - A range of page numbers (1-based) to process
  • fun - A function that receives the page number (1-based) and page data for each page
§Errors

Returns an error if the range is invalid or pages cannot be read.

Source

pub async fn with_connection<F, R>(&self, f: F) -> Result<R, Error>
where F: FnOnce(&Connection) -> R + Send + 'static, R: Send + 'static,

Executes a closure with access to the underlying rusqlite::Connection.

This method provides synchronous access to the rusqlite::Connection for operations that require direct SQL access, such as diagnostic queries, test setup, or arbitrary SQL execution.

The closure runs inside spawn_blocking to avoid blocking the async runtime.

§Arguments
  • f - A closure that receives a reference to the connection and returns a value.
§Errors

Returns an error if the inner connection cannot be accessed or if the spawned task fails.

Source

pub async fn commit(self) -> Result<AsyncSqliteIo<AsyncNormal>, Error>

Commits the transaction, persisting all changes to the database.

Returns the connection back to normal mode.

§Errors

Returns an error if the transaction cannot be committed.

Source

pub async fn rollback(self) -> Result<AsyncSqliteIo<AsyncNormal>, Error>

Rolls back the transaction, discarding all changes.

Returns the connection back to normal mode.

§Errors

Returns an error if the rollback fails.

Auto Trait Implementations§

§

impl<S> Freeze for AsyncSqliteIo<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for AsyncSqliteIo<S>
where S: RefUnwindSafe,

§

impl<S> Send for AsyncSqliteIo<S>
where S: Send,

§

impl<S> Sync for AsyncSqliteIo<S>
where S: Sync,

§

impl<S> Unpin for AsyncSqliteIo<S>
where S: Unpin,

§

impl<S> UnwindSafe for AsyncSqliteIo<S>
where S: UnwindSafe,

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.