ruby-redis 0.0.1
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.
- 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
@@ -0,0 +1,170 @@
|
|
1
|
+
require File.expand_path '../redis', File.dirname(__FILE__)
|
2
|
+
require 'eventmachine'
|
3
|
+
require_relative 'reader'
|
4
|
+
require_relative 'sender'
|
5
|
+
|
6
|
+
class Redis
|
7
|
+
|
8
|
+
# Use to respond with raw protocol
|
9
|
+
# Response["+",data,"\n\r"]
|
10
|
+
# Response::OK
|
11
|
+
# Response[]
|
12
|
+
class Response < Array
|
13
|
+
OK = self["+OK\r\n".freeze].freeze
|
14
|
+
PONG = self["+PONG\r\n".freeze].freeze
|
15
|
+
NIL_MB = self["*-1\r\n".freeze].freeze
|
16
|
+
QUEUED = self["+QUEUED\r\n".freeze].freeze
|
17
|
+
end
|
18
|
+
|
19
|
+
class Watcher
|
20
|
+
include EventMachine::Deferrable
|
21
|
+
|
22
|
+
attr_reader :bound
|
23
|
+
|
24
|
+
def initialize
|
25
|
+
@watched = []
|
26
|
+
@bound = true
|
27
|
+
errback { unbind }
|
28
|
+
callback { unbind }
|
29
|
+
end
|
30
|
+
|
31
|
+
def bind database, *keys
|
32
|
+
return unless @bound
|
33
|
+
keys.each do |key|
|
34
|
+
entry = [database, key]
|
35
|
+
next if @watched.include? entry
|
36
|
+
@watched << entry
|
37
|
+
(database.watchers[key] ||= []).push self
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def unbind
|
42
|
+
return unless @bound
|
43
|
+
@watched.each do |database, key|
|
44
|
+
key_df_list = database.watchers[key]
|
45
|
+
next unless key_df_list
|
46
|
+
key_df_list.delete_if { |e| e == self }
|
47
|
+
end
|
48
|
+
@bound = false
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
module Protocol
|
54
|
+
|
55
|
+
include Sender
|
56
|
+
|
57
|
+
# Typically raised by redis_QUIT
|
58
|
+
class CloseConnection < Exception
|
59
|
+
end
|
60
|
+
|
61
|
+
def initialize *args
|
62
|
+
@reader = Reader.new
|
63
|
+
@multi = nil
|
64
|
+
@deferred = nil
|
65
|
+
@watcher = nil
|
66
|
+
super
|
67
|
+
end
|
68
|
+
|
69
|
+
def unbind
|
70
|
+
@deferred.unbind if @deferred
|
71
|
+
@watcher.unbind if @watcher
|
72
|
+
end
|
73
|
+
|
74
|
+
# Companion to send_data.
|
75
|
+
def send_redis data
|
76
|
+
if EventMachine::Deferrable === data
|
77
|
+
@deferred.unbind if @deferred and @deferred != data
|
78
|
+
@deferred = data
|
79
|
+
elsif Response === data
|
80
|
+
data.each do |item|
|
81
|
+
send_data item
|
82
|
+
end
|
83
|
+
elsif Integer === data
|
84
|
+
send_data ":#{data}\r\n"
|
85
|
+
else
|
86
|
+
super
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def redis_WATCH *keys
|
91
|
+
@watcher ||= Watcher.new
|
92
|
+
@watcher.bind @database, *keys
|
93
|
+
Response::OK
|
94
|
+
end
|
95
|
+
|
96
|
+
def redis_UNWATCH
|
97
|
+
if @watcher
|
98
|
+
@watcher.unbind
|
99
|
+
@watcher = nil
|
100
|
+
end
|
101
|
+
Response::OK
|
102
|
+
end
|
103
|
+
|
104
|
+
def redis_MULTI
|
105
|
+
raise 'MULTI nesting not allowed' if @multi
|
106
|
+
@multi = []
|
107
|
+
Response::OK
|
108
|
+
end
|
109
|
+
|
110
|
+
def redis_DISCARD
|
111
|
+
redis_UNWATCH
|
112
|
+
@multi = nil
|
113
|
+
Response::OK
|
114
|
+
end
|
115
|
+
|
116
|
+
def redis_EXEC
|
117
|
+
if @watcher
|
118
|
+
still_bound = @watcher.bound
|
119
|
+
redis_UNWATCH
|
120
|
+
unless still_bound
|
121
|
+
@multi = nil
|
122
|
+
return Response::NIL_MB
|
123
|
+
end
|
124
|
+
end
|
125
|
+
send_data "*#{@multi.size}\r\n"
|
126
|
+
response = []
|
127
|
+
@multi.each do |strings|
|
128
|
+
result = call_redis *strings
|
129
|
+
if EventMachine::Deferrable === result
|
130
|
+
result.unbind
|
131
|
+
send_redis nil
|
132
|
+
else
|
133
|
+
send_redis result
|
134
|
+
end
|
135
|
+
end
|
136
|
+
@multi = nil
|
137
|
+
Response[]
|
138
|
+
end
|
139
|
+
|
140
|
+
def call_redis command, *arguments
|
141
|
+
send "redis_#{command.upcase}", *arguments
|
142
|
+
rescue Exception => e
|
143
|
+
raise e if CloseConnection === e
|
144
|
+
# Redis.logger.warn "#{command.dump}: #{e.class}:/#{e.backtrace[0]} #{e.message}"
|
145
|
+
# e.backtrace[1..-1].each {|bt|Redis.logger.warn bt}
|
146
|
+
Response["-ERR #{e.class.name}: #{e.message}\r\n" ]
|
147
|
+
end
|
148
|
+
|
149
|
+
# Process incoming redis protocol
|
150
|
+
def receive_data data
|
151
|
+
@reader.feed data
|
152
|
+
until (strings = @reader.gets) == false
|
153
|
+
# Redis.logger.warn "#{strings.collect{|a|a.dump}.join ' '}"
|
154
|
+
if @multi and !%w{MULTI EXEC DEBUG DISCARD}.include?(strings[0].upcase)
|
155
|
+
@multi << strings
|
156
|
+
send_redis Response::QUEUED
|
157
|
+
else
|
158
|
+
send_redis call_redis *strings
|
159
|
+
end
|
160
|
+
end
|
161
|
+
rescue Exception => e
|
162
|
+
if CloseConnection === e
|
163
|
+
close_connection_after_writing
|
164
|
+
else
|
165
|
+
send_data "-ERR #{e.class.name}: #{e.message}\r\n"
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
end
|
170
|
+
end
|