redis-file 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +7 -0
- data/.rspec +1 -0
- data/.travis.yml +8 -0
- data/Gemfile +7 -0
- data/LICENSE +21 -0
- data/README.md +86 -0
- data/Rakefile +27 -0
- data/lib/redis-file.rb +6 -0
- data/lib/redis-file/expiring_hash.rb +70 -0
- data/lib/redis-file/rspec.rb +23 -0
- data/lib/redis-file/sorted_set_argument_handler.rb +74 -0
- data/lib/redis-file/sorted_set_store.rb +80 -0
- data/lib/redis-file/version.rb +3 -0
- data/lib/redis-file/zset.rb +29 -0
- data/lib/redis/connection/file.rb +960 -0
- data/redis-file.gemspec +23 -0
- data/spec/compatibility_spec.rb +9 -0
- data/spec/connection_spec.rb +85 -0
- data/spec/hashes_spec.rb +182 -0
- data/spec/keys_spec.rb +248 -0
- data/spec/lists_spec.rb +195 -0
- data/spec/server_spec.rb +100 -0
- data/spec/sets_spec.rb +178 -0
- data/spec/sorted_sets_spec.rb +425 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/spec_helper_live_redis.rb +14 -0
- data/spec/strings_spec.rb +236 -0
- data/spec/transactions_spec.rb +19 -0
- data/spec/upcase_method_name_spec.rb +18 -0
- metadata +103 -0
data/spec/lists_spec.rb
ADDED
@@ -0,0 +1,195 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module RedisFile
|
4
|
+
describe "ListsMethods" do
|
5
|
+
before(:each) do
|
6
|
+
@client = Redis.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should get an element from a list by its index" do
|
10
|
+
@client.lpush("key1", "val1")
|
11
|
+
@client.lpush("key1", "val2")
|
12
|
+
|
13
|
+
@client.lindex("key1", 0).should be == "val2"
|
14
|
+
@client.lindex("key1", -1).should be == "val1"
|
15
|
+
@client.lindex("key1", 3).should be == nil
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should insert an element before or after another element in a list" do
|
19
|
+
@client.rpush("key1", "v1")
|
20
|
+
@client.rpush("key1", "v3")
|
21
|
+
@client.linsert("key1", :before, "v3", "v2")
|
22
|
+
|
23
|
+
@client.lrange("key1", 0, -1).should be == ["v1", "v2", "v3"]
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should allow multiple values to be added to a list in a single rpush' do
|
27
|
+
@client.rpush('key1', [1, 2, 3])
|
28
|
+
@client.lrange('key1', 0, -1).should be == ['1', '2', '3']
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should allow multiple values to be added to a list in a single lpush' do
|
32
|
+
@client.lpush('key1', [1, 2, 3])
|
33
|
+
@client.lrange('key1', 0, -1).should be == ['3', '2', '1']
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should error if an invalid where argument is given" do
|
37
|
+
@client.rpush("key1", "v1")
|
38
|
+
@client.rpush("key1", "v3")
|
39
|
+
lambda { @client.linsert("key1", :invalid, "v3", "v2") }.should raise_error(Redis::CommandError, "ERR syntax error")
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should get the length of a list" do
|
43
|
+
@client.rpush("key1", "v1")
|
44
|
+
@client.rpush("key1", "v2")
|
45
|
+
|
46
|
+
@client.llen("key1").should be == 2
|
47
|
+
@client.llen("key2").should be == 0
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should remove and get the first element in a list" do
|
51
|
+
@client.rpush("key1", "v1")
|
52
|
+
@client.rpush("key1", "v2")
|
53
|
+
@client.rpush("key1", "v3")
|
54
|
+
|
55
|
+
@client.lpop("key1").should be == "v1"
|
56
|
+
@client.lrange("key1", 0, -1).should be == ["v2", "v3"]
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should prepend a value to a list" do
|
60
|
+
@client.rpush("key1", "v1")
|
61
|
+
@client.rpush("key1", "v2")
|
62
|
+
|
63
|
+
@client.lrange("key1", 0, -1).should be == ["v1", "v2"]
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should prepend a value to a list, only if the list exists" do
|
67
|
+
@client.lpush("key1", "v1")
|
68
|
+
|
69
|
+
@client.lpushx("key1", "v2")
|
70
|
+
@client.lpushx("key2", "v3")
|
71
|
+
|
72
|
+
@client.lrange("key1", 0, -1).should be == ["v2", "v1"]
|
73
|
+
@client.llen("key2").should be == 0
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should get a range of elements from a list" do
|
77
|
+
@client.rpush("key1", "v1")
|
78
|
+
@client.rpush("key1", "v2")
|
79
|
+
@client.rpush("key1", "v3")
|
80
|
+
|
81
|
+
@client.lrange("key1", 1, -1).should be == ["v2", "v3"]
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should remove elements from a list" do
|
85
|
+
@client.rpush("key1", "v1")
|
86
|
+
@client.rpush("key1", "v2")
|
87
|
+
@client.rpush("key1", "v2")
|
88
|
+
@client.rpush("key1", "v2")
|
89
|
+
@client.rpush("key1", "v1")
|
90
|
+
|
91
|
+
@client.lrem("key1", 1, "v1").should be == 1
|
92
|
+
@client.lrem("key1", -2, "v2").should be == 2
|
93
|
+
@client.llen("key1").should be == 2
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should remove list's key when list is empty" do
|
97
|
+
@client.rpush("key1", "v1")
|
98
|
+
@client.rpush("key1", "v2")
|
99
|
+
@client.lrem("key1", 1, "v1")
|
100
|
+
@client.lrem("key1", 1, "v2")
|
101
|
+
|
102
|
+
@client.exists("key1").should be == false
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should set the value of an element in a list by its index" do
|
106
|
+
@client.rpush("key1", "one")
|
107
|
+
@client.rpush("key1", "two")
|
108
|
+
@client.rpush("key1", "three")
|
109
|
+
|
110
|
+
@client.lset("key1", 0, "four")
|
111
|
+
@client.lset("key1", -2, "five")
|
112
|
+
@client.lrange("key1", 0, -1).should be == ["four", "five", "three"]
|
113
|
+
|
114
|
+
lambda { @client.lset("key1", 4, "six") }.should raise_error(Redis::CommandError, "ERR index out of range")
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should trim a list to the specified range" do
|
118
|
+
@client.rpush("key1", "one")
|
119
|
+
@client.rpush("key1", "two")
|
120
|
+
@client.rpush("key1", "three")
|
121
|
+
|
122
|
+
@client.ltrim("key1", 1, -1)
|
123
|
+
@client.lrange("key1", 0, -1).should be == ["two", "three"]
|
124
|
+
end
|
125
|
+
|
126
|
+
|
127
|
+
context "when the list is smaller than the requested trim" do
|
128
|
+
before { @client.rpush("listOfOne", "one") }
|
129
|
+
|
130
|
+
context "trimming with a negative start (specifying a max)" do
|
131
|
+
before { @client.ltrim("listOfOne", -5, -1) }
|
132
|
+
|
133
|
+
it "returns the unmodified list" do
|
134
|
+
@client.lrange("listOfOne", 0, -1).should be == ["one"]
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
context "when the list is larger than the requested trim" do
|
140
|
+
before do
|
141
|
+
@client.rpush("maxTest", "one")
|
142
|
+
@client.rpush("maxTest", "two")
|
143
|
+
@client.rpush("maxTest", "three")
|
144
|
+
@client.rpush("maxTest", "four")
|
145
|
+
@client.rpush("maxTest", "five")
|
146
|
+
@client.rpush("maxTest", "six")
|
147
|
+
end
|
148
|
+
|
149
|
+
context "trimming with a negative start (specifying a max)" do
|
150
|
+
before { @client.ltrim("maxTest", -5, -1) }
|
151
|
+
|
152
|
+
it "should trim a list to the specified maximum size" do
|
153
|
+
@client.lrange("maxTest", 0, -1).should be == ["two","three", "four", "five", "six"]
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
|
159
|
+
it "should remove and return the last element in a list" do
|
160
|
+
@client.rpush("key1", "one")
|
161
|
+
@client.rpush("key1", "two")
|
162
|
+
@client.rpush("key1", "three")
|
163
|
+
|
164
|
+
@client.rpop("key1").should be == "three"
|
165
|
+
@client.lrange("key1", 0, -1).should be == ["one", "two"]
|
166
|
+
end
|
167
|
+
|
168
|
+
it "should remove the last element in a list, append it to another list and return it" do
|
169
|
+
@client.rpush("key1", "one")
|
170
|
+
@client.rpush("key1", "two")
|
171
|
+
@client.rpush("key1", "three")
|
172
|
+
|
173
|
+
@client.rpoplpush("key1", "key2").should be == "three"
|
174
|
+
|
175
|
+
@client.lrange("key1", 0, -1).should be == ["one", "two"]
|
176
|
+
@client.lrange("key2", 0, -1).should be == ["three"]
|
177
|
+
end
|
178
|
+
|
179
|
+
it "should append a value to a list" do
|
180
|
+
@client.rpush("key1", "one")
|
181
|
+
@client.rpush("key1", "two")
|
182
|
+
|
183
|
+
@client.lrange("key1", 0, -1).should be == ["one", "two"]
|
184
|
+
end
|
185
|
+
|
186
|
+
it "should append a value to a list, only if the list exists" do
|
187
|
+
@client.rpush("key1", "one")
|
188
|
+
@client.rpushx("key1", "two")
|
189
|
+
@client.rpushx("key2", "two")
|
190
|
+
|
191
|
+
@client.lrange("key1", 0, -1).should be == ["one", "two"]
|
192
|
+
@client.lrange("key2", 0, -1).should be == []
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
data/spec/server_spec.rb
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module RedisFile
|
4
|
+
describe "ServerMethods" do
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
@client = Redis.new
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should return the number of keys in the selected database" do
|
11
|
+
@client.set("key1", "1")
|
12
|
+
@client.set("key2", "2")
|
13
|
+
@client.set("key2", "two")
|
14
|
+
|
15
|
+
@client.dbsize.should be == 2
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should get information and statistics about the server" do
|
19
|
+
@client.info.key?("redis_version").should be == true
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should handle non-existent methods" do
|
23
|
+
lambda { @client.idontexist }.should raise_error(Redis::CommandError, "ERR unknown command 'idontexist'")
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "multiple databases" do
|
27
|
+
it "should default to database 0" do
|
28
|
+
@client.inspect.should =~ %r#/0>$#
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should select another database" do
|
32
|
+
@client.select(1)
|
33
|
+
@client.inspect.should =~ %r#/1>$#
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should store keys separately in each database" do
|
37
|
+
@client.select(0).should be == "OK"
|
38
|
+
@client.set("key1", "1")
|
39
|
+
@client.set("key2", "2")
|
40
|
+
|
41
|
+
@client.select(1)
|
42
|
+
@client.set("key3", "3")
|
43
|
+
@client.set("key4", "4")
|
44
|
+
@client.set("key5", "5")
|
45
|
+
|
46
|
+
@client.select(0)
|
47
|
+
@client.dbsize.should be == 2
|
48
|
+
@client.exists("key1").should be_true
|
49
|
+
@client.exists("key3").should be_false
|
50
|
+
|
51
|
+
@client.select(1)
|
52
|
+
@client.dbsize.should be == 3
|
53
|
+
@client.exists("key4").should be_true
|
54
|
+
@client.exists("key2").should be_false
|
55
|
+
|
56
|
+
@client.flushall
|
57
|
+
@client.dbsize.should be == 0
|
58
|
+
|
59
|
+
@client.select(0)
|
60
|
+
@client.dbsize.should be == 0
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should flush a database" do
|
64
|
+
@client.select(0)
|
65
|
+
@client.set("key1", "1")
|
66
|
+
@client.set("key2", "2")
|
67
|
+
@client.dbsize.should be == 2
|
68
|
+
|
69
|
+
@client.select(1)
|
70
|
+
@client.set("key3", "3")
|
71
|
+
@client.set("key4", "4")
|
72
|
+
@client.dbsize.should be == 2
|
73
|
+
|
74
|
+
@client.flushdb.should be == "OK"
|
75
|
+
|
76
|
+
@client.dbsize.should be == 0
|
77
|
+
@client.select(0)
|
78
|
+
@client.dbsize.should be == 2
|
79
|
+
end
|
80
|
+
|
81
|
+
it "should flush all databases" do
|
82
|
+
@client.select(0)
|
83
|
+
@client.set("key3", "3")
|
84
|
+
@client.set("key4", "4")
|
85
|
+
@client.dbsize.should be == 2
|
86
|
+
|
87
|
+
@client.select(1)
|
88
|
+
@client.set("key3", "3")
|
89
|
+
@client.set("key4", "4")
|
90
|
+
@client.dbsize.should be == 2
|
91
|
+
|
92
|
+
@client.flushall.should be == "OK"
|
93
|
+
|
94
|
+
@client.dbsize.should be == 0
|
95
|
+
@client.select(0)
|
96
|
+
@client.dbsize.should be == 0
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
data/spec/sets_spec.rb
ADDED
@@ -0,0 +1,178 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module RedisFile
|
4
|
+
describe "SetsMethods" do
|
5
|
+
before(:each) do
|
6
|
+
@client = Redis.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should add a member to a set" do
|
10
|
+
@client.sadd("key", "value").should be == true
|
11
|
+
@client.sadd("key", "value").should be == false
|
12
|
+
|
13
|
+
@client.smembers("key").should be == ["value"]
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should raise error if command arguments count is not enough" do
|
17
|
+
expect { @client.sadd("key", []) }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for 'sadd' command")
|
18
|
+
expect { @client.sinter(*[]) }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for 'sinter' command")
|
19
|
+
|
20
|
+
@client.smembers("key").should be_empty
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should add multiple members to a set" do
|
24
|
+
@client.sadd("key", %w(value other something more)).should be == 4
|
25
|
+
@client.sadd("key", %w(and additional values)).should be == 3
|
26
|
+
@client.smembers("key").should =~ ["value", "other", "something", "more", "and", "additional", "values"]
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should get the number of members in a set" do
|
30
|
+
@client.sadd("key", "val1")
|
31
|
+
@client.sadd("key", "val2")
|
32
|
+
|
33
|
+
@client.scard("key").should be == 2
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should subtract multiple sets" do
|
37
|
+
@client.sadd("key1", "a")
|
38
|
+
@client.sadd("key1", "b")
|
39
|
+
@client.sadd("key1", "c")
|
40
|
+
@client.sadd("key1", "d")
|
41
|
+
@client.sadd("key2", "c")
|
42
|
+
@client.sadd("key3", "a")
|
43
|
+
@client.sadd("key3", "c")
|
44
|
+
@client.sadd("key3", "e")
|
45
|
+
|
46
|
+
@client.sdiff("key1", "key2", "key3").should =~ ["b", "d"]
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should subtract multiple sets and store the resulting set in a key" do
|
50
|
+
@client.sadd("key1", "a")
|
51
|
+
@client.sadd("key1", "b")
|
52
|
+
@client.sadd("key1", "c")
|
53
|
+
@client.sadd("key1", "d")
|
54
|
+
@client.sadd("key2", "c")
|
55
|
+
@client.sadd("key3", "a")
|
56
|
+
@client.sadd("key3", "c")
|
57
|
+
@client.sadd("key3", "e")
|
58
|
+
@client.sdiffstore("key", "key1", "key2", "key3")
|
59
|
+
|
60
|
+
@client.smembers("key").should =~ ["b", "d"]
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should intersect multiple sets" do
|
64
|
+
@client.sadd("key1", "a")
|
65
|
+
@client.sadd("key1", "b")
|
66
|
+
@client.sadd("key1", "c")
|
67
|
+
@client.sadd("key1", "d")
|
68
|
+
@client.sadd("key2", "c")
|
69
|
+
@client.sadd("key3", "a")
|
70
|
+
@client.sadd("key3", "c")
|
71
|
+
@client.sadd("key3", "e")
|
72
|
+
|
73
|
+
@client.sinter("key1", "key2", "key3").should be == ["c"]
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should intersect multiple sets and store the resulting set in a key" do
|
77
|
+
@client.sadd("key1", "a")
|
78
|
+
@client.sadd("key1", "b")
|
79
|
+
@client.sadd("key1", "c")
|
80
|
+
@client.sadd("key1", "d")
|
81
|
+
@client.sadd("key2", "c")
|
82
|
+
@client.sadd("key3", "a")
|
83
|
+
@client.sadd("key3", "c")
|
84
|
+
@client.sadd("key3", "e")
|
85
|
+
@client.sinterstore("key", "key1", "key2", "key3")
|
86
|
+
@client.smembers("key").should be == ["c"]
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should determine if a given value is a member of a set" do
|
90
|
+
@client.sadd("key1", "a")
|
91
|
+
|
92
|
+
@client.sismember("key1", "a").should be == true
|
93
|
+
@client.sismember("key1", "b").should be == false
|
94
|
+
@client.sismember("key2", "a").should be == false
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should get all the members in a set" do
|
98
|
+
@client.sadd("key", "a")
|
99
|
+
@client.sadd("key", "b")
|
100
|
+
@client.sadd("key", "c")
|
101
|
+
@client.sadd("key", "d")
|
102
|
+
|
103
|
+
@client.smembers("key").should =~ ["a", "b", "c", "d"]
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should move a member from one set to another" do
|
107
|
+
@client.sadd("key1", "a")
|
108
|
+
@client.sadd("key1", "b")
|
109
|
+
@client.sadd("key2", "c")
|
110
|
+
@client.smove("key1", "key2", "a").should be == true
|
111
|
+
@client.smove("key1", "key2", "a").should be == false
|
112
|
+
|
113
|
+
@client.smembers("key1").should be == ["b"]
|
114
|
+
@client.smembers("key2").should =~ ["c", "a"]
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should remove and return a random member from a set" do
|
118
|
+
@client.sadd("key1", "a")
|
119
|
+
@client.sadd("key1", "b")
|
120
|
+
|
121
|
+
["a", "b"].include?(@client.spop("key1")).should be_true
|
122
|
+
["a", "b"].include?(@client.spop("key1")).should be_true
|
123
|
+
@client.spop("key1").should be_nil
|
124
|
+
end
|
125
|
+
|
126
|
+
it "should get a random member from a set" do
|
127
|
+
@client.sadd("key1", "a")
|
128
|
+
@client.sadd("key1", "b")
|
129
|
+
|
130
|
+
["a", "b"].include?(@client.spop("key1")).should be_true
|
131
|
+
end
|
132
|
+
|
133
|
+
it "should remove a member from a set" do
|
134
|
+
@client.sadd("key1", "a")
|
135
|
+
@client.sadd("key1", "b")
|
136
|
+
@client.srem("key1", "a").should be == true
|
137
|
+
@client.srem("key1", "a").should be == false
|
138
|
+
|
139
|
+
@client.smembers("key1").should be == ["b"]
|
140
|
+
end
|
141
|
+
|
142
|
+
it "should remove the set's key once it's empty" do
|
143
|
+
@client.sadd("key1", "a")
|
144
|
+
@client.sadd("key1", "b")
|
145
|
+
@client.srem("key1", "b")
|
146
|
+
@client.srem("key1", "a")
|
147
|
+
|
148
|
+
@client.exists("key1").should be == false
|
149
|
+
end
|
150
|
+
|
151
|
+
it "should add multiple sets" do
|
152
|
+
@client.sadd("key1", "a")
|
153
|
+
@client.sadd("key1", "b")
|
154
|
+
@client.sadd("key1", "c")
|
155
|
+
@client.sadd("key1", "d")
|
156
|
+
@client.sadd("key2", "c")
|
157
|
+
@client.sadd("key3", "a")
|
158
|
+
@client.sadd("key3", "c")
|
159
|
+
@client.sadd("key3", "e")
|
160
|
+
|
161
|
+
@client.sunion("key1", "key2", "key3").should =~ ["a", "b", "c", "d", "e"]
|
162
|
+
end
|
163
|
+
|
164
|
+
it "should add multiple sets and store the resulting set in a key" do
|
165
|
+
@client.sadd("key1", "a")
|
166
|
+
@client.sadd("key1", "b")
|
167
|
+
@client.sadd("key1", "c")
|
168
|
+
@client.sadd("key1", "d")
|
169
|
+
@client.sadd("key2", "c")
|
170
|
+
@client.sadd("key3", "a")
|
171
|
+
@client.sadd("key3", "c")
|
172
|
+
@client.sadd("key3", "e")
|
173
|
+
@client.sunionstore("key", "key1", "key2", "key3")
|
174
|
+
|
175
|
+
@client.smembers("key").should =~ ["a", "b", "c", "d", "e"]
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|