Class: Farce::Transaction::Vector

Inherits:
Abstract::Vector show all
Defined in:
lib/farce/transaction/vector.rb

Overview

A Vector that is part of a transaction.

Instance Method Summary collapse

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

#duplicable?

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.

Parameters:

  • index (Integer) —

    The index. Negative indexes count from the end.

Returns:

  • (BasicObject, nil) —

    The value, or nil for an index outside the vector.

See Also:



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.

Parameters:

  • index (Integer) —

    The index. Negative indexes count from the end.

  • value (BasicObject) —

    The value to store.

Returns:

  • (BasicObject) —

    The assigned value.

See Also:



37
38
39
# File 'lib/farce/transaction/vector.rb', line 37

def []=(index, value)
  store(index, value)
end

#clear ⇒ self

Remove all slots.

Returns:

  • (self)

See Also:



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.

Parameters:

  • index (Integer) —

    The index. Negative indexes count from the end.

  • expected (BasicObject) —

    The value that must match the current entry.

  • replacement (BasicObject) —

    The value to store on a match.

  • timeout (Numeric, nil) —

    The maximum wait in seconds. Nil waits indefinitely.

Returns:

  • (Boolean) —

    Whether the replacement succeeded. False on timeout.

See Also:



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.

Returns:

  • (Boolean) —

    Whether values are compared by identity.

See Also:



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.

Yields:

  • (value) —

    Called for each entry. Returns an Enumerator without a block.

Yield Parameters:

  • value (BasicObject) —

    The current value.

Yield Returns:

  • (void) —

    The result is ignored.

Returns:

  • (self, Enumerator)

See Also:



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

Returns:

  • (Boolean)

See Also:

  • Abstract::Vector#empty?


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

def empty? = size.zero?

#get(index) ⇒ BasicObject?

Read an index after acquiring atomic-update access.

Parameters:

  • index (Integer) —

    The index. Negative indexes count from the end.

  • timeout (Numeric, nil) —

    The maximum wait in seconds. Nil waits indefinitely.

Returns:

  • (BasicObject, nil) —

    The value, or nil if absent or timed out.

See Also:



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.

Parameters:

  • timeout (Numeric, nil) —

    The maximum wait in seconds. Nil waits indefinitely.

Returns:

  • (BasicObject, nil) —

    The last value, or nil if empty or timed out.

See Also:



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.

Parameters:

  • value (BasicObject) —

    The value to append.

  • timeout (Numeric, nil) —

    The maximum wait in seconds. Nil waits indefinitely.

Returns:

  • (self, false) —

    Self on success, or false on timeout.

See Also:



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.

Returns:

  • (Boolean) —

    Whether stored values must be Ractor-shareable.

See Also:



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.

Returns:

  • (Integer) —

    The number of slots, including nil slots.

See Also:



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.

Parameters:

  • index (Integer) —

    The index. Negative indexes count from the end.

  • value (BasicObject) —

    The value to store.

  • timeout (Numeric, nil) —

    The maximum wait in seconds. Nil waits indefinitely.

Returns:

  • (BasicObject, false) —

    The value, or false on timeout.

See Also:



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.

Parameters:

  • index (Integer) —

    The index. Negative indexes count from the end.

  • timeout (Numeric, nil) —

    The maximum wait in seconds. Nil waits indefinitely.

Yields:

  • Called without arguments to compute a value when the slot is absent or nil.

Yield Returns:

  • (BasicObject) —

    The value to store.

Returns:

  • (BasicObject, nil) —

    The existing or computed value, or nil on timeout.

See Also:



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.

Parameters:

  • index (Integer) —

    The index. Negative indexes count from the end.

  • replacement (BasicObject) —

    The new value.

  • timeout (Numeric, nil) —

    The maximum wait in seconds. Nil waits indefinitely.

Returns:

  • (BasicObject, nil) —

    The previous value, or nil if absent or timed out.

See Also:



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.

Parameters:

  • index (Integer) —

    The index. Negative indexes count from the end.

  • timeout (Numeric, nil) —

    The maximum wait in seconds. Nil waits indefinitely.

Yields:

  • (value) —

    Compute the replacement while holding atomic-update access.

Yield Parameters:

  • value (BasicObject, nil) —

    The current value, or nil if absent.

Yield Returns:

  • (BasicObject) —

    The replacement value.

Returns:

  • (BasicObject, nil) —

    The replacement value, or nil on timeout.

See Also:



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.

Parameters:

  • index (Integer) —

    The index. Negative indexes count from the end.

  • initial (BasicObject) —

    The value to store when the slot is absent or nil.

  • timeout (Numeric, nil) —

    The maximum wait in seconds. Nil waits indefinitely.

Yields:

  • (value) —

    Compute a replacement for an existing non-nil entry.

Yield Parameters:

  • value (BasicObject) —

    The current non-nil value.

Yield Returns:

  • (BasicObject) —

    The replacement value.

Returns:

  • (BasicObject, nil) —

    The stored value, or nil on timeout.

See Also:



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