rails_kvs_driver 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 43aaa6cb1c1eda29e5fde51a68910efef0130eab
4
- data.tar.gz: f4b8ad4279aeb5bb9fcc4932a7f19fc0f86397ca
3
+ metadata.gz: 1e593c4955c29dcdfe11f5ba320aa3d5e3262251
4
+ data.tar.gz: bb9a9327fdef6c49cf64acb0565a4ed0163142f8
5
5
  SHA512:
6
- metadata.gz: e067da7e256b8ed8c2e10e150a544d982543c44fb13e42aecd6d9b40f7404ea6dcc830152f4f8aa1e641118fbffb5d01a86491e250623415c410495f227b2a77
7
- data.tar.gz: 7b4fa836600e0f9a8134c2cd7b2a6443038786ad87139bb8de6af8cd9a3784e4f6015af0552bf02b5e1b54cc05df602378732565fe329a16cc1f451fa5b236b0
6
+ metadata.gz: a01624d8410630bba00809f990423862107247a81c78a17e207c3d65d54e4cb738414dc9ac13341784cb524926c108ccfb4f6194c6ea0be0e7793ad0e4b4db74
7
+ data.tar.gz: a46652a0bf33b7014009b6dceac46a8e37a75b98594a93a0ab40824f751110c260edf49d239630a73d4fdd5f0b73c5579a3cf2f9a42f0f3b3486cae36a22df1d
@@ -0,0 +1,30 @@
1
+ require 'rails_kvs_driver/sorted_set'
2
+ module RailsKvsDriver
3
+ module DefinedBaseMethod
4
+ # initialize driver.
5
+ # @param kvs_instance [Object] instance of key-value store.
6
+ # @param driver_config [Hash] driver config.
7
+ def initialize(kvs_instance, driver_config)
8
+ @kvs_instance = kvs_instance
9
+ @driver_config = driver_config
10
+ end
11
+
12
+ # return initialized SortedSet class.
13
+ #
14
+ def sorted_sets
15
+ RailsKvsDriver::SortedSet.new(self)
16
+ end
17
+
18
+ # execute the block of code for each key having string, and value.
19
+ # @param &block [{|key, value| }] each the block of code for each key having string, and value.
20
+ def each
21
+ keys.each {|key| yield key, self[key] }
22
+ end
23
+
24
+ # get length of keys.
25
+ def length
26
+ keys.length
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,110 @@
1
+ require 'connection_pool'
2
+ require 'rails_kvs_driver/validation_driver_config'
3
+
4
+ module RailsKvsDriver
5
+ # @attr DriverConnectionPool [Hash] This Constants pools the connecting driver.
6
+ # {
7
+ # driver_class_name => [{:config => driver_config, :pool => ConnectionPool }, .... ]
8
+ #
9
+ # ...
10
+ #
11
+ # }
12
+ KVS_CONNECTION_POOL = Hash.new
13
+
14
+
15
+ module Session
16
+ include RailsKvsDriver::ValidationDriverConfig
17
+
18
+ # connect kvs and exec block.
19
+ # This function pools the connecting driver.
20
+ #
21
+ # @example
22
+ # config = {
23
+ # :host => 'localhost', # host of KVS.
24
+ # :port => 6379, # port of KVS.
25
+ # :namespace => 'Example', # namespace of avoid a conflict with key
26
+ # :timeout_sec => 5, # timeout seconds.
27
+ # :pool_size => 5, # connection pool size.
28
+ # :config_key => :none # this key is option.(defaults=:none)
29
+ # # when set this key.
30
+ # # will refer to a connection-pool based on config_key,
31
+ # # even if driver setting is the same without this key.
32
+ # }
33
+ # result = Driver.session(config) do |kvs|
34
+ # kvs['example'] = 'abc'
35
+ # kvs['example']
36
+ # end
37
+ #
38
+ # puts result # => 'abc'
39
+ #
40
+ # @param driver_config [Hash] driver_config.
41
+ # @param &block [{|driver_instance| #something... }] exec block.
42
+ # @return [Object] status
43
+ def session(driver_config, &block)
44
+ driver_config = validate_driver_config!(driver_config)
45
+ driver_connection_pool(self, driver_config).with do |kvs_instance|
46
+ block.call(self.new(kvs_instance, driver_config))
47
+ end
48
+ end
49
+
50
+
51
+ private
52
+ # get driver connection pool
53
+ # if doesn't exist pool, it's made newly.
54
+ #
55
+ # @param driver_class [RailsKvsDriver::Base] driver_class
56
+ # @param driver_config [Hash] driver_config
57
+ # @return [ConnectionPool] connection pool of driver
58
+ def driver_connection_pool(driver_class, driver_config)
59
+ pool = search_driver_connection_pool(driver_class, driver_config)
60
+ return (pool.nil?) ? set_driver_connection_pool(driver_class, driver_config) : pool
61
+ end
62
+
63
+ # set driver connection pool
64
+ #
65
+ # @param driver_class [RailsKvsDriver::Base] driver_class
66
+ # @param driver_config [Hash] driver_config
67
+ # @return [ConnectionPool] connection pool of driver
68
+ def set_driver_connection_pool(driver_class, driver_config)
69
+ conf = {
70
+ size: driver_config[:pool_size],
71
+ timeout: driver_config[:timeout_sec]
72
+ }
73
+ pool = ConnectionPool.new(conf) { driver_class.connect(driver_config) }
74
+
75
+ RailsKvsDriver::KVS_CONNECTION_POOL[driver_class.name] ||= Array.new
76
+ RailsKvsDriver::KVS_CONNECTION_POOL[driver_class.name].push({config: driver_config, pool: pool})
77
+
78
+ return pool
79
+ end
80
+
81
+ # search driver connection pool
82
+ #
83
+ # @param driver_class [RailsKvsDriver::Base] driver_class
84
+ # @param driver_config [Hash] driver_config
85
+ # @return [ConnectionPool] connection pool of driver, or nil
86
+ def search_driver_connection_pool(driver_class, driver_config)
87
+ if RailsKvsDriver::KVS_CONNECTION_POOL.has_key?(driver_class.name)
88
+ RailsKvsDriver::KVS_CONNECTION_POOL[driver_class.name].each do |pool_set|
89
+ return pool_set[:pool] if equal_driver_config?(pool_set[:config], driver_config)
90
+ end
91
+ end
92
+
93
+ return nil
94
+ end
95
+
96
+ # compare driver config.
97
+ #
98
+ # @param config1 [Hash] driver config
99
+ # @param config2 [Hash] driver config
100
+ def equal_driver_config?(config1, config2)
101
+ return false unless config1[:host] == config2[:host]
102
+ return false unless config1[:port] == config2[:port]
103
+ return false unless config1[:timeout_sec] == config2[:timeout_sec]
104
+ return false unless config1[:pool_size] == config2[:pool_size]
105
+ return false unless config1[:config_key] == config2[:config_key]
106
+ return true
107
+ end
108
+
109
+ end
110
+ end
@@ -0,0 +1,88 @@
1
+ module RailsKvsDriver
2
+ class SortedSet
3
+ attr_accessor :driver_instance
4
+
5
+ # initialize sorted_set
6
+ #
7
+ # @param driver_instance [RailsKvsDriver::Base]
8
+ def initialize(driver_instance)
9
+ @driver_instance = driver_instance
10
+ end
11
+
12
+ # get sorted_set
13
+ # when the key doesn't exist, it's made newly.
14
+ #
15
+ # @param key [String] sorted_set key.
16
+ # @param member [String] member of sorted_set.(default=nil)
17
+ # @return [Array<Array> or Integer] member_set[member, score]. when set member, return score of member.
18
+ def [](key, member=nil)
19
+ if member.nil?
20
+ @driver_instance.get_sorted_set(key)
21
+ else
22
+ @driver_instance.get_sorted_set_score(key, member)
23
+ end
24
+ end
25
+
26
+ # add sorted_set
27
+ # when the key doesn't exist, it's made newly.
28
+ #
29
+ # @param key [String] sorted_set key
30
+ # @param member_set [Array<member,score>] array of member<String> and score<Float>
31
+ # @return [Array] member_set
32
+ def []=(key, member_set)
33
+ @driver_instance.add_sorted_set(key, member_set[0], member_set[1])
34
+ return member_set
35
+ end
36
+
37
+ # delete key
38
+ def delete(key)
39
+ @driver_instance.delete(key)
40
+ end
41
+
42
+ # execute the block of code for each sorted_set.
43
+ # @param &block [{|key| }] each the block of code for each key of sorted set.
44
+ def each
45
+ keys.each {|key| yield key }
46
+ end
47
+
48
+ # execute the block of code for each member of sorted set.
49
+ #
50
+ # @param key [String] key of sorted set.
51
+ # @param reverse [Boolean] order by desc.
52
+ # @param limit [Integer] limit number to acquire at a time.
53
+ # @param &block [{|member, score, position| }] each the block of code for each member of sorted set.
54
+ def each_member(key, reverse=false, limit=1000)
55
+ member_count = @driver_instance.count_sorted_set_member(key)
56
+ member_position = 0
57
+
58
+ while member_position < member_count
59
+ @driver_instance.get_sorted_set(key, member_position, member_position + (limit-1), reverse).each do |data|
60
+ yield data[0], data[1], member_position
61
+ member_position += 1
62
+ end
63
+ end
64
+ end
65
+
66
+ # increment member's score of sorted set.
67
+ #
68
+ # @param key [String] key of sorted set.
69
+ # @param member [String] member of sorted set.
70
+ # @param score [Float] increment score
71
+ # @return [Float] score after increment.
72
+ def increment(key, member, score)
73
+ @driver_instance.increment_sorted_set(key, member, score)
74
+ end
75
+
76
+ # get all keys from kvs.
77
+ # @return [Array<String>] array of key names.
78
+ # @abstract get all keys from kvs.(only having string value)
79
+ def keys
80
+ @driver_instance.get_sorted_set_keys
81
+ end
82
+
83
+ # get length of sorted_set.
84
+ def length
85
+ keys.length
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,27 @@
1
+ module RailsKvsDriver
2
+ module ValidationDriverConfig
3
+ # Validate driver_config.
4
+ # This method raise ArgumentError, if missing driver_config.
5
+ #
6
+ # @param driver_config [Hash] driver config.
7
+ # @return [Hash] driver_config
8
+ def validate_driver_config!(driver_config)
9
+ raise_argument_error!(:host) unless driver_config.has_key? :host
10
+ raise_argument_error!(:port) unless driver_config.has_key? :port
11
+ raise_argument_error!(:namespace) unless driver_config.has_key? :namespace
12
+ raise_argument_error!(:timeout_sec) unless driver_config.has_key? :timeout_sec
13
+ raise_argument_error!(:pool_size) unless driver_config.has_key? :pool_size
14
+
15
+ driver_config[:config_key] = :none unless driver_config.has_key? :config_key
16
+
17
+ return driver_config
18
+ end
19
+
20
+ # raise argument error.
21
+ #
22
+ # @param key [String] not exists key.
23
+ def raise_argument_error!(key)
24
+ raise ArgumentError, "driver_config does not include #{key}"
25
+ end
26
+ end
27
+ end
@@ -1,3 +1,3 @@
1
1
  module RailsKvsDriver
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -1,47 +1,40 @@
1
1
  require 'rails_kvs_driver/version'
