oxblood 0.1.0.dev3 → 0.1.0.dev4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2d7a859b01da306481014391452d354b78e733d9
4
- data.tar.gz: b5b6a5ccc51f7b07c562c29b434a85f65b9bd4cd
3
+ metadata.gz: 8a369a489b3733b77b4742e0d35cbaf30f8a794f
4
+ data.tar.gz: 081150bc06a0c6c2d3dd280f7ac0405caabd46a2
5
5
  SHA512:
6
- metadata.gz: c1dffe945b9bb71adec19fc0f33a6847ca90fa325d061f699e7b784ced2aedcc4ec0f7685a17764e94db4f2ca513e830f0233e7a379b89a75584409f08862ce0
7
- data.tar.gz: 53ccf202c3821c309a5d7e4c998280f682f7197d54f9c2f2cb6e7295bd07acb7054a5b4df50086e7b604a91f2911301d79677f416dcd8dd046a2202ec561b42e
6
+ metadata.gz: 86727e0c5bcfb0960204d6bd5e516d21614a9fb992e9d1cfe7c424872c759dc2673e1faafd2f82ca7035f64524f6f46839498ff0811e018045c854bb6e32467c
7
+ data.tar.gz: 24d72c0595245b1f19950acd2f65ae1ce19c32a9eed0b37893e9543bda7fb42b48523bd55511e5c87150260f5926e5a7de9f4502bffdc7152ed2df6945e250e1
@@ -5,13 +5,14 @@ require 'redis/connection/command_helper'
5
5
  CommandHelper = Object.new.tap { |o| o.extend Redis::Connection::CommandHelper }
6
6
 
7
7
  command = [:set, 'foo', ['bar', Float::INFINITY, -Float::INFINITY, 3]]
8
-
9
- p CommandHelper.build_command(command)
10
- p Oxblood::Protocol.build_command(command)
11
- raise unless CommandHelper.build_command(command) == Oxblood::Protocol.build_command(command)
8
+ command_name = :set
9
+ command_args = ['foo', ['bar', Float::INFINITY, -Float::INFINITY, 3]]
10
+ p ch_result = CommandHelper.build_command(command)
11
+ p ox_result = Oxblood::Protocol.build_command(command_name, *command_args)
12
+ raise unless ch_result == ox_result
12
13
 
13
14
  Benchmark.ips do |x|
14
15
  x.config(warmup: 20, benchmark: 10)
15
16
  x.report('redis-ruby') { CommandHelper.build_command(command) }
16
- x.report('Oxblood') { Oxblood::Protocol.build_command(command) }
17
+ x.report('Oxblood') { Oxblood::Protocol.build_command(command_name, *command_args) }
17
18
  end
@@ -63,11 +63,11 @@ module Oxblood
63
63
  end
64
64
 
65
65
  # Send comand to Redis server
66
- # @example send_command(['CONFIG', 'GET', '*']) => 32
66
+ # @example send_command('CONFIG', 'GET', '*') => 32
67
67
  # @param [Array] command Array of command name with it's args
68
68
  # @return [Integer] Number of bytes written to socket
69
- def send_command(command)
70
- write(Protocol.build_command(command))
69
+ def send_command(*command)
70
+ write(Protocol.build_command(*command))
71
71
  end
72
72
 
73
73
  # Write data to socket
@@ -78,10 +78,10 @@ module Oxblood
78
78
  end
79
79
 
80
80
  # Send command to Redis server and read response from it
81
- # @example run_command(['PING']) => PONG
81
+ # @example run_command('PING') => PONG
82
82
  # @param [Array] command Array of command name with it's args
83
- def run_command(command)
84
- send_command(command)
83
+ def run_command(*command)
84
+ send_command(*command)
85
85
  read_response
86
86
  end
87
87
 
@@ -1,5 +1,3 @@
1
- require 'oxblood/command'
2
-
3
1
  module Oxblood
4
2
  class Pipeline < Session
5
3
  def initialize(connection)
@@ -12,7 +10,8 @@ module Oxblood
12
10
  end
13
11
 
14
12
  def sync
