2009-05-10 19:06:28 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								! Copyright (C) 2009 Bruno Deferrari
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								! See http://factorcode.org/license.txt for BSD license.
							 | 
						
					
						
							
								
									
										
										
										
											2014-05-02 16:41:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								USING: arrays assocs formatting io io.crlf kernel math
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								math.parser sequences strings locals ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-10 19:06:28 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								IN: redis.command-writer
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								<PRIVATE
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2014-05-02 16:41:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								GENERIC: write-resp ( value -- )
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-10 19:06:28 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2014-05-02 16:41:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								M: string write-resp ( string -- )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    [ length ] keep "$%s\r\n%s\r\n" printf ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-10 19:06:28 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2014-05-02 16:41:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								M: integer write-resp ( integer -- )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    ":%s\r\n" printf ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-10 19:06:28 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2014-05-02 16:41:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								M: sequence write-resp ( sequence -- )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    [ length "*%s\r\n" printf ] keep
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    [ write-resp ] each ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-10 19:06:28 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2014-05-02 16:41:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: write-command ( sequence command -- )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    suffix reverse
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    [ dup number? [ number>string ] when ] map
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    write-resp ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-10 19:06:28 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								PRIVATE>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2015-09-09 21:28:08 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								! Connection
							 | 
						
					
						
							
								
									
										
										
										
											2014-05-02 16:41:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: quit ( -- ) { "QUIT" } write-resp ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: ping ( -- ) { "PING" } write-resp ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: auth ( password -- ) 1array "AUTH" write-command ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-10 19:06:28 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2015-09-09 21:28:08 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								! String values
							 | 
						
					
						
							
								
									
										
										
										
											2014-05-02 16:41:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: set ( value key -- ) 2array "SET" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: get ( key -- ) 1array "GET" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: getset ( value key -- ) 2array "GETSET" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: mget ( keys -- ) reverse "MGET" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: setnx ( value key -- ) 2array "SETNX" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: incr ( key -- ) 1array "INCR" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: incrby ( integer key -- ) 2array "INCRBY" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: decr ( key -- ) 1array "DECR" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: decrby ( integer key -- ) 2array "DECRBY" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: exists ( key -- ) 1array "EXISTS" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: del ( key -- ) 1array "DEL" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: type ( key -- ) 1array "TYPE" write-command ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-10 19:06:28 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2015-09-09 21:28:08 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								! Key space
							 | 
						
					
						
							
								
									
										
										
										
											2014-05-02 16:41:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: keys ( pattern -- ) 1array "KEYS" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: randomkey ( -- ) { "RANDOMKEY" } write-resp ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: rename ( newkey key -- ) 2array "RENAME" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: renamenx ( newkey key -- ) 2array "RENAMENX" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: dbsize ( -- ) { "DBSIZE" } write-resp ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: expire ( integer key -- ) 2array "EXPIRE" write-command ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-10 19:06:28 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2015-09-09 21:28:08 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								! Lists
							 | 
						
					
						
							
								
									
										
										
										
											2014-05-02 16:41:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: rpush ( value key -- ) 2array "RPUSH" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: lpush ( value key -- ) 2array "LPUSH" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: llen ( key -- ) 1array "LLEN" write-command ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-10 19:06:28 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: lrange ( start end key -- )
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-21 14:56:17 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    swapd 3array "LRANGE" write-command ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-10 19:06:28 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: ltrim ( start end key -- )
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-21 14:56:17 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    swapd 3array "LTRIM" write-command ;
							 | 
						
					
						
							
								
									
										
										
										
											2014-05-02 16:41:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: lindex ( integer key -- ) 2array "LINDEX" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: lset ( value index key -- ) 3array "LSET" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: lrem ( value amount key -- ) 3array "LREM" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: lpop ( key -- ) 1array "LPOP" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: rpop ( key -- ) 1array "RPOP" write-command ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-10 19:06:28 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2015-09-09 21:28:08 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								! Sets
							 | 
						
					
						
							
								
									
										
										
										
											2014-05-02 16:41:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: sadd ( member key -- ) 2array "SADD" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: srem  ( member key -- ) 2array "SREM" write-command ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-10 19:06:28 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: smove ( member newkey key -- )
							 | 
						
					
						
							
								
									
										
										
										
											2014-05-02 16:41:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    3array "SMOVE" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: scard ( key -- ) 1array "SCARD" write-command ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-10 19:06:28 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: sismember ( member key -- )
							 | 
						
					
						
							
								
									
										
										
										
											2014-05-02 16:41:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    2array "SISMEMBER" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: sinter ( keys -- ) reverse "SINTER" write-command ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-10 19:06:28 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: sinterstore ( keys destkey -- )
							 | 
						
					
						
							
								
									
										
										
										
											2014-05-02 16:41:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    [ reverse ] dip suffix "SINTERSTORE" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: sunion ( keys -- ) reverse "SUNION" write-command ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-10 19:06:28 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: sunionstore ( keys destkey -- )
							 | 
						
					
						
							
								
									
										
										
										
											2014-05-02 16:41:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    [ reverse ] dip suffix "SUNIONSTORE" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: smembers ( key -- ) 1array "SMEMBERS" write-command ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-10 19:06:28 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2015-09-09 21:28:08 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								! Hashes
							 | 
						
					
						
							
								
									
										
										
										
											2014-05-02 16:41:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: hdel ( field key -- ) 2array "HDEL" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: hexists ( field key -- ) 2array "HEXISTS" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: hget ( field key -- ) 2array "HGET" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: hgetall ( key -- ) 1array "HGETALL" write-command ;
							 | 
						
					
						
							
								
									
										
										
										
											2013-07-30 14:42:40 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: hincrby ( integer field key -- )
							 | 
						
					
						
							
								
									
										
										
										
											2014-05-02 16:41:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    3array "HINCRBY" write-command ;
							 | 
						
					
						
							
								
									
										
										
										
											2013-07-30 14:42:40 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: hincrbyfloat (  float field key -- )
							 | 
						
					
						
							
								
									
										
										
										
											2014-05-02 16:41:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    3array "HINCRBYFLOAT" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: hkeys ( key -- ) 1array "HKEYS" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: hlen ( key -- ) 1array "HLEN" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: hmget ( seq key  -- ) prefix reverse "HMGET" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: hmset ( assoc key -- )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    [
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        >alist concat reverse
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    ] dip suffix "HMSET" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: hset ( value field key -- ) 3array "HSET" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: hsetnx ( value field key -- )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    3array "HSETNX" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: hvals ( key -- ) 1array "HVALS" write-command ;
							 | 
						
					
						
							
								
									
										
										
										
											2013-07-30 14:42:40 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2015-09-09 21:28:08 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								! Multiple db
							 | 
						
					
						
							
								
									
										
										
										
											2014-05-02 16:41:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: select ( integer -- ) 1array "SELECT" write-command ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: move ( integer key -- ) 2array "MOVE" write-command ;
							 | 
						
					
						
							
								
									
										
										
										
											2016-10-14 19:54:32 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: swapdb ( old new -- ) 2array "SWAPDB" write-command ;
							 | 
						
					
						
							
								
									
										
										
										
											2014-05-02 16:41:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: flushdb ( -- ) { "FLUSHDB" } write-resp ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: flushall ( -- ) { "FLUSHALL" } write-resp ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-10 19:06:28 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2015-09-09 21:28:08 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								! Sorting
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-10 19:06:28 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								! sort
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2015-09-09 21:28:08 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								! Persistence control
							 | 
						
					
						
							
								
									
										
										
										
											2014-05-02 16:41:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: save ( -- ) { "SAVE" } write-resp ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: bgsave ( -- ) { "BGSAVE" } write-resp ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: lastsave ( -- ) { "LASTSAVE" } write-resp ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: shutdown ( -- ) { "SHUTDOWN" } write-resp ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-05-10 19:06:28 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2015-09-09 21:28:08 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								! Remote server control
							 | 
						
					
						
							
								
									
										
										
										
											2014-05-02 16:41:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: info ( -- ) { "INFO" } write-resp ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: monitor ( -- ) { "MONITOR" } write-resp ;
							 |