2
- require 'connection_pool'
2
+ require 'rails_kvs_driver/session'
3
+ require 'rails_kvs_driver/defined_base_method'
3
4
 
4
5
  module RailsKvsDriver
5
6
  # This abstract class wrap accessing to Key-Value Store.
6
7
  # @abstract
7
8
  class Base
8
-
9
- # @attr DriverConnectionPool [Hash] This Constants pools the connecting driver.
10
- # {
11
- # driver_class_name => [{:config => driver_config, :pool => ConnectionPool }, .... ]
12
- #
13
- # ...
14
- #
15
- # }
16
- DriverConnectionPool = Hash.new
9
+ extend RailsKvsDriver::Session
10
+ include RailsKvsDriver::DefinedBaseMethod
17
11
 
18
12
  # @attr @kvs_instance [Object] instance of KVS.
19
13
  attr_accessor :kvs_instance
20
14
 
21
15
  # @attr @driver_config [Hash] config of driver.
22
- # {
23
- # :host => 'localhost' # host of KVS.
24
- # :port => 6379 # port of KVS.
25
- # :namespace => 'Example' # namespace of avoid a conflict with key
26
- # :timeout_sec => 5 # timeout seconds.
27
- # :pool_size => 5 # connection pool size.
28
- # }
16
+ # {
17
+ # :host => 'localhost', # host of KVS.
18
+ # :port => 6379, # port of KVS.
19
+ # :namespace => 'Example', # namespace of avoid a conflict with key
20
+ # :timeout_sec => 5, # timeout seconds.
21
+ # :pool_size => 5, # connection pool size.
22
+ # :config_key => :none, # this key is option.(defaults=:none)
23
+ # # when set this key.
24
+ # # will refer to a connection-pool based on config_key,
25
+ # # even if driver setting is the same without this key.
26
+ # }
29
27
  attr_accessor :driver_config
