holochain_data 0.7.0-rc.0

Database abstraction layer for Holochain using sqlx
Documentation
//! `DbRead<Dht>` / `DbWrite<Dht>` API for the `ScheduledFunction` table.

use super::super::inner::scheduled_function::{self, InsertScheduledFunction};
use crate::handles::{DbRead, DbWrite};
use crate::kind::Dht;
use holo_hash::AgentPubKey;
use holochain_timestamp::Timestamp;

impl DbRead<Dht> {
    /// Fetch persisted (non-ephemeral) scheduled-function rows for `author` whose
    /// `end_at` is before `now`. Returns `(zome_name, scheduled_fn, maybe_schedule_blob)` tuples.
    pub async fn get_expired_persisted_scheduled_functions(
        &self,
        author: &AgentPubKey,
        now: Timestamp,
    ) -> sqlx::Result<Vec<(String, String, Vec<u8>)>> {
        let mut conn = self.timed_conn().await?;
        scheduled_function::get_expired_persisted_scheduled_functions(&mut *conn, author, now).await
    }

    /// Return live scheduled-function rows for `author` at `now`.
    ///
    /// A row is "live" when `start_at <= now AND now <= end_at`. Returns
    /// `(zome_name, scheduled_fn, maybe_schedule_blob, ephemeral)` tuples.
    pub async fn get_live_scheduled_functions(
        &self,
        author: &AgentPubKey,
        now: Timestamp,
    ) -> sqlx::Result<Vec<(String, String, Vec<u8>, bool)>> {
        let mut conn = self.timed_conn().await?;
        scheduled_function::get_live_scheduled_functions(&mut *conn, author, now).await
    }

    /// Return `true` if a scheduled-function row exists for the given
    /// `(author, zome_name, scheduled_fn)` tuple, regardless of liveness.
    pub async fn is_function_scheduled(
        &self,
        author: &AgentPubKey,
        zome_name: &str,
        scheduled_fn: &str,
    ) -> sqlx::Result<bool> {
        let mut conn = self.timed_conn().await?;
        scheduled_function::is_function_scheduled(&mut *conn, author, zome_name, scheduled_fn).await
    }
}

impl DbWrite<Dht> {
    /// Upsert a scheduled-function row. Returns the number of rows written.
    pub async fn upsert_scheduled_function(
        &self,
        f: InsertScheduledFunction<'_>,
    ) -> sqlx::Result<u64> {
        scheduled_function::upsert_scheduled_function(self.pool(), f).await
    }

    /// Delete the scheduled-function row for the given `(author, zome_name, scheduled_fn)` tuple.
    /// Returns the number of rows deleted.
    pub async fn delete_scheduled_function(
        &self,
        author: &AgentPubKey,
        zome_name: &str,
        scheduled_fn: &str,
    ) -> sqlx::Result<u64> {
        scheduled_function::delete_scheduled_function(self.pool(), author, zome_name, scheduled_fn)
            .await
    }

    /// Delete all live ephemeral scheduled-function rows for `author` whose
    /// `start_at` is at or before `now`. Returns the number of rows deleted.
    pub async fn delete_live_ephemeral_scheduled_functions(
        &self,
        author: &AgentPubKey,
        now: Timestamp,
    ) -> sqlx::Result<u64> {
        scheduled_function::delete_live_ephemeral_scheduled_functions(self.pool(), author, now)
            .await
    }

    /// Delete every ephemeral scheduled-function row, regardless of author or
    /// liveness. Returns the number of rows deleted.
    pub async fn delete_all_ephemeral_scheduled_functions(&self) -> sqlx::Result<u64> {
        scheduled_function::delete_all_ephemeral_scheduled_functions(self.pool()).await
    }
}