Class: Farce::Local::LFUMap

Inherits:
Abstract::LFUMap show all
Includes:
Shareable::Tracked
Defined in:
lib/farce/local/lfu_map.rb

Overview

A shareable LFU map with independent mutable storage and capacity in each scope.

Instance Attribute Summary

Attributes included from Scoped

#scope

Instance Method Summary collapse

Methods included from Shareable

#ractor_shareable?

Methods inherited from Abstract::BoundedMap

#[], #[]=, #clear, #compare_keys_by_identity?, #compare_values_by_identity?, #delete, #each, #each_key, #each_value, #fetch, #getkey, #key?, #keys, #max_size, #max_size=, #prune, #shift, #size, #store_if_absent, #values

Methods included from Abstract::DuplicableMap

#compact, #compact_blank, #deep_dup, #duplicable?, #except, #flatten, #invert, #merge, #reject, #reverse_merge, #select, #slice, #stringify_keys, #symbolize_keys, #to_proc, #transform_keys, #transform_values, #with_indifferent_access

Methods inherited from Abstract::Map

#[], #[]=, #as_extended_json, #as_json, #assert_valid_keys, #assoc, #bson_type, #clear, #compare_by_identity?, #compare_keys_by_identity?, #compare_values_by_identity?, #deconstruct_keys, #delete, #dig, #each, #each_key, #each_pair, #each_value, #empty?, #fetch, #fetch_values, #getkey, #has_key?, #key, #key?, #keys, #length, #rassoc, #shareable_keys?, #shareable_values?, #size, #store, #store_if_absent, #to_a, #to_bson, #to_bson_normalized_value, #to_cbor, #to_h, #to_hash, #to_json, #to_msgpack, #to_query, #to_s, #value?, #values_at, #weak_keys?, #weak_values?

Constructor Details

#initialize(entries = nil, max_size:, scope: :ractor, **options) ⇒ LFUMap

Valid scopes are:

  • :ractor - The value is shared by all threads within the same Ractor.
  • :thread_group - The value is shared by all threads within the same thread group.
  • :thread - The value is shared by all fibers within the same thread.
  • :fiber_storage - The value is shared by all fibers using the same storage. Unless explicitly specified, storage is inherited from the parent fiber for blocking fibers, but not for non-blocking fibers (such as those created by Scheduler#schedule).
  • :fiber - Each fiber has its own independent value.

Parameters:

  • entries (Hash, Array<Array(BasicObject, BasicObject)>, Abstract::Map, #each, nil) (defaults to: nil) —

    Initial entries for each new scope. Entries are stored sequentially and may be evicted.

  • max_size (Integer) —

    Maximum entries retained by each new scoped map.

  • scope (Symbol) (defaults to: :ractor) —

    The scope of the LFU map.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/farce/local/lfu_map.rb', line 16

class LFUMap < Abstract::LFUMap
  include Shareable::Tracked
  include Scoped::Tracked

  State = Data.define(:map, :key_locks)
  private_constant :State

  protected

  def internal_map = scoped_value.map

  private

  def freeze_scoped_value(state) = state.map.freeze

  def new_copied_scoped_value(map)
    locks = Internal::KeyLockMap.new(
      registry_class:           Farce::Unshared::Map,
      compare_keys_by_identity: map.compare_keys_by_identity?,
    )
    State.new(map, locks)
  end

  def with_key_lock(key, &) = scoped_value.key_locks.synchronize(key, &)

  def new_scoped_value(
    entries = nil,
    compare_by_identity: false,
    compare_keys_by_identity: compare_by_identity,
    **
  )
    entries = convert_entries(entries)
    map     = Internal::LFUMap.new(compare_by_identity:, compare_keys_by_identity:, **)
    locks   = Internal::KeyLockMap.new(registry_class: Farce::Unshared::Map, compare_keys_by_identity:)
    entries&.each { map[_1] = _2 }
    State.new(map, locks)
  end
end