Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the upsert and compute methods for modifying a cached entry #370

Merged
merged 16 commits into from
Jan 8, 2024

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
39 checks passed
@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
1 participant