]> git.saurik.com Git - redis.git/blob - redis.tcl
Revert "version 1.3.10"
[redis.git] / redis.tcl
1 # Tcl clinet library - used by test-redis.tcl script for now
2 # Copyright (C) 2009 Salvatore Sanfilippo
3 # Released under the BSD license like Redis itself
4 #
5 # Example usage:
6 #
7 # set r [redis 127.0.0.1 6379]
8 # $r lpush mylist foo
9 # $r lpush mylist bar
10 # $r lrange mylist 0 -1
11 # $r close
12 #
13 # Non blocking usage example:
14 #
15 # proc handlePong {r type reply} {
16 # puts "PONG $type '$reply'"
17 # if {$reply ne "PONG"} {
18 # $r ping [list handlePong]
19 # }
20 # }
21 #
22 # set r [redis]
23 # $r blocking 0
24 # $r get fo [list handlePong]
25 #
26 # vwait forever
27
28 package require Tcl 8.5
29 package provide redis 0.1
30
31 namespace eval redis {}
32 set ::redis::id 0
33 array set ::redis::fd {}
34 array set ::redis::blocking {}
35 array set ::redis::callback {}
36 array set ::redis::state {} ;# State in non-blocking reply reading
37 array set ::redis::statestack {} ;# Stack of states, for nested mbulks
38 array set ::redis::bulkarg {}
39 array set ::redis::multibulkarg {}
40
41 # Flag commands requiring last argument as a bulk write operation
42 foreach redis_bulk_cmd {
43 set setnx rpush lpush lset lrem sadd srem sismember echo getset smove zadd zrem zscore zincrby append zrank zrevrank hget hdel hexists
44 } {
45 set ::redis::bulkarg($redis_bulk_cmd) {}
46 }
47
48 # Flag commands requiring last argument as a bulk write operation
49 foreach redis_multibulk_cmd {
50 mset msetnx hset hsetnx hmset hmget
51 } {
52 set ::redis::multibulkarg($redis_multibulk_cmd) {}
53 }
54
55 unset redis_bulk_cmd
56 unset redis_multibulk_cmd
57
58 proc redis {{server 127.0.0.1} {port 6379}} {
59 set fd [socket $server $port]
60 fconfigure $fd -translation binary
61 set id [incr ::redis::id]
62 set ::redis::fd($id) $fd
63 set ::redis::blocking($id) 1
64 ::redis::redis_reset_state $id
65 interp alias {} ::redis::redisHandle$id {} ::redis::__dispatch__ $id
66 }
67
68 proc ::redis::__dispatch__ {id method args} {
69 set fd $::redis::fd($id)
70 set blocking $::redis::blocking($id)
71 if {$blocking == 0} {
72 if {[llength $args] == 0} {
73 error "Please provide a callback in non-blocking mode"
74 }
75 set callback [lindex $args end]
76 set args [lrange $args 0 end-1]
77 }
78 if {[info command ::redis::__method__$method] eq {}} {
79 if {[info exists ::redis::bulkarg($method)]} {
80 set cmd "$method "
81 append cmd [join [lrange $args 0 end-1]]
82 append cmd " [string length [lindex $args end]]\r\n"
83 append cmd [lindex $args end]
84 ::redis::redis_writenl $fd $cmd
85 } elseif {[info exists ::redis::multibulkarg($method)]} {
86 set cmd "*[expr {[llength $args]+1}]\r\n"
87 append cmd "$[string length $method]\r\n$method\r\n"
88 foreach a $args {
89 append cmd "$[string length $a]\r\n$a\r\n"
90 }
91 ::redis::redis_write $fd $cmd
92 flush $fd
93 } else {
94 set cmd "$method "
95 append cmd [join $args]
96 ::redis::redis_writenl $fd $cmd
97 }
98 if {$blocking} {
99 ::redis::redis_read_reply $fd
100 } else {
101 # Every well formed reply read will pop an element from this
102 # list and use it as a callback. So pipelining is supported
103 # in non blocking mode.
104 lappend ::redis::callback($id) $callback
105 fileevent $fd readable [list ::redis::redis_readable $fd $id]
106 }
107 } else {
108 uplevel 1 [list ::redis::__method__$method $id $fd] $args
109 }
110 }
111
112 proc ::redis::__method__blocking {id fd val} {
113 set ::redis::blocking($id) $val
114 fconfigure $fd -blocking $val
115 }
116
117 proc ::redis::__method__close {id fd} {
118 catch {close $fd}
119 catch {unset ::redis::fd($id)}
120 catch {unset ::redis::blocking($id)}
121 catch {unset ::redis::state($id)}
122 catch {unset ::redis::statestack($id)}
123 catch {unset ::redis::callback($id)}
124 catch {interp alias {} ::redis::redisHandle$id {}}
125 }
126
127 proc ::redis::__method__channel {id fd} {
128 return $fd
129 }
130
131 proc ::redis::redis_write {fd buf} {
132 puts -nonewline $fd $buf
133 }
134
135 proc ::redis::redis_writenl {fd buf} {
136 redis_write $fd $buf
137 redis_write $fd "\r\n"
138 flush $fd
139 }
140
141 proc ::redis::redis_readnl {fd len} {
142 set buf [read $fd $len]
143 read $fd 2 ; # discard CR LF
144 return $buf
145 }
146
147 proc ::redis::redis_bulk_read {fd} {
148 set count [redis_read_line $fd]
149 if {$count == -1} return {}
150 set buf [redis_readnl $fd $count]
151 return $buf
152 }
153
154 proc ::redis::redis_multi_bulk_read fd {
155 set count [redis_read_line $fd]
156 if {$count == -1} return {}
157 set l {}
158 for {set i 0} {$i < $count} {incr i} {
159 lappend l [redis_read_reply $fd]
160 }
161 return $l
162 }
163
164 proc ::redis::redis_read_line fd {
165 string trim [gets $fd]
166 }
167
168 proc ::redis::redis_read_reply fd {
169 set type [read $fd 1]
170 switch -exact -- $type {
171 : -
172 + {redis_read_line $fd}
173 - {return -code error [redis_read_line $fd]}
174 $ {redis_bulk_read $fd}
175 * {redis_multi_bulk_read $fd}
176 default {return -code error "Bad protocol, $type as reply type byte"}
177 }
178 }
179
180 proc ::redis::redis_reset_state id {
181 set ::redis::state($id) [dict create buf {} mbulk -1 bulk -1 reply {}]
182 set ::redis::statestack($id) {}
183 }
184
185 proc ::redis::redis_call_callback {id type reply} {
186 set cb [lindex $::redis::callback($id) 0]
187 set ::redis::callback($id) [lrange $::redis::callback($id) 1 end]
188 uplevel #0 $cb [list ::redis::redisHandle$id $type $reply]
189 ::redis::redis_reset_state $id
190 }
191
192 # Read a reply in non-blocking mode.
193 proc ::redis::redis_readable {fd id} {
194 if {[eof $fd]} {
195 redis_call_callback $id eof {}
196 ::redis::__method__close $id $fd
197 return
198 }
199 if {[dict get $::redis::state($id) bulk] == -1} {
200 set line [gets $fd]
201 if {$line eq {}} return ;# No complete line available, return
202 switch -exact -- [string index $line 0] {
203 : -
204 + {redis_call_callback $id reply [string range $line 1 end-1]}
205 - {redis_call_callback $id err [string range $line 1 end-1]}
206 $ {
207 dict set ::redis::state($id) bulk \
208 [expr [string range $line 1 end-1]+2]
209 if {[dict get $::redis::state($id) bulk] == 1} {
210 # We got a $-1, hack the state to play well with this.
211 dict set ::redis::state($id) bulk 2
212 dict set ::redis::state($id) buf "\r\n"
213 ::redis::redis_readable $fd $id
214 }
215 }
216 * {
217 dict set ::redis::state($id) mbulk [string range $line 1 end-1]
218 # Handle *-1
219 if {[dict get $::redis::state($id) mbulk] == -1} {
220 redis_call_callback $id reply {}
221 }
222 }
223 default {
224 redis_call_callback $id err \
225 "Bad protocol, $type as reply type byte"
226 }
227 }
228 } else {
229 set totlen [dict get $::redis::state($id) bulk]
230 set buflen [string length [dict get $::redis::state($id) buf]]
231 set toread [expr {$totlen-$buflen}]
232 set data [read $fd $toread]
233 set nread [string length $data]
234 dict append ::redis::state($id) buf $data
235 # Check if we read a complete bulk reply
236 if {[string length [dict get $::redis::state($id) buf]] ==
237 [dict get $::redis::state($id) bulk]} {
238 if {[dict get $::redis::state($id) mbulk] == -1} {
239 redis_call_callback $id reply \
240 [string range [dict get $::redis::state($id) buf] 0 end-2]
241 } else {
242 dict with ::redis::state($id) {
243 lappend reply [string range $buf 0 end-2]
244 incr mbulk -1
245 set bulk -1
246 }
247 if {[dict get $::redis::state($id) mbulk] == 0} {
248 redis_call_callback $id reply \
249 [dict get $::redis::state($id) reply]
250 }
251 }
252 }
253 }
254 }