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