Skip to content

Commit

Permalink
redis: ZINTER
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Janusz committed Oct 20, 2021
1 parent eb34843 commit a67f510
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 6 deletions.
Expand Up @@ -73,8 +73,41 @@ trait SortedSetsApi extends ApiSubset {
def zincrby(key: Key, increment: Double, member: Value): Result[Double] =
execute(new Zincrby(key, increment, member))

/** Executes [[http://redis.io/commands/zinter ZINTER]] */
def zinter(key: Key, keys: Key*): Result[Seq[Value]] =
zinter(key +:: keys)

/** Executes [[http://redis.io/commands/zinter ZINTER]] */
def zinter(keys: Iterable[Key], aggregation: OptArg[Aggregation] = OptArg.Empty): Result[Seq[Value]] =
execute(new Zinter(keys, Opt.Empty, aggregation.toOpt))

/** Executes [[http://redis.io/commands/zinter ZINTER]] */
def zinterWeights(keyWeight: (Key, Double), keysWeights: (Key, Double)*): Result[Seq[Value]] =
zinterWeights(keyWeight +:: keysWeights)

/** Executes [[http://redis.io/commands/zinter ZINTER]] */
def zinterWeights(keysWeights: Iterable[(Key, Double)], aggregation: OptArg[Aggregation] = OptArg.Empty): Result[Seq[Value]] =
execute(new Zinter(keysWeights.map(_._1), keysWeights.map(_._2).opt, aggregation.toOpt))

/** Executes [[http://redis.io/commands/zinter ZINTER]] */
def zinterWithscores(key: Key, keys: Key*): Result[Seq[(Value, Double)]] =
zinterWithscores(key +:: keys)

/** Executes [[http://redis.io/commands/zinter ZINTER]] */
def zinterWithscores(keys: Iterable[Key], aggregation: OptArg[Aggregation] = OptArg.Empty): Result[Seq[(Value, Double)]] =
execute(new ZinterWithscores(keys, Opt.Empty, aggregation.toOpt))

/** Executes [[http://redis.io/commands/zinter ZINTER]] */
def zinterWeightsWithscores(keyWeight: (Key, Double), keysWeights: (Key, Double)*): Result[Seq[(Value, Double)]] =
zinterWeightsWithscores(keyWeight +:: keysWeights)

/** Executes [[http://redis.io/commands/zinter ZINTER]] */
def zinterWeightsWithscores(keysWeights: Iterable[(Key, Double)], aggregation: OptArg[Aggregation] = OptArg.Empty): Result[Seq[(Value, Double)]] =
execute(new ZinterWithscores(keysWeights.map(_._1), keysWeights.map(_._2).opt, aggregation.toOpt))

/** Executes [[http://redis.io/commands/zinterstore ZINTERSTORE]] */
def zinterstore(destination: Key, key: Key, keys: Key*): Result[Long] = zinterstore(destination, key +:: keys)
def zinterstore(destination: Key, key: Key, keys: Key*): Result[Long] =
zinterstore(destination, key +:: keys)

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

private final class Zinter(keys: Iterable[Key], weights: Opt[Iterable[Double]], aggregation: Opt[Aggregation])
extends RedisDataSeqCommand[Value] with NodeCommand {
val encoded: Encoded = encoder("ZINTER").add(keys.size).keys(keys)
.optAdd("WEIGHTS", weights).optAdd("AGGREGATE", aggregation).result
}

private final class Zinterstore(destination: Key, keys: Iterable[Key], weights: Opt[Iterable[Double]], aggregation: Opt[Aggregation])
extends RedisLongCommand with NodeCommand {
val encoded: Encoded = encoder("ZINTERSTORE").key(destination).add(keys.size).keys(keys)
.optAdd("WEIGHTS", weights).optAdd("AGGREGATE", aggregation).result
}

private final class ZinterWithscores(keys: Iterable[Key], weights: Opt[Iterable[Double]], aggregation: Opt[Aggregation])
extends AbstractRedisCommand[Seq[(Value, Double)]](flatMultiBulkAsPairSeq(bulkAs[Value], bulkAsDouble)) with NodeCommand {
val encoded: Encoded = encoder("ZINTER").add(keys.size).keys(keys)
.optAdd("WEIGHTS", weights).optAdd("AGGREGATE", aggregation).add("WITHSCORES").result
}

private final class Zlexcount(key: Key, min: LexLimit[Value], max: LexLimit[Value])
extends RedisLongCommand with NodeCommand {
val encoded: Encoded = encoder("ZLEXCOUNT").key(key).add(LexLimit.repr(min)).add(LexLimit.repr(max)).result
Expand Down Expand Up @@ -306,8 +351,8 @@ trait SortedSetsApi extends ApiSubset {
}

private abstract class AbstractZrangebyscore[T](cmd: String, decoder: ReplyDecoder[Seq[T]])(
key: Key, firstLimit: ScoreLimit, secondLimit: ScoreLimit, withscores: Boolean, limit: Opt[Limit])
extends AbstractRedisCommand[Seq[T]](decoder) with NodeCommand {
key: Key, firstLimit: ScoreLimit, secondLimit: ScoreLimit, withscores: Boolean, limit: Opt[Limit]
) extends AbstractRedisCommand[Seq[T]](decoder) with NodeCommand {
val encoded: Encoded =
encoder(cmd).key(key).add(firstLimit.repr).add(secondLimit.repr)
.addFlag("WITHSCORES", withscores).optAdd("LIMIT", limit).result
Expand Down
Expand Up @@ -40,9 +40,11 @@ trait SortedSetsApiSuite extends CommandsSuite {
}

apiTest("ZDIFF") {
setup(zadd("{key}1", "lol" -> 1.0, "fuu" -> 2.0, "oof" -> 3.0))
setup(zadd("{key}2", "lol" -> 1.0, "fag" -> 2.0))
setup(zadd("{key}3", "fuu" -> 1.0, "fag" -> 2.0))
setup(
zadd("{key}1", "lol" -> 1.0, "fuu" -> 2.0, "oof" -> 3.0),
zadd("{key}2", "lol" -> 1.0, "fag" -> 2.0),
zadd("{key}3", "fuu" -> 1.0, "fag" -> 2.0)
)
zdiff("{key}1", "{key}2").assertEquals(Seq("fuu", "oof"))
zdiff("{key}1", "{key}2", "{key}3").assertEquals(Seq("oof"))
zdiffWithscores("{key}1", "{key}2").assertEquals(Seq("fuu" -> 2.0, "oof" -> 3.0))
Expand All @@ -60,6 +62,27 @@ trait SortedSetsApiSuite extends CommandsSuite {
zincrby("key", 1.0, "value").assertEquals(2.0)
}

apiTest("ZINTER") {
setup(
zadd("{key}1", "foo" -> 1.0, "bar" -> 2.0),
zadd("{key}2", "bar" -> 3.0, "lol" -> 4.0)
)
zinter( "{key}1", "{key}2").assertEquals(Seq("bar"))
zinterWithscores( "{key}1", "{key}2").assertEquals(Seq("bar" -> 5.0))
zinterWithscores( Seq("{key}1", "{key}2"), Aggregation.Sum).assertEquals(Seq("bar" -> 5.0))
zinterWithscores( Seq("{key}1", "{key}2"), Aggregation.Min).assertEquals(Seq("bar" -> 2.0))
zinterWithscores( Seq("{key}1", "{key}2"), Aggregation.Max).assertEquals(Seq("bar" -> 3.0))
}

apiTest("ZINTER with WEIGHTS") {
setup(
zadd("{key}1", "foo" -> 1.0, "bar" -> 2.0),
zadd("{key}2", "bar" -> 3.0, "lol" -> 4.0)
)
zinterWeights("{key}1" -> 1.0, "{key}2" -> 2.0).assertEquals(Seq("bar"))
zinterWeightsWithscores("{key}1" -> 1.0, "{key}2" -> 2.0).assertEquals(Seq("bar" -> 8.0))
}

apiTest("ZINTERSTORE") {
setup(
zadd("{key}1", "foo" -> 1.0, "bar" -> 2.0),
Expand Down

0 comments on commit a67f510

Please sign in to comment.