redislike 0.2.0 → 0.2.4

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: be9073ce31c76bc2fe4b3395ea7118953d792c7b
4
- data.tar.gz: 83f2fb7b888471aac831d9bed7ec2ac0a040c00e
3
+ metadata.gz: eb04d4b6e5e86b980b69f5798f5f615104c117c9
4
+ data.tar.gz: 8e59980011755be17d0a2d87913ba59dfc6736f9
5
5
  SHA512:
6
- metadata.gz: 5f208e78072393e3f78831956a921fec73994e6f1761b1043c7ae6d47d272e9d8265e568ecffb368370fa36deb6b3ca161eb7eeb990a54df4daa23a94c3cf89f
7
- data.tar.gz: 63531c739bd0928a9e8e201147817b80014822b88a8c39e7c91bca6a5f9cc9e46247bc6de15dc10ecc1f2feb4aee4086c1f5c2bf99bcd32013c28e7260dd0f3d
6
+ metadata.gz: 948a85049408f51cea11c1e428ca0c00aa242bb2f54dc5348e83cc375b6cb9597c9a448c2dec9a8ccd0a6ef0254fb9552a602a34fe3891e6a23eb48bda0a63e5
7
+ data.tar.gz: 668fee9ae35e5273bf5d907dfdf2c28deda1e15ef97740b9bd88400b5a6a32c32f5a439218c5faea533e3299ea8b003839ef38e74644b57080c54c570d558c4e
data/README.md CHANGED
@@ -7,6 +7,9 @@ redislike adds backend-independent support for redis-like list operations to any
7
7
 
8
8
  Supported Operations
9
9
  --------------------
10
+ * BLPOP
11
+ * BRPOP
12
+ * BRPOPLPUSH
10
13
  * LINDEX
11
14
  * LINSERT
12
15
  * LLEN
@@ -22,16 +25,15 @@ Supported Operations
22
25
  * RPUSH
23
26
  * RPUSHX
24
27
 
25
- Missing
26
- -------
27
- * BLPOP
28
- * BRPOP
29
- * BRPOPLPUSH
30
-
31
28
  Other Supported Operations
32
29
  --------------------------
33
30
  * EXISTS
34
31
 
32
+ Differences from Redis
33
+ ----------------------
34
+ * LSET with a non-existent list raises a ```KeyError``` rather than a ```Redis::CommandError```.
35
+ * LSET with an out of range index raises an ```IndexError``` rather than a ```Redis::CommandError```.
36
+
35
37
  Installation
36
38
  ------------
37
39
 
@@ -35,6 +35,10 @@ module RedisLike
35
35
  # store list, remaining
36
36
  # items.length - remaining.length
37
37
  end
38
+
39
+ def block_while_empty(list, interval = 0.010)
40
+ sleep interval until llen(list) > 0
41
+ end
38
42
  end
39
43
  end
40
44
  end
@@ -24,6 +24,11 @@ module RedisLike
24
24
  enqueue list, item, to: :tail, allow_create: false
25
25
  end
26
26
 
27
+ def blpop(list)
28
+ block_while_empty list
29
+ lpop list
30
+ end
31
+
27
32
  def lpop(list)
28
33
  dequeue list, from: :head
29
34
  end
@@ -32,12 +37,22 @@ module RedisLike
32
37
  dequeue list, from: :tail
33
38
  end
34
39
 
40
+ def brpop(list)
41
+ block_while_empty list
42
+ rpop list
43
+ end
44
+
35
45
  def rpoplpush(source, destination)
36
46
  item = rpop source
37
47
  lpush destination, item
38
48
  item
39
49
  end
40
50
 
51
+ def brpoplpush(source, destination)
52
+ block_while_empty source
53
+ brpoplpush source, destination
54
+ end
55
+
41
56
  def lindex(list, item)
42
57
  fetch(list, []).at item
43
58
  end
data/redislike.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = 'redislike'
3
- gem.version = '0.2.0'
3
+ gem.version = '0.2.4'
4
4
  gem.licenses = 'MIT'
5
5
  gem.authors = ['Chris Olstrom']
6
6
  gem.email = 'chris@olstrom.com'
