]> git.saurik.com Git - redis.git/blob - client-libraries/scala/src/main/scala/com/redis/Operations/SetOperations.scala
13432d35a7309556647e0963dba66b8f9e377c20
[redis.git] / client-libraries / scala / src / main / scala / com / redis / Operations / SetOperations.scala
1 package com.redis.operations
2
3 /**
4 * Redis set operations
5 *
6 */
7
8 trait SetOperations{
9
10 def getConnection(key: String): Connection
11
12 // SADD
13 // Add the specified member to the set value stored at key.
14 def setAdd(key: String, value: String): Boolean = {
15 val connection = getConnection(key)
16 connection.write("SADD "+key+" "+value.length+"\r\n"+value+"\r\n")
17 connection.readBoolean
18 }
19
20 // SREM
21 // Remove the specified member from the set value stored at key.
22 def setDelete(key: String, value: String): Boolean = {
23 val connection = getConnection(key)
24 connection.write("SREM "+key+" "+value.length+"\r\n"+value+"\r\n")
25 connection.readBoolean
26 }
27
28 // SCARD
29 // Return the number of elements (the cardinality) of the Set at key.
30 def setCount(key: String): Int = {
31 val connection = getConnection(key)
32 connection.write("SCARD "+key+"\r\n")
33 connection.readInt
34 }
35
36 // SMEMBERS
37 // Return all the members of the Set value at key.
38 def setMembers(key: String): Set[String] = {
39 val connection = getConnection(key)
40 connection.write("SMEMBERS "+key+"\r\n")
41 connection.readSet
42 }
43
44 // SPOP
45 // Remove and return (pop) a random element from the Set value at key.
46 def setPop(key: String): String = {
47 val connection = getConnection(key)
48 connection.write("SPOP "+key+"\r\n")
49 connection.readString
50 }
51
52 // SMOVE
53 // Move the specified member from one Set to another atomically.
54 def setMove(sourceKey: String, destKey: String, value: String): Boolean = {
55 val connection = getConnection(sourceKey)
56 connection.write("SMOVE "+sourceKey+" "+destKey+" "+value+"\r\n")
57 connection.readBoolean
58 }
59
60 // SISMEMBER
61 // Test if the specified value is a member of the Set at key.
62 def setMemberExists(key: String, value: String): Boolean = {
63 val connection = getConnection(key)
64 connection.write("SISMEMBER "+key+" "+value.length+"\r\n"+value+"\r\n")
65 connection.readBoolean
66 }
67
68 // SINTER
69 // Return the intersection between the Sets stored at key1, key2, ..., keyN.
70 def setIntersect(keys: String*): Set[String] = {
71 val connection = getConnection(keys(0))
72 connection.write("SINTER "+keys.mkString(" ")+"\r\n")
73 connection.readSet
74 }
75
76 // SINTERSTORE
77 // Compute the intersection between the Sets stored at key1, key2, ..., keyN, and store the resulting Set at dstkey.
78 def setInterStore(key: String, keys: String*): Boolean = {
79 val connection = getConnection(key)
80 connection.write("SINTERSTORE "+key+" "+keys.mkString(" ")+"\r\n")
81 connection.readBoolean
82 }
83
84 // SDIFF
85 // Return the difference between the Set stored at key1 and all the Sets key2, ..., keyN.
86 def setDiff(keys: String*): Set[String] = {
87 val connection = getConnection(keys(0))
88 connection.write("SDIFF "+keys.mkString(" ")+"\r\n")
89 connection.readSet
90 }
91
92 // SDIFFSTORE
93 // Compute the difference between the Set key1 and all the Sets key2, ..., keyN, and store the resulting Set at dstkey.
94 def setDiffStore(key: String, keys: String*): Boolean = {
95 val connection = getConnection(key)
96 connection.write("SDIFFSTORE "+key+" "+keys.mkString(" ")+"\r\n")
97 connection.readBoolean
98 }
99
100 // SUNION
101 // Return the union between the Sets stored at key1, key2, ..., keyN.
102 def setUnion(keys: String*): Set[String] = {
103 val connection = getConnection(keys(0))
104 connection.write("SUNION "+keys.mkString(" ")+"\r\n")
105 connection.readSet
106 }
107
108 // SUNIONSTORE
109 // Compute the union between the Sets stored at key1, key2, ..., keyN, and store the resulting Set at dstkey.
110 def setUnionStore(key: String, keys: String*): Boolean = {
111 val connection = getConnection(key)
112 connection.write("SUNIONSTORE "+key+" "+keys.mkString(" ")+"\r\n")
113 connection.readBoolean
114 }
115 }