rails_kvs_driver 0.1.6 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -0
- data/lib/rails_kvs_driver/defined_base_method.rb +21 -3
- data/lib/rails_kvs_driver/sorted_sets/sorted_set.rb +93 -0
- data/lib/rails_kvs_driver/sorted_sets/sorted_sets.rb +73 -0
- data/lib/rails_kvs_driver/version.rb +1 -1
- data/lib/rails_kvs_driver.rb +12 -11
- data/spec/common_example.rb +19 -6
- data/spec/rails_kvs_driver/defined_base_method_spec.rb +1 -1
- data/spec/rails_kvs_driver/sorted_sets/sorted_set_spec.rb +61 -0
- data/spec/rails_kvs_driver/{sorted_set_spec.rb → sorted_sets/sorted_sets_spec.rb} +6 -31
- metadata +8 -5
- data/lib/rails_kvs_driver/sorted_set.rb +0 -124
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9739938d3667ed90c199b022fc20750e7d721218
|
4
|
+
data.tar.gz: 132108582e21c4d1b96a46fca297bdebcaa99c4c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cf393acdf5228ceab39e2a73aa50ecaebabe089e9fed92f81685e59d75e9d1fd56a279b8f2f85c5070968df5c73894f33f0bfa427508f06a781dd1fd29df2fd2
|
7
|
+
data.tar.gz: a0b3a886926497d72bddf2467a7afbc02502e3b56b6a0ef2a65c6f0c33674a1e5058da3a060a86242a46d65c5d180166d9331b67998ca4fcd044f8a199827f0e
|
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
require 'rails_kvs_driver/
|
1
|
+
require 'rails_kvs_driver/sorted_sets/sorted_sets'
|
2
|
+
|
2
3
|
module RailsKvsDriver
|
3
4
|
module DefinedBaseMethod
|
4
5
|
# initialize driver.
|
@@ -9,11 +10,28 @@ module RailsKvsDriver
|
|
9
10
|
@driver_config = driver_config
|
10
11
|
end
|
11
12
|
|
13
|
+
# get string value from kvs.
|
14
|
+
# @param key [String] key.
|
15
|
+
# @return [String] value. when doesn't exist, nil
|
16
|
+
# @abstract get value from kvs. when doesn't exist, nil
|
17
|
+
def [](key)
|
18
|
+
get(key)
|
19
|
+
end
|
20
|
+
|
21
|
+
# set string value to kvs.
|
22
|
+
# @param key [String] key.
|
23
|
+
# @param value [String] value.
|
24
|
+
# @return [Boolean] result
|
25
|
+
# @abstract set value to kvs.
|
26
|
+
def []=(key, value)
|
27
|
+
set(key, value)
|
28
|
+
end
|
29
|
+
|
12
30
|
# return initialized SortedSet class.
|
13
31
|
#
|
14
|
-
# @return [RailsKvsDriver::
|
32
|
+
# @return [RailsKvsDriver::SortedSets] SortedSet
|
15
33
|
def sorted_sets
|
16
|
-
|
34
|
+
SortedSets::SortedSets.new(self)
|
17
35
|
end
|
18
36
|
|
19
37
|
# execute the block of code for each key having string, and value.
|
@@ -0,0 +1,93 @@
|
|
1
|
+
module RailsKvsDriver
|
2
|
+
module SortedSets
|
3
|
+
class SortedSet
|
4
|
+
attr_accessor :driver_instance
|
5
|
+
attr_accessor :key
|
6
|
+
|
7
|
+
# initialize sorted_set
|
8
|
+
#
|
9
|
+
# @param driver_instance [RailsKvsDriver::Base] instance of driver.
|
10
|
+
# @param key [String] key of sorted_set.
|
11
|
+
def initialize(driver_instance, key)
|
12
|
+
@driver_instance = driver_instance
|
13
|
+
@key = key
|
14
|
+
end
|
15
|
+
|
16
|
+
# get score of member.
|
17
|
+
# when the key or member doesn't exist, return 0.
|
18
|
+
#
|
19
|
+
# @param member [String] member of sorted_set.
|
20
|
+
# @return [Integer] score.
|
21
|
+
def [](member)
|
22
|
+
@driver_instance.get_sorted_set_score(@key, member)
|
23
|
+
end
|
24
|
+
|
25
|
+
# add member in sorted_set.
|
26
|
+
# when the key doesn't exist, it's made newly.
|
27
|
+
#
|
28
|
+
# @param member [String] member of sorted_set.
|
29
|
+
# @param score [Flaot] default score.
|
30
|
+
# @return [Float] score.
|
31
|
+
def []=(member, score)
|
32
|
+
@driver_instance.add_sorted_set(@key, member, score)
|
33
|
+
return score
|
34
|
+
end
|
35
|
+
|
36
|
+
# execute the block of code for each member of sorted set.
|
37
|
+
#
|
38
|
+
# @param reverse [Boolean] order by desc. (default=false(acs))
|
39
|
+
# @param limit [Integer] limit number to acquire at a time. (default=1000)
|
40
|
+
# @param &block [{|member, score, position| }] each the block of code for each member of sorted set.
|
41
|
+
def each(reverse=false, limit=1000)
|
42
|
+
count = length
|
43
|
+
position = 0
|
44
|
+
|
45
|
+
while position < count
|
46
|
+
@driver_instance.get_sorted_set(@key, position, position + (limit-1), reverse).each do |data|
|
47
|
+
yield data[0], data[1], position
|
48
|
+
position += 1
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# check having member.
|
54
|
+
#
|
55
|
+
# @return [Boolean] result
|
56
|
+
def has_member?(member)
|
57
|
+
members.include?(member)
|
58
|
+
end
|
59
|
+
|
60
|
+
# increment member's score of sorted set.
|
61
|
+
#
|
62
|
+
# @param member [String] member of sorted set.
|
63
|
+
# @param score [Float] increment score
|
64
|
+
# @return [Float] score after increment.
|
65
|
+
def increment(member, score)
|
66
|
+
@driver_instance.increment_sorted_set(@key, member, score)
|
67
|
+
end
|
68
|
+
|
69
|
+
# length of sorted_set
|
70
|
+
#
|
71
|
+
# @return [Integer] length of sorted_set
|
72
|
+
def length
|
73
|
+
@driver_instance.count_sorted_set_member(@key)
|
74
|
+
end
|
75
|
+
|
76
|
+
# get all member.
|
77
|
+
# @return [Array<String>] array of member.
|
78
|
+
def members
|
79
|
+
members = Array.new
|
80
|
+
each{|member| members.push(member)}
|
81
|
+
return members
|
82
|
+
end
|
83
|
+
|
84
|
+
# remove member of sorted_set.
|
85
|
+
#
|
86
|
+
# @param member [String] member of sorted_set.
|
87
|
+
def remove(member)
|
88
|
+
@driver_instance.remove_sorted_set(@key, member)
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'rails_kvs_driver/sorted_sets/sorted_set'
|
2
|
+
|
3
|
+
module RailsKvsDriver
|
4
|
+
module SortedSets
|
5
|
+
class SortedSets
|
6
|
+
attr_accessor :driver_instance
|
7
|
+
|
8
|
+
# initialize sorted_sets
|
9
|
+
#
|
10
|
+
# @param driver_instance [RailsKvsDriver::Base]
|
11
|
+
def initialize(driver_instance)
|
12
|
+
@driver_instance = driver_instance
|
13
|
+
end
|
14
|
+
|
15
|
+
# get sorted_set.
|
16
|
+
#
|
17
|
+
# @param key [String] sorted_set key.
|
18
|
+
# @return [SortedSet] sorted_set of key.
|
19
|
+
def [](key)
|
20
|
+
SortedSet.new(@driver_instance, key)
|
21
|
+
end
|
22
|
+
|
23
|
+
# add new sorted_set.
|
24
|
+
# if key exists, delete old sorted_set.
|
25
|
+
#
|
26
|
+
# @param key [String] sorted_set key.
|
27
|
+
# @param member_sets [Array<Array<member,score>>] array of array of member<String> and score<Float>.
|
28
|
+
# @return [Array<Array<member,score>>] meber_sets
|
29
|
+
def []=(key, member_sets)
|
30
|
+
delete(key) if has_key?(key)
|
31
|
+
sorted_set = SortedSet.new(@driver_instance,key)
|
32
|
+
member_sets.each {|member_set| sorted_set[member_set[0]] = member_set}
|
33
|
+
end
|
34
|
+
|
35
|
+
# delete sorted_set.
|
36
|
+
#
|
37
|
+
# @param key [String] key of sorted_set.
|
38
|
+
def delete(key)
|
39
|
+
@driver_instance.delete(key)
|
40
|
+
end
|
41
|
+
|
42
|
+
# execute the block of code for each sorted_set.
|
43
|
+
#
|
44
|
+
# @param &block [{|key| }] each the block of code for each key of sorted set.
|
45
|
+
def each
|
46
|
+
keys.each {|key| yield key }
|
47
|
+
end
|
48
|
+
|
49
|
+
# check key in sorted set.
|
50
|
+
#
|
51
|
+
# @param key [String] key name.
|
52
|
+
# @return [Boolean] result.
|
53
|
+
def has_key?(key)
|
54
|
+
keys.include?(key)
|
55
|
+
end
|
56
|
+
|
57
|
+
# get all keys from kvs.
|
58
|
+
#
|
59
|
+
# @return [Array<String>] array of key names.
|
60
|
+
def keys
|
61
|
+
@driver_instance.get_sorted_set_keys
|
62
|
+
end
|
63
|
+
|
64
|
+
# get length of sorted_set.
|
65
|
+
#
|
66
|
+
# @return [Integer] length of keys.
|
67
|
+
def length
|
68
|
+
keys.length
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
data/lib/rails_kvs_driver.rb
CHANGED
@@ -38,7 +38,7 @@ module RailsKvsDriver
|
|
38
38
|
# @param key [String] key.
|
39
39
|
# @return [String] value. when doesn't exist, nil
|
40
40
|
# @abstract get value from kvs. when doesn't exist, nil
|
41
|
-
def
|
41
|
+
def get(key)
|
42
42
|
raise NoMethodError
|
43
43
|
end
|
44
44
|
|
@@ -47,7 +47,7 @@ module RailsKvsDriver
|
|
47
47
|
# @param value [String] value.
|
48
48
|
# @return [Boolean] result
|
49
49
|
# @abstract set value to kvs.
|
50
|
-
def
|
50
|
+
def set(key, value)
|
51
51
|
raise NoMethodError
|
52
52
|
end
|
53
53
|
|
@@ -89,7 +89,8 @@ module RailsKvsDriver
|
|
89
89
|
# @note same as sorted set of redis. refer to redis.
|
90
90
|
#
|
91
91
|
# @param key [String] key of sorted set.
|
92
|
-
# @return [Integer] members num
|
92
|
+
# @return [Integer] members num. when doesn't exist key, return nil
|
93
|
+
# @abstract count members of sorted set. when doesn't exist key, return nil
|
93
94
|
def count_sorted_set_member(key)
|
94
95
|
raise NoMethodError
|
95
96
|
end
|
@@ -108,8 +109,8 @@ module RailsKvsDriver
|
|
108
109
|
end
|
109
110
|
|
110
111
|
# get all sorted_set keys.
|
111
|
-
# @return [Array<String>] array of key names.
|
112
|
-
# @abstract get all keys from kvs.(only having sorted_set)
|
112
|
+
# @return [Array<String>] array of key names. when doesn't exist keys, return Array of length 0.
|
113
|
+
# @abstract get all keys from kvs.(only having sorted_set), when doesn't exist keys, return Array of length 0.
|
113
114
|
def get_sorted_set_keys
|
114
115
|
raise NoMethodError
|
115
116
|
end
|
@@ -119,8 +120,8 @@ module RailsKvsDriver
|
|
119
120
|
#
|
120
121
|
# @param key [String] key of sorted set.
|
121
122
|
# @param member [String] member of sorted set.
|
122
|
-
# @return [Float] score of member.
|
123
|
-
# @abstract get the score of member.
|
123
|
+
# @return [Float] score of member. when doesn't exist key or member, nil.
|
124
|
+
# @abstract get the score of member. when doesn't exist key or member, nil.
|
124
125
|
def get_sorted_set_score(key, member)
|
125
126
|
raise NoMethodError
|
126
127
|
end
|
@@ -131,8 +132,8 @@ module RailsKvsDriver
|
|
131
132
|
# @param key [String] key of sorted set.
|
132
133
|
# @param member [String] member of sorted set.
|
133
134
|
# @param score [Float] increment score.
|
134
|
-
# @return [Float] value after increment
|
135
|
-
# @abstract increment score of member from sorted set.
|
135
|
+
# @return [Float] value after increment. when doesn't exist key or member, create.
|
136
|
+
# @abstract increment score of member from sorted set. when doesn't exist key or member, create.
|
136
137
|
def increment_sorted_set(key, member, score)
|
137
138
|
raise NoMethodError
|
138
139
|
end
|
@@ -143,8 +144,8 @@ module RailsKvsDriver
|
|
143
144
|
#
|
144
145
|
# @param key [String] key of sorted set.
|
145
146
|
# @param member [String] member of sorted set.
|
146
|
-
# @return [Boolean] result.
|
147
|
-
# @abstract remove sorted set from kvs.
|
147
|
+
# @return [Boolean] result. when doesn't exist key or member, false.
|
148
|
+
# @abstract remove sorted set from kvs. when doesn't exist key or member, do nothing.
|
148
149
|
def remove_sorted_set(key, member)
|
149
150
|
raise NoMethodError
|
150
151
|
end
|
data/spec/common_example.rb
CHANGED
@@ -26,17 +26,17 @@ shared_examples_for 'RailsKvsDriver example' do |driver_class, driver_config|
|
|
26
26
|
end
|
27
27
|
|
28
28
|
context 'override methods' do
|
29
|
-
it 'call
|
29
|
+
it 'call get' do
|
30
30
|
driver_class::session(driver_config) do |kvs|
|
31
|
-
kvs
|
32
|
-
expect(kvs
|
33
|
-
expect(kvs
|
31
|
+
kvs.set('example', 'nico-nico')
|
32
|
+
expect(kvs.get('example')).to eq('nico-nico')
|
33
|
+
expect(kvs.get('nothing key')).to be_nil
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
-
it 'call
|
37
|
+
it 'call set' do
|
38
38
|
driver_class::session(driver_config) do |kvs|
|
39
|
-
expect{ kvs
|
39
|
+
expect{ kvs.set('example', 'nico-nico') }.to change{ kvs.keys.length }.by(1)
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
@@ -156,6 +156,19 @@ shared_examples_for 'RailsKvsDriver example' do |driver_class, driver_config|
|
|
156
156
|
|
157
157
|
|
158
158
|
context 'inheritance methods' do
|
159
|
+
it 'call []' do
|
160
|
+
driver_class::session(driver_config) do |kvs|
|
161
|
+
kvs['example'] = 'nico-nico'
|
162
|
+
expect(kvs['example']).to eq('nico-nico')
|
163
|
+
expect(kvs['nothing key']).to be_nil
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
it 'call []=' do
|
168
|
+
driver_class::session(driver_config) do |kvs|
|
169
|
+
expect{ kvs['example'] = 'nico-nico' }.to change{ kvs.keys.length }.by(1)
|
170
|
+
end
|
171
|
+
end
|
159
172
|
|
160
173
|
it 'call each' do
|
161
174
|
driver_class::session(driver_config) do |kvs|
|
@@ -15,7 +15,7 @@ describe RailsKvsDriver::DefinedBaseMethod do
|
|
15
15
|
|
16
16
|
context 'call sorted_sets' do
|
17
17
|
it 'return instance of RailsKvsDriver::SortedSet' do
|
18
|
-
expect(MockDriver.new(nil,nil).sorted_sets.instance_of?(RailsKvsDriver::
|
18
|
+
expect(MockDriver.new(nil,nil).sorted_sets.instance_of?(RailsKvsDriver::SortedSets::SortedSets)).to be_true
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe RailsKvsDriver::SortedSets::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
|
+
remove_sorted_set: true
|
17
|
+
})
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'call []' do
|
21
|
+
expect(@driver.sorted_sets[:key][:member] = 1).to eq(1)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'call []=' do
|
25
|
+
expect(@driver.sorted_sets[:key][:member]).to eq(100)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'call length' do
|
29
|
+
expect(@driver.sorted_sets[:key].length).to eq(100)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'call each' do
|
33
|
+
count = 0
|
34
|
+
expect{
|
35
|
+
@driver.sorted_sets[:key].each(false, 10) do |member, score, position|
|
36
|
+
expect(position).to eq(count)
|
37
|
+
count += 1
|
38
|
+
end
|
39
|
+
}.to change{count}.by(100)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'call has_member?' do
|
43
|
+
expect(@driver.sorted_sets[:key].has_member?(:a)).to be_true
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'call increment' do
|
47
|
+
expect( @driver.sorted_sets[:key].increment(:b, 1) ).to eq(10)
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'call length' do
|
51
|
+
expect(@driver.sorted_sets[:key].length).to eq(100)
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'call members' do
|
55
|
+
expect(@driver.sorted_sets[:key].members.length).to eq(100)
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'call remove' do
|
59
|
+
expect(@driver.sorted_sets[:key].remove(:b)).to be_true
|
60
|
+
end
|
61
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'rspec'
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
|
-
describe RailsKvsDriver::
|
4
|
+
describe RailsKvsDriver::SortedSets::SortedSets do
|
5
5
|
|
6
6
|
before(:each) do
|
7
7
|
@driver = MockDriver.new(nil,nil)
|
@@ -18,16 +18,12 @@ describe RailsKvsDriver::SortedSet do
|
|
18
18
|
end
|
19
19
|
|
20
20
|
it 'call []' do
|
21
|
-
|
21
|
+
sorted_set = [['member', 1],['member', 2]]
|
22
|
+
expect(@driver.sorted_sets[:key] = sorted_set).to eq(sorted_set)
|
22
23
|
end
|
23
24
|
|
24
25
|
it 'call []=' do
|
25
|
-
expect((@driver.sorted_sets[
|
26
|
-
expect(@driver.sorted_sets['hoge','fuga']).to eq(100)
|
27
|
-
end
|
28
|
-
|
29
|
-
it 'call count' do
|
30
|
-
expect(@driver.sorted_sets.count(:a)).to eq(100)
|
26
|
+
expect((@driver.sorted_sets[:key]).instance_of?(RailsKvsDriver::SortedSets::SortedSet)).to be_true
|
31
27
|
end
|
32
28
|
|
33
29
|
it 'call delete' do
|
@@ -43,37 +39,16 @@ describe RailsKvsDriver::SortedSet do
|
|
43
39
|
}.to change{count}.by(3)
|
44
40
|
end
|
45
41
|
|
46
|
-
it 'call each_member' do
|
47
|
-
count = 0
|
48
|
-
expect{
|
49
|
-
@driver.sorted_sets.each_member('test',false, 10) do |member, score, position|
|
50
|
-
expect(position).to eq(count)
|
51
|
-
count += 1
|
52
|
-
end
|
53
|
-
}.to change{count}.by(100)
|
54
|
-
end
|
55
|
-
|
56
|
-
it 'call increment_member' do
|
57
|
-
expect( @driver.sorted_sets.increment(:a, :b, 1) ).to eq(10)
|
58
|
-
end
|
59
|
-
|
60
|
-
it 'call keys' do
|
61
|
-
expect(@driver.sorted_sets.keys.length).to eq(3)
|
62
|
-
end
|
63
|
-
|
64
42
|
it 'call has_key?' do
|
65
43
|
expect(@driver.sorted_sets.has_key?(:a)).to be_true
|
66
44
|
end
|
67
45
|
|
68
|
-
it 'call
|
69
|
-
expect(@driver.sorted_sets.
|
46
|
+
it 'call keys' do
|
47
|
+
expect(@driver.sorted_sets.keys.length).to eq(3)
|
70
48
|
end
|
71
49
|
|
72
50
|
it 'call length' do
|
73
51
|
expect(@driver.sorted_sets.length).to eq(3)
|
74
52
|
end
|
75
53
|
|
76
|
-
it 'call remove' do
|
77
|
-
expect(@driver.sorted_sets.remove(:a, :b)).to be_true
|
78
|
-
end
|
79
54
|
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.
|
4
|
+
version: 0.2.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-
|
11
|
+
date: 2013-11-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -96,14 +96,16 @@ files:
|
|
96
96
|
- lib/rails_kvs_driver.rb
|
97
97
|
- lib/rails_kvs_driver/defined_base_method.rb
|
98
98
|
- lib/rails_kvs_driver/session.rb
|
99
|
-
- lib/rails_kvs_driver/sorted_set.rb
|
99
|
+
- lib/rails_kvs_driver/sorted_sets/sorted_set.rb
|
100
|
+
- lib/rails_kvs_driver/sorted_sets/sorted_sets.rb
|
100
101
|
- lib/rails_kvs_driver/validation_driver_config.rb
|
101
102
|
- lib/rails_kvs_driver/version.rb
|
102
103
|
- rails_kvs_driver.gemspec
|
103
104
|
- spec/common_example.rb
|
104
105
|
- spec/rails_kvs_driver/defined_base_method_spec.rb
|
105
106
|
- spec/rails_kvs_driver/session_spec.rb
|
106
|
-
- spec/rails_kvs_driver/sorted_set_spec.rb
|
107
|
+
- spec/rails_kvs_driver/sorted_sets/sorted_set_spec.rb
|
108
|
+
- spec/rails_kvs_driver/sorted_sets/sorted_sets_spec.rb
|
107
109
|
- spec/rails_kvs_driver/validate_driver_config_spec.rb
|
108
110
|
- spec/rails_kvs_driver_spec.rb
|
109
111
|
- spec/spec_helper.rb
|
@@ -136,7 +138,8 @@ test_files:
|
|
136
138
|
- spec/common_example.rb
|
137
139
|
- spec/rails_kvs_driver/defined_base_method_spec.rb
|
138
140
|
- spec/rails_kvs_driver/session_spec.rb
|
139
|
-
- spec/rails_kvs_driver/sorted_set_spec.rb
|
141
|
+
- spec/rails_kvs_driver/sorted_sets/sorted_set_spec.rb
|
142
|
+
- spec/rails_kvs_driver/sorted_sets/sorted_sets_spec.rb
|
140
143
|
- spec/rails_kvs_driver/validate_driver_config_spec.rb
|
141
144
|
- spec/rails_kvs_driver_spec.rb
|
142
145
|
- spec/spec_helper.rb
|
@@ -1,124 +0,0 @@
|
|
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
|
-
# count member of sorted_set
|
38
|
-
#
|
39
|
-
# @param key [String] sorted_set key
|
40
|
-
def count(key)
|
41
|
-
@driver_instance.count_sorted_set_member(key)
|
42
|
-
end
|
43
|
-
|
44
|
-
# delete key
|
45
|
-
#
|
46
|
-
# @param key [String] sorted_set key
|
47
|
-
def delete(key)
|
48
|
-
@driver_instance.delete(key)
|
49
|
-
end
|
50
|
-
|
51
|
-
# execute the block of code for each sorted_set.
|
52
|
-
# @param &block [{|key| }] each the block of code for each key of sorted set.
|
53
|
-
def each
|
54
|
-
keys.each {|key| yield key }
|
55
|
-
end
|
56
|
-
|
57
|
-
# execute the block of code for each member of sorted set.
|
58
|
-
#
|
59
|
-
# @param key [String] key of sorted set.
|
60
|
-
# @param reverse [Boolean] order by desc.
|
61
|
-
# @param limit [Integer] limit number to acquire at a time.
|
62
|
-
# @param &block [{|member, score, position| }] each the block of code for each member of sorted set.
|
63
|
-
def each_member(key, reverse=false, limit=1000)
|
64
|
-
member_count = count(key)
|
65
|
-
member_position = 0
|
66
|
-
|
67
|
-
while member_position < member_count
|
68
|
-
@driver_instance.get_sorted_set(key, member_position, member_position + (limit-1), reverse).each do |data|
|
69
|
-
yield data[0], data[1], member_position
|
70
|
-
member_position += 1
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
# check key in sorted set.
|
76
|
-
#
|
77
|
-
# @param key [String] key name.
|
78
|
-
# @return [Boolean] result.
|
79
|
-
def has_key?(key)
|
80
|
-
!(self[key].nil?)
|
81
|
-
end
|
82
|
-
|
83
|
-
# check member in sorted set.
|
84
|
-
#
|
85
|
-
# @param key [String] key name.
|
86
|
-
# @param member [String] member name.
|
87
|
-
# @return [Boolean] result.
|
88
|
-
def has_member?(key, member)
|
89
|
-
!(self[key, member].nil?)
|
90
|
-
end
|
91
|
-
|
92
|
-
# increment member's score of sorted set.
|
93
|
-
#
|
94
|
-
# @param key [String] key of sorted set.
|
95
|
-
# @param member [String] member of sorted set.
|
96
|
-
# @param score [Float] increment score
|
97
|
-
# @return [Float] score after increment.
|
98
|
-
def increment(key, member, score)
|
99
|
-
@driver_instance.increment_sorted_set(key, member, score)
|
100
|
-
end
|
101
|
-
|
102
|
-
# get all keys from kvs.
|
103
|
-
# @return [Array<String>] array of key names.
|
104
|
-
# @abstract get all keys from kvs.(only having string value)
|
105
|
-
def keys
|
106
|
-
@driver_instance.get_sorted_set_keys
|
107
|
-
end
|
108
|
-
|
109
|
-
# get length of sorted_set.
|
110
|
-
#
|
111
|
-
# @return [Integer] length of keys.
|
112
|
-
def length
|
113
|
-
keys.length
|
114
|
-
end
|
115
|
-
|
116
|
-
# remove member of sorted_set
|
117
|
-
#
|
118
|
-
# @param key [String] sorted_set key
|
119
|
-
# @param member [String] member of sorted_se
|
120
|
-
def remove(key, member)
|
121
|
-
@driver_instance.remove_sorted_set(key, member)
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|