@@ -0,0 +1,37 @@
1
+ require 'timeout'
2
+
3
+ context 'BLPOP' do
4
+ setup do
5
+ @db = Moneta.new :Memory
6
+ end
7
+
8
+ given 'an empty list' do
9
+ should 'block' do
10
+ assert_raises Timeout::Error do
11
+ Timeout.timeout(0.1) { @db.blpop 'test:nx' }
12
+ end
13
+ end
14
+ end
15
+
16
+ given 'a list with two items in it' do
17
+ setup do
18
+ @db.lpush 'test:blpop', 'foo'
19
+ @db.lpush 'test:blpop', 'bar'
20
+ end
21
+
22
+ should 'return items from the head of the list' do
23
+ values = 2.times.map { @db.blpop 'test:blpop' }
24
+ assert_equal ['bar', 'foo'], values
25
+ end
26
+
27
+ should 'block when there are no items in the list' do
28
+ 2.times { @db.blpop 'test:blpop' }
29
+
30
+ assert_raises Timeout::Error do
31
+ Timeout.timeout(0.1) { @db.blpop 'test:blpop' }
32
+ end
33
+ end
34
+ end
35
+
36
+ teardown { @db.close }
37
+ end
@@ -0,0 +1,37 @@
1
+ require 'timeout'
2
+
3
+ context 'BRPOP' do
4
+ setup do
5
+ @db = Moneta.new :Memory
6
+ end
7
+
8
+ given 'an empty list' do
9
+ should 'block' do
10
+ assert_raises Timeout::Error do
11
+ Timeout.timeout(0.1) { @db.brpop 'test:nx' }
12
+ end
13
+ end
14
+ end
15
+
16
+ given 'a list with two items in it' do
17
+ setup do
18
+ @db.lpush 'test:brpop', 'foo'
19
+ @db.lpush 'test:brpop', 'bar'
20
+ end
21
+
22
+ should 'return items from the head of the list' do
23
+ values = 2.times.map { @db.brpop 'test:brpop' }
24
+ assert_equal ['foo', 'bar'], values
25
+ end
26
+
27
+ should 'block when there are no items in the list' do
28
+ 2.times { @db.brpop 'test:brpop' }
29
+
30
+ assert_raises Timeout::Error do
31
+ Timeout.timeout(0.1) { @db.brpop 'test:brpop' }
32
+ end
33
+ end
34
+ end
35
+
36
+ teardown { @db.close }
37
+ end
@@ -0,0 +1,17 @@
1
+ require 'timeout'
2
+
3
+ context 'BRPOPLPUSH' do
4
+ setup do
5
+ @db = Moneta.new :Memory
6
+ end
7
+
8
+ given 'an empty list' do
9
+ should 'block' do
10
+ assert_raises Timeout::Error do
11
+ Timeout.timeout(0.1) { @db.brpoplpush 'test:nx', 'test:rpoplpush:b' }
12
+ end
13
+ end
14
+ end
15
+
16
+ teardown { @db.close }
17
+ end
@@ -0,0 +1,73 @@
1
+ context 'LREM' do
2
+ setup do
3
+ @db = Moneta.new :Memory
4
+ end
5
+
6
+ given 'an empty list' do
7
+ should 'return no items removed' do
8
+ assert_equal 0, @db.lrem('test:nx', 0, 'foo')
9
+ end
10
+ end
11
+
12
+ given 'a list with 6 items' do
13
+ setup do
14
+ 3.times { @db.lpush 'test:lrem', 'foo' }
15
+ 3.times { @db.lpush 'test:lrem', 'bar' }
16
+ end
17
+
18
+ context 'when the target is present 3 times' do
19
+ [1, -1].each do |multiplier|
20
+ direction = multiplier > 0 ? 'head' : 'tail'
21
+ context "when removing 9 times from #{direction}" do
22
+ should 'remove 3 items from' do
23
+ assert_equal 3, @db.lrem('test:lrem', 9 * multiplier, 'foo')
24
+ end
25
+
26
+ should 'not remove other elements' do
27
+ @db.lrem('test:lrem', 9 * multiplier, 'foo')
28
+ assert_equal 3, @db.lrange('test:lrem', 0, -1).count('bar')
29
+ end
30
+
31
+ should 'not contain the target anymore' do
32
+ @db.lrem('test:lrem', 9 * multiplier, 'foo')
33
+ assert !@db.lrange('test:lrem', 0, -1).include?('foo')
34
+ end
35
+ end
36
+
37
+ context "when removing 2 times from #{direction}" do
38
+ should 'remove 2 items' do
39
+ assert_equal 2, @db.lrem('test:lrem', 2 * multiplier, 'foo')
40
+ end
41
+
42
+ should 'not remove other elements' do
43
+ @db.lrem('test:lrem', 2 * multiplier, 'foo')
44
+ assert_equal 3, @db.lrange('test:lrem', 0, -1).count('bar')
45
+ end
46
+
47
+ should 'have one of the target remaining' do
48
+ @db.lrem('test:lrem', 2 * multiplier, 'foo')
49
+ assert_equal 1, @db.lrange('test:lrem', 0, -1).count('foo')
50
+ end
51
+ end
52
+
53
+ context "when removing all from #{direction}" do
54
+ should 'remove 3 items' do
55
+ assert_equal 3, @db.lrem('test:lrem', 0 * multiplier, 'foo')
56
+ end
57
+
58
+ should 'not remove other elements' do
59
+ @db.lrem('test:lrem', 0 * multiplier, 'foo')
60
+ assert_equal 3, @db.lrange('test:lrem', 0, -1).count('bar')
61
+ end
62
+
63
+ should 'not contain the target anymore' do
64
+ @db.lrem('test:lrem', 0 * multiplier, 'foo')
65
+ assert !@db.lrange('test:lrem', 0, -1).include?('foo')
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+
72
+ teardown { @db.close }
73
+ end
@@ -0,0 +1,51 @@
1
+ context 'LTRIM' do
2
+ setup do
3
+ @db = Moneta.new :Memory
4
+ end
5
+
6
+ given 'an empty list' do
7
+ should 'return success' do
8
+ assert_equal 'OK', @db.ltrim('test:nx', 0, 99)
9
+ end
10
+ end
11
+
12
+ given 'a list with 100 items' do
13
+ setup do
14
+ 50.times { @db.lpush 'test:ltrim', 'foo' }
15
+ 50.times { @db.lpush 'test:ltrim', 'bar' }
16
+ end
17
+
18
+ context 'stop beyond list length' do
19
+ should 'not truncate the list' do
20
+ assert_equal 'OK', @db.ltrim('test:ltrim', 0, 200)
21
+ assert_equal 100, @db.lrange('test:ltrim', 0, -1).length
22
+ end
23
+ end
24
+
25
+ context 'when stop is positive' do
26
+ setup { @db.ltrim('test:ltrim', 0, 49) }
27
+
28
+ should 'contain items within the range' do
29
+ assert @db.lrange('test:ltrim', 0, -1).include?('bar')
30
+ end
31
+
32
+ should 'not contain items outside the range' do
33
+ assert !@db.lrange('test:ltrim', 0, -1).include?('foo')
34
+ end
35
+ end
36
+
37
+ context 'when stop is negative' do
38
+ setup { @db.ltrim('test:ltrim', -50, -1) }
39
+
40
+ should 'contain items within the range' do
41
+ assert !@db.lrange('test:ltrim', 0, -1).include?('bar')
42
+ end
43
+
44
+ should 'not contain items outside the range' do
45
+ assert @db.lrange('test:ltrim', 0, -1).include?('foo')
46
+ end
47
+ end
48
+ end
49
+
50
+ teardown { @db.close }
51
+ end
@@ -1,3 +1,6 @@
1
+ require_relative 'lists/blpop'
2
+ require_relative 'lists/brpop'
3
+ require_relative 'lists/brpoplpush'
1
4
  require_relative 'lists/lindex'
