Skip to content

Conversation

tatsuya6502
Copy link
Member

@tatsuya6502 tatsuya6502 commented Dec 31, 2023

Fixes #227, #353.

This PR adds compute family-methods to the entry API. They take a closure to insert, update, or remove a value for a key depending on the current cached value.

Also bumps the version to v0.12.3.

API: future::Cache

/// Upsert (update or insert) the cached entry.
pub async fn and_upsert_with<F, Fut>(self, f: F) -> Entry<K, V>
where
    F: FnOnce(Option<Entry<K, V>>) -> Fut,
    Fut: Future<Output = V>;


/// Upsert, remove or nop (no-operation) on the cached entry.
pub async fn and_compute_with<F, Fut>(self, f: F) -> compute::CompResult<K, V>)
where
    F: FnOnce(Option<Entry<K, V>>) -> Fut,
    Fut: Future<Output = compute::Op<V>>;


/// When Ok(Op), upsert, remove or nop (no-operation) on the cached entry.
/// Otherwise return Err(E)
pub async fn and_try_compute_with<F, Fut, E>(
    self,
    f: F,
) -> Result<compute::CompResult<K, V>, E>
where
    F: FnOnce(Option<Entry<K, V>>) -> Fut,
    Fut: Future<Output = Result<compute::Op<V>, E>>,
    E: Send + Sync + 'static;

Examples

See the following examples in the examples directory of the compute-api branch:

  • counter_async.rs
    • Uses the and_upsert_with method.
    • Atomically increment a cached u64.
  • bounded_counter_async.rs
    • Uses the and_compute_with method.
    • Atomically increment a cached u64 but reset (remove) it when it is 2.
  • append_value_async.rs
    • Uses the and_upsert_with method.
    • Atomically append an i32 to a cached Arc<RwLock<Vec<i32>>>.
  • try_append_value_async.rs
    • Uses the and_try_compute_with method.
    • Atomically read an char and append to a cached Arc<RwLock<String>>, but reading can fail by EOF.

API: sync::Cache and sync::SegmentedCache

/// Upsert (update or insert) the cached entry.
pub fn and_upsert_with<F>(self, f: F) -> Entry<K, V>
where
    F: FnOnce(Option<Entry<K, V>>) -> V;


/// Upsert, remove or nop (no-operation) on the cached entry.
pub fn and_compute_with<F>(self, f: F) -> compute::CompResult<K, V>
where
    F: FnOnce(Option<Entry<K, V>>) -> compute::Op<V>;


/// When Ok(Op), upsert, remove or nop (no-operation) on the cached entry.
/// Otherwise return Err(E)
pub fn and_try_compute_with<F, E>(
    self,
    f: F,
) -> Result<compute::CompResult<K, V>, E>
where
    F: FnOnce(Option<Entry<K, V>>) -> Result<compute::Op<V>, E>,
    E: Send + Sync + 'static;

Tasks

future::Cache:

  • and_upsert_with method. (upsert: insert or update)
    • Implementation.
    • Automated tests.
    • Documents.
    • Examples.
  • and_compute_with method.
    • Implementation.
    • Automated tests.
    • Documents.
    • Examples.
  • and_try_compute_with method.
    • Implementation.
    • Automated tests.
    • Documents.
    • Examples.

sync::Cache, sync::SegmentedCache:

  • and_upsert_with method. (upsert: insert or update)
    • Implementation.
    • Automated tests.
    • Documents.
    • Examples.
  • and_compute_with method.
    • Implementation.
    • Automated tests.
    • Documents.
    • Examples.
  • and_try_compute_with method.
    • Implementation.
    • Automated tests.
    • Documents.
    • Examples.

@tatsuya6502 tatsuya6502 added this to the v0.12.3 milestone Dec 31, 2023
@tatsuya6502 tatsuya6502 added the enhancement New feature or request label Dec 31, 2023
@tatsuya6502 tatsuya6502 changed the title Add the compute API for modifying a cached entry Add the upsert and compute methods for modifying a cached entry Jan 3, 2024
@tatsuya6502 tatsuya6502 linked an issue Jan 3, 2024 that may be closed by this pull request
@tatsuya6502 tatsuya6502 marked this pull request as ready for review January 6, 2024 08:22
access to the entry

Also improve the docs and source code comments of the compute family methods.
@tatsuya6502
Copy link
Member Author

tatsuya6502 commented Jan 7, 2024

I changed the return types of and_compute_with and and_try_compute_with methods (commit: aee7ebe).

future::Cache

-pub async fn and_compute_with<F, Fut>(self, f: F) -> (Option<Entry<K, V>>, compute::PerformedOp)
+pub async fn and_compute_with<F, Fut>(self, f: F) -> compute::CompResult<K, V>)
 where
     F: FnOnce(Option<Entry<K, V>>) -> Fut,
     Fut: Future<Output = compute::Op<V>>;

 pub async fn and_try_compute_with<F, Fut, E>(
     self,
     f: F,
-) -> Result<(Option<Entry<K, V>>, compute::PerformedOp), E>
+) -> Result<compute::CompResult<K, V>, E>
 where
     F: FnOnce(Option<Entry<K, V>>) -> Fut,
     Fut: Future<Output = Result<compute::Op<V>, E>>,
     E: Send + Sync + 'static;

sync::Cache and sync::SegmentedCache

-pub fn and_compute_with<F>(self, f: F) -> (Option<Entry<K, V>>, compute::PerformedOp)
+pub fn and_compute_with<F>(self, f: F) -> compute::CompResult<K, V>
 where
     F: FnOnce(Option<Entry<K, V>>) -> compute::Op<V>;

 pub fn and_try_compute_with<F, E>(
     self,
     f: F,
-) -> Result<(Option<Entry<K, V>>, compute::PerformedOp), E>
+) -> Result<compute::CompResult<K, V>, E>
 where
     F: FnOnce(Option<Entry<K, V>>) -> Result<compute::Op<V>, E>,
     E: Send + Sync + 'static;

Copy link
Member Author

@tatsuya6502 tatsuya6502 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging.

@tatsuya6502 tatsuya6502 merged commit cc36d72 into main Jan 8, 2024
@tatsuya6502 tatsuya6502 deleted the compute-api branch January 8, 2024 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Can Moka combine entries ? Entry and_compute method for performing an insert, update, or remove operation with single closure
1 participant