15
- @connection.write(@commands.join)
13
+ serialized_commands = @commands.map { |c| serialize(command) }
14
+ @connection.write(serialized_commands.join)
16
15
  @connection.read_responses(@commands.size)
17
16
  end
18
17
  end
@@ -92,13 +92,19 @@ module Oxblood
92
92
  # @note Written in non-idiomatic ruby without error handling due to
93
93
  # performance reasons
94
94
  # @see http://www.redis.io/topics/protocol#sending-commands-to-a-redis-server
95
+ #
95
96
  # @raise [SerializerError] if unable to serialize given command
96
- # @param [Array] command array consisting of redis command and arguments
97
+ #
98
+ # @param [#to_s] command name
99
+ # @param [Array] args array consisting of command arguments
100
+ #
97
101
  # @return [String] serialized command
98
- def build_command(command)
99
- result = COMMAND_HEADER.dup
100
- size = 0
101
- command.each do |c|
102
+ def build_command(command = nil, *args)
103
+ return EMPTY_ARRAY_RESPONSE if command.nil?
104
+
105
+ result = append!(command, COMMAND_HEADER.dup)
106
+ size = 1
107
+ args.each do |c|
102
108
  if Array === c
103
109
  c.each do |e|
104
110
  append!(e, result)
@@ -1,5 +1,3 @@
1
- require 'oxblood/command'
2
-
3
1
  module Oxblood
4
2
  class Session
5
3
  def initialize(connection)
@@ -18,7 +16,7 @@ module Oxblood
18
16
  #
19
17
  # @return [Integer] the number of fields that were removed from the hash
20
18
  def hdel(key, fields)
21
- run(cmd.hdel(key, fields))
19
+ run(:HDEL, key, fields)
22
20
  end
23
21
 
24
22
  # Returns if field is an existing field in the hash stored at key
@@ -29,7 +27,7 @@ module Oxblood
29
27
  #
30
28
  # @return [Boolean] do hash contains field or not
31
29
  def hexists(key, field)
32
- 1 == run(cmd.hexists(key, field))
30
+ 1 == run(:HEXISTS, key, field)
33
31
  end
34
32
 
35
33
  # Get the value of a hash field
@@ -41,7 +39,7 @@ module Oxblood
41
39
  # @return [String, nil] the value associated with field
42
40
  # or nil when field is not present in the hash or key does not exist.
43
41
  def hget(key, field)
44
- run(cmd.hget(key, field))
42
+ run(:HGET, key, field)
45
43
  end
46
44
 
47
45
  # Get all the fields and values in a hash
@@ -51,7 +49,7 @@ module Oxblood
51
49
  #
52
50
  # @return [Hash] of fields and their values
53
51
  def hgetall(key)
54
- Hash[*run(cmd.hgetall(key))]
52
+ Hash[*run(:HGETALL, key)]
55
53
  end
56
54
 
57
55
  # Increment the integer value of a hash field by the given number
@@ -63,7 +61,7 @@ module Oxblood
63
61
  #
64
62
  # @return [Integer] the value at field after the increment operation
65
63
  def hincrby(key, field, increment)
66
- run(cmd.hincrby(key, field, increment))
64
+ run(:HINCRBY, key, field, increment)
67
65
  end
68
66
 
69
67
  # Increment the float value of a hash field by the given number
@@ -75,7 +73,7 @@ module Oxblood
75
73
  #
76
74
  # @return [String] the value of field after the increment
77
75
  def hincrbyfloat(key, field, increment)
78
- run(cmd.hincrbyfloat(key, field, increment))
76
+ run(:HINCRBYFLOAT, key, field, increment)
79
77
  end
80
78
 
81
79
  # Get all the keys in a hash
@@ -86,7 +84,7 @@ module Oxblood
86
84
  # @return [Array] list of fields in the hash, or an empty list when
87
85
  # key does not exist.
88
86
  def hkeys(key)
89
- run(cmd.hkeys(key))
87
+ run(:HKEYS, key)
90
88
  end
91
89
 
92
90
  # Get the number of keys in a hash
@@ -97,7 +95,7 @@ module Oxblood
97
95
  # @return [Integer] number of fields in the hash, or 0 when