2
5
  require_relative 'lists/linsert'
3
6
  require_relative 'lists/llen'
@@ -5,7 +8,9 @@ require_relative 'lists/lpop'
5
8
  require_relative 'lists/lpush'
6
9
  require_relative 'lists/lpushx'
7
10
  require_relative 'lists/lrange'
11
+ require_relative 'lists/lrem'
8
12
  require_relative 'lists/lset'
13
+ require_relative 'lists/ltrim'
9
14
  require_relative 'lists/rpop'
10
15
  require_relative 'lists/rpoplpush'
11
16
  require_relative 'lists/rpush'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redislike
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Olstrom
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-07 00:00:00.000000000 Z
11
+ date: 2015-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: moneta
@@ -102,6 +102,9 @@ files:
102
102
  - tests/compatibility/redislike/lists/rpushx.rb
103
103
  - tests/redislike.rb
104
104
  - tests/redislike/lists.rb
105
+ - tests/redislike/lists/blpop.rb
106
+ - tests/redislike/lists/brpop.rb
107
+ - tests/redislike/lists/brpoplpush.rb
105
108
  - tests/redislike/lists/lindex.rb
106
109
  - tests/redislike/lists/linsert.rb
107
110
  - tests/redislike/lists/llen.rb
@@ -109,7 +112,9 @@ files:
109
112
  - tests/redislike/lists/lpush.rb
110
113
  - tests/redislike/lists/lpushx.rb
111
114
  - tests/redislike/lists/lrange.rb
115
+ - tests/redislike/lists/lrem.rb
112
116
  - tests/redislike/lists/lset.rb
117
+ - tests/redislike/lists/ltrim.rb
113
118
  - tests/redislike/lists/rpop.rb
114
119
  - tests/redislike/lists/rpoplpush.rb
115
120
  - tests/redislike/lists/rpush.rb