Class: Farce::Local::Map

Inherits:
Abstract::ConcurrentMap show all
Includes:
Shareable::Tracked
Defined in:
lib/farce/local/map.rb,
lib/farce/local/map.rb

Overview

A shareable map with independent mutable storage 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::ConcurrentMap

#[], #[]=, #clear, #compact!, #compact_blank!, #compare_and_set, #compare_keys_by_identity?, #compare_values_by_identity?, #delete, #delete_if, #each, #each_key, #each_value, #fetch, #get, #getkey, #keep_if, #key?, #keys, #merge!, #reject!, #reverse_merge!, #select!, #size, #store, #store_if_absent, #swap, #transform_values!, #update, #upsert, #values, #wait_until, #wait_until_changed, #wait_until_match, #wait_until_non_nil, #wait_until_value, #wait_while, #wait_while_match, #wait_while_value

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(initial_mapping = nil, scope: :ractor, normalize_keys: nil, compare_by_identity: false, compare_keys_by_identity: compare_by_identity, compare_values_by_identity: compare_by_identity) ⇒ Map

Create a map with optional initial entries. Initial entries are reused for each new scope. 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:

  • initial_mapping (Hash, Farce::Abstract::Map, #each, nil) (defaults to: nil) —

    The entries to store initially. Each iteration must yield a key-value pair. Nil creates an empty map.

  • normalize_keys (Symbol, Proc, Hash, Farce::Abstract::Map, nil) (defaults to: nil) —

    Converts incoming keys to their canonical stored form:

    • If a Symbol is provided, it will be used as a method name to call on each key.
    • If a Proc is provided, it will be called with each key and should return the normalized key.
    • If a Hash or Map is provided, it will be used to look up the normalized key for each incoming key.
  • compare_by_identity (Boolean) (defaults to: false) —

    The default identity-comparison setting for keys and values. Defaults to false.

  • compare_keys_by_identity (Boolean) (defaults to: compare_by_identity) —

    Whether keys use object identity instead of hash and eql?. Defaults to compare_by_identity.

  • compare_values_by_identity (Boolean) (defaults to: compare_by_identity) —

    Whether values use object identity instead of ==. Defaults to compare_by_identity.

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

    The scope of the map. Defaults to :ractor.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/farce/local/map.rb', line 26

class Map < Abstract::ConcurrentMap
  include Shareable::Tracked
  include Scoped::Tracked

  protected

  def internal_map = scoped_value

  private

  def new_scoped_value(entries = nil, **)
    unless @key_normalizer || Internal::KeyNormalizer.canonical_entries?(entries)
      return Internal::UnsharedMap.new(entries, **)
    end

    map = Internal::UnsharedMap.new(nil, **)
    entries&.each { map[_1] = _2 }
    Internal::KeyNormalizer.wrap_concurrent(map, @key_normalizer)
  end
end