98
96
  # key does not exist.
99
97
  def hlen(key)
100
- run(cmd.hlen(key))
98
+ run(:HLEN, key)
101
99
  end
102
100
 
103
101
  # Get the field values of all given hash fields
@@ -109,7 +107,7 @@ module Oxblood
109
107
  # @return [Array] list of values associated with the given fields,
110
108
  # in the same order as they are requested.
111
109
  def hmget(key, *fields)
112
- run(cmd.hmget(key, *fields))
110
+ run(*fields.unshift(:HMGET, key))
113
111
  end
114
112
 
115
113
  # Set multiple hash fields to multiple values
@@ -120,7 +118,7 @@ module Oxblood
120
118
  #
121
119
  # @return [String] 'OK'
122
120
  def hmset(key, *args)
123
- run(cmd.hmset(key, *args))
121
+ run(*args.unshift(:HMSET, key))
124
122
  end
125
123
 
126
124
 
@@ -134,7 +132,7 @@ module Oxblood
134
132
  # @return [Integer] 1 if field is a new field in the hash and value was set.
135
133
  # 0 if field already exists in the hash and the value was updated.
136
134
  def hset(key, field, value)
137
- run(cmd.hset(key, field, value))
135
+ run(:HSET, key, field, value)
138
136
  end
139
137
 
140
138
  # Set the value of a hash field, only if the field does not exist
@@ -147,7 +145,7 @@ module Oxblood
147
145
  # @return [Integer] 1 if field is a new field in the hash and value was set.
148
146
  # 0 if field already exists in the hash and no operation was performed.
149
147
  def hsetnx(key, field, value)
150
- run(cmd.hsetnx(key, field, value))
148
+ run(:HSETNX, key, field, value)
151
149
  end
152
150
 
153
151
  # Get the length of the value of a hash field
@@ -159,7 +157,7 @@ module Oxblood
159
157
  # @return [Integer] the string length of the value associated with field,
160
158
  # or 0 when field is not present in the hash or key does not exist at all.
161
159
  def hstrlen(key, field)
162
- run(cmd.hstrlen(key, field))
160
+ run(:HSTRLEN, key, field)
163
161
  end
164
162
 
165
163
  # Get all values in a hash
@@ -170,7 +168,7 @@ module Oxblood
170
168
  # @return [Array] list of values in the hash, or an empty list when
171
169
  # key does not exist
172
170
  def hvals(key)
173
- run(cmd.hvals(key))
171
+ run(:HVALS, key)
174
172
  end
175
173
 
176
174
  # Incrementally iterate hash fields and associated values
@@ -191,7 +189,7 @@ module Oxblood
191
189
  #
192
190
  # @return [String] 'OK'
193
191
  def auth(password)
194
- run(cmd.auth(password))
192
+ run(:AUTH, password)
195
193
  end
196
194
 
197
195
  # Like {#auth}, except that if error returned, raises it.
@@ -214,7 +212,7 @@ module Oxblood
214
212
  #
215
213
  # @return [String] message passed as argument
216
214
  def ping(message = nil)
217
- run(cmd.ping(message))
215
+ message ? run(:PING, message) : run(:PING)
218
216
  end
219
217
 
220
218
  # Change the selected database for the current connection
@@ -224,7 +222,7 @@ module Oxblood
224
222
  #
225
223
  # @return [String] 'OK'
226
224
  def select(index)
227
- run(cmd.select(index))
225
+ run(:SELECT, index)
228
226
  end
229
227
 
230
228
  # ------------------ Server ---------------------
@@ -235,7 +233,7 @@ module Oxblood
235
233
  #
236
234
  # @param [String] section used to select a specific section of information
237
235
  def info(section = nil)
238
- run(cmd.info(section))
236
+ section ? run(:INFO, section) : run(:INFO)
239
237
  # FIXME: Parse response
240
238
  end
241
239
 
@@ -248,7 +246,7 @@ module Oxblood
248
246
  #
249
247
  # @return [Integer] the number of keys that were removed
250
248
  def del(*keys)
