oxblood 0.1.0.dev8 → 0.1.0.dev9

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,272 @@
1
+ module Oxblood
2
+ module Commands
3
+ module Strings
4
+ # Append a value to a key
5
+ # @see http://redis.io/commands/append
6
+ #
7
+ # @param [String] key
8
+ # @param [String] value
9
+ #
10
+ # @return [Integer] the length of the string after the append operation
11
+ def append(key, value)
12
+ run(:APPEND, key, value)
13
+ end
14
+
15
+ # Count set bits in a string
16
+ # @see http://redis.io/commands/bitcount
17
+ #
18
+ # @param [String] key
19
+ # @param [Array] interval to count in
20
+ #
21
+ # @return [Integer] the number of bits set to 1
22
+ def bitcount(key, *interval)
23
+ run(*interval.unshift(:BITCOUNT, key))
24
+ end
25
+
26
+ # Perform bitwise operations between strings
27
+ # @see http://redis.io/commands/bitop
28
+ #
29
+ # @param [String] operation
30
+ # @param [String] destkey
31
+ # @param [Array] keys
32
+ #
33
+ # @return [Integer] the size of the string stored in the destination key,
34
+ # that is equal to the size of the longest input string
35
+ def bitop(operation, destkey, *keys)
36
+ run(*keys.unshift(:BITOP, operation, destkey))
37
+ end
38
+
39
+ # Find first bit set or clear in a string
40
+ # @see http://redis.io/commands/bitpos
41
+ #
42
+ # @param [String] key
43
+ # @param [Integer] bit
44
+ # @param [Array] interval
45
+ #
46
+ # @return [Integer] the command returns the position of the first bit set to
47
+ # 1 or 0 according to the request
48
+ def bitpos(key, bit, *interval)
49
+ run(*interval.unshift(:BITPOS, key, bit))
50
+ end
51
+
52
+ # Decrement the integer value of a key by one
53
+ # @see http://redis.io/commands/decr
54
+ #
55
+ # @param [String] key
56
+ #
57
+ # @return [Integer] the value of key after the decrement
58
+ # @return [RError] if value is not an integer or out of range
59
+ def decr(key)
60
+ run(:DECR, key)
61
+ end
62
+
63
+ # Decrement the integer value of a key by the given number
64
+ # @see http://redis.io/commands/decrby
65
+ #
66
+ # @param [String] key
67
+ # @param [Integer] decrement
68
+ #
69
+ # @return [Integer] the value of key after the decrement
70
+ # @return [RError] if the key contains a value of the wrong type or contains
71
+ # a string that can not be represented as integer
72
+ def decrby(key, decrement)
73
+ run(:DECRBY, key, decrement)
74
+ end
75
+
76
+ # Get the value of a key
77
+ # @see http://redis.io/commands/get
78
+ #
79
+ # @param [String] key
80
+ #
81
+ # @return [String, nil] the value of key, or nil when key does not exists
82
+ def get(key)
83
+ run(:GET, key)
84
+ end
85
+
86
+ # Returns the bit value at offset in the string value stored at key
87
+ # @see http://redis.io/commands/getbit
88
+ #
89
+ # @param [String] key
90
+ # @param [Integer] offset
91
+ #
92
+ # @return [Integer] the bit value stored at offset
93
+ def getbit(key, offset)
94
+ run(:GETBIT, key, offset)
95
+ end
96
+
97
+ # Get a substring of the string stored at a key
98
+ # @see http://redis.io/commands/getrange
99
+ #
100
+ # @param [String] key
101
+ # @param [Integer] start_pos
102
+ # @param [Integer] end_pos
103
+ #
104
+ # @return [String] substring
105
+ def getrange(key, start_pos, end_pos)
106
+ run(:GETRANGE, key, start_pos, end_pos)
107
+ end
108
+
109
+ # Set the string value of a key and return its old value
110
+ # @see http://redis.io/commands/getset
111
+ #
112
+ # @param [String] key
113
+ # @param [String] value
114
+ #
115
+ # @return [String, nil] the old value stored at key, or nil when
116
+ # key did not exist
117
+ def getset(key, value)
118
+ run(:GETSET, key, value)
119
+ end
120
+
121
+ # Increment the integer value of a key by one
122
+ # @see http://redis.io/commands/incr
123
+ #
124
+ # @param [String] key
125
+ #
126
+ # @return [Integer] the value of key after the increment
127
+ # @return [RError] if the key contains a value of the wrong type or contains
128
+ # a string that can not be represented as integer
129
+ def incr(key)
130
+ run(:INCR, key)
131
+ end
132
+
133
+ # Increment the integer value of a key by the given amount
134
+ # @see http://redis.io/commands/incrby
135
+ #
136
+ # @param [String] key
137
+ # @param [Integer] increment
138
+ #
139
+ # @return [Integer] the value of key after the increment
140
+ def incrby(key, increment)
141
+ run(:INCRBY, key, increment)
142
+ end
143
+
144
+ # Increment the float value of a key by the given amount
145
+ # @see http://redis.io/commands/incrbyfloat
146
+ #
147
+ # @param [String] key
148
+ # @param [Float] increment
149
+ #
150
+ # @return [String] the value of key after the increment
151
+ def incrbyfloat(key, increment)
152
+ run(:INCRBYFLOAT, key, increment)
153
+ end
154
+
155
+ # Get the values of all the given keys
156
+ # @see http://redis.io/commands/mget
157
+ #
158
+ # @param [Array<String>] keys to retrieve
159
+ #
160
+ # @return [Array] list of values at the specified keys
161
+ def mget(*keys)
162
+ run(*keys.unshift(:MGET))
163
+ end
164
+
165
+ # Set multiple keys to multiple values
166
+ # @see http://redis.io/commands/mset
167
+ #
168
+ # @param [Array] keys_and_values
169
+ #
170
+ # @return [String] 'OK'
171
+ def mset(*keys_and_values)
172
+ run(*keys_and_values.unshift(:MSET))
173
+ end
174
+
175
+ # Set multiple keys to multiple values, only if none of the keys exist
176
+ # @see http://redis.io/commands/msetnx
177
+ #
178
+ # @param [Array] keys_and_values
179
+ #
180
+ # @return [Integer] 1 if the all the keys were set, or
181
+ # 0 if no key was set (at least one key already existed)
182
+ def msetnx(*keys_and_values)
183
+ run(*keys_and_values.unshift(:MSETNX))
184
+ end
185
+
186
+ # Set the value and expiration in milliseconds of a key
187
+ # @see http://redis.io/commands/psetex
188
+ #
189
+ # @param [String] key
190
+ # @param [Integer] milliseconds expire time
191
+ # @param [String] value
192
+ #
193
+ # @return [String] 'OK'
194
+ def psetex(key, milliseconds, value)
195
+ run(:PSETEX, key, milliseconds, value)
196
+ end
197
+
198
+ # Set the string value of a key
199
+ # @see http://redis.io/commands/set
200
+ #
201
+ # @todo Add support for set options
202
+ # http://redis.io/commands/set#options
203
+ #
204
+ # @param [String] key
205
+ # @param [String] value
206
+ #
207
+ # @return [String] 'OK' if SET was executed correctly
208
+ def set(key, value)
209
+ run(:SET, key, value)
210
+ end
211
+
212
+ # Set or clear the bit at offset in the string value stored at key
213
+ # @see http://redis.io/commands/setbit
214
+ #
215
+ # @param [String] key
216
+ # @param [Integer] offset
217
+ # @param [String] value
218
+ #
219
+ # @return [Integer] the original bit value stored at offset
220
+ def setbit(key, offset, value)
221
+ run(:SETBIT, key, offset, value)
222
+ end
223
+
224
+ # Set the value and expiration of a key
225
+ # @see http://redis.io/commands/setex
226
+ #
227
+ # @param [String] key
228
+ # @param [Integer] seconds expire time
229
+ # @param [String] value
230
+ #
231
+ # @return [String] 'OK'
232
+ def setex(key, seconds, value)
233
+ run(:SETEX, key, seconds, value)
234
+ end
235
+
236
+ # Set the value of a key, only if the key does not exist
237
+ # @see http://redis.io/commands/setnx
238
+ #
239
+ # @param [String] key
240
+ # @param [String] value
241
+ #
242
+ # @return [Integer] 1 if the key was set, or 0 if the key was not set
243
+ def setnx(key, value)
244
+ run(:SETNX, key, value)
245
+ end
246
+
247
+ # Overwrite part of a string at key starting at the specified offset
248
+ # @see http://redis.io/commands/setrange
249
+ #
250
+ # @param [String] key
251
+ # @param [Integer] offset
252
+ # @param [String] value
253
+ #
254
+ # @return [Integer] the length of the string after it was modified by
255
+ # the command
256
+ def setrange(key, offset, value)
257
+ run(:SETRANGE, key, offset, value)
258
+ end
259
+
260
+ # Get the length of the value stored in a key
261
+ # @see http://redis.io/commands/strlen
262
+ #
263
+ # @param [String] key
264
+ #
265
+ # @return [Integer] the length of the string at key,
266
+ # or 0 when key does not exist
267
+ def strlen(key)
268
+ run(:STRLEN, key)
269
+ end
270
+ end
271
+ end
272
+ end
@@ -0,0 +1,33 @@
1
+ module Oxblood
2
+ module Commands
3
+ module Transactions
4
+ # Mark the start of a transaction block
5
+ # @see http://redis.io/commands/multi
6
+ #
7
+ # @return [String] 'OK'
8
+ # @return [RError] if multi called inside transaction
9
+ def multi
10
+ run(:MULTI)
11
+ end
12
+
13
+ # Execute all commands issued after MULTI
14
+ # @see http://redis.io/commands/exec
15
+ #
16
+ # @return [Array] each element being the reply to each of the commands
17
+ # in the atomic transaction
18
+ # @return [nil] when WATCH was used and execution was aborted
19
+ def exec
20
+ run(:EXEC)
21
+ end
22
+
23
+ # Discard all commands issued after MULTI
24
+ # @see http://redis.io/commands/discard
25
+ #
26
+ # @return [String] 'OK'
27
+ # @return [RError] if called without transaction started
28
+ def discard
29
+ run(:DISCARD)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,23 @@
1
+ require 'oxblood/commands/hashes'
2
+ require 'oxblood/commands/strings'
3
+ require 'oxblood/commands/connection'
4
+ require 'oxblood/commands/server'
5
+ require 'oxblood/commands/keys'
6
+ require 'oxblood/commands/lists'
7
+ require 'oxblood/commands/sets'
8
+ require 'oxblood/commands/sorted_sets'
9
+ require 'oxblood/commands/transactions'
10
+
11
+ module Oxblood
12
+ module Commands
13
+ include Hashes
14
+ include Strings
15
+ include Connection
16
+ include Server
17
+ include Keys
18
+ include Lists
19
+ include Sets
20
+ include SortedSets
21
+ include Transactions
22
+ end
23
+ end
@@ -26,7 +26,7 @@ module Oxblood
26
26
  @socket = RSocket.new(opts)
