]> git.saurik.com Git - redis.git/blame - redis.tcl
added dictFetchValue() to dict.c to make hash table API a bit less verbose in the...
[redis.git] / redis.tcl
CommitLineData
f89c3a35 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
686bc0ac 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
f89c3a35 27
28package provide redis 0.1
29
30namespace eval redis {}
31set ::redis::id 0
32array set ::redis::fd {}
686bc0ac 33array set ::redis::blocking {}
34array set ::redis::callback {}
35array set ::redis::state {} ;# State in non-blocking reply reading
6fcb1800 36array set ::redis::statestack {} ;# Stack of states, for nested mbulks
f89c3a35 37array set ::redis::bulkarg {}
38array set ::redis::multibulkarg {}
39
40# Flag commands requiring last argument as a bulk write operation
41foreach redis_bulk_cmd {
a86f14b1 42 set setnx rpush lpush lset lrem sadd srem sismember echo getset smove zadd zrem zscore zincrby append zrank zrevrank hget hdel hexists
f89c3a35 43} {
44 set ::redis::bulkarg($redis_bulk_cmd) {}
45}
46
47# Flag commands requiring last argument as a bulk write operation
48foreach redis_multibulk_cmd {
09aeb579 49 mset msetnx hset hmset hmget
f89c3a35 50} {
51 set ::redis::multibulkarg($redis_multibulk_cmd) {}
52}
53
54unset redis_bulk_cmd
55unset redis_multibulk_cmd
56
57proc 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
686bc0ac 62 set ::redis::blocking($id) 1
63 ::redis::redis_reset_state $id
f89c3a35 64 interp alias {} ::redis::redisHandle$id {} ::redis::__dispatch__ $id
65}
66
67proc ::redis::__dispatch__ {id method args} {
68 set fd $::redis::fd($id)
686bc0ac 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 }
f89c3a35 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 }
686bc0ac 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 }
f89c3a35 106 } else {
107 uplevel 1 [list ::redis::__method__$method $id $fd] $args
108 }
109}
110
686bc0ac 111proc ::redis::__method__blocking {id fd val} {
112 set ::redis::blocking($id) $val
113 fconfigure $fd -blocking $val
114}
115
f89c3a35 116proc ::redis::__method__close {id fd} {
117 catch {close $fd}
118 catch {unset ::redis::fd($id)}
686bc0ac 119 catch {unset ::redis::blocking($id)}
120 catch {unset ::redis::state($id)}
6fcb1800 121 catch {unset ::redis::statestack($id)}
686bc0ac 122 catch {unset ::redis::callback($id)}
f89c3a35 123 catch {interp alias {} ::redis::redisHandle$id {}}
124}
125
126proc ::redis::__method__channel {id fd} {
127 return $fd
128}
129
130proc ::redis::redis_write {fd buf} {
131 puts -nonewline $fd $buf
132}
133
134proc ::redis::redis_writenl {fd buf} {
135 redis_write $fd $buf
136 redis_write $fd "\r\n"
137 flush $fd
138}
139
140proc ::redis::redis_readnl {fd len} {
141 set buf [read $fd $len]
142 read $fd 2 ; # discard CR LF
143 return $buf
144}
145
146proc ::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
153proc ::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
163proc ::redis::redis_read_line fd {
164 string trim [gets $fd]
165}
166
167proc ::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}
686bc0ac 178
179proc ::redis::redis_reset_state id {
180 set ::redis::state($id) [dict create buf {} mbulk -1 bulk -1 reply {}]
6fcb1800 181 set ::redis::statestack($id) {}
686bc0ac 182}
183
184proc ::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.
192proc ::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 }
6fcb1800 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 }
686bc0ac 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}