30
28
 
31
- # initialize driver, and connect kvs.
32
- # @param driver_config [Hash] driver config.
33
- def initialize(driver_config)
34
- @driver_config = validate_driver_config!(driver_config)
35
- connect
36
- end
37
-
38
29
  # connect with driver config.
39
- # @return [Boolean] result
40
- def connect
30
+ # @param driver_config [Hash] driver config.
31
+ # @return [Object] instance of key-value store.
32
+ # @abstract connect with driver config. and return instance of kvs.
33
+ def self.connect(driver_config)
41
34
  raise NoMethodError
42
35
  end
43
36
 
44
- # get value from kvs.
37
+ # get string value from kvs.
45
38
  # @param key [String] key.
46
39
  # @return [String] value
47
40
  # @abstract get value from kvs.
@@ -49,7 +42,7 @@ module RailsKvsDriver
49
42
  raise NoMethodError
50
43
  end
51
44
 
52
- # set value to kvs.
45
+ # set string value to kvs.
53
46
  # @param key [String] key.
54
47
  # @param value [String] value.
55
48
  # @return [Boolean] result
@@ -60,8 +53,8 @@ module RailsKvsDriver
60
53
 
61
54
  # get all keys from kvs.
62
55
  # @return [Array<String>] array of key names.
63
- # @abstract get all keys from kvs.
64
- def all_keys
56
+ # @abstract get all keys from kvs.(only having string value)
57
+ def keys
65
58
  raise NoMethodError
66
59
  end
67
60
 
@@ -92,27 +85,32 @@ module RailsKvsDriver
92
85
  raise NoMethodError
93
86
  end
94
87
 
95
- # remove sorted set from kvs.
96
- # This function doesn't delete a key.
88
+ # count members of sorted set
97
89
  # @note same as sorted set of redis. refer to redis.
98
90
  #
99
- # @param key [String] key of sorted set.
100
- # @param member [String] member of sorted set.
101
- # @return [Boolean] result.
102
- # @abstract remove sorted set from kvs.
103
- def remove_sorted_set(key, member)
91
+ # @param key [String] key of sorted set.
92
+ # @return [Integer] members num
93
+ def count_sorted_set_member(key)
104
94
  raise NoMethodError
105
95
  end
106
96
 
107
- # increment score of member from sorted set.
97
+ # get array of sorted set.
108
98
  # @note same as sorted set of redis. refer to redis.
109
99
  #
110
- # @param key [String] key of sorted set.
111
- # @param member [String] member of sorted set.
112
- # @param score [Float] increment score.
113
- # @return [Float] value after increment
114
- # @abstract increment score of member from sorted set.
115
- def increment_sorted_set(key, member, score)
100
+ # @param key [String] key of sorted set.
101
+ # @param start [Integer] start index
102
+ # @param stop [Integer] stop index
103
+ # @param reverse [Boolean] order by desc.
104
+ # @return [Array<String, Float>>] array of the member and score.
105
+ # @abstract get array of sorted set.
106
+ def get_sorted_set(key, start=0, stop=-1, reverse=false)
107
+ raise NoMethodError
108
+ end
109
+
110
+ # get all sorted_set keys.
111
+ # @return [Array<String>] array of key names.
112
+ # @abstract get all keys from kvs.(only having sorted_set)
113
+ def get_sorted_set_keys
116
114
  raise NoMethodError
