Class: Curator::Riak::DataStore

Inherits:
Object
  • Object
show all
Defined in:
lib/curator/riak/data_store.rb

Direct Known Subclasses

Curator::ResettableRiak::DataStore

Instance Method Summary collapse

Instance Method Details

#_bucket(name) ⇒ Object



76
77
78
# File 'lib/curator/riak/data_store.rb', line 76

def _bucket(name)
  client.bucket(_bucket_name(name))
end

#_bucket_name(name) ⇒ Object



80
81
82
# File 'lib/curator/riak/data_store.rb', line 80

def _bucket_name(name)
  bucket_prefix + ":" + name
end

#_deserialize(data) ⇒ Object



84
85
86
87
88
89
# File 'lib/curator/riak/data_store.rb', line 84

def _deserialize(data)
  deserialized_data = data.dup
  deserialized_data["created_at"] = Time.parse(data["created_at"]) if data["created_at"]
  deserialized_data["updated_at"] = Time.parse(data["updated_at"]) if data["updated_at"]
  deserialized_data
end

#_find_key_by_index(bucket, index_name, query) ⇒ Object



91
92
93
# File 'lib/curator/riak/data_store.rb', line 91

def _find_key_by_index(bucket, index_name, query)
  bucket.get_index("#{index_name}_bin", query)
end

#bucket_prefixObject



15
16
17
# File 'lib/curator/riak/data_store.rb', line 15

def bucket_prefix
  "#{Curator.config.bucket_prefix}:#{Curator.config.environment}"
end

#clientObject



7
8
9
10
11
12
13
# File 'lib/curator/riak/data_store.rb', line 7

def client
  return @client if @client
  return @client = Curator.config.client if Curator.config.client

  yml_config = YAML.load(File.read(Curator.config.riak_config_file))[Curator.config.environment]
  @client = ::Riak::Client.new(yml_config)
end

#delete(bucket_name, key) ⇒ Object



19
20
21
22
23
# File 'lib/curator/riak/data_store.rb', line 19

def delete(bucket_name, key)
  bucket = _bucket(bucket_name)
  object = bucket.get(key)
  object.delete
end

#find_all(bucket_name) ⇒ Object



50
51
52
53
# File 'lib/curator/riak/data_store.rb', line 50

def find_all(bucket_name)
  bucket = _bucket(bucket_name)
  bucket.keys.map { |key| find_by_key(bucket_name, key) }
end

#find_by_attribute(bucket_name, index_name, query) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/curator/riak/data_store.rb', line 65

def find_by_attribute(bucket_name, index_name, query)
  return [] if query.nil?
  bucket = _bucket(bucket_name)
  begin
    keys = _find_key_by_index(bucket, index_name.to_s, query)
    keys.map { |key| find_by_key(bucket_name, key) }
  rescue ::Riak::ProtobuffsFailedRequest => failed_request
    raise failed_request unless failed_request.not_found?
  end
end

#find_by_key(bucket_name, key) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/curator/riak/data_store.rb', line 55

def find_by_key(bucket_name, key)
  bucket = _bucket(bucket_name)
  begin
    object = bucket.get(key)
    { :key => object.key, :data => _deserialize(object.data) } unless object.data.empty?
  rescue ::Riak::ProtobuffsFailedRequest => failed_request
    raise failed_request unless failed_request.not_found?
  end
end

#pingObject



25
26
27
# File 'lib/curator/riak/data_store.rb', line 25

def ping
  client.ping
end

#save(options) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/curator/riak/data_store.rb', line 38

def save(options)
  bucket = _bucket(options[:collection_name])
  object = ::Riak::RObject.new(bucket, options[:key])
  object.content_type = options.fetch(:content_type, "application/json")
  object.data = options[:value]
  options.fetch(:index, {}).each do |index_name, index_data|
    object.indexes["#{index_name}_bin"].merge(Array(index_data))
  end
  result = object.store
  result.key
end

#settings(bucket_name) ⇒ Object



29
30
31
# File 'lib/curator/riak/data_store.rb', line 29

def settings(bucket_name)
  _bucket(bucket_name).props.dup.freeze
end

#update_settings!(bucket_name, updated_settings) ⇒ Object



33
34
35
36
# File 'lib/curator/riak/data_store.rb', line 33

def update_settings!(bucket_name, updated_settings)
  return true if updated_settings.empty?
  _bucket(bucket_name).props = updated_settings
end