123456789_123456789_123456789_123456789_123456789_

Class: Concurrent::LazyRegister

Overview

Note:

**Edge Features** are under active development and may change frequently.

  • Deprecations are not added before incompatible changes.

  • Edge version: major is always 0, minor bump means incompatible change, patch bump means compatible change.

  • Edge features may also lack tests and documentation.

  • Features developed in concurrent-ruby-edge are expected to move to concurrent-ruby when finalised.

Hash-like collection that store lazy evaluated values.

Examples:

register = Concurrent::LazyRegister.new
#=> #<Concurrent::LazyRegister:0x007fd7ecd5e230 @Data=#<Concurrent::AtomicReference:0x007fd7ecd5e1e0>>
register[:key]
#=> nil
register.add(:key) { Concurrent::Actor.spawn!(Actor::AdHoc, :ping) { -> message { message } } }
#=> #<Concurrent::LazyRegister:0x007fd7ecd5e230 @Data=#<Concurrent::AtomicReference:0x007fd7ecd5e1e0>>
register[:key]
#=> #<Concurrent::Actor::Reference /ping (Concurrent::Actor::AdHoc)>

Class Attribute Summary

Class Method Summary

Synchronization::Object - Inherited

.atomic_attribute?, .atomic_attributes,
.attr_atomic

Creates methods for reading and writing to a instance variable with volatile (Java) semantic as .attr_volatile does.

.attr_volatile

Creates methods for reading and writing (as attr_accessor does) to a instance variable with volatile (Java) semantic.

.ensure_safe_initialization_when_final_fields_are_present

For testing purposes, quite slow.

.new

Has to be called by children.

.safe_initialization!, .define_initialize_atomic_fields

Synchronization::AbstractObject - Inherited

Instance Method Summary

Synchronization::Object - Inherited

Synchronization::Volatile - Included

Synchronization::AbstractObject - Inherited

Constructor Details

.newLazyRegister

[ GitHub ]

  
# File 'lib/concurrent-ruby-edge/concurrent/lazy_register.rb', line 25

def initialize
  super
  self.data = {}
end

Instance Method Details

#[](key) ⇒ Object

Element reference. Retrieves the value object corresponding to the key object. Returns nil if the key is not found. Raises an exception if the stored item raised an exception when the block was evaluated.

Parameters:

Returns:

  • (Object)

    value stored for the key or nil if the key is not found

Raises:

  • Exception when the initialization block fails

[ GitHub ]

  
# File 'lib/concurrent-ruby-edge/concurrent/lazy_register.rb', line 38

def [](key)
  delay = data[key]
  delay ? delay.value! : nil
end

#add(key, &block)

Alias for #register.

[ GitHub ]

  
# File 'lib/concurrent-ruby-edge/concurrent/lazy_register.rb', line 67

alias_method :add, :register

#delete(key)

Alias for #unregister.

[ GitHub ]

  
# File 'lib/concurrent-ruby-edge/concurrent/lazy_register.rb', line 81

alias_method :delete, :unregister

#has_key?(key)

Alias for #registered?.

[ GitHub ]

  
# File 'lib/concurrent-ruby-edge/concurrent/lazy_register.rb', line 52

alias_method :has_key?, :registered?

#key?(key)

Alias for #registered?.

[ GitHub ]

  
# File 'lib/concurrent-ruby-edge/concurrent/lazy_register.rb', line 51

alias_method :key?, :registered?

#register(key) { ... } ⇒ LazyRegister Also known as: #add, #store

Element assignment. Associates the value given by value with the key given by key.

Parameters:

Yields:

  • the object to store under the key

Returns:

  • (LazyRegister)

    self

[ GitHub ]

  
# File 'lib/concurrent-ruby-edge/concurrent/lazy_register.rb', line 61

def register(key, &block)
  delay = Delay.new(executor: :immediate, &block)
  update_data { |h| h.merge(key => delay) }
  self
end

#registered?(key) ⇒ true, false Also known as: #key?, #has_key?

Returns true if the given key is present.

Parameters:

Returns:

  • (true, false)

    if the key is registered

[ GitHub ]

  
# File 'lib/concurrent-ruby-edge/concurrent/lazy_register.rb', line 47

def registered?(key)
  data.key?(key)
end

#remove(key)

Alias for #unregister.

[ GitHub ]

  
# File 'lib/concurrent-ruby-edge/concurrent/lazy_register.rb', line 80

alias_method :remove, :unregister

#store(key, &block)

Alias for #register.

[ GitHub ]

  
# File 'lib/concurrent-ruby-edge/concurrent/lazy_register.rb', line 68

alias_method :store, :register

#unregister(key) ⇒ LazyRegister Also known as: #remove, #delete

Un-registers the object under key, realized or not.

Parameters:

Returns:

  • (LazyRegister)

    self

[ GitHub ]

  
# File 'lib/concurrent-ruby-edge/concurrent/lazy_register.rb', line 75

def unregister(key)
  update_data { |h| h.dup.tap { |j| j.delete(key) } }
  self
end