Skip to content

Commit

Permalink
Merge remote branch 'upstream/master'
Browse files Browse the repository at this point in the history
Conflicts:
	project/build.properties
  • Loading branch information
kevinpet authored and Kevin Peterson committed Apr 26, 2011
2 parents d045321 + bdf2d79 commit 47091f2
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 36 deletions.
4 changes: 2 additions & 2 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ let's connect and get a key:

scala> import com.redis._
scala> val r = new Redis("localhost", 6379)
scala> val r.set("key", "some value")
scala> val r.get("key")
scala> r.set("key", "some value")
scala> r.get("key")


Alejandro Crosa <<alejandrocrosa@gmail.com>>
Expand Down
6 changes: 3 additions & 3 deletions project/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#Mon Jun 21 11:31:01 PDT 2010
project.organization=com.redis
project.name=RedisClient
sbt.version=0.7.3
project.version=1.0.2
build.scala.versions=2.8.0.RC6
sbt.version=0.7.4
project.version=1.0.3
build.scala.versions=2.8.1
project.initialize=false
28 changes: 14 additions & 14 deletions src/main/scala/com/redis/HashRing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,52 @@ import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.Map

trait HashRing {

val replicas: Int

var sortedKeys: List[Long] = List()
var cluster = new ArrayBuffer[Redis]
val ring = Map[Long, Redis]()

// Adds the node to the hashRing
// including a number of replicas.
def addNode(node: Redis) = {
cluster += node
(1 to replicas).foreach{ replica =>
(1 to replicas).foreach{ replica =>
val key = calculateChecksum(node+":"+replica)
ring += (key -> node)
sortedKeys = sortedKeys ::: List(key)
}
sortedKeys = sortedKeys.sort(_ < _)
sortedKeys = sortedKeys.sortWith(_ < _)
}

// get the node in the hash ring for this key
def getNode(key: String) = getNodePos(key)._1

def getNodePos(key: String): (Redis, Int) = {
val crc = calculateChecksum(key)
val idx = binarySearch(crc)
(ring(sortedKeys(idx)), idx)
}

// TODO this should perform a Bynary search
def binarySearch(value: Long): Int = {
var upper = (sortedKeys.length -1)
var lower = 0
var idx = 0
var comp: Long = 0

while(lower <= upper){
idx = (lower + upper) / 2
comp = sortedKeys(idx)

if(comp == value) { return idx }
if(comp < value) { upper = idx -1 }
if(comp > value) { lower = idx +1 }
if(comp < value) { upper = idx - 1 }
if(comp > value) { lower = idx + 1 }
}
return upper
return (upper + sortedKeys.length) % sortedKeys.length
}

// Computes the CRC-32 of the given String
def calculateChecksum(value: String): Long = {
val checksum = new CRC32
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/com/redis/Operations/ListOperations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ trait ListOperations{
// LRANGE
// return the specified elements of the list stored at the specified key.
// Start and end are zero-based indexes. 0 is the first element of the list (the list head), 1 the next element and so on.
def listRange(key: String, start: Int, end: Int): Option[List[String]] = {
def listRange(key: String, start: Int, end: Int): Option[List[Option[String]]] = {
val connection = getConnection(key)
connection.write("LRANGE "+key+" "+start+" "+end+"\r\n")
connection.readList
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/com/redis/Operations/NodeOperations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ trait NodeOperations {
// MGET (key, key, key, ...)
// get the values of all the specified keys.
def mget(keys: String*) = {
connection.write("MGET "+keys.mkString(" ")+"\r\n")
connection.writeMultiBulk(keys.size, "MGET", keys)
connection.readList
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/com/redis/Operations/SortOperations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ trait SortOperations{

// SORT
// Sort a Set or a List accordingly to the specified parameters.
def sort(args: Any): Option[List[String]] = args match {
def sort(args: Any): Option[List[Option[String]]] = args match {
case (key: String, command: String) => doSort(key, command)
case (key: String) => doSort(key, "")
}

def doSort(key: String, command: String): Option[List[String]] = {
def doSort(key: String, command: String): Option[List[Option[String]]] = {
val connection = getConnection(key)
if(command != "") {
connection.write("SORT "+key+" "+command+"\r\n")
Expand Down
17 changes: 9 additions & 8 deletions src/main/scala/com/redis/SocketOperations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ trait SocketOperations {
case _ => false
}
}
def readList: Option[List[String]] = {
def readList: Option[List[Option[String]]] = {
readResponse match {
case Some(s: String) => listReply(s)
case _ => None
Expand Down Expand Up @@ -163,15 +163,12 @@ trait SocketOperations {
}
}

def listReply(response: String): Option[List[String]] = {
def listReply(response: String): Option[List[Option[String]]] = {
val total = Integer.parseInt(response.split('*')(1))
if(total != -1) {
var list: List[String] = List()
(1 to total).foreach { i =>
bulkReply(readtype._2) match {
case Some(s) => list = (list ::: List(s))
case _ => None
}
var list: List[Option[String]] = List()
(1 to total).foreach { i =>
list = list ::: List(bulkReply(readtype._2))
}
Some(list)
} else {
Expand Down Expand Up @@ -214,6 +211,10 @@ trait SocketOperations {
def writeMultiBulk(size: Int, command: String, arguments: Map[String, String]) = {
write("*"+ (size+1) +"\r\n"+ bulkFormat(command) + mapToMultiBulkFormat(arguments))
}

def writeMultiBulk(size: Int, command: String, arguments: Seq[String]) = {
write("*"+ (size+1) +"\r\n"+ bulkFormat(command) + arguments.map(bulkFormat(_)).mkString)
}

def bulkFormat(value: String): String = "$"+ value.length+"\r\n"+ value +"\r\n"

Expand Down
4 changes: 2 additions & 2 deletions src/test/scala/com/redis/operations/ListOperationsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ object ListOperationsSpec extends Specification with Mockito {
}

"return list range" in {
val listResult = Some(List("one", "two", "three", "four", "five"))
val listResult = Some(List("one", "two", "three", "four", "five").map(Some(_)))
connection.readList returns listResult
client.listRange("k", 2, 4) mustEqual listResult
connection.write("LRANGE k 2 4\r\n") was called
Expand All @@ -84,4 +84,4 @@ object ListOperationsSpec extends Specification with Mockito {
connection.write("RPOPLPUSH a b\r\n") was called
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object NodeOperationsSpec extends Specification with Mockito {
}

"return all specified keys" in {
val list = Some(List[String]("hola", null, null))
val list = Some(List[Option[String]](Some("hola"), None, None))
connection.readList returns list
client.mget("a", "b", "c") mustEqual list
connection.write("MGET a b c\r\n") was called
Expand Down
4 changes: 2 additions & 2 deletions src/test/scala/com/redis/operations/SortOperationsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ object SortOperationsSpec extends Specification with Mockito {
}

"sort the contents of the specified key" in {
val listResult = Some(List("one", "two", "three"))
val listResult = Some(List("one", "two", "three").map(Some(_)))
connection.readList returns listResult
client.sort("set", "ALPHA DESC") mustEqual listResult
connection.write("SORT set ALPHA DESC\r\n") was called
}

"sort the contents of the specified key with default" in {
val listResult = Some(List("one", "two", "three"))
val listResult = Some(List("one", "two", "three").map(Some(_)))
connection.readList returns listResult
client.sort("set") mustEqual listResult
connection.write("SORT set\r\n") was called
Expand Down

0 comments on commit 47091f2

Please sign in to comment.