Skip to content
Browse files

Add support for ZSETs, currently in Redis edge

  • Loading branch information...
1 parent 76f1665 commit eb0f1c40623048d5e88a7d85a702ee5d666517ef @ragnard committed
Showing with 64 additions and 22 deletions.
  1. +3 −9 benchmarks/clojure.clj
  2. +11 −3 src/main/clojure/redis.clj
  3. +4 −6 src/main/clojure/redis/internal.clj
  4. +46 −4 src/test/clojure/tests.clj
View
12 benchmarks/clojure.clj
@@ -1,10 +1,5 @@
-
-
-(add-classpath "file:///Users/ragge/Projects/clojure/redis-clojure/redis-clojure.jar")
-
(ns benchmarks.clojure
- (:use clojure.contrib.pprint)
- (:require redis))
+ (:require redis))
(defstruct benchmark-options
:host
@@ -16,7 +11,6 @@
:keyspace-size
:data-size)
-
(defstruct client
:id
:request-times
@@ -76,7 +70,7 @@
:host "127.0.0.1"
:port 6379
:db 15
- :clients 1
+ :clients 2
:requests 10000))
(defn create-clients [options]
@@ -151,7 +145,7 @@
(let [elapsed (/ (double (- (System/nanoTime) start)) 1000000.0)]
(dorun
(map #(when (agent-errors %)
- (pprint (agent-errors %))) clients))
+ (pr (agent-errors %))) clients))
(assoc result
:name (str fn)
:options options
View
14 src/main/clojure/redis.clj
@@ -2,8 +2,6 @@
(:refer-clojure :exclude [get set type keys sort])
(:use redis.internal))
-;(set! *warn-on-reflection* true)
-
(defmacro with-server
"Evaluates body in the context of a new connection to a Redis server
then closes the connection.
@@ -18,7 +16,6 @@
(redis/select (:db *connection*))
~@body))))
-
;;
;; Reply conversion functions
;;
@@ -46,6 +43,11 @@
(let [lines (.split string "(\\r\\n|:)")]
(apply hash-map lines)))
+(defn string-to-double
+ "Convert a string in floating point format to a double"
+ [string]
+ (Double/parseDouble string))
+
(defn int-to-date
"Return a Date representation of a UNIX timestamp"
[int]
@@ -111,6 +113,12 @@
(sdiff [key & keys] :inline seq-to-set)
(sdiffstore [destkey key & keys] :inline)
(smembers [key] :inline seq-to-set)
+ ;; ZSet commands
+ (zadd [key score member] :bulk int-to-bool)
+ (zrem [key member] :bulk int-to-bool)
+ (zscore [key member] :bulk string-to-double)
+ (zrange [key start end] :inline)
+ (zrangebyscore [key start end] :inline)
;; Multiple database handling commands
(select [index] :inline)
(move [key dbindex] :inline)
View
10 src/main/clojure/redis/internal.clj
@@ -1,11 +1,9 @@
(ns redis.internal
(:refer-clojure :exclude [send read read-line])
- ;(:require redis.client.default)
- ;(:require redis.client.netty)
- ;(:require redis.client.nio)
(:import [java.io Reader BufferedReader InputStreamReader StringReader]
[java.net Socket]))
+(set! *warn-on-reflection* true)
(defstruct connection :host :port :db :timeout :socket :reader :writer)
@@ -76,9 +74,9 @@
"Read from reader until exactly a CR+LF combination is
found. Returns the line read without trailing CR+LF.
- This is used instead of Reader.readLine() method since it tries to
- read either a CR, a LF or a CR+LF, which we don't want in this
- case."
+ This is used instead of Reader.readLine() method since that method
+ tries to read either a CR, a LF or a CR+LF, which we don't want in
+ this case."
[#^BufferedReader reader]
(loop [line []
c (.read reader)]
View
50 src/test/clojure/tests.clj
@@ -202,12 +202,14 @@
(is (= ["one" "two"] (redis/lrange "list" 0 1)))
(is (= ["one" "two" "three"] (redis/lrange "list" 0 2)))
(is (= ["one" "two" "three"] (redis/lrange "list" 0 42)))
- (is (= [] (redis/lrange "list" 42 0)))
-)
+ (is (= [] (redis/lrange "list" 42 0))))
-;; TBD
(deftest ltrim
- (is (thrown? Exception (redis/ltrim "foo" 0 0))))
+ (is (thrown? Exception (redis/ltrim "foo" 0 0)))
+ (redis/ltrim "list" 0 1)
+ (is (= ["one" "two"] (redis/lrange "list" 0 99)))
+ (redis/ltrim "list" 1 99)
+ (is (= ["two"] (redis/lrange "list" 0 99))))
(deftest lindex
(is (thrown? Exception (redis/lindex "foo" 0)))
@@ -343,6 +345,46 @@
(is (thrown? Exception (redis/smembers "foo")))
(is (= #{"one" "two" "three"} (redis/smembers "set"))))
+;;
+;; ZSet commands
+;;
+(deftest zadd
+ (is (thrown? Exception (redis/zadd "foo" 1 "bar")))
+ (is (= true (redis/zadd "zset" 3.141592 "foo")))
+ (is (= true (redis/zadd "zset" -42 "bar")))
+ (is (= true (redis/zadd "zset" 2393845792384752345239485723984534589739284579348.349857983457398457934857 "baz")))
+ (is (= ["bar" "foo"] (redis/zrange "zset" 0 1))))
+
+(deftest zrem
+ (is (thrown? Exception (redis/zrem "foo" "bar")))
+ (is (= false (redis/zrem "zset" "foobar")))
+ (redis/zadd "zset" 1.0 "one")
+ (redis/zadd "zset" 2.0 "two")
+ (redis/zadd "zset" 3.0 "three")
+ (is (= true (redis/zrem "zset" "two")))
+ (is (= ["one" "three"] (redis/zrange "zset" 0 1))))
+
+(deftest zscore
+ (is (thrown? Exception (redis/zscore "foo")))
+ (redis/zadd "zset" 3.141592 "pi")
+ (is (= 3.141592 (redis/zscore "zset" "pi")))
+ (redis/zadd "zset" -42 "neg")
+ (is (= -42 (redis/zscore "zset" "neg"))))
+
+
+(deftest zrange
+ (is (thrown? Exception (redis/zrange "foo")))
+ (is (= nil (redis/zrange "zset" 0 99)))
+ (redis/zadd "zset" 12349809.23873948579348750 "one")
+ (redis/zadd "zset" -42 "two")
+ (redis/zadd "zset" 3.141592 "three")
+ (is (= ["two" "three" "one"] (redis/zrange "zset" 0 2)))
+ (is (= ["three" "one"] (redis/zrange "zset" 1 2))))
+
+(deftest zrangebyscore
+ (is (thrown? Exception (redis/zrangebyscore "foo")))
+ (is (= nil (redis/zrangebyscore "zset" 0 99))))
+
;;
;; Sorting

0 comments on commit eb0f1c4

Please sign in to comment.
Something went wrong with that request. Please try again.