251
- run(cmd.del(*keys))
249
+ run(*keys.unshift(:DEL))
252
250
  end
253
251
 
254
252
  # Find all keys matching the given pattern
@@ -256,7 +254,7 @@ module Oxblood
256
254
  #
257
255
  # @param [String] pattern used to match keys
258
256
  def keys(pattern)
259
- run(cmd.keys(pattern))
257
+ run(:KEYS, pattern)
260
258
  end
261
259
 
262
260
  # Set a key's time to live in seconds
@@ -268,7 +266,7 @@ module Oxblood
268
266
  # @return [Integer] 1 if the timeout was set. 0 if key does not exist or
269
267
  # the timeout could not be set.
270
268
  def expire(key, seconds)
271
- run(cmd.expire(key, seconds))
269
+ run(:EXPIRE, key, seconds)
272
270
  end
273
271
 
274
272
  # ------------------ Sets ------------------------
@@ -282,7 +280,7 @@ module Oxblood
282
280
  # @return [Integer] the number of elements that were added to the set,
283
281
  # not including all the elements already present into the set.
284
282
  def sadd(key, *members)
285
- run(cmd.sadd(key, *members))
283
+ run(*members.unshift(:SADD, key))
286
284
  end
287
285
 
288
286
  # Add multiple sets
@@ -292,7 +290,7 @@ module Oxblood
292
290
  #
293
291
  # @return [Array] list with members of the resulting set
294
292
  def sunion(*keys)
295
- run(cmd.sunion(*keys))
293
+ run(*keys.unshift(:SUNION))
296
294
  end
297
295
 
298
296
  # ------------------ Sorted Sets -----------------
@@ -307,7 +305,7 @@ module Oxblood
307
305
  # @param [String] key under which store set
308
306
  # @param [[Float, String], Array<[Float, String]>] args scores and members
309
307
  def zadd(key, *args)
310
- run(cmd.zadd(key, *args))
308
+ run(*args.unshift(:ZADD, key))
311
309
  end
312
310
 
313
311
  # Return a range of members in a sorted set, by score
@@ -319,17 +317,17 @@ module Oxblood
319
317
  # @param [String] min value
320
318
  # @param [String] max value
321
319
  def zrangebyscore(key, min, max)
322
- run(cmd.zrangebyscore(key, min, max))
320
+ run(:ZRANGEBYSCORE, key, min, max)
323
321
  end
324
322
 
325
323
  protected
326
324
 
327
- def cmd
328
- Command
325
+ def serialize(*command)
326
+ Protocol.build_command(*command)
329
327
  end
330
328
 
331
- def run(command)
332
- @connection.write(command)
329
+ def run(*command)
330
+ @connection.write(serialize(*command))
333
331
  @connection.read_response
334
332
  end
335
333
 
@@ -1,3 +1,3 @@
1
1
  module Oxblood
2
- VERSION = '0.1.0.dev3'
2
+ VERSION = '0.1.0.dev4'
3
3
  end
@@ -6,8 +6,11 @@ class Redis
6
6
  module Connection
7
7
  class Oxblood
8
8
  def self.connect(config)
9
- conn_type = config[:scheme] == 'unix' ? :unix : :tcp
10
- connection = ::Oxblood::Connection.public_send(:"connect_#{conn_type}", config)
9
+ unless config[:path]
10
+ config = config.dup
11
+ config.delete(:path)
12
+ end
13
+ connection = ::Oxblood::Connection.open(config)
11
14
 
12
15
  new(connection)
13
16
  end
@@ -29,7 +32,7 @@ class Redis
29
32
  end
30
33
 
31
34
  def write(command)
32
- @connection.send_command(command)
35
+ @connection.send_command(*command)
33
36
  end
34
37
 
35
38
  def read
@@ -39,6 +42,8 @@ class Redis
39
42
  reply
40
43
  rescue ::Oxblood::Protocol::ParserError => e
41
44
  raise Redis::ProtocolError.new(e.message)
45
+ rescue ::Oxblood::Connection::TimeoutError => e
46
+ raise Redis::TimeoutError.new(e.message)
42
47
  end
43
48
 
