Class: RedisProtocol::UnifiedProtocol

Inherits:
Object
  • Object
show all
Defined in:
lib/redis-protocol/unified_protocol.rb

Constant Summary collapse

DELIMITER =
"\r\n"

Class Method Summary collapse

Class Method Details

.parse(data) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/redis-protocol/unified_protocol.rb', line 4

def parse(data)
  processed, index = 0, data.index(DELIMITER)
  index ||= data.length
  result = case data[processed]
             when '*'
               parse_multi_chunked data
             when '$'
               parse_chunked data
             when '+'
               parse_status data
             when '-'
               parse_error data
             when ':'
               parse_integer data
           end
end

.parse_chunked(data, start = 0) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/redis-protocol/unified_protocol.rb', line 35

def parse_chunked(data, start=0)
  index = data.index DELIMITER, start
  length = data[(start+1)...index].to_i
  return nil if length == -1
  result = [data[index+DELIMITER.length, length]]

  start == 0 ? result : [result, index+DELIMITER.length + length]
end

.parse_error(data) ⇒ Object



48
49
50
# File 'lib/redis-protocol/unified_protocol.rb', line 48

def parse_error(data)
  [false, data[1..-1]]
end

.parse_integer(data) ⇒ Object



52
53
54
# File 'lib/redis-protocol/unified_protocol.rb', line 52

def parse_integer(data)
  [data[1..-1].to_i]
end

.parse_multi_chunked(data) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/redis-protocol/unified_protocol.rb', line 21

def parse_multi_chunked(data)
  index = data.index DELIMITER
  count = data[1...index].to_i
  result = []
  start = index + DELIMITER.length
  1.upto(count) do |_|
    chunk, length = parse_chunked data, start
    start = length + DELIMITER.length
    result += chunk
  end

  result
end

.parse_status(data) ⇒ Object



44
45
46
# File 'lib/redis-protocol/unified_protocol.rb', line 44

def parse_status(data)
  [true, data[1..-1]]
end