27
27
 
28
28
  session = Session.new(self)
29
- session.auth!(opts[:password]) if opts[:password]
29
+ session.auth(opts[:password]) if opts[:password]
30
30
  session.select(opts[:db]) if opts[:db]
31
31
  end
32
32
 
@@ -1,7 +1,10 @@
1
- require 'oxblood/session'
1
+ require 'oxblood/protocol'
2
+ require 'oxblood/commands'
2
3
 
3
4
  module Oxblood
4
5
  # Redis pipeling class. Commands won't be send until {#sync} is called.
6
+ # Error responses won't be raises and should be checked manually in the
7
+ # responses array.
5
8
  # @see http://redis.io/topics/pipelining#redis-pipelining
6
9
  #
7
10
  # @example Basic workflow
@@ -10,19 +13,25 @@ module Oxblood
10
13
  # pipeline.ping
11
14
  # pipeline.echo('!')
12
15
  # pipeline.sync # => ["ping", "PONG", "!"]
13
- class Pipeline < Session
16
+ class Pipeline
17
+ include Oxblood::Commands
18
+
14
19
  def initialize(connection)
15
- super
20
+ @connection = connection
16
21
  @commands = Array.new
17
22
  end
18
23
 
19
24
  # Sends all commands at once and reads responses
20
25
  # @return [Array] of responses
21
26
  def sync
22
- serialized_commands = @commands.map { |c| serialize(*c) }
27
+ serialized_commands = @commands.map do |c|
28
+ Oxblood::Protocol.build_command(*c)
29
+ end
23
30
 
24
31
  @connection.socket.write(serialized_commands.join)
25
32
  @connection.read_responses(@commands.size)
33
+ ensure
34
+ @commands.clear
26
35
  end
27
36
 
28
37
  private