curator 0.8.0 → 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/curator/memory/configuration.rb +1 -1
- data/lib/curator/memory/data_store.rb +17 -16
- data/lib/curator/mongo/configuration.rb +1 -1
- data/lib/curator/mongo/data_store.rb +21 -16
- data/lib/curator/repository.rb +10 -12
- data/lib/curator/resettable_riak/configuration.rb +3 -1
- data/lib/curator/resettable_riak/data_store.rb +8 -6
- data/lib/curator/riak/configuration.rb +1 -1
- data/lib/curator/riak/data_store.rb +20 -13
- data/lib/curator.rb +1 -8
- metadata +2 -2
@@ -3,14 +3,15 @@ require 'ostruct'
|
|
3
3
|
module Curator
|
4
4
|
module Memory
|
5
5
|
class DataStore
|
6
|
-
def
|
6
|
+
def remove_all_keys
|
7
7
|
@data = {}
|
8
8
|
end
|
9
|
-
|
10
|
-
|
9
|
+
|
10
|
+
def reset!
|
11
|
+
remove_all_keys
|
11
12
|
end
|
12
13
|
|
13
|
-
def
|
14
|
+
def save(options)
|
14
15
|
bucket = _bucket_name(options[:collection_name])
|
15
16
|
object = options[:value]
|
16
17
|
key = options[:key]
|
@@ -31,7 +32,7 @@ module Curator
|
|
31
32
|
key
|
32
33
|
end
|
33
34
|
|
34
|
-
def
|
35
|
+
def delete(collection_name, key)
|
35
36
|
bucket = _bucket_name(collection_name)
|
36
37
|
_records(bucket).delete(key)
|
37
38
|
_indices(bucket).each_key do |name|
|
@@ -44,17 +45,17 @@ module Curator
|
|
44
45
|
end
|
45
46
|
end
|
46
47
|
|
47
|
-
def
|
48
|
+
def find_by_key(collection_name, key)
|
48
49
|
bucket = _bucket_name(collection_name)
|
49
50
|
value = _records(bucket).fetch(key, nil)
|
50
51
|
return if value.nil?
|
51
52
|
{:key => key, :data => value}
|
52
53
|
end
|
53
54
|
|
54
|
-
def
|
55
|
+
def find_by_attribute(collection_name, attribute, query)
|
55
56
|
return [] if query.nil?
|
56
57
|
bucket = _bucket_name(collection_name)
|
57
|
-
index = _index(bucket,
|
58
|
+
index = _index(bucket, attribute)
|
58
59
|
keys = case query
|
59
60
|
when Range
|
60
61
|
keys = index.keys.select do |key|
|
@@ -69,27 +70,27 @@ module Curator
|
|
69
70
|
end
|
70
71
|
end
|
71
72
|
|
72
|
-
def
|
73
|
+
def _data
|
73
74
|
@data ||= {}
|
74
75
|
end
|
75
76
|
|
76
|
-
def
|
77
|
+
def _bucket(bucket)
|
77
78
|
_data[bucket] ||= {}
|
78
79
|
end
|
79
80
|
|
80
|
-
def
|
81
|
+
def _records(bucket)
|
81
82
|
_bucket(bucket)[:records] ||= {}
|
82
83
|
end
|
83
84
|
|
84
|
-
def
|
85
|
+
def _indices(bucket)
|
85
86
|
_bucket(bucket)[:indices] ||= {}
|
86
87
|
end
|
87
88
|
|
88
|
-
def
|
89
|
+
def _index(bucket, index_name)
|
89
90
|
_indices(bucket)[index_name] ||= {}
|
90
91
|
end
|
91
92
|
|
92
|
-
def
|
93
|
+
def _normalized_index_values(indexed_data)
|
93
94
|
if indexed_data.is_a?(Array)
|
94
95
|
indexed_data
|
95
96
|
else
|
@@ -97,13 +98,13 @@ module Curator
|
|
97
98
|
end
|
98
99
|
end
|
99
100
|
|
100
|
-
def
|
101
|
+
def _generate_key(bucket)
|
101
102
|
keys = _records(bucket).keys
|
102
103
|
keys = [0] if keys.empty?
|
103
104
|
keys.max.next
|
104
105
|
end
|
105
106
|
|
106
|
-
def
|
107
|
+
def _bucket_name(name)
|
107
108
|
"#{Curator.config.environment}:#{name}"
|
108
109
|
end
|
109
110
|
end
|
@@ -4,23 +4,28 @@ require 'yaml'
|
|
4
4
|
module Curator
|
5
5
|
module Mongo
|
6
6
|
class DataStore
|
7
|
-
def
|
7
|
+
def client
|
8
8
|
return @client if @client
|
9
9
|
config = YAML.load(File.read(Curator.config.mongo_config_file))[Curator.config.environment]
|
10
10
|
host = config.delete(:host)
|
11
11
|
port = config.delete(:port)
|
12
|
+
password = config.delete(:password)
|
13
|
+
username = config.delete(:username)
|
14
|
+
@database_name = config.delete(:database) || default_db_name
|
12
15
|
@client = ::Mongo::Connection.new(host, port, config)
|
16
|
+
@client.add_auth(@database_name, username, password) if username and password
|
17
|
+
@client
|
13
18
|
end
|
14
19
|
|
15
|
-
def
|
20
|
+
def remove_all_keys
|
16
21
|
self.reset!
|
17
22
|
end
|
18
23
|
|
19
|
-
def
|
24
|
+
def reset!
|
20
25
|
_db.collections.each {|coll| coll.drop unless coll.name =~ /system/ }
|
21
26
|
end
|
22
27
|
|
23
|
-
def
|
28
|
+
def save(options)
|
24
29
|
collection = _collection options[:collection_name]
|
25
30
|
key = options.delete(:key)
|
26
31
|
document = options[:value]
|
@@ -31,12 +36,12 @@ module Curator
|
|
31
36
|
collection.save document
|
32
37
|
end
|
33
38
|
|
34
|
-
def
|
39
|
+
def delete(collection_name, id)
|
35
40
|
collection = _collection(collection_name)
|
36
41
|
collection.remove(:_id => id)
|
37
42
|
end
|
38
43
|
|
39
|
-
def
|
44
|
+
def find_by_attribute(collection_name, field, query)
|
40
45
|
return [] if query.nil?
|
41
46
|
|
42
47
|
exp = {}
|
@@ -46,34 +51,34 @@ module Curator
|
|
46
51
|
documents.map {|doc| normalize_document(doc) }
|
47
52
|
end
|
48
53
|
|
49
|
-
def
|
54
|
+
def find_by_key(collection_name, id)
|
50
55
|
collection = _collection(collection_name)
|
51
56
|
document = collection.find_one({:_id => id})
|
52
57
|
normalize_document(document) unless document.nil?
|
53
58
|
end
|
54
59
|
|
55
|
-
def
|
60
|
+
def _collection(name)
|
56
61
|
_db.collection(name)
|
57
62
|
end
|
58
63
|
|
59
|
-
def
|
60
|
-
_db.collection(name).name
|
61
|
-
end
|
62
|
-
|
63
|
-
def self._db
|
64
|
+
def _db
|
64
65
|
client.db(_db_name)
|
65
66
|
end
|
66
67
|
|
67
|
-
def
|
68
|
+
def default_db_name
|
68
69
|
"#{Curator.config.database}:#{Curator.config.environment}"
|
69
70
|
end
|
70
71
|
|
71
|
-
def
|
72
|
+
def _db_name
|
73
|
+
@database_name
|
74
|
+
end
|
75
|
+
|
76
|
+
def normalize_document(doc)
|
72
77
|
key = doc.delete '_id'
|
73
78
|
Hash[:key => key, :data => doc]
|
74
79
|
end
|
75
80
|
|
76
|
-
def
|
81
|
+
def _normalize_query(query)
|
77
82
|
query.inject({}) do |hash, (key, value)|
|
78
83
|
case value
|
79
84
|
when Range
|
data/lib/curator/repository.rb
CHANGED
@@ -34,15 +34,15 @@ module Curator
|
|
34
34
|
end
|
35
35
|
|
36
36
|
def find_by_created_at(start_time, end_time)
|
37
|
-
|
37
|
+
_find_by_attribute(:created_at, _format_time_for_index(start_time).._format_time_for_index(end_time))
|
38
38
|
end
|
39
39
|
|
40
40
|
def find_by_updated_at(start_time, end_time)
|
41
|
-
|
41
|
+
_find_by_attribute(:updated_at, _format_time_for_index(start_time).._format_time_for_index(end_time))
|
42
42
|
end
|
43
43
|
|
44
44
|
def find_by_version(version)
|
45
|
-
|
45
|
+
_find_by_attribute(:version, version)
|
46
46
|
end
|
47
47
|
|
48
48
|
def find_by_id(id)
|
@@ -93,20 +93,20 @@ module Curator
|
|
93
93
|
object.instance_values
|
94
94
|
end
|
95
95
|
|
96
|
-
def _build_finder_methods(
|
96
|
+
def _build_finder_methods(attribute)
|
97
97
|
eigenclass = class << self; self; end
|
98
98
|
eigenclass.class_eval do
|
99
|
-
define_method("find_by_#{
|
100
|
-
|
99
|
+
define_method("find_by_#{attribute}") do |value|
|
100
|
+
_find_by_attribute(attribute, value)
|
101
101
|
end
|
102
|
-
define_method("find_first_by_#{
|
103
|
-
|
102
|
+
define_method("find_first_by_#{attribute}") do |value|
|
103
|
+
_find_by_attribute(attribute, value).first
|
104
104
|
end
|
105
105
|
end
|
106
106
|
end
|
107
107
|
|
108
|
-
def
|
109
|
-
if results = data_store.
|
108
|
+
def _find_by_attribute(attribute, value)
|
109
|
+
if results = data_store.find_by_attribute(collection_name, attribute, value)
|
110
110
|
results.map do |hash|
|
111
111
|
_deserialize(hash[:key], hash[:data])
|
112
112
|
end
|
@@ -121,8 +121,6 @@ module Curator
|
|
121
121
|
attributes = data.with_indifferent_access
|
122
122
|
migrated_attributes = migrator.migrate(attributes)
|
123
123
|
migrated_attributes[:id] = id
|
124
|
-
migrated_attributes[:created_at] = Time.parse(migrated_attributes[:created_at]) if migrated_attributes[:created_at]
|
125
|
-
migrated_attributes[:updated_at] = Time.parse(migrated_attributes[:updated_at]) if migrated_attributes[:updated_at]
|
126
124
|
deserialize(migrated_attributes)
|
127
125
|
end
|
128
126
|
|
@@ -1,7 +1,9 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../riak/configuration')
|
2
|
+
|
1
3
|
module Curator::ResettableRiak
|
2
4
|
class Configuration < Curator::Riak::Configuration
|
3
5
|
def data_store
|
4
|
-
Curator::ResettableRiak::DataStore
|
6
|
+
Curator::ResettableRiak::DataStore.new
|
5
7
|
end
|
6
8
|
end
|
7
9
|
end
|
@@ -1,22 +1,24 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../riak/data_store')
|
1
2
|
require 'active_support/core_ext/kernel/reporting'
|
2
3
|
|
3
4
|
module Curator
|
4
5
|
module ResettableRiak
|
5
6
|
class DataStore < Riak::DataStore
|
6
|
-
def
|
7
|
+
def bucket_prefix
|
7
8
|
job = "#{ENV['JOB_NAME'].gsub(/[^[:alnum:]]/, '_')}" if ENV['JOB_NAME'].present?
|
8
9
|
[Curator.config.bucket_prefix, job, Curator.config.environment].compact.join(':')
|
9
10
|
end
|
10
11
|
|
11
|
-
def
|
12
|
+
def exclude_from_reset(&block)
|
12
13
|
@exclude_from_reset = true
|
13
14
|
yield
|
15
|
+
ensure
|
14
16
|
@exclude_from_reset = false
|
15
17
|
end
|
16
18
|
|
17
|
-
def
|
19
|
+
def remove_all_keys
|
18
20
|
silence_warnings do
|
19
|
-
buckets = client.buckets.select { |bucket| bucket.name.start_with?(
|
21
|
+
buckets = client.buckets.select { |bucket| bucket.name.start_with?(bucket_prefix) }
|
20
22
|
buckets.each do |bucket|
|
21
23
|
bucket.keys do |keys|
|
22
24
|
keys.each { |key| bucket.delete(key) }
|
@@ -25,7 +27,7 @@ module Curator
|
|
25
27
|
end
|
26
28
|
end
|
27
29
|
|
28
|
-
def
|
30
|
+
def reset!
|
29
31
|
@bucket_names ||= {}
|
30
32
|
deletable_buckets = @bucket_names.each do |bucket_name, keys|
|
31
33
|
bucket = _bucket(bucket_name)
|
@@ -34,7 +36,7 @@ module Curator
|
|
34
36
|
@bucket_names = {}
|
35
37
|
end
|
36
38
|
|
37
|
-
def
|
39
|
+
def save(options)
|
38
40
|
key = super
|
39
41
|
|
40
42
|
unless @exclude_from_reset
|
@@ -4,23 +4,27 @@ require 'yaml'
|
|
4
4
|
module Curator
|
5
5
|
module Riak
|
6
6
|
class DataStore
|
7
|
-
def
|
7
|
+
def client
|
8
8
|
return @client if @client
|
9
9
|
yml_config = YAML.load(File.read(Curator.config.riak_config_file))[Curator.config.environment]
|
10
10
|
@client = ::Riak::Client.new(yml_config)
|
11
11
|
end
|
12
12
|
|
13
|
-
def
|
13
|
+
def bucket_prefix
|
14
|
+
"#{Curator.config.bucket_prefix}:#{Curator.config.environment}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def delete(bucket_name, key)
|
14
18
|
bucket = _bucket(bucket_name)
|
15
19
|
object = bucket.get(key)
|
16
20
|
object.delete
|
17
21
|
end
|
18
22
|
|
19
|
-
def
|
23
|
+
def ping
|
20
24
|
client.ping
|
21
25
|
end
|
22
26
|
|
23
|
-
def
|
27
|
+
def save(options)
|
24
28
|
bucket = _bucket(options[:collection_name])
|
25
29
|
object = ::Riak::RObject.new(bucket, options[:key])
|
26
30
|
object.content_type = options.fetch(:content_type, "application/json")
|
@@ -32,17 +36,17 @@ module Curator
|
|
32
36
|
result.key
|
33
37
|
end
|
34
38
|
|
35
|
-
def
|
39
|
+
def find_by_key(bucket_name, key)
|
36
40
|
bucket = _bucket(bucket_name)
|
37
41
|
begin
|
38
42
|
object = bucket.get(key)
|
39
|
-
{ :key => object.key, :data => object.data } unless object.data.empty?
|
43
|
+
{ :key => object.key, :data => _deserialize(object.data) } unless object.data.empty?
|
40
44
|
rescue ::Riak::HTTPFailedRequest => failed_request
|
41
45
|
raise failed_request unless failed_request.not_found?
|
42
46
|
end
|
43
47
|
end
|
44
48
|
|
45
|
-
def
|
49
|
+
def find_by_attribute(bucket_name, index_name, query)
|
46
50
|
return [] if query.nil?
|
47
51
|
|
48
52
|
bucket = _bucket(bucket_name)
|
@@ -54,23 +58,26 @@ module Curator
|
|
54
58
|
end
|
55
59
|
end
|
56
60
|
|
57
|
-
def
|
61
|
+
def _bucket(name)
|
58
62
|
client.bucket(_bucket_name(name))
|
59
63
|
end
|
60
64
|
|
61
|
-
def
|
65
|
+
def _bucket_name(name)
|
62
66
|
bucket_prefix + ":" + name
|
63
67
|
end
|
64
68
|
|
65
|
-
def
|
66
|
-
|
69
|
+
def _deserialize(data)
|
70
|
+
deserialized_data = data.dup
|
71
|
+
deserialized_data["created_at"] = Time.parse(data["created_at"]) if data["created_at"]
|
72
|
+
deserialized_data["updated_at"] = Time.parse(data["updated_at"]) if data["updated_at"]
|
73
|
+
deserialized_data
|
67
74
|
end
|
68
75
|
|
69
|
-
def
|
76
|
+
def _find_key_by_index(bucket, index_name, query)
|
70
77
|
bucket.get_index("#{index_name}_bin", query)
|
71
78
|
end
|
72
79
|
|
73
|
-
def
|
80
|
+
def _normalized_index_data(index_data)
|
74
81
|
if index_data.is_a?(Array)
|
75
82
|
index_data.join(", ")
|
76
83
|
else
|
data/lib/curator.rb
CHANGED
@@ -21,13 +21,6 @@ module Curator
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def self.data_store
|
24
|
-
config.data_store
|
25
|
-
end
|
26
|
-
|
27
|
-
self.configure(:riak) do |config|
|
28
|
-
config.environment = 'development'
|
29
|
-
config.migrations_path = File.expand_path(File.dirname(__FILE__) + "/../db/migrate")
|
30
|
-
config.bucket_prefix = 'curator'
|
31
|
-
config.riak_config_file = File.expand_path(File.dirname(__FILE__) + "/../config/riak.yml")
|
24
|
+
@data_store ||= config.data_store
|
32
25
|
end
|
33
26
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: curator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-06-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|