class Curator::Memory::DataStore

Public Instance Methods

_bucket(bucket) click to toggle source
# File lib/curator/memory/data_store.rb, line 94
def _bucket(bucket)
  _data[bucket] ||= {}
end
_bucket_name(name) click to toggle source
# File lib/curator/memory/data_store.rb, line 124
def _bucket_name(name)
  "#{Curator.config.environment}:#{name}"
end
_data() click to toggle source
# File lib/curator/memory/data_store.rb, line 90
def _data
  @data ||= {}
end
_generate_key(bucket) click to toggle source
# File lib/curator/memory/data_store.rb, line 118
def _generate_key(bucket)
  keys = _records(bucket).keys
  keys = [0] if keys.empty?
  keys.max.next
end
_index(bucket, index_name) click to toggle source
# File lib/curator/memory/data_store.rb, line 106
def _index(bucket, index_name)
  _indices(bucket)[index_name] ||= {}
end
_indices(bucket) click to toggle source
# File lib/curator/memory/data_store.rb, line 102
def _indices(bucket)
  _bucket(bucket)[:indices] ||= {}
end
_normalized_index_values(indexed_data) click to toggle source
# File lib/curator/memory/data_store.rb, line 110
def _normalized_index_values(indexed_data)
  if indexed_data.is_a?(Array)
    indexed_data
  else
    [indexed_data]
  end
end
_records(bucket) click to toggle source
# File lib/curator/memory/data_store.rb, line 98
def _records(bucket)
  _bucket(bucket)[:records] ||= {}
end
delete(collection_name, key) click to toggle source
# File lib/curator/memory/data_store.rb, line 44
def delete(collection_name, key)
  bucket = _bucket_name(collection_name)
  _records(bucket).delete(key)
  _indices(bucket).each_key do |name|
    index = _index(bucket, name)
    index.each do |value, keys|
      next unless keys.include?(key)
      index[value].delete(key)
    end
    index.delete_if { |value, keys| keys.empty? }
  end
end
find_all(collection_name) click to toggle source
# File lib/curator/memory/data_store.rb, line 57
def find_all(collection_name)
  bucket = _bucket_name(collection_name)
  _records(bucket).inject([]) do |results, (key,value)|
    results << {:key => key, :data => value}
    results
  end
end
find_by_attribute(collection_name, attribute, query) click to toggle source
# File lib/curator/memory/data_store.rb, line 72
def find_by_attribute(collection_name, attribute, query)
  return [] if query.nil?
  bucket = _bucket_name(collection_name)
  index = _index(bucket, attribute)
  keys = case query
         when Range
           keys = index.keys.select do |key|
             key.between?(query.first, query.last)
           end
           index.values_at(*keys).flatten
         else
           index.fetch(query, [])
         end
  keys.map do |key|
    find_by_key(collection_name, key)
  end
end
find_by_key(collection_name, key) click to toggle source
# File lib/curator/memory/data_store.rb, line 65
def find_by_key(collection_name, key)
  bucket = _bucket_name(collection_name)
  value = _records(bucket).fetch(key, nil)
  return if value.nil?
  {:key => key, :data => value}
end
remove_all_keys() click to toggle source
# File lib/curator/memory/data_store.rb, line 15
def remove_all_keys
  @data = {}
end
reset!() click to toggle source
# File lib/curator/memory/data_store.rb, line 19
def reset!
  remove_all_keys
end
save(options) click to toggle source
# File lib/curator/memory/data_store.rb, line 23
def save(options)
  bucket = _bucket_name(options[:collection_name])
  object = options[:value]
  key = options[:key]
  indexes = options.fetch(:index, {})

  key = _generate_key(bucket) unless key

  _records(bucket).store(key, object)
  indexes.each do |index_name, index_data|
    index = _index(bucket, index_name)

    _normalized_index_values(index_data).each do |index_value|
      index[index_value] ||= []
      index[index_value] << key unless index[index_value].include?(key)
    end
  end

  key
end
settings(bucket_name) click to toggle source
# File lib/curator/memory/data_store.rb, line 7
def settings(bucket_name)
  {}
end
update_settings!(collection_name, updated_settings) click to toggle source
# File lib/curator/memory/data_store.rb, line 11
def update_settings!(collection_name, updated_settings)
  # NOOP
end