Skip to content

Commit

Permalink
Add hash operating commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
manjilab committed Apr 13, 2010
1 parent 14bec85 commit 3f67e05
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/redis.clj
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@
(zrevrange [key start end] :inline)
(zrangebyscore [key start end] :inline)
(zremrangebyscore [key start end] :inline)
;; Hash commands
(hset [key field value] :bulk int-to-bool)
(hget [key field] :bulk);;;;
(hmset [key field value & more] :bulk)
(hincrby [key field integer] :inline)
(hexists [key field] :bulk int-to-bool);;;
(hdel [key field] :bulk int-to-bool);;;
(hlen [key] :inline)
(hkeys [key] :inline)
(hvals [key] :inline)
(hgetall [key] :inline)
;; MULTI/EXEC/DISCARD
(multi [] :inline)
(exec [] :inline)
Expand Down
60 changes: 60 additions & 0 deletions test/redis/tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
(redis/sadd "set" "one")
(redis/sadd "set" "two")
(redis/sadd "set" "three")
;; Hash with three fields
(redis/hset "hash" "one" "foo")
(redis/hset "hash" "two" "bar")
(redis/hset "hash" "three" "baz")
(f)
(redis/flushdb)))

Expand Down Expand Up @@ -418,6 +422,62 @@
(redis/zadd "zset" 1.0 "one")
(is (= 1 (redis/zcard "zset"))))

;;
;; Hash commands
;;
(deftest hset
(is (thrown? Exception (redis/hset "foo" "baz" "poe")))
(redis/hset "bar" "foo" "hoge")
(is (= "hoge" (redis/hget "bar" "foo")))

(deftest hget
(is (= nil (redis/hget "bar" "baz")))
(is (= "bar" (redis/hget "hash" "two"))))

(deftest hset
(is (thrown? Exception (redis/hmset "key1" "field1")))
(is (thrown? Exception (redis/hmset "key" "field" "value" "feild1")))
(redis/hmset "key1" "field1" "value1" "field2" "value2" "field3" "value3")
(is (= ["value1" "value2" "value3"] (redis/hvals "key1"))))

(deftest hincrby
(is (= 42 (redis/hincrby "non-exist-key" "non-exist-field" 42)))
(is (thrown? Exception (redis/hincrby "foo" "bar" 0)))
(is (= 0 (redis/hincrby "key1" "field1" 0))))
(is (= 5 (redis/hincrby "key1" "field1" 5))))

(deftest hexists
(is (= true (redis/hexists "hash" "one")))
(is (= false (redis/hexists "non-exist-key" "non-exist-field"))))

(deftest hdel
(is (= false (redis/hdel "non-exist-key" "non-exist-field")))
(is (= true (redis/hdel "hash" "three")))
(is (= nil (redis/hget "hash" "three"))))

(deftest hlen
(is (thrown? Exception (redis/hlen "foo")))
(is (= 0 (redis/hlen "newhash")))
(is (= 3 (redis/hlen "hash"))))

(deftest hkeys
(is (= nil (redis/hkeys "noexistent")))
(is (= ["one" "two" "three"] (redis/hkeys "hash")))
(redis/hset "hash" "four" "hoge")
(is (= 4 (count (redis/hkeys "hash")))))

(deftest hvals
(is (= nil (redis/hvals "noexistent")))
(is (= ["foo" "bar" "baz"] (redis/hvals "hash")))
(redis/hdel "hash" "two")
(is (= ["foo" "baz"] (redis/hvals "hash"))))

(deftest hgetall
(is (= nil (redis/hgetall "noexistent")))
(is (= ["one" "foo" "two" "bar" "three" "baz"] (redis/hgetall "hash")))
(redis/hdel "hash" "one")
(is (= {"two" "bar", "three" "baz"} (apply hash-map (redis/hgetall "hash")))))

;;
;; MULTI/EXEC/DISCARD
;;
Expand Down

0 comments on commit 3f67e05

Please sign in to comment.