ruby-redis 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +9 -0
- data/README +90 -0
- data/bin/ruby-redis +12 -0
- data/bin/ruby-redis.compiled.rbc +243 -0
- data/lib/redis/bin.rb +74 -0
- data/lib/redis/bin.rbc +1325 -0
- data/lib/redis/buftok.rbc +2658 -0
- data/lib/redis/config.rb +46 -0
- data/lib/redis/config.rbc +1009 -0
- data/lib/redis/connection.rb +69 -0
- data/lib/redis/connection.rbc +1354 -0
- data/lib/redis/database.rb +109 -0
- data/lib/redis/database.rbc +2275 -0
- data/lib/redis/hashes.rb +72 -0
- data/lib/redis/hashes.rbc +1843 -0
- data/lib/redis/hiredis.rbc +658 -0
- data/lib/redis/keys.rb +165 -0
- data/lib/redis/keys.rbc +3386 -0
- data/lib/redis/lists.rb +227 -0
- data/lib/redis/lists.rbc +5241 -0
- data/lib/redis/logger.rb +81 -0
- data/lib/redis/logger.rbc +2106 -0
- data/lib/redis/protocol.rb +170 -0
- data/lib/redis/protocol.rbc +3735 -0
- data/lib/redis/pubsub.rb +153 -0
- data/lib/redis/pubsub.rbc +3447 -0
- data/lib/redis/reader.rb +164 -0
- data/lib/redis/reader.rbc +2769 -0
- data/lib/redis/send.rbc +1268 -0
- data/lib/redis/sender.rb +49 -0
- data/lib/redis/sender.rbc +1057 -0
- data/lib/redis/server.rb +62 -0
- data/lib/redis/server.rbc +1177 -0
- data/lib/redis/sets.rb +105 -0
- data/lib/redis/sets.rbc +2800 -0
- data/lib/redis/strict.rb +67 -0
- data/lib/redis/strict.rbc +1419 -0
- data/lib/redis/strings.rb +144 -0
- data/lib/redis/strings.rbc +3338 -0
- data/lib/redis/synchrony.rb +58 -0
- data/lib/redis/synchrony.rbc +1397 -0
- data/lib/redis/version.rb +7 -0
- data/lib/redis/version.rbc +180 -0
- data/lib/redis/zsets.rb +281 -0
- data/lib/redis/zsets.rbc +6596 -0
- data/lib/redis.rb +215 -0
- data/lib/redis.rbc +4391 -0
- metadata +117 -0
data/lib/redis/reader.rb
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
require File.expand_path '../redis', File.dirname(__FILE__)
|
2
|
+
|
3
|
+
class Redis
|
4
|
+
|
5
|
+
# This is almost as fast as hiredis/reader plus it supports servers
|
6
|
+
class Reader < Array
|
7
|
+
|
8
|
+
# Minimize the amount of memory copying. The primary
|
9
|
+
# performance trick is to String#split and work with that.
|
10
|
+
|
11
|
+
#TODO configurable limits
|
12
|
+
#TODO max buffer size based on limits
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
super()
|
16
|
+
flush
|
17
|
+
end
|
18
|
+
|
19
|
+
alias :feed :push
|
20
|
+
|
21
|
+
def gets
|
22
|
+
if @completed.empty?
|
23
|
+
unshift_split if @split
|
24
|
+
frame do |str|
|
25
|
+
@elements << str
|
26
|
+
if @remaining > 0
|
27
|
+
@remaining -= 1
|
28
|
+
if @remaining == 0 and !@stack.empty?
|
29
|
+
elements = @elements
|
30
|
+
@elements, @remaining = @stack.pop
|
31
|
+
@elements << elements
|
32
|
+
@remaining -= 1
|
33
|
+
end
|
34
|
+
next unless @remaining == 0
|
35
|
+
@completed << @elements
|
36
|
+
elsif @remaining < 0
|
37
|
+
@completed << nil
|
38
|
+
elsif !@elements.empty?
|
39
|
+
@completed << @elements[0]
|
40
|
+
end
|
41
|
+
@elements = []
|
42
|
+
@remaining = 0
|
43
|
+
end
|
44
|
+
end
|
45
|
+
return false if @completed.empty?
|
46
|
+
@completed.shift
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def flush
|
52
|
+
@split = nil
|
53
|
+
@pending = nil
|
54
|
+
@binary_size = nil
|
55
|
+
@remaining = 0
|
56
|
+
@elements = []
|
57
|
+
@stack = []
|
58
|
+
@completed = []
|
59
|
+
clear
|
60
|
+
end
|
61
|
+
|
62
|
+
def unshift_split
|
63
|
+
unshift @split.join "\n"
|
64
|
+
@split = nil
|
65
|
+
end
|
66
|
+
|
67
|
+
# yields redis data until no more found in buffer
|
68
|
+
def frame
|
69
|
+
while true
|
70
|
+
if @binary_size
|
71
|
+
s = read_binary @binary_size
|
72
|
+
break unless s
|
73
|
+
@binary_size = nil
|
74
|
+
yield s
|
75
|
+
else
|
76
|
+
line = read_line
|
77
|
+
break unless line
|
78
|
+
case line[0..0]
|
79
|
+
when '-'
|
80
|
+
yield RuntimeError.new line[1..-1]
|
81
|
+
when '+'
|
82
|
+
yield line[1..-1]
|
83
|
+
when ':'
|
84
|
+
yield line[1..-1].to_i
|
85
|
+
when '*'
|
86
|
+
prev_remaining = @remaining
|
87
|
+
@remaining = line[1..-1].to_i
|
88
|
+
if @remaining == -1
|
89
|
+
yield nil
|
90
|
+
elsif @remaining > 1024*1024
|
91
|
+
flush
|
92
|
+
raise 'invalid multibulk length'
|
93
|
+
elsif prev_remaining > 0
|
94
|
+
@stack << [@elements, prev_remaining]
|
95
|
+
@elements = []
|
96
|
+
end
|
97
|
+
when '$'
|
98
|
+
@binary_size = line[1..-1].to_i
|
99
|
+
if @binary_size == -1
|
100
|
+
@binary_size = nil
|
101
|
+
yield nil
|
102
|
+
elsif (@binary_size == 0 and line[1..1] != '0') or @binary_size < 0 or @binary_size > 512*1024*1024
|
103
|
+
flush
|
104
|
+
raise 'invalid bulk length'
|
105
|
+
end
|
106
|
+
else
|
107
|
+
if @remaining > 0
|
108
|
+
flush
|
109
|
+
raise "expected '$', got '#{line[0].chr}'"
|
110
|
+
end
|
111
|
+
parts = line.split(' ')
|
112
|
+
@remaining = parts.size
|
113
|
+
parts.each {|l| yield l}
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
# Read a binary redis token, nil if none available
|
120
|
+
def read_binary length
|
121
|
+
if @split
|
122
|
+
if @split.first.size >= length
|
123
|
+
result = @split.shift[0...length]
|
124
|
+
unshift_split if @split.size == 1
|
125
|
+
return result
|
126
|
+
end
|
127
|
+
unshift_split
|
128
|
+
end
|
129
|
+
unless @pending
|
130
|
+
size = reduce(0){|x,y|x+y.size}
|
131
|
+
return nil unless size >= length
|
132
|
+
@pending = dup
|
133
|
+
clear
|
134
|
+
remainder = size - length
|
135
|
+
if remainder > 0
|
136
|
+
last_string = @pending[-1]
|
137
|
+
@pending[-1] = last_string[0...-remainder]
|
138
|
+
push last_string[-remainder..-1]
|
139
|
+
end
|
140
|
+
end
|
141
|
+
# eat newline
|
142
|
+
return nil unless read_line
|
143
|
+
result = @pending.join
|
144
|
+
@pending = nil
|
145
|
+
result
|
146
|
+
end
|
147
|
+
|
148
|
+
# Read a newline terminated redis token, nil if none available
|
149
|
+
def read_line
|
150
|
+
unless @split
|
151
|
+
@split = join.split "\n", -1
|
152
|
+
clear
|
153
|
+
end
|
154
|
+
if @split.size > 1
|
155
|
+
result = @split.shift.chomp "\n"
|
156
|
+
else
|
157
|
+
result = nil
|
158
|
+
end
|
159
|
+
unshift_split if @split.size == 1
|
160
|
+
result
|
161
|
+
end
|
162
|
+
|
163
|
+
end
|
164
|
+
end
|