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 iter method to create an iterator to unsync, sync and future caches #114

Merged
merged 11 commits into from
Apr 10, 2022

Conversation

tatsuya6502
Copy link
Member

@tatsuya6502 tatsuya6502 commented Apr 9, 2022

Changes

This PR adds iterator visiting all key-value pairs in arbitrary order.

  • Add the iter method, who returns an iterator, to the following caches:
    • Not-thread-safe cache:
      • unsync::Cache
    • Concurrent Caches:
      • sync::Cache
      • sync::SegmentedCache
      • future::Cache
  • Implement IntoIterator to all the caches including experimental dash::Cache.

Fixes #112

Details

unsync cache's iterator

The unsync::Cache employs std::collections::HashMap as the central key-value storage:

  • The iterator element type is (&K, &V).
  • No mutation on the cache is allowed while an iterator is alive.
    • Mutations includes both read operations like get and write operations like insert.

sync and future caches' iterators

These concurrent caches employ moka::cht::SegmentedHashMap as the central key-value storage:

  • The iterator element type is (std::sync::Arc<K>, V), where V is a clone of the stored values.
  • Concurrent mutations on the cache is allowed while an iterator is alive.

To allow concurrent mutations, iterator's next method does not guarantee the following:

  • It does not guarantee to return a key-value pair (aka entry) if its key has been inserted to the cache after the iterator was created.
    • Such an entry may or may not be returned depending on key's hash and timing.

and the next method guarantees the followings:

  • It guarantees not to return the same entry more than once.
  • It guarantees not to return an entry if it has been removed from the cache after the iterator was created.
    • Note: An entry can be removed by following reasons:
      • It has been manually invalidated.
      • It has been expired (e.g. time-to-live).
      • It has been evicted as the cache capacity exceeded.

An implementation note

sync and future caches' iterators have some memory overheads:

It creates a snapshot of keys (Vec<std::sync::Weak<K>> Vec<std::sync::Arc<K>>) of an internal segment of cht::SegmentedHashMap. It uses some amount of heap memory depending on the number of keys in the segment. The number of segments of cht::SegmentedHashMap per cache is currently hard-coded to 64 (since Moka v0.1.0).

@tatsuya6502 tatsuya6502 self-assigned this Apr 9, 2022
@tatsuya6502 tatsuya6502 added the enhancement New feature or request label Apr 9, 2022
@tatsuya6502 tatsuya6502 added this to the v0.8.2 milestone Apr 9, 2022
@tatsuya6502 tatsuya6502 mentioned this pull request Apr 9, 2022
@tatsuya6502 tatsuya6502 marked this pull request as ready for review April 10, 2022 16:27
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 aedb98c into master Apr 10, 2022
@tatsuya6502 tatsuya6502 deleted the iterator branch April 10, 2022 23:16
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
None yet
Development

Successfully merging this pull request may close these issues.

Iterating cache
1 participant