Class: Farce::Transaction::Atom

Inherits:
Abstract::Atom show all
Defined in:
lib/farce/transaction/atom.rb

Overview

An Atom that is part of a transaction.

Instance Method Summary collapse

Methods included from Unshareable

#initialize, #ractor_shareable?

Methods inherited from Abstract::Atom

#blank?, #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 Internal::ValueSerialization

#as_extended_json, #as_json, #to_bson, #to_bson_normalized_value, #to_cbor, #to_json, #to_msgpack

Methods included from Abstract::Value

#blank?, #unwrap

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

#compare_and_set(expected, replacement, mode: nil) ⇒ Boolean

Atomically replace the current value if it matches the expected value.

Parameters:

  • expected (BasicObject, nil) —

    the value to compare with the current value

  • new_value (BasicObject, nil) —

    the replacement value

  • timeout (Numeric, nil) —

    the maximum number of seconds to wait

Returns:

  • (Boolean) —

    whether the value was replaced

See Also:



61
62
63
64
65
66
67
68
# File 'lib/farce/transaction/atom.rb', line 61

def compare_and_set(expected, replacement, mode: nil)
  write do
    next compared(@working.compare_and_set(expected, replacement)) unless @manager
    next compared(false) unless matches?(@working.value, expected, identity: @object.compare_by_identity?)
    @working.store(wrap(replacement, mode:))
    true
  end
end

#compare_by_identity? ⇒ Boolean

Whether comparisons use object identity instead of equality.

Returns:

  • (Boolean)

See Also:



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

def compare_by_identity? = access { @object.compare_by_identity? }

#store(value, mode: nil) { ... } ⇒ BasicObject?

Store a new value, waiting for any update in progress.

Parameters:

  • new_value (BasicObject, nil) —

    the new value

  • timeout (Numeric, nil) —

    the maximum number of seconds to wait

Yields:

  • called when the timeout expires

Returns:

  • (BasicObject, nil) —

    the stored value or the fallback result

See Also:



28
29
30
# File 'lib/farce/transaction/atom.rb', line 28

def store(value, mode: nil)
  write { unwrap_stored(@working.store(wrap(value, mode:))) }
end

#store_if_absent(mode: nil) { ... } ⇒ BasicObject?

Compute and store a value if the current value is nil.

Parameters:

  • timeout (Numeric, nil) —

    the maximum number of seconds to wait

Yields:

  • computes the value to store when the current value is nil

Yield Returns:

  • (BasicObject, nil) —

    the value to store

Returns:

  • (BasicObject, nil) —

    the current or newly stored value, or nil when the timeout expires

See Also:



47
48
49
50
# File 'lib/farce/transaction/atom.rb', line 47

def store_if_absent(mode: nil)
  require_block!(block_given?)
  write { value.nil? ? store(yield, mode:) : value }
end

#swap(value, mode: nil) { ... } ⇒ BasicObject?

Replace the current value and return the previous value.

Parameters:

  • new_value (BasicObject, nil) —

    the new value

  • timeout (Numeric, nil) —

    the maximum number of seconds to wait

Yields:

  • called when the timeout expires

Returns:

  • (BasicObject, nil) —

    the previous value or the fallback result

See Also:



34
35
36
# File 'lib/farce/transaction/atom.rb', line 34

def swap(value, mode: nil)
  write { unwrap_stored(@working.swap(wrap(value, mode:))) }
end

#update(mode: nil) {|current| ... } ⇒ BasicObject?

Atomically replace the current value with the result of a block.

Parameters:

  • timeout (Numeric, nil) —

    the maximum number of seconds to wait

Yields:

  • receives the current value and computes its replacement

Yield Parameters:

  • current (BasicObject, nil) —

    the current value

Yield Returns:

  • (BasicObject, nil) —

    the replacement value

Returns:

  • (BasicObject, nil) —

    the replacement value, or nil when the timeout expires

See Also:



40
41
42
43
# File 'lib/farce/transaction/atom.rb', line 40

def update(mode: nil)
  require_block!(block_given?)
  write { unwrap_stored(@working.store(wrap(yield(unwrap_stored(@working.value)), mode:))) }
end

#upsert(initial, mode: nil) {|current| ... } ⇒ BasicObject?

Store an initial value if the current value is nil, otherwise replace it with the result of a block.

Parameters:

  • initial_value (BasicObject, nil) —

    the value to store when the current value is nil

  • timeout (Numeric, nil) —

    the maximum number of seconds to wait

Yields:

  • receives a non-nil current value and computes its replacement

Yield Parameters:

  • current (BasicObject) —

    the current value

Yield Returns:

  • (BasicObject, nil) —

    the replacement value

Returns:

  • (BasicObject, nil) —

    the replacement or initial value, or nil when the timeout expires

See Also:



54
55
56
57
# File 'lib/farce/transaction/atom.rb', line 54

def upsert(initial, mode: nil)
  require_block!(block_given?)
  write { store(value.nil? ? initial : yield(value), mode:) }
end

#value ⇒ BasicObject? Also known as: get

Return the current value without waiting for an update in progress.

Returns:

  • (BasicObject, nil) —

    the current value

See Also:



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

def value = access { unwrap_stored(@working.value) }

#value=(value) ⇒ BasicObject?

Store a new value.

Parameters:

  • new_value (BasicObject, nil) —

    the new value

Returns:

  • (BasicObject, nil) —

    the new value

See Also:



22
23
24
# File 'lib/farce/transaction/atom.rb', line 22

def value=(value)
  store(value)
end