123456789_123456789_123456789_123456789_123456789_

Class: Concurrent::Collection::NonConcurrentMapBackend

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Inherits: Object
Defined in: lib/concurrent-ruby/concurrent/collection/map/non_concurrent_map_backend.rb

Class Method Summary

Instance Method Summary

Constructor Details

.new(options = nil, &default_proc) ⇒ NonConcurrentMapBackend

WARNING: all public methods of the class must operate on the @backend directly without calling each other. This is important because of the SynchronizedMapBackend which uses a non-reentrant mutex for performance reasons.

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/map/non_concurrent_map_backend.rb', line 15

def initialize(options = nil, &default_proc)
  validate_options_hash!(options) if options.kind_of?(::Hash)
  set_backend(default_proc)
  @default_proc = default_proc
end

Instance Method Details

#[](key)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/map/non_concurrent_map_backend.rb', line 21

def [](key)
  @backend[key]
end

#[]=(key, value)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/map/non_concurrent_map_backend.rb', line 25

def []=(key, value)
  @backend[key] = value
end

#clear

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/map/non_concurrent_map_backend.rb', line 94

def clear
  @backend.clear
  self
end

#compute(key)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/map/non_concurrent_map_backend.rb', line 59

def compute(key)
  store_computed_value(key, yield(get_or_default(key, nil)))
end

#compute_if_absent(key)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/map/non_concurrent_map_backend.rb', line 29

def compute_if_absent(key)
  if NULL != (stored_value = @backend.fetch(key, NULL))
    stored_value
  else
    @backend[key] = yield
  end
end

#compute_if_present(key)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/map/non_concurrent_map_backend.rb', line 53

def compute_if_present(key)
  if NULL != (stored_value = @backend.fetch(key, NULL))
    store_computed_value(key, yield(stored_value))
  end
end

#delete(key)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/map/non_concurrent_map_backend.rb', line 81

def delete(key)
  @backend.delete(key)
end

#delete_pair(key, value)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/map/non_concurrent_map_backend.rb', line 85

def delete_pair(key, value)
  if pair?(key, value)
    @backend.delete(key)
    true
  else
    false
  end
end

#dupped_backend (private)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/map/non_concurrent_map_backend.rb', line 130

def dupped_backend
  @backend.dup
end

#each_pair

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/map/non_concurrent_map_backend.rb', line 99

def each_pair
  dupped_backend.each_pair do |k, v|
    yield k, v
  end
  self
end

#get_and_set(key, value)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/map/non_concurrent_map_backend.rb', line 71

def get_and_set(key, value)
  stored_value = get_or_default(key, nil)
  @backend[key] = value
  stored_value
end

#get_or_default(key, default_value)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/map/non_concurrent_map_backend.rb', line 110

def get_or_default(key, default_value)
  @backend.fetch(key, default_value)
end

#initialize_copy(other) (private)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/map/non_concurrent_map_backend.rb', line 124

def initialize_copy(other)
  super
  set_backend(@default_proc)
  self
end

#key?(key) ⇒ Boolean

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/map/non_concurrent_map_backend.rb', line 77

def key?(key)
  @backend.key?(key)
end

#merge_pair(key, value)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/map/non_concurrent_map_backend.rb', line 63

def merge_pair(key, value)
  if NULL == (stored_value = @backend.fetch(key, NULL))
    @backend[key] = value
  else
    store_computed_value(key, yield(stored_value))
  end
end

#pair?(key, expected_value) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/map/non_concurrent_map_backend.rb', line 134

def pair?(key, expected_value)
  NULL != (stored_value = @backend.fetch(key, NULL)) && expected_value.equal?(stored_value)
end

#replace_if_exists(key, new_value)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/map/non_concurrent_map_backend.rb', line 46

def replace_if_exists(key, new_value)
  if NULL != (stored_value = @backend.fetch(key, NULL))
    @backend[key] = new_value
    stored_value
  end
end

#replace_pair(key, old_value, new_value)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/map/non_concurrent_map_backend.rb', line 37

def replace_pair(key, old_value, new_value)
  if pair?(key, old_value)
    @backend[key] = new_value
    true
  else
    false
  end
end

#set_backend(default_proc) (private)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/map/non_concurrent_map_backend.rb', line 116

def set_backend(default_proc)
  if default_proc
    @backend = ::Hash.new { |_h, key| default_proc.call(self, key) }
  else
    @backend = {}
  end
end

#size

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/map/non_concurrent_map_backend.rb', line 106

def size
  @backend.size
end

#store_computed_value(key, new_value) (private)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/collection/map/non_concurrent_map_backend.rb', line 138

def store_computed_value(key, new_value)
  if new_value.nil?
    @backend.delete(key)
    nil
  else
    @backend[key] = new_value
  end
end