117
115
  end
118
116
 
@@ -123,140 +121,33 @@ module RailsKvsDriver
123
121
  # @param member [String] member of sorted set.
124
122
  # @return [Float] score of member.
125
123
  # @abstract get the score of member.
126
- def sorted_set_score(key, member)
124
+ def get_sorted_set_score(key, member)
127
125
  raise NoMethodError
128
126
  end
129
127
 
130
- # get array of sorted set.
128
+ # increment score of member from sorted set.
131
129
  # @note same as sorted set of redis. refer to redis.
132
130
  #
133
- # @param key [String] key of sorted set.
134
- # @param start [Integer] start index
135
- # @param stop [Integer] stop index
136
- # @param reverse [Boolean] order by desc.
137
- # @return [Array<String, Float>>] array of the member and score.
138
- # @abstract get array of sorted set.
139
- def sorted_set(key, start=0, stop=-1, reverse=false)
131
+ # @param key [String] key of sorted set.
132
+ # @param member [String] member of sorted set.
133
+ # @param score [Float] increment score.
134
+ # @return [Float] value after increment
135
+ # @abstract increment score of member from sorted set.
136
+ def increment_sorted_set(key, member, score)
140
137
  raise NoMethodError
141
138
  end
142
139
 
143
- # count members of sorted set
140
+ # remove sorted set from kvs.
141
+ # This function doesn't delete a key.
144
142
  # @note same as sorted set of redis. refer to redis.
145
143
  #
146
- # @param key [String] key of sorted set.
147
- # @return [Integer] members num
148
- def count_sorted_set_member(key)
144
+ # @param key [String] key of sorted set.
145
+ # @param member [String] member of sorted set.
146
+ # @return [Boolean] result.
147
+ # @abstract remove sorted set from kvs.
148
+ def remove_sorted_set(key, member)
149
149
  raise NoMethodError
150
150
  end
151
151
 
152
- # execute the block of code for each member of sorted set.
153
- # @param key [String] key of sorted set.
154
- # @param reverse [Boolean] order by desc.
155
- # @param limit [Integer] limit number to acquire at a time.
156
- # @param &block [{|member, score, position| }] each the block of code for each member of sorted set.
157
- def each_sorted_set(key, reverse=false, limit=1000)
158
- member_count = count_sorted_set_member(key)
159
- member_position = 0
160
-
161
- while member_position < member_count
162
- sorted_set(key, member_position, member_position + (limit-1), reverse).each do |data|
163
- yield data[0], data[1], member_position
164
- member_position += 1
165
- end
166
- end
167
- end
168
-
169
- # connect kvs and exec block.
170
- # This function pools the connecting driver.
171
- #
172
- # @example
173
- # config = {
174
- # :host => 'localhost' # host of KVS.
175
- # :port => 6379 # port of KVS.
176
- # :namespace => 'Example' # namespace of avoid a conflict with key
177
- # :timeout_sec => 5 # timeout seconds.
178
- # :pool_size => 5 # connection pool size.
179
- # }
180
- # result = Driver.session(config) do |kvs|
181
- # kvs['example'] = 'abc'
182
- # kvs['example']
183
- # end
184
- #
185
- # puts result # => 'abc'
186
- #
187
- # @param driver_config [Hash] driver_config.
188
- # @param &block [{|driver_instance| #something... }] exec block.
189
- # @return [Object] status
190
- def self.session(driver_config, &block)
191
- driver_connection_pool(self, driver_config).with &block
192
- end
193
-
194
-
195
- private
196
- # get driver connection pool
197
- # if doesn't exist pool, it's made newly.
198
- #
199
- # @param driver_class [RailsKvsDriver::Base] driver_class
200
- # @param driver_config [Hash] driver_config
201
- # @return [ConnectionPool] connection pool of driver
202
- def self.driver_connection_pool(driver_class, driver_config)
203
- pool = search_driver_connection_pool(driver_class, driver_config)
204
- return (pool.nil?) ? set_driver_connection_pool(driver_class, driver_config) : pool
205
- end
206
-
207
- # set driver connection pool
208
- #
209
- # @param driver_class [RailsKvsDriver::Base] driver_class
210
- # @param driver_config [Hash] driver_config
211
- # @return [ConnectionPool] connection pool of driver
212
- def self.set_driver_connection_pool(driver_class, driver_config)
213
- conf = {
214
- size: driver_config[:pool_size],
215
- timeout: driver_config[:timeout_sec]
216
- }
217
- pool = ConnectionPool.new(conf) { driver_class.new(driver_config) }
218
-
219
- DriverConnectionPool[driver_class.name] = Array.new unless DriverConnectionPool.has_key?(driver_class.name)
220
- DriverConnectionPool[driver_class.name].push({config: driver_config, pool: pool})
221
-
222
- return pool
223
- end
224
-
225
- # search driver connection pool
226
- #
227
- # @param driver_class [RailsKvsDriver::Base] driver_class
228
- # @param driver_config [Hash] driver_config
229
- # @return [ConnectionPool] connection pool of driver, or nil
230
- def self.search_driver_connection_pool(driver_class, driver_config)
231
- DriverConnectionPool[driver_class.name].each do |pool_set|
232
- if pool_set[:config] == driver_config
233
- return pool_set[:pool]
234
- end
235
- end if DriverConnectionPool.has_key?(driver_class.name)
236
-
237
- return nil
238
- end
239
-
240
- # Validate driver_config.
241
- # This method raise ArgumentError, if missing driver_config.
242
- #
243
- # @param driver_config [Hash] driver config.
244
- # @return [Hash] driver_config
245
- def validate_driver_config!(driver_config)
246
- raise_argument_error!(:host) unless driver_config.has_key? :host
247
- raise_argument_error!(:port) unless driver_config.has_key? :port
248
- raise_argument_error!(:namespace) unless driver_config.has_key? :namespace
249
- raise_argument_error!(:timeout_sec) unless driver_config.has_key? :timeout_sec
250
- raise_argument_error!(:pool_size) unless driver_config.has_key? :pool_size
251
-
252
- return driver_config
253
- end
254
-
255
- # raise argument error.
256
- #
257
- # @param key [String] not exists key.
258
- def raise_argument_error!(key)
259
- raise "driver_config does not include #{key}", ArgumentError
260
- end
261
152
  end
