From d45d63715b0eb653bc76ca87b99c8de64d482d51 Mon Sep 17 00:00:00 2001 From: Bruno Deferrari Date: Mon, 11 May 2009 00:08:34 -0300 Subject: [PATCH 1/4] extra.redis.assoc: Assoc protocol implementation for Redis --- extra/redis/assoc/assoc.factor | 41 ++++++++++++++++++++++++++++++++++ extra/redis/assoc/authors.txt | 1 + extra/redis/assoc/summary.txt | 1 + 3 files changed, 43 insertions(+) create mode 100644 extra/redis/assoc/assoc.factor create mode 100644 extra/redis/assoc/authors.txt create mode 100644 extra/redis/assoc/summary.txt diff --git a/extra/redis/assoc/assoc.factor b/extra/redis/assoc/assoc.factor new file mode 100644 index 0000000000..2ddf746344 --- /dev/null +++ b/extra/redis/assoc/assoc.factor @@ -0,0 +1,41 @@ +! Copyright (C) 2009 Bruno Deferrari +! See http://factorcode.org/license.txt for BSD license. +USING: accessors assocs io.encodings.8-bit io.sockets +io.streams.duplex kernel redis sequences ; +IN: redis.assoc + +TUPLE: redis-assoc host port encoding password ; + +CONSTANT: default-redis-port 6379 + +: ( -- redis-assoc ) + redis-assoc new + "127.0.0.1" >>host + default-redis-port >>port + latin1 >>encoding ; + +INSTANCE: redis-assoc assoc + +: with-redis-assoc ( redis-assoc quot -- ) + [ + [ host>> ] [ port>> ] [ encoding>> ] tri + [ ] dip drop + ] dip with-stream ; inline + +M: redis-assoc at* [ redis-get dup >boolean ] with-redis-assoc ; + +M: redis-assoc assoc-size [ redis-dbsize ] with-redis-assoc ; + +M: redis-assoc >alist + [ "*" redis-keys dup redis-mget zip ] with-redis-assoc ; + +M: redis-assoc set-at [ redis-set drop ] with-redis-assoc ; + +M: redis-assoc delete-at [ redis-del drop ] with-redis-assoc ; + +M: redis-assoc clear-assoc + [ "*" redis-keys [ redis-del drop ] each ] with-redis-assoc ; + +M: redis-assoc equal? assoc= ; + +M: redis-assoc hashcode* assoc-hashcode ; diff --git a/extra/redis/assoc/authors.txt b/extra/redis/assoc/authors.txt new file mode 100644 index 0000000000..f4a8cb1dc2 --- /dev/null +++ b/extra/redis/assoc/authors.txt @@ -0,0 +1 @@ +Bruno Deferrari diff --git a/extra/redis/assoc/summary.txt b/extra/redis/assoc/summary.txt new file mode 100644 index 0000000000..72a76ab9f0 --- /dev/null +++ b/extra/redis/assoc/summary.txt @@ -0,0 +1 @@ +Assoc protocol implementation for Redis From 462b66a696368d0121ec4e808a8ee3f2b96f9d2e Mon Sep 17 00:00:00 2001 From: Bruno Deferrari Date: Mon, 11 May 2009 00:09:32 -0300 Subject: [PATCH 2/4] extra.redis: Make redis-keys return an array of keys instead of a space separated string of keys --- extra/redis/redis.factor | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extra/redis/redis.factor b/extra/redis/redis.factor index 1f6d732407..a5e7d74d46 100644 --- a/extra/redis/redis.factor +++ b/extra/redis/redis.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2009 Bruno Deferrari ! See http://factorcode.org/license.txt for BSD license. -USING: io redis.response-parser redis.command-writer ; +USING: io redis.response-parser redis.command-writer splitting ; IN: redis #! Connection @@ -23,7 +23,7 @@ IN: redis : redis-type ( key -- response ) type flush read-response ; #! Key space -: redis-keys ( pattern -- response ) keys flush read-response ; +: redis-keys ( pattern -- response ) keys flush read-response " " split ; : redis-randomkey ( -- response ) randomkey flush read-response ; : redis-rename ( newkey key -- response ) rename flush read-response ; : redis-renamenx ( newkey key -- response ) renamenx flush read-response ; From 7edcc651593967de799df3d711d5f603474e25ec Mon Sep 17 00:00:00 2001 From: Bruno Deferrari Date: Mon, 11 May 2009 00:44:09 -0300 Subject: [PATCH 3/4] extra.redis: Replace 'redis-assoc' with the more general 'redis' type (now in the redis vocab), add support for automatic authentification when calling 'with-redis' --- extra/redis/assoc/assoc.factor | 39 +++++++++------------------------- extra/redis/redis.factor | 25 +++++++++++++++++++++- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/extra/redis/assoc/assoc.factor b/extra/redis/assoc/assoc.factor index 2ddf746344..2830e93b25 100644 --- a/extra/redis/assoc/assoc.factor +++ b/extra/redis/assoc/assoc.factor @@ -1,41 +1,22 @@ ! Copyright (C) 2009 Bruno Deferrari ! See http://factorcode.org/license.txt for BSD license. -USING: accessors assocs io.encodings.8-bit io.sockets -io.streams.duplex kernel redis sequences ; +USING: assocs kernel redis sequences ; IN: redis.assoc -TUPLE: redis-assoc host port encoding password ; +INSTANCE: redis assoc -CONSTANT: default-redis-port 6379 +M: redis at* [ redis-get dup >boolean ] with-redis ; -: ( -- redis-assoc ) - redis-assoc new - "127.0.0.1" >>host - default-redis-port >>port - latin1 >>encoding ; +M: redis assoc-size [ redis-dbsize ] with-redis ; -INSTANCE: redis-assoc assoc +M: redis >alist [ "*" redis-keys dup redis-mget zip ] with-redis ; -: with-redis-assoc ( redis-assoc quot -- ) - [ - [ host>> ] [ port>> ] [ encoding>> ] tri - [ ] dip drop - ] dip with-stream ; inline +M: redis set-at [ redis-set drop ] with-redis ; -M: redis-assoc at* [ redis-get dup >boolean ] with-redis-assoc ; +M: redis delete-at [ redis-del drop ] with-redis ; -M: redis-assoc assoc-size [ redis-dbsize ] with-redis-assoc ; +M: redis clear-assoc [ "*" redis-keys [ redis-del drop ] each ] with-redis ; -M: redis-assoc >alist - [ "*" redis-keys dup redis-mget zip ] with-redis-assoc ; +M: redis equal? assoc= ; -M: redis-assoc set-at [ redis-set drop ] with-redis-assoc ; - -M: redis-assoc delete-at [ redis-del drop ] with-redis-assoc ; - -M: redis-assoc clear-assoc - [ "*" redis-keys [ redis-del drop ] each ] with-redis-assoc ; - -M: redis-assoc equal? assoc= ; - -M: redis-assoc hashcode* assoc-hashcode ; +M: redis hashcode* assoc-hashcode ; diff --git a/extra/redis/redis.factor b/extra/redis/redis.factor index a5e7d74d46..466fdc9937 100644 --- a/extra/redis/redis.factor +++ b/extra/redis/redis.factor @@ -1,6 +1,8 @@ ! Copyright (C) 2009 Bruno Deferrari ! See http://factorcode.org/license.txt for BSD license. -USING: io redis.response-parser redis.command-writer splitting ; +USING: accessors io io.encodings.8-bit io.sockets +io.streams.duplex kernel redis.command-writer +redis.response-parser splitting ; IN: redis #! Connection @@ -72,3 +74,24 @@ IN: redis #! Remote server control : redis-info ( -- response ) info flush read-response ; : redis-monitor ( -- response ) monitor flush read-response ; + +#! Redis object +TUPLE: redis host port encoding password ; + +CONSTANT: default-redis-port 6379 + +: ( -- redis ) + redis new + "127.0.0.1" >>host + default-redis-port >>port + latin1 >>encoding ; + +: redis-do-connect ( redis -- stream ) + [ host>> ] [ port>> ] [ encoding>> ] tri + [ ] dip drop ; + +: with-redis ( redis quot -- ) + [ + [ redis-do-connect ] [ password>> ] bi + [ swap [ [ redis-auth drop ] with-stream* ] keep ] when* + ] dip with-stream ; inline From cb76bddd8fb523d90d447d95b2fd9bf82c974c69 Mon Sep 17 00:00:00 2001 From: Bruno Deferrari Date: Mon, 11 May 2009 00:50:22 -0300 Subject: [PATCH 4/4] redis.assoc: Use redis-flushdb in clear-assoc --- extra/redis/assoc/assoc.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra/redis/assoc/assoc.factor b/extra/redis/assoc/assoc.factor index 2830e93b25..e8bdbbb935 100644 --- a/extra/redis/assoc/assoc.factor +++ b/extra/redis/assoc/assoc.factor @@ -15,7 +15,7 @@ M: redis set-at [ redis-set drop ] with-redis ; M: redis delete-at [ redis-del drop ] with-redis ; -M: redis clear-assoc [ "*" redis-keys [ redis-del drop ] each ] with-redis ; +M: redis clear-assoc [ redis-flushdb drop ] with-redis ; M: redis equal? assoc= ;