Skip to content

Commit 9ba10dd

Browse files
authored
Merge pull request #203 from hunzinker/spop-count
Add spop with count since redis 3.2
2 parents e19efee + 6f4b670 commit 9ba10dd

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/main/scala/com/redis/SetOperations.scala

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ trait SetOperations { self: Redis =>
1919
def spop[A](key: Any)(implicit format: Format, parse: Parse[A]): Option[A] =
2020
send("SPOP", List(key))(asBulk)
2121

22+
// SPOP
23+
// Remove and return multiple random elements (pop) from the Set value at key since (3.2).
24+
def spop[A](key: Any, count: Int)(implicit format: Format, parse: Parse[A]): Option[Set[Option[A]]] =
25+
send("SPOP", List(key, count))(asSet)
26+
2227
// SMOVE
2328
// Move the specified member from one Set to another atomically.
2429
def smove(sourceKey: Any, destKey: Any, value: Any)(implicit format: Format): Option[Long] =
@@ -40,7 +45,7 @@ trait SetOperations { self: Redis =>
4045
send("SINTER", key :: keys.toList)(asSet)
4146

4247
// SINTERSTORE
43-
// Compute the intersection between the Sets stored at key1, key2, ..., keyN,
48+
// Compute the intersection between the Sets stored at key1, key2, ..., keyN,
4449
// and store the resulting Set at dstkey.
4550
// SINTERSTORE returns the size of the intersection, unlike what the documentation says
4651
// refer http://code.google.com/p/redis/issues/detail?id=121
@@ -53,7 +58,7 @@ trait SetOperations { self: Redis =>
5358
send("SUNION", key :: keys.toList)(asSet)
5459

5560
// SUNIONSTORE
56-
// Compute the union between the Sets stored at key1, key2, ..., keyN,
61+
// Compute the union between the Sets stored at key1, key2, ..., keyN,
5762
// and store the resulting Set at dstkey.
5863
// SUNIONSTORE returns the size of the union, unlike what the documentation says
5964
// refer http://code.google.com/p/redis/issues/detail?id=121
@@ -66,7 +71,7 @@ trait SetOperations { self: Redis =>
6671
send("SDIFF", key :: keys.toList)(asSet)
6772

6873
// SDIFFSTORE
69-
// Compute the difference between the Set key1 and all the Sets key2, ..., keyN,
74+
// Compute the difference between the Set key1 and all the Sets key2, ..., keyN,
7075
// and store the resulting Set at dstkey.
7176
def sdiffstore(key: Any, keys: Any*)(implicit format: Format): Option[Long] =
7277
send("SDIFFSTORE", key :: keys.toList)(asLong)

src/test/scala/com/redis/SetOperationsSpec.scala

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import org.junit.runner.RunWith
99

1010

1111
@RunWith(classOf[JUnitRunner])
12-
class SetOperationsSpec extends FunSpec
12+
class SetOperationsSpec extends FunSpec
1313
with Matchers
1414
with BeforeAndAfterEach
1515
with BeforeAndAfterAll {
@@ -90,6 +90,27 @@ class SetOperationsSpec extends FunSpec
9090
}
9191
}
9292

93+
describe("spop with count") {
94+
it("should pop a list of random members") {
95+
r.sadd("set-1", "one").get should equal(1)
96+
r.sadd("set-1", "two").get should equal(1)
97+
r.sadd("set-1", "three").get should equal(1)
98+
r.sadd("set-1", "four").get should equal(1)
99+
r.sadd("set-1", "five").get should equal(1)
100+
r.sadd("set-1", "six").get should equal(1)
101+
r.sadd("set-1", "seven").get should equal(1)
102+
r.sadd("set-1", "eight").get should equal(1)
103+
104+
r.spop("set-1", 2).get.size should equal(2)
105+
106+
// if supplied count > size, then whole set is returned
107+
r.spop("set-1", 24).get.size should equal(6)
108+
109+
// if empty, returned set is empty
110+
r.spop("set-1", 5).get shouldBe empty
111+
}
112+
}
113+
93114
describe("smove") {
94115
it("should move from one set to another") {
95116
r.sadd("set-1", "foo").get should equal(1)
@@ -171,7 +192,7 @@ class SetOperationsSpec extends FunSpec
171192
r.sadd("set-1", "foo").get should equal(1)
172193
r.sadd("set-1", "bar").get should equal(1)
173194
r.sadd("set-1", "baz").get should equal(1)
174-
r.sinter("set-1", "set-4") should equal(Some(Set()))
195+
r.sinter("set-1", "set-4") should equal(Some(Set()))
175196
}
176197
}
177198

0 commit comments

Comments
 (0)