Skip to main content

MemoryStore

Struct MemoryStore 

Source
pub struct MemoryStore { /* private fields */ }
Expand description

In-memory store used for examples and tests.

Implementations§

Source§

impl MemoryStore

Source

pub fn new() -> Self

Examples found in repository?
examples/durable_dag.rs (line 34)
32async fn main() {
33    let dag = DagRunner::new();
34    let db: Arc<dyn duraflow_rs::Storage + Send + Sync> = Arc::new(MemoryStore::new());
35    let ctx = Arc::new(Context {
36        db,
37        completed_count: Arc::new(AtomicUsize::new(0)),
38    });
39
40    let d_dag = DurableDag::new(&dag, ctx.clone()).with_progress(|id, completed| {
41        println!("progress: task={} completed_count={}", id, completed);
42    });
43
44    // Build DAG (same as before)
45    let val1 = d_dag.add("v1", LoadValue(10));
46    let val2 = d_dag.add("v2", LoadValue(20));
47    let val3 = d_dag.add("v3", LoadValue(2));
48
49    let sum_builder = d_dag.add("sum_1_2", Add);
50    let one = d_dag.add("v4", LoadValue(1));
51    let product = d_dag
52        .add("final_prod", Multiply)
53        .depends_on((&sum_builder, &val3, &one));
54    let _sum = sum_builder.depends_on((&val1, &val2));
55
56    dag.run(|fut| {
57        tokio::spawn(fut);
58    })
59    .await
60    .unwrap();
61
62    println!(
63        "Completed tasks: {}",
64        ctx.completed_count
65            .load(std::sync::atomic::Ordering::SeqCst)
66    );
67    println!("Final Result: {}", dag.get(product).unwrap());
68
69    // Resumption
70    println!("\nRestarting DAG...");
71    let dag2 = DagRunner::new();
72    let d_dag2 = DurableDag::new(&dag2, ctx.clone()).with_progress(|id, completed| {
73        println!(
74            "(resumed) progress: task={} completed_count={}",
75            id, completed
76        );
77    });
78
79    let v1 = d_dag2.add("v1", LoadValue(10));
80    let v2 = d_dag2.add("v2", LoadValue(20));
81    let s = d_dag2.add("sum_1_2", Add).depends_on((&v1, &v2));
82
83    dag2.run(|fut| {
84        tokio::spawn(fut);
85    })
86    .await
87    .unwrap();
88    println!("Retrieved cached sum: {}", dag2.get(s).unwrap());
89}

Trait Implementations§

Source§

impl Default for MemoryStore

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Storage for MemoryStore

Source§

fn get_raw(&self, key: &str) -> Option<String>

Return the raw JSON string stored for key, if any.
Source§

fn save_raw(&self, key: &str, value: &str) -> Result<()>

Store a raw JSON string for key.

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.