262
153
  end
@@ -0,0 +1,44 @@
1
+ require 'rspec'
2
+ require 'spec_helper'
3
+
4
+ describe RailsKvsDriver::DefinedBaseMethod do
5
+
6
+ context 'call initialize' do
7
+ it 'coping instance of driver_config' do
8
+ expect(MockDriver.new('kvs_dummy',nil).kvs_instance).to eq('kvs_dummy')
9
+ end
10
+
11
+ it 'coping instance of driver_config' do
12
+ expect(MockDriver.new(nil,@driver_config).driver_config).to eq(@driver_config)
13
+ end
14
+ end
15
+
16
+ context 'call sorted_sets' do
17
+ it 'return instance of RailsKvsDriver::SortedSet' do
18
+ expect(MockDriver.new(nil,nil).sorted_sets.instance_of?(RailsKvsDriver::SortedSet)).to be_true
19
+ end
20
+ end
21
+
22
+ context 'after iniaialized' do
23
+ before(:each) do
24
+ @instance = MockDriver.new(nil, nil)
25
+ @instance.stub({
26
+ :keys => [:a,:b,:c],
27
+ :[] => 'nyaruko'
28
+ })
29
+ end
30
+
31
+ it 'call each' do
32
+ count = 0
33
+ expect{
34
+ @instance.each do |key, value|
35
+ count += 1
36
+ end
37
+ }.to change{count}.by(3)
38
+ end
39
+
40
+ it 'call length' do
41
+ expect(@instance.length).to eq(3)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,35 @@
1
+ require 'rspec'
2
+ require 'spec_helper'
3
+
4
+ describe RailsKvsDriver::Session do
5
+ it 'defined KVS_CONNECTION_POOL' do
6
+ expect(RailsKvsDriver.const_defined?(:KVS_CONNECTION_POOL)).to be_true
7
+ end
8
+
9
+ it 'The structure of RailsKvsDriver::KVS_CONNECTION_POOL is correct.' do
10
+ MockDriver.session(@driver_config) {}
11
+ MockDriver2.session(@driver_config) {}
12
+ MockDriver2.session(@driver_config.merge(a:0)) {}
13
+ MockDriver2.session(@driver_config.merge(port:100)) {}
14
+
15
+ expect(RailsKvsDriver::KVS_CONNECTION_POOL.length).to eq(2)
16
+ expect(RailsKvsDriver::KVS_CONNECTION_POOL['MockDriver2'].length).to eq(2)
17
+ expect(RailsKvsDriver::KVS_CONNECTION_POOL['MockDriver2'][0].length).to eq(2)
18
+ expect(RailsKvsDriver::KVS_CONNECTION_POOL['MockDriver2'][0].has_key? :config).to be_true
19
+ expect(RailsKvsDriver::KVS_CONNECTION_POOL['MockDriver2'][0].has_key? :pool).to be_true
20
+ end
21
+
22
+ context 'call session' do
23
+ it 'to hand over driver instance to the block' do
24
+ MockDriver.session(@driver_config) do |instance|
25
+ expect(instance.class).to eq(MockDriver)
26
+ expect(instance.kvs_instance).to eq('dummy1')
27
+ end
28
+
29
+ MockDriver2.session(@driver_config) do |instance|
30
+ expect(instance.class).to eq(MockDriver2)
31
+ expect(instance.kvs_instance).to eq('dummy2')
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,62 @@
1
+ require 'rspec'
2
+ require 'spec_helper'
3
+
4
+ describe RailsKvsDriver::SortedSet do
5
+
6
+ before(:each) do
7
+ @driver = MockDriver.new(nil,nil)
8
+ @driver.stub({
9
+ add_sorted_set: nil,
10
+ count_sorted_set_member: 100,
11
+ delete: true,
12
+ get_sorted_set: [[:a,:b],[:c,:d]],
13
+ get_sorted_set_keys: [:a,:b,:c],
14
+ get_sorted_set_score: 100,
15
+ increment_sorted_set: 10
16
+ })
17
+ end
18
+
19
+ it 'call []' do
20
+ expect((@driver.sorted_sets['hoge'] = ['test', 1])[0]).to eq('test')
21
+ end
22
+
23
+ it 'call []=' do
24
+ expect((@driver.sorted_sets['hoge'])[0][0]).to eq(:a)
25
+ expect(@driver.sorted_sets['hoge','fuga']).to eq(100)
26
+ end
27
+
28
+ it 'call delete' do
29
+ expect(@driver.sorted_sets.delete(:a)).to be_true
30
+ end
31
+
32
+ it 'call each' do
33
+ count = 0
34
+ expect{
35
+ @driver.sorted_sets.each do |key|
36
+ count += 1
37
+ end
38
+ }.to change{count}.by(3)
39
+ end
40
+
41
+ it 'call each_member' do
42
+ count = 0
43
+ expect{
44
+ @driver.sorted_sets.each_member('test',false, 10) do |member, score, position|
45
+ expect(position).to eq(count)
46
+ count += 1
47
+ end
48
+ }.to change{count}.by(100)
49
+ end
50
+
51
+ it 'call increment_member' do
52
+ expect( @driver.sorted_sets.increment(:a, :b, 1) ).to eq(10)
53
+ end
54
+
55
+ it 'call keys' do
56
+ expect(@driver.sorted_sets.keys.length).to eq(3)
57
+ end
58
+
59
+ it 'call length' do
60
+ expect(@driver.sorted_sets.length).to eq(3)
61
+ end
62
+ end
@@ -0,0 +1,45 @@
1
+ require 'rspec'
2
+ require 'spec_helper'
3
+
4
+ describe RailsKvsDriver::ValidationDriverConfig do
5
+ class MockValidate
6
+ extend RailsKvsDriver::ValidationDriverConfig
7
+ end
8
+
9
+ context 'call validate_driver_config' do
10
+ it 'raise ArgumentError when there is not host.' do
11
+ @driver_config.delete(:host)
12
+ expect{ MockValidate.validate_driver_config!(@driver_config) }.to raise_error(ArgumentError)
13
+ end
14
+
15
+ it 'raise ArgumentError when there is not port.' do
16
+ @driver_config.delete(:port)
17
+ expect{ MockValidate.validate_driver_config!(@driver_config) }.to raise_error(ArgumentError)
18
+ end
19
+
20
+ it 'raise ArgumentError when there is not namespace.' do
21
+ @driver_config.delete(:namespace)
22
+ expect{ MockValidate.validate_driver_config!(@driver_config) }.to raise_error(ArgumentError)
23
+ end
24
+
25
+ it 'raise ArgumentError when there is not timeout_sec.' do
26
+ @driver_config.delete(:timeout_sec)
27
+ expect{ MockValidate.validate_driver_config!(@driver_config) }.to raise_error(ArgumentError)
28
+ end
29
+
30
+ it 'raise ArgumentError when there is not pool_size.' do
31
+ @driver_config.delete(:pool_size)
32
+ expect{ MockValidate.validate_driver_config!(@driver_config) }.to raise_error(ArgumentError)
33
+ end
34
+
35
+ it 'insert default value when there is not config_key' do
36
+ @driver_config[:config_key] = :Nyarukosan
37
+ validated_config = MockValidate.validate_driver_config!(@driver_config)
38
+ expect(validated_config[:config_key]).to eq(:Nyarukosan)
39
+
40
+ @driver_config.delete(:config_key)
41
+ validated_config = MockValidate.validate_driver_config!(@driver_config)
42
+ expect(validated_config[:config_key]).to eq(:none)
43
+ end
44
+ end
45
+ end
@@ -1,183 +1,69 @@
1
1
  require 'rspec'
