-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedis.scala
96 lines (82 loc) · 2.83 KB
/
Redis.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Xiang Zhu
// DS4300 HW4 Part B: 4
import scala.collection.mutable.HashMap
/*
Implement a Scala- based key-value store as a single class.
The class should have the following redis-like methods:
get(key: String): String
set(key: String, value: String)
lpush(key: String, value: String)
rpush(key: String, value: String)
lpop(key: String): String
rpop(key: String): String
lrange(key: String, start: Int, stop: Int): List[String]
llen(key: String)
flushall()
*/
class Redis {
var keyValue = HashMap[String, List[String]]()
// Get the value of key.
def get(key: String): List[String] = {
val value = keyValue get key
value match {
case Some(x) => x
case None => Nil
}
}
// Set key to hold the string value.
def set(key: String, value: String): Unit = {
if (!(keyValue contains key))
(keyValue += (key -> List(value)))
else
(keyValue += (key -> (value +: findValue(key))))
}
// Insert all the specified values at the head of the list stored at key.
// RETURN the length of the list
def lpush(key: String, value: String): Int = {
(keyValue += (key -> (value +: findValue(key))))
llen(key)
}
// Insert all the specified values at the tail of the list stored at key.
// RETURN the length of the list
def rpush(key: String, value: String): Int = {
(keyValue += (key -> (findValue(key) :+ value)))
llen(key)
}
// Removes and returns the first element of the list stored at key.
def lpop(key: String): String = {
val losValue = findValue(key)
(keyValue += (key -> losValue.drop(1)))
losValue.head
}
// Removes and returns the last element of the list stored at key.
def rpop(key: String): String = {
val losValue = findValue(key)
(keyValue += (key -> losValue.dropRight(1)))
losValue.last
}
// Returns the specified elements of the list stored at key.
// The offsets start and stop are zero-based indexes, with 0 being the first element of
// the list (the head of the list), 1 being the next element and so on.
def lrange(key: String, start: Int, stop: Int): List[String] = {
val losValue = findValue(key)
var specifiedElements = List[String]()
if ((start > stop) || (stop > (losValue length)))
throw new Exception("The start index should be less than the end index.")
else
for(i <- start until stop) {
specifiedElements = (specifiedElements :+ losValue(i))
}
return specifiedElements
}
// Returns the length of the list stored at key
def llen(key: String): Int = {
findValue(key) length
}
// reset the class
def flushall() = {
keyValue = HashMap[String, List[String]]()
}
// Returns the value of the key, if not, return empty set.
def findValue(k:String): List[String] = keyValue getOrElse(k, List.empty)
}