Class: Farce::Transaction::TreeMap

Inherits:
Abstract::TreeMap show all
Defined in:
lib/farce/transaction/tree_map.rb

Overview

A TreeMap that is part of a transaction.

Constant Summary

Constants included from MapOperations

MapOperations::READ_HELPERS

Instance Method Summary collapse

Methods included from Unshareable

#initialize, #ractor_shareable?

Methods inherited from Abstract::TreeMap

#[], #[]=, #clear, #compare_keys_by_identity?, #compare_values_by_identity?, #delete, #each, #each_key, #each_value, #empty?, #fetch, #initialize, #key?, #length, #shareable_keys?, #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, #has_key?, #key, #key?, #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?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Farce::Transaction::Wrapper

Instance Method Details

#compare_and_set(key, expected, replacement, mode: nil) ⇒ BasicObject

See Also:

  • Abstract::TreeMap#compare_and_set


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/farce/transaction/tree_map.rb', line 48

def compare_and_set(key, expected, replacement, mode: nil)
  write do
    canonical = prepare(key)
    next compared(false) unless @working.key?(canonical)
    current = @working[canonical]
    matches = @manager ? matches?(current, expected, identity: false) : current == expected
    next compared(false) unless matches
    store_value(canonical, wrap(replacement, mode:))
    true
  end
end

#first_key ⇒ BasicObject?

Return the smallest key according to the map's ordering.

Returns:

  • (BasicObject, nil) —

    The first key, or nil if the map is empty.

See Also:



13
# File 'lib/farce/transaction/tree_map.rb', line 13

def first_key = access { @working.first_key }

#getkey(key) ⇒ BasicObject?

This method is abstract.

Return the stored key that matches a lookup key.

Parameters:

  • key (BasicObject) —

    The key to match.

Returns:

  • (BasicObject, nil) —

    The matching stored key, or nil if no key matches.

See Also:



21
# File 'lib/farce/transaction/tree_map.rb', line 21

def getkey(key) = access { @working.getkey(prepare(key)) }

#keys ⇒ Array<BasicObject>

Return the keys currently stored in the map. The result is frozen and ordered from smallest to largest key.

Returns:

  • (Array<BasicObject>) —

    The keys currently stored in the map, in order from smallest to largest.

See Also:



25
# File 'lib/farce/transaction/tree_map.rb', line 25

def keys = access { @working.each.map { |key, _| key }.freeze }

#last_key ⇒ BasicObject?

Return the largest key according to the map's ordering.

Returns:

  • (BasicObject, nil) —

    The last key, or nil if the map is empty.

See Also:



17
# File 'lib/farce/transaction/tree_map.rb', line 17

def last_key = access { @working.last_key }

#pop ⇒ Array(BasicObject, BasicObject)?

Remove and return the entry with the largest key according to the map's ordering.

Returns:

  • (Array(BasicObject, BasicObject), nil) —

    The last key-value pair, or nil if the map is empty.

See Also:



29
# File 'lib/farce/transaction/tree_map.rb', line 29

def pop = write { public_pair(@working.pop) }

#shift ⇒ Array(BasicObject, BasicObject)?

Remove and return the entry with the smallest key according to the map's ordering.

Returns:

  • (Array(BasicObject, BasicObject), nil) —

    The first key-value pair, or nil if the map is empty.

See Also:



33
# File 'lib/farce/transaction/tree_map.rb', line 33

def shift = write { public_pair(@working.shift) }

#swap(key, value, mode: nil) ⇒ BasicObject

See Also:

  • Abstract::TreeMap#swap


37
38
39
40
41
42
43
44
# File 'lib/farce/transaction/tree_map.rb', line 37

def swap(key, value, mode: nil)
  write do
    canonical = prepare(key)
    previous = unwrap_stored(@working[canonical])
    store_value(canonical, wrap(value, mode:))
    previous
  end
end