2
2
  require 'spec_helper'
3
- require 'rails_kvs_driver'
4
-
5
- describe RailsKvsDriver do
6
-
7
- before(:each) do
8
- @driver_config = {
9
- :host => 'localhost', # host of KVS.
10
- :port => 6379, # port of KVS.
11
- :namespace => 'Example', # namespace of avoid a conflict with key
12
- :timeout_sec => 5, # timeout seconds.
13
- :pool_size => 5, # connection pool size.
14
- }
15
-
16
- class MockDriver < RailsKvsDriver::Base
17
- def connect
18
- end
19
- end
20
-
21
- end
22
3
 
4
+ describe RailsKvsDriver::Base do
23
5
  it 'raise NoMethodError when does not override connect' do
24
- expect{RailsKvsDriver::Base.new}.to raise_error
6
+ expect(RailsKvsDriver::Base.public_class_method('connect')).to be_true
7
+ expect{RailsKvsDriver::Base.connect('dummy')}.to raise_error(NoMethodError)
25
8
  end
26
9
 
27
10
  it 'raise NoMethodError when does not override []' do
28
11
  expect(MockDriver.method_defined?('[]')).to be_true
29
- expect{MockDriver.new['a']}.to raise_error
12
+ expect{MockDriver.new(nil,nil)[nil]}.to raise_error(NoMethodError)
30
13
  end
31
14
 
32
15
  it 'raise NoMethodError when does not override []=' do
33
16
  expect(MockDriver.method_defined?('[]=')).to be_true
34
- expect{MockDriver.new['a']='b'}.to raise_error
17
+ expect{MockDriver.new(nil,nil)[nil]=nil}.to raise_error(NoMethodError)
35
18
  end
36
19
 
37
- it 'raise NoMethodError when does not override all_keys' do
38
- expect(MockDriver.method_defined?(:all_keys)).to be_true
39
- expect{MockDriver.new.all_keys}.to raise_error
20
+ it 'raise NoMethodError when does not override keys' do
21
+ expect(MockDriver.method_defined?(:keys)).to be_true
22
+ expect{MockDriver.new(nil,nil).keys}.to raise_error(NoMethodError)
40
23
  end
