Skip to content

Commit

Permalink
fixed bug in SortedSetOperations and bumped up the version to 1.2-SNA…
Browse files Browse the repository at this point in the history
…PSHOT
  • Loading branch information
Debasish Ghosh committed Mar 18, 2010
1 parent 9c3eaf3 commit f075029
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 14 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<groupId>com.redis</groupId>
<artifactId>scala-redis</artifactId>
<packaging>jar</packaging>
<version>1.1-SNAPSHOT</version>
<version>1.2-SNAPSHOT</version>
<name>scala-redis</name>

<properties>
<scala-redis.version>1.1-SNAPSHOT</scala-redis.version>
<scala-redis.version>1.2-SNAPSHOT</scala-redis.version>
<scala-redis.groupId>scala-redis</scala-redis.groupId>
<scala.version>2.8.0.Beta1</scala.version>
</properties>
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
project.organization=com.redis
project.name=RedisClient
sbt.version=0.7.1
project.version=1.1-SNAPSHOT
project.version=1.2-SNAPSHOT
scala.version=2.8.0.Beta1
def.scala.version=2.7.7
build.scala.versions=2.8.0.Beta1
Expand Down
32 changes: 21 additions & 11 deletions src/main/scala/com/redis/SortedSetOperations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,32 @@ trait SortedSetOperations { self: RedisClient =>
import RedisClient._

def zrange(key: String, start: String, end: String, sortAs: SortOrder, withScores: Boolean ): Option[List[Option[String]]] = {
val command =
val sort =
sortAs match {
case ASC =>
cmd("ZRANGE", key, start, end)
case DESC =>
cmd("ZREVRANGE", key, start, end)
case ASC => "ZRANGE"
case _ => "ZREVRANGE"
}
withScores match {
case true =>
send(command + "WITHSCORES" + Commands.LS)
case false =>
send(command)
}
val command =
withScores match {
case true => cmd(sort, key, start, end, "WITHSCORES")
case _ => cmd(sort, key, start, end)
}
send(command)
asList
}

def zrangeWithScore(key: String, start: String, end: String, sortAs: SortOrder): Option[List[(Option[String], Option[String])]] = {
zrange(key, start, end, sortAs, true) match {
case None => None
case Some(l) => Some(makeTuple(l))
}
}

private def makeTuple(l: List[Option[String]]): List[(Option[String], Option[String])] = l match {
case List() => List()
case a :: b :: rest => (a, b) :: makeTuple(rest)
}

// ZRANGEBYSCORE
//
def zrangebyscore(key: String, min: String, max: String, limit: Option[(String, String)]): Option[List[Option[String]]] = limit match {
Expand Down
65 changes: 65 additions & 0 deletions src/test/scala/com/redis/SortedSetOperationsSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.redis

import org.scalatest.Spec
import org.scalatest.BeforeAndAfterEach
import org.scalatest.BeforeAndAfterAll
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.junit.JUnitRunner
import org.junit.runner.RunWith


@RunWith(classOf[JUnitRunner])
class SortedSetOperationsSpec extends Spec
with ShouldMatchers
with BeforeAndAfterEach
with BeforeAndAfterAll {

val r = new RedisClient("localhost", 6379)

override def beforeEach = {
}

override def afterEach = {
r.flushDb
}

override def afterAll = {
r.disconnect
}

import r._

private def add = {
zadd("hackers", "1965", "yukihiro matsumoto").get should equal(1)
zadd("hackers", "1953", "richard stallman").get should equal(1)
zadd("hackers", "1916", "claude shannon").get should equal(1)
zadd("hackers", "1969", "linus torvalds").get should equal(1)
zadd("hackers", "1940", "alan kay").get should equal(1)
zadd("hackers", "1912", "alan turing").get should equal(1)
}

describe("zadd") {
it("should add based on proper sorted set semantics") {
add
zadd("hackers", "1912", "alan turing").get should equal(0)
zcard("hackers").get should equal(6)
}
}

describe("zrange") {
it("should get the proper range") {
add
zrange("hackers", "0", "-1", RedisClient.ASC, false).get should have size (6)

// should equal(Some(List(Some("alan turing"), Some("claude shannon"), Some("alan kay"), Some("richard stallman"), Some("yukihiro matsumoto"), Some("linus torvalds"))))

zrange("hackers", "0", "-1", RedisClient.ASC, true).get should have size(12)

// should equal(Some(List(Some("alan turing"), Some(1912), Some("claude shannon"), Some(1916), Some("alan kay"), Some(1940), Some("richard stallman"), Some(1953), Some("yukihiro matsumoto"), Some(1965), Some("linus torvalds"), Some(1969))))

zrangeWithScore("hackers", "0", "-1", RedisClient.ASC).get should have size(6)

// should equal (Some(List((Some("alan turing"),Some(1912)), (Some("claude shannon"),Some(1916)), (Some("alan kay"),Some(1940)), (Some("richard stallman"),Some(1953)), (Some("yukihiro matsumoto"),Some(1965)), (Some("linus torvalds"),Some(1969)))))
}
}
}

0 comments on commit f075029

Please sign in to comment.