Skip to content

Commit a67f510

Browse files
author
Roman Janusz
committed
redis: ZINTER
1 parent eb34843 commit a67f510

File tree

2 files changed

+74
-6
lines changed

2 files changed

+74
-6
lines changed

commons-redis/src/main/scala/com/avsystem/commons/redis/commands/sortedSets.scala

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,41 @@ trait SortedSetsApi extends ApiSubset {
7373
def zincrby(key: Key, increment: Double, member: Value): Result[Double] =
7474
execute(new Zincrby(key, increment, member))
7575

76+
/** Executes [[http://redis.io/commands/zinter ZINTER]] */
77+
def zinter(key: Key, keys: Key*): Result[Seq[Value]] =
78+
zinter(key +:: keys)
79+
80+
/** Executes [[http://redis.io/commands/zinter ZINTER]] */
81+
def zinter(keys: Iterable[Key], aggregation: OptArg[Aggregation] = OptArg.Empty): Result[Seq[Value]] =
82+
execute(new Zinter(keys, Opt.Empty, aggregation.toOpt))
83+
84+
/** Executes [[http://redis.io/commands/zinter ZINTER]] */
85+
def zinterWeights(keyWeight: (Key, Double), keysWeights: (Key, Double)*): Result[Seq[Value]] =
86+
zinterWeights(keyWeight +:: keysWeights)
87+
88+
/** Executes [[http://redis.io/commands/zinter ZINTER]] */
89+
def zinterWeights(keysWeights: Iterable[(Key, Double)], aggregation: OptArg[Aggregation] = OptArg.Empty): Result[Seq[Value]] =
90+
execute(new Zinter(keysWeights.map(_._1), keysWeights.map(_._2).opt, aggregation.toOpt))
91+
92+
/** Executes [[http://redis.io/commands/zinter ZINTER]] */
93+
def zinterWithscores(key: Key, keys: Key*): Result[Seq[(Value, Double)]] =
94+
zinterWithscores(key +:: keys)
95+
96+
/** Executes [[http://redis.io/commands/zinter ZINTER]] */
97+
def zinterWithscores(keys: Iterable[Key], aggregation: OptArg[Aggregation] = OptArg.Empty): Result[Seq[(Value, Double)]] =
98+
execute(new ZinterWithscores(keys, Opt.Empty, aggregation.toOpt))
99+
100+
/** Executes [[http://redis.io/commands/zinter ZINTER]] */
101+
def zinterWeightsWithscores(keyWeight: (Key, Double), keysWeights: (Key, Double)*): Result[Seq[(Value, Double)]] =
102+
zinterWeightsWithscores(keyWeight +:: keysWeights)
103+
104+
/** Executes [[http://redis.io/commands/zinter ZINTER]] */
105+
def zinterWeightsWithscores(keysWeights: Iterable[(Key, Double)], aggregation: OptArg[Aggregation] = OptArg.Empty): Result[Seq[(Value, Double)]] =
106+
execute(new ZinterWithscores(keysWeights.map(_._1), keysWeights.map(_._2).opt, aggregation.toOpt))
107+
76108
/** Executes [[http://redis.io/commands/zinterstore ZINTERSTORE]] */
77-
def zinterstore(destination: Key, key: Key, keys: Key*): Result[Long] = zinterstore(destination, key +:: keys)
109+
def zinterstore(destination: Key, key: Key, keys: Key*): Result[Long] =
110+
zinterstore(destination, key +:: keys)
78111

79112
/** Executes [[http://redis.io/commands/zinterstore ZINTERSTORE]]
80113
* NOTE: `keys` MUST NOT be empty */
@@ -265,12 +298,24 @@ trait SortedSetsApi extends ApiSubset {
265298
val encoded: Encoded = encoder("ZINCRBY").key(key).add(increment).data(member).result
266299
}
267300

301+
private final class Zinter(keys: Iterable[Key], weights: Opt[Iterable[Double]], aggregation: Opt[Aggregation])
302+
extends RedisDataSeqCommand[Value] with NodeCommand {
303+
val encoded: Encoded = encoder("ZINTER").add(keys.size).keys(keys)
304+
.optAdd("WEIGHTS", weights).optAdd("AGGREGATE", aggregation).result
305+
}
306+
268307
private final class Zinterstore(destination: Key, keys: Iterable[Key], weights: Opt[Iterable[Double]], aggregation: Opt[Aggregation])
269308
extends RedisLongCommand with NodeCommand {
270309
val encoded: Encoded = encoder("ZINTERSTORE").key(destination).add(keys.size).keys(keys)
271310
.optAdd("WEIGHTS", weights).optAdd("AGGREGATE", aggregation).result
272311
}
273312

313+
private final class ZinterWithscores(keys: Iterable[Key], weights: Opt[Iterable[Double]], aggregation: Opt[Aggregation])
314+
extends AbstractRedisCommand[Seq[(Value, Double)]](flatMultiBulkAsPairSeq(bulkAs[Value], bulkAsDouble)) with NodeCommand {
315+
val encoded: Encoded = encoder("ZINTER").add(keys.size).keys(keys)
316+
.optAdd("WEIGHTS", weights).optAdd("AGGREGATE", aggregation).add("WITHSCORES").result
317+
}
318+
274319
private final class Zlexcount(key: Key, min: LexLimit[Value], max: LexLimit[Value])
275320
extends RedisLongCommand with NodeCommand {
276321
val encoded: Encoded = encoder("ZLEXCOUNT").key(key).add(LexLimit.repr(min)).add(LexLimit.repr(max)).result
@@ -306,8 +351,8 @@ trait SortedSetsApi extends ApiSubset {
306351
}
307352

308353
private abstract class AbstractZrangebyscore[T](cmd: String, decoder: ReplyDecoder[Seq[T]])(
309-
key: Key, firstLimit: ScoreLimit, secondLimit: ScoreLimit, withscores: Boolean, limit: Opt[Limit])
310-
extends AbstractRedisCommand[Seq[T]](decoder) with NodeCommand {
354+
key: Key, firstLimit: ScoreLimit, secondLimit: ScoreLimit, withscores: Boolean, limit: Opt[Limit]
355+
) extends AbstractRedisCommand[Seq[T]](decoder) with NodeCommand {
311356
val encoded: Encoded =
312357
encoder(cmd).key(key).add(firstLimit.repr).add(secondLimit.repr)
313358
.addFlag("WITHSCORES", withscores).optAdd("LIMIT", limit).result

commons-redis/src/test/scala/com/avsystem/commons/redis/commands/SortedSetsApiSuite.scala

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ trait SortedSetsApiSuite extends CommandsSuite {
4040
}
4141

4242
apiTest("ZDIFF") {
43-
setup(zadd("{key}1", "lol" -> 1.0, "fuu" -> 2.0, "oof" -> 3.0))
44-
setup(zadd("{key}2", "lol" -> 1.0, "fag" -> 2.0))
45-
setup(zadd("{key}3", "fuu" -> 1.0, "fag" -> 2.0))
43+
setup(
44+
zadd("{key}1", "lol" -> 1.0, "fuu" -> 2.0, "oof" -> 3.0),
45+
zadd("{key}2", "lol" -> 1.0, "fag" -> 2.0),
46+
zadd("{key}3", "fuu" -> 1.0, "fag" -> 2.0)
47+
)
4648
zdiff("{key}1", "{key}2").assertEquals(Seq("fuu", "oof"))
4749
zdiff("{key}1", "{key}2", "{key}3").assertEquals(Seq("oof"))
4850
zdiffWithscores("{key}1", "{key}2").assertEquals(Seq("fuu" -> 2.0, "oof" -> 3.0))
@@ -60,6 +62,27 @@ trait SortedSetsApiSuite extends CommandsSuite {
6062
zincrby("key", 1.0, "value").assertEquals(2.0)
6163
}
6264

65+
apiTest("ZINTER") {
66+
setup(
67+
zadd("{key}1", "foo" -> 1.0, "bar" -> 2.0),
68+
zadd("{key}2", "bar" -> 3.0, "lol" -> 4.0)
69+
)
70+
zinter( "{key}1", "{key}2").assertEquals(Seq("bar"))
71+
zinterWithscores( "{key}1", "{key}2").assertEquals(Seq("bar" -> 5.0))
72+
zinterWithscores( Seq("{key}1", "{key}2"), Aggregation.Sum).assertEquals(Seq("bar" -> 5.0))
73+
zinterWithscores( Seq("{key}1", "{key}2"), Aggregation.Min).assertEquals(Seq("bar" -> 2.0))
74+
zinterWithscores( Seq("{key}1", "{key}2"), Aggregation.Max).assertEquals(Seq("bar" -> 3.0))
75+
}
76+
77+
apiTest("ZINTER with WEIGHTS") {
78+
setup(
79+
zadd("{key}1", "foo" -> 1.0, "bar" -> 2.0),
80+
zadd("{key}2", "bar" -> 3.0, "lol" -> 4.0)
81+
)
82+
zinterWeights("{key}1" -> 1.0, "{key}2" -> 2.0).assertEquals(Seq("bar"))
83+
zinterWeightsWithscores("{key}1" -> 1.0, "{key}2" -> 2.0).assertEquals(Seq("bar" -> 8.0))
84+
}
85+
6386
apiTest("ZINTERSTORE") {
6487
setup(
6588
zadd("{key}1", "foo" -> 1.0, "bar" -> 2.0),

0 commit comments

Comments
 (0)