41
24
 
42
25
  it 'raise NoMethodError when does not override delete' do
43
26
  expect(MockDriver.method_defined?(:delete)).to be_true
44
- expect{MockDriver.new.delete}.to raise_error
27
+ expect{MockDriver.new(nil,nil).delete(nil)}.to raise_error(NoMethodError)
45
28
  end
46
29
 
47
30
  it 'raise NoMethodError when does not override delete_all' do
48
31
  expect(MockDriver.method_defined?(:delete_all)).to be_true
49
- expect{MockDriver.new.delete_all}.to raise_error
32
+ expect{MockDriver.new(nil,nil).delete_all}.to raise_error(NoMethodError)
50
33
  end
51
34
 
52
35
  it 'raise NoMethodError when does not override add_sorted_set' do
53
36
  expect(MockDriver.method_defined?(:add_sorted_set)).to be_true
54
- expect{MockDriver.new.add_sorted_set}.to raise_error
37
+ expect{MockDriver.new(nil,nil).add_sorted_set(nil,nil,nil)}.to raise_error(NoMethodError)
55
38
  end
56
39
 
57
- it 'raise NoMethodError when does not override remove_sorted_set' do
58
- expect(MockDriver.method_defined?(:remove_sorted_set)).to be_true
59
- expect{MockDriver.new.remove_sorted_set}.to raise_error
40
+ it 'raise NoMethodError when does not override get_sorted_set' do
41
+ expect(MockDriver.method_defined?(:get_sorted_set)).to be_true
42
+ expect{MockDriver.new(nil,nil).get_sorted_set(nil)}.to raise_error(NoMethodError)
60
43
  end
61
44
 
62
- it 'raise NoMethodError when does not override increment_sorted_set' do
63
- expect(MockDriver.method_defined?(:increment_sorted_set)).to be_true
64
- expect{MockDriver.new.increment_sorted_set}.to raise_error
65
- end
66
-
67
- it 'raise NoMethodError when does not override sorted_set_score' do
68
- expect(MockDriver.method_defined?(:sorted_set_score)).to be_true
69
- expect{MockDriver.new.sorted_set_score}.to raise_error
70
- end
71
-
72
- it 'raise NoMethodError when does not override sorted_set' do
73
- expect(MockDriver.method_defined?(:sorted_set)).to be_true
74
- expect{MockDriver.new.sorted_set}.to raise_error
45
+ it 'raise NoMethodError when does not override get_sorted_set_keys' do
46
+ expect(MockDriver.method_defined?(:get_sorted_set_keys)).to be_true
47
+ expect{MockDriver.new(nil,nil).get_sorted_set_keys}.to raise_error(NoMethodError)
75
48
  end
76
49
 
77
50
  it 'raise NoMethodError when does not override count_sorted_set_member' do
78
51
  expect(MockDriver.method_defined?(:count_sorted_set_member)).to be_true
79
- expect{MockDriver.new.count_sorted_set_member}.to raise_error
52
+ expect{MockDriver.new(nil,nil).count_sorted_set_member(nil)}.to raise_error(NoMethodError)
80
53
  end
81
54
 
82
- context 'call initialize' do
83
- it 'exec validation' do
84
- expect{ MockDriver.new(@driver_config) }.not_to raise_error
85
- end
86
-
87
- it 'coping instance of driver_config' do
88
- expect(MockDriver.new(@driver_config).driver_config).to eq(@driver_config)
89
- end
55
+ it 'raise NoMethodError when does not override get_sorted_set_score' do
56
+ expect(MockDriver.method_defined?(:get_sorted_set_score)).to be_true
57
+ expect{MockDriver.new(nil,nil).get_sorted_set_score(nil,nil)}.to raise_error(NoMethodError)
90
58
  end
91
59
 
92
- context 'call validate_driver_config' do
93
- it 'raise ArgumentError when there is not host.' do
94
- @driver_config.delete(:host)
95
- expect{ MockDriver.new(@driver_config) }.to raise_error
96
- end
97
-
98
- it 'raise ArgumentError when there is not port.' do
99
- @driver_config.delete(:port)
100
- expect{ MockDriver.new(@driver_config) }.to raise_error
101
- end
102
-
103
- it 'raise ArgumentError when there is not namespace.' do
104
- @driver_config.delete(:namespace)
105
- expect{ MockDriver.new(@driver_config) }.to raise_error
106
- end
107
-
108
- it 'raise ArgumentError when there is not timeout_sec.' do
109
- @driver_config.delete(:timeout_sec)
110
- expect{ MockDriver.new(@driver_config) }.to raise_error
111
- end
112
-
113
- it 'raise ArgumentError when there is not pool_size.' do
114
- @driver_config.delete(:pool_size)
115
- expect{ MockDriver.new(@driver_config) }.to raise_error
116
- end
60
+ it 'raise NoMethodError when does not override increment_sorted_set' do
61
+ expect(MockDriver.method_defined?(:increment_sorted_set)).to be_true
62
+ expect{MockDriver.new(nil,nil).increment_sorted_set(nil,nil,nil)}.to raise_error(NoMethodError)
117
63
  end
118
64
 