44
49
  if defined?(Encoding::default_external)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oxblood
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.dev3
4
+ version: 0.1.0.dev4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Konstantin Shabanov
@@ -114,7 +114,6 @@ files:
114
114
  - benchmarks/serializer.rb
115
115
  - lib/oxblood.rb
116
116
  - lib/oxblood/buffered_io.rb
117
- - lib/oxblood/command.rb
118
117
  - lib/oxblood/connection.rb
119
118
  - lib/oxblood/pipeline.rb
120
119
  - lib/oxblood/pool.rb
@@ -1,138 +0,0 @@
1
- module Oxblood
2
- module Command
3
- class << self
4
- #
5
- # Hashes
6
- #
7
-
8
- def hdel(key, fields)
9
- serialize([:HDEL, key, fields])
10
- end
11
-
12
- def hexists(key, field)
13
- serialize([:HEXISTS, key, field])
14
- end
15
-
16
- def hget(key, field)
17
- serialize([:HGET, key, field])
18
- end
19
-
20
- def hgetall(key)
21
- serialize([:HGETALL, key])
22
- end
23
-
24
- def hincrby(key, field, increment)
25
- serialize([:HINCRBY, key, field, increment])
26
- end
27
-
28
- def hincrbyfloat(key, field, increment)
29
- serialize([:HINCRBYFLOAT, key, field, increment])
30
- end
31
-
32
- def hkeys(key)
33
- serialize([:HKEYS, key])
34
- end
35
-
36
- def hlen(key)
37
- serialize([:HLEN, key])
38
- end
39
-
40
- def hmget(key, *fields)
41
- serialize(fields.unshift(:HMGET, key))
42
- end
43
-
44
- def hmset(key, *args)
45
- serialize(args.unshift(:HMSET, key))
46
- end
47
-
48
- def hset(key, field, value)
49
- serialize([:HSET, key, field, value])
50
- end
51
-
52
- def hsetnx(key, field, value)
53
- serialize([:HSETNX, key, field, value])
54
- end
55
-
56
- def hstrlen(key, field)
57
- serialize([:HSTRLEN, key, field])
58
- end
59
-
60
- def hvals(key)
61
- serialize([:HVALS, key])
62
- end
63
-
64
- def hscan(_key, _cursor)
65
- raise 'Not implemented!'
66
- end
67
-
68
- # ------------------ Strings ---------------------
69
-
70
- # ------------------ Connection ---------------------
71
-
72
- def auth(password)
73
- serialize([:AUTH, password])
74
- end
75
-
76
- def ping(message = nil)
77
- command = [:PING]
78
- command << message if message
79
-
80
- serialize(command)
81
- end
82
-
83
- def select(index)
84
- serialize([:SELECT, index])
85
- end
86
-
87
- # ------------------ Server ---------------------
88
-
89
- def info(section = nil)
90
- command = [:INFO]
91
- command << section if section
92
-
93
- serialize(command)
94
- end
95
-
96
- # ------------------ Keys ------------------------
97
-
98
- def del(*keys)
99
- serialize(keys.unshift(:DEL))
100
- end
101
-
102
- def keys(pattern)
103
- serialize([:KEYS, pattern])
104
- end
105
-
106
- def expire(key, seconds)
107
- serialize([:EXPIRE, key, seconds])
108
- end
109
-
110
- # ------------------ Sets ------------------------
111
-
112
- def sadd(key, *members)
113
- serialize(members.unshift(:SADD, key))
114
- end
115
-
116
- def sunion(*keys)
117
- serialize(keys.unshift(:SUNION))
118
- end
119
-
120
- # ------------------ Sorted Sets -----------------
121
-
122
- def zadd(key, *args)
123
- serialize(args.unshift(:ZADD, key))
124
- end
125
-
126
- # @todo Support optional args (WITHSCORES/LIMIT)
127
- def zrangebyscore(key, min, max)
128
- serialize([:ZRANGEBYSCORE, key, min, max])
129
- end
130
-
131
- private
132
-
133
- def serialize(command)
134
- Protocol.build_command(command)
135
- end
136
- end
137
- end
138
- end