Class: Farce::Transaction::Vector
- Inherits:
-
Abstract::Vector
- Object
- Abstract::Collection
- Abstract::Vector
- Farce::Transaction::Vector
- Defined in:
- lib/farce/transaction/vector.rb
Overview
A Vector that is part of a transaction.
Instance Method Summary collapse
-
#[](index) ⇒ BasicObject?
Read an index without waiting for atomic-update access.
-
#[]=(index, value) ⇒ BasicObject
Store a value, growing the vector if necessary.
-
#clear ⇒ self
Remove all slots.
-
#compare_and_set(index, expected, replacement, mode: nil) ⇒ Boolean
Replace an existing index only if its value matches the expected value.
-
#compare_by_identity? ⇒ Boolean
Whether values are compared by identity.
-
#each {|value| ... } ⇒ self, Enumerator
Iterate over live entries, up to the length when enumeration starts.
- #empty? ⇒ Boolean
-
#get(index) ⇒ BasicObject?
Read an index after acquiring atomic-update access.
-
#pop ⇒ BasicObject?
Remove and return the last value.
-
#push(value, mode: nil) ⇒ self, false
(also: #<<)
Append a single value.
-
#shareable_values? ⇒ Boolean
Whether stored values must be Ractor-shareable.
-
#size ⇒ Integer
The number of slots, including nil slots.
-
#store(index, value, mode: nil) ⇒ BasicObject, false
Store a value after acquiring atomic-update access.
-
#store_if_absent(index, mode: nil) { ... } ⇒ BasicObject?
Compute and store a value only when the index is absent or contains nil.
-
#swap(index, value, mode: nil) ⇒ BasicObject?
Replace an index and return its previous value, growing the vector if necessary.
-
#update(index, mode: nil) {|value| ... } ⇒ BasicObject?
Atomically replace an index with the block result, growing the vector if necessary.
-
#upsert(index, initial, mode: nil) {|value| ... } ⇒ BasicObject?
Store initial for an absent or nil index, otherwise replace it with the block result.
Methods included from Unshareable
#initialize, #ractor_shareable?
Methods inherited from Abstract::Vector
#&, #*, #+, #-, #append, #as_extended_json, #as_json, #assoc, #at, #bsearch, #bsearch_index, #bson_type, #chunk, #chunk_while, #compact, #compact_blank, #concat, #deconstruct, #deep_dup, #difference, #dig, #drop, #drop_while, #each_cons, #each_index, #each_slice, #entries, #excluding, #fetch, #fetch_values, #fifth, #filter_map, #first, #flat_map, #forty_two, #fourth, #from, #grep, #grep_v, #group_by, #in_groups, #in_groups_of, #in_order_of, #include?, #including, #index, #inquiry, #intersect?, #intersection, #last, #map, #max, #max_by, #maximum, #min, #min_by, #minimum, #minmax, #minmax_by, #pack, #partition, #pick, #pluck, #rassoc, #reject, #reverse, #reverse_each, #rfind, #rindex, #rotate, #sample, #second, #second_to_last, #select, #shuffle, #slice, #slice_after, #slice_before, #slice_when, #sort, #sort_by, #split, #take, #take_while, #third, #third_to_last, #to, #to_a, #to_bson, #to_bson_normalized_value, #to_cbor, #to_fs, #to_h, #to_json, #to_msgpack, #to_param, #to_query, #to_sentence, #to_xml, #union, #uniq, #values_at, #wait_until, #wait_until_changed, #wait_until_match, #wait_until_non_nil, #wait_until_value, #wait_while, #wait_while_match, #wait_while_value, #zip, #|
Methods inherited from Abstract::Collection
[], #count, #filter, #include?, #join, #length, #member?, #reject, #select, #to_a, #to_s
Methods included from Internal::Copyable
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Farce::Transaction::Wrapper
Instance Method Details
#[](index) ⇒ BasicObject?
Read an index without waiting for atomic-update access.
21 |
# File 'lib/farce/transaction/vector.rb', line 21 def [](index) = access { unwrap_stored(@working[index]) } |
#[]=(index, value) ⇒ BasicObject
Store a value, growing the vector if necessary.
37 38 39 |
# File 'lib/farce/transaction/vector.rb', line 37 def []=(index, value) store(index, value) end |
#clear ⇒ self
Remove all slots.
67 68 69 70 |
# File 'lib/farce/transaction/vector.rb', line 67 def clear write { @working.clear } self end |
#compare_and_set(index, expected, replacement, mode: nil) ⇒ Boolean
Replace an existing index only if its value matches the expected value. This never grows the vector. Matching uses the configured comparison mode.
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/farce/transaction/vector.rb', line 107 def compare_and_set(index, expected, replacement, mode: nil) write do next compared(@working.compare_and_set(index, expected, replacement)) unless @manager index = Integer(index) next compared(false) unless index >= -@working.size && index < @working.size && matches?(@working[index], expected, identity: @object.compare_by_identity?) @working.store(index, wrap(replacement, mode:)) true end end |
#compare_by_identity? ⇒ Boolean
Returns Whether values are compared by identity.
13 |
# File 'lib/farce/transaction/vector.rb', line 13 def compare_by_identity? = access { @object.compare_by_identity? } |
#each {|value| ... } ⇒ self, Enumerator
Iterate over live entries, up to the length when enumeration starts. Changes can affect entries not yet visited. Indexes beyond the starting length are not visited. Each entry is read separately. The block runs without holding a collection lock.
74 75 76 77 78 |
# File 'lib/farce/transaction/vector.rb', line 74 def each return enum_for(__method__) unless block_given? access { @working.snapshot.each { |value| yield unwrap_stored(value) } } self end |
#empty? ⇒ Boolean
33 |
# File 'lib/farce/transaction/vector.rb', line 33 def empty? = size.zero? |
#get(index) ⇒ BasicObject?
Read an index after acquiring atomic-update access.
25 |
# File 'lib/farce/transaction/vector.rb', line 25 def get(index) = self[index] |
#pop ⇒ BasicObject?
Remove and return the last value. This does not wait for an empty vector to become nonempty.
63 |
# File 'lib/farce/transaction/vector.rb', line 63 def pop = write { unwrap_stored(@working.pop) } |
#push(value, mode: nil) ⇒ self, false Also known as: <<
Append a single value.
55 56 57 58 |
# File 'lib/farce/transaction/vector.rb', line 55 def push(value, mode: nil) write { @working.push(wrap(value, mode:)) } self end |
#shareable_values? ⇒ Boolean
Returns Whether stored values must be Ractor-shareable.
17 |
# File 'lib/farce/transaction/vector.rb', line 17 def shareable_values? = access { @object.shareable_values? } |
#size ⇒ Integer
Returns The number of slots, including nil slots.
29 |
# File 'lib/farce/transaction/vector.rb', line 29 def size = access { @working.size } |
#store(index, value, mode: nil) ⇒ BasicObject, false
Store a value after acquiring atomic-update access.
43 44 45 |
# File 'lib/farce/transaction/vector.rb', line 43 def store(index, value, mode: nil) write { unwrap_stored(@working.store(index, wrap(value, mode:))) } end |
#store_if_absent(index, mode: nil) { ... } ⇒ BasicObject?
Compute and store a value only when the index is absent or contains nil.
89 90 91 92 |
# File 'lib/farce/transaction/vector.rb', line 89 def store_if_absent(index, mode: nil) require_block!(block_given?) write { unwrap_stored(@working.store_if_absent(index) { wrap(yield, mode:) }) } end |
#swap(index, value, mode: nil) ⇒ BasicObject?
Replace an index and return its previous value, growing the vector if necessary.
49 50 51 |
# File 'lib/farce/transaction/vector.rb', line 49 def swap(index, value, mode: nil) write { unwrap_stored(@working.swap(index, wrap(value, mode:))) } end |
#update(index, mode: nil) {|value| ... } ⇒ BasicObject?
Atomically replace an index with the block result, growing the vector if necessary.
82 83 84 85 |
# File 'lib/farce/transaction/vector.rb', line 82 def update(index, mode: nil) require_block!(block_given?) write { unwrap_stored(@working.update(index) { |value| wrap(yield(unwrap_stored(value)), mode:) }) } end |
#upsert(index, initial, mode: nil) {|value| ... } ⇒ BasicObject?
Store initial for an absent or nil index, otherwise replace it with the block result.
96 97 98 99 100 101 102 103 |
# File 'lib/farce/transaction/vector.rb', line 96 def upsert(index, initial, mode: nil) require_block!(block_given?) write do unwrap_stored(@working.upsert(index, wrap(initial, mode:)) do |value| wrap(yield(unwrap_stored(value)), mode:) end) end end |