119
- context 'call session' do
120
- before(:each) do
121
- class MockDriver2 < RailsKvsDriver::Base
122
- def connect
123
- end
124
- end
125
- end
126
-
127
- it 'DriverConnectionPool is same instance.' do
128
- expect(MockDriver::DriverConnectionPool.equal?(RailsKvsDriver::Base::DriverConnectionPool)).to be_true
129
- end
130
-
131
- it 'to hand over driver instance to the block' do
132
- MockDriver.session(@driver_config) do |instance|
133
- expect(instance.class).to eq(MockDriver)
134
- end
135
-
136
- MockDriver2.session(@driver_config) do |instance|
137
- expect(instance.class).to eq(MockDriver2)
138
- end
139
- end
140
-
141
- it 'The structure of DriverConnectionPool is correct.' do
142
- MockDriver.session(@driver_config) {}
143
- MockDriver2.session(@driver_config) {}
144
- MockDriver2.session(@driver_config.merge(a:0)) {}
145
- MockDriver2.session(@driver_config.merge(a:1)) {}
146
-
147
- expect(MockDriver::DriverConnectionPool.length).to eq(2)
148
- expect(MockDriver::DriverConnectionPool['MockDriver2'].length).to eq(3)
149
- expect(MockDriver::DriverConnectionPool['MockDriver2'][0].length).to eq(2)
150
- expect(MockDriver::DriverConnectionPool['MockDriver2'][0].has_key? :config).to be_true
151
- expect(MockDriver::DriverConnectionPool['MockDriver2'][0].has_key? :pool).to be_true
152
- end
153
-
154
- it 'call each_sorted_set' do
155
- class MockDriverEachTest < MockDriver
156
- def sorted_set(key, start, stop, reverse)
157
- count = 0
158
- result = Array.new
159
- (start .. stop).each{ |num|
160
- result.push ["example#{num}", count]
161
- count += 1
162
- }
163
- return result
164
- end
165
- def count_sorted_set_member(*args)
166
- 100
167
- end
168
- end
169
-
170
- MockDriverEachTest.session(@driver_config) do |instance|
171
- count = 0
172
- expect{
173
- instance.each_sorted_set('test',false, 10) do |member, score, position|
174
- expect(member).to eq("example#{count}")
175
- expect(score).to eq(count%10)
176
- expect(position).to eq(count)
177
- count += 1
178
- end
179
- }.to change{count}.by(100)
180
- end
181
- end
65
+ it 'raise NoMethodError when does not override remove_sorted_set' do
66
+ expect(MockDriver.method_defined?(:remove_sorted_set)).to be_true
67
+ expect{MockDriver.new(nil,nil).remove_sorted_set(nil,nil)}.to raise_error(NoMethodError)
182
68
  end
183
69
  end
data/spec/spec_helper.rb CHANGED
@@ -4,4 +4,29 @@ require 'rails_kvs_driver'
4
4
 
5
5
  RSpec.configure do |config|
6
6
  config.mock_framework = :rspec
7
+ config.before(:each) {
8
+ RailsKvsDriver::KVS_CONNECTION_POOL.clear
9
+
10
+ # mock config
11
+ @driver_config = {
12
+ :host => 'localhost', # host of KVS.
13
+ :port => 6379, # port of KVS.
14
+ :namespace => 'Example', # namespace of avoid a conflict with key
15
+ :timeout_sec => 5, # timeout seconds.
16
+ :pool_size => 5, # connection pool size.
17
+ }
18
+ }
19
+ end
20
+
21
+ # mock driver class
22
+ class MockDriver < RailsKvsDriver::Base
23
+ def self.connect(args)
24
+ 'dummy1'
25
+ end
26
+ end
27
+
28
+ class MockDriver2 < MockDriver
29
+ def self.connect(args)
30
+ 'dummy2'
31
+ end
7
32
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_kvs_driver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - alfa-jpn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-08 00:00:00.000000000 Z
11
+ date: 2013-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -93,9 +93,17 @@ files:
93
93
  - README.md
94
94
  - Rakefile
95
95
  - lib/rails_kvs_driver.rb
96
+ - lib/rails_kvs_driver/defined_base_method.rb
97
+ - lib/rails_kvs_driver/session.rb
98
+ - lib/rails_kvs_driver/sorted_set.rb
99
+ - lib/rails_kvs_driver/validation_driver_config.rb
96
100
  - lib/rails_kvs_driver/version.rb
97
101
  - rails_kvs_driver.gemspec
98
102
  - spec/common_example.rb
103
+ - spec/rails_kvs_driver/defined_base_method_spec.rb
104
+ - spec/rails_kvs_driver/session_spec.rb
105
+ - spec/rails_kvs_driver/sorted_set_spec.rb
106
+ - spec/rails_kvs_driver/validate_driver_config_spec.rb
99
107
  - spec/rails_kvs_driver_spec.rb
100
108
  - spec/spec_helper.rb
101
109
  homepage: https://github.com/alfa-jpn/rails-kvs-driver
@@ -124,6 +132,10 @@ specification_version: 4
124
132
  summary: rails kvs driver wrap access key-value store.
125
133
  test_files:
126
134
  - spec/common_example.rb
135
+ - spec/rails_kvs_driver/defined_base_method_spec.rb
136
+ - spec/rails_kvs_driver/session_spec.rb
137
+ - spec/rails_kvs_driver/sorted_set_spec.rb
138
+ - spec/rails_kvs_driver/validate_driver_config_spec.rb
127
139
  - spec/rails_kvs_driver_spec.rb
128
140
  - spec/spec_helper.rb
129
141
  has_rdoc: