Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'WithScore' option both to 'zRank' and 'zRevRank' commands #824

Merged
merged 6 commits into from
Apr 15, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docker/redis-cluster-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '3.3'
services:
redis-cluster:
image: 'redis:6.2.7-alpine'
image: redis:7.2-rc1-alpine
container_name: redis-cluster
volumes:
- ./redis-cluster.sh:/data/redis-cluster.sh
Expand Down
4 changes: 2 additions & 2 deletions docker/redis-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ version: "3.2"
services:
redis1:
container_name: test_redis_1
image: redis:6.2.7-alpine
image: redis:7.2-rc1-alpine
ports:
- "6379:6379"

redis2:
container_name: test_redis_2
image: redis:6.2.7-alpine
image: redis:7.2-rc1-alpine
ports:
- "6380:6379"
5 changes: 5 additions & 0 deletions modules/redis/src/main/scala/zio/redis/Input.scala
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,11 @@ object Input {
RespCommand(RespCommandArgument.Literal(data.asString))
}

case object WithScoreInput extends Input[WithScore] {
def encode(data: WithScore): RespCommand =
RespCommand(RespCommandArgument.Literal(data.asString))
}

case object WithScoresInput extends Input[WithScores] {
def encode(data: WithScores): RespCommand =
RespCommand(RespCommandArgument.Literal(data.asString))
Expand Down
79 changes: 60 additions & 19 deletions modules/redis/src/main/scala/zio/redis/api/SortedSets.scala
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,13 @@ trait SortedSets extends RedisEnvironment {
Tuple3(
IntInput,
NonEmptyList(ArbitraryKeyInput[K]()),
ArbitraryValueInput[String]()
WithScoresInput
),
ChunkTuple2Output(ArbitraryOutput[M](), DoubleOutput)
.map(_.map { case (m, s) => MemberScore(s, m) }),
executor
)
command.run((keys.size + 1, (key, keys.toList), WithScores.asString))
command.run((keys.size + 1, (key, keys.toList), WithScores))
}
}

Expand Down Expand Up @@ -366,13 +366,13 @@ trait SortedSets extends RedisEnvironment {
NonEmptyList(ArbitraryKeyInput[K]()),
OptionalInput(AggregateInput),
OptionalInput(WeightsInput),
ArbitraryValueInput[String]()
WithScoresInput
),
ChunkTuple2Output(ArbitraryOutput[M](), DoubleOutput)
.map(_.map { case (m, s) => MemberScore(s, m) }),
executor
)
command.run((keys.size + 1, (key, keys.toList), aggregate, weights, WithScores.asString))
command.run((keys.size + 1, (key, keys.toList), aggregate, weights, WithScores))
}
}

Expand Down Expand Up @@ -558,13 +558,13 @@ trait SortedSets extends RedisEnvironment {
def returning[M: Schema]: IO[RedisError, Chunk[MemberScore[M]]] = {
val command = RedisCommand(
ZRandMember,
Tuple3(ArbitraryKeyInput[K](), LongInput, ArbitraryValueInput[String]()),
Tuple3(ArbitraryKeyInput[K](), LongInput, WithScoresInput),
ZRandMemberTuple2Output(ArbitraryOutput[M](), DoubleOutput)
.map(_.map { case (m, s) => MemberScore(s, m) }),
executor
)

command.run((key, count, WithScores.asString))
command.run((key, count, WithScores))
}
}

Expand Down Expand Up @@ -602,12 +602,12 @@ trait SortedSets extends RedisEnvironment {
def returning[M: Schema]: IO[RedisError, Chunk[MemberScore[M]]] = {
val command = RedisCommand(
ZRange,
Tuple3(ArbitraryKeyInput[K](), RangeInput, ArbitraryValueInput[String]()),
Tuple3(ArbitraryKeyInput[K](), RangeInput, WithScoresInput),
ChunkTuple2Output(ArbitraryOutput[M](), DoubleOutput)
.map(_.map { case (m, s) => MemberScore(s, m) }),
executor
)
command.run((key, range, WithScores.asString))
command.run((key, range, WithScores))
}
}

Expand Down Expand Up @@ -703,26 +703,26 @@ trait SortedSets extends RedisEnvironment {
ArbitraryKeyInput[K](),
ArbitraryValueInput[String](),
ArbitraryValueInput[String](),
ArbitraryValueInput[String](),
WithScoresInput,
OptionalInput(LimitInput)
),
ChunkTuple2Output(ArbitraryOutput[M](), DoubleOutput)
.map(_.map { case (m, s) => MemberScore(s, m) }),
executor
)
command.run((key, scoreRange.min.asString, scoreRange.max.asString, WithScores.asString, limit))
command.run((key, scoreRange.min.asString, scoreRange.max.asString, WithScores, limit))
}
}

/**
* Determine the index of a member in a sorted set.
* Determine the index of a member in a sorted set, with scores ordered from low to high.
*
* @param key
* Key of a sorted set
* @param member
* Member of sorted set
* @return
* The rank of member in the sorted set stored at key, with the scores ordered from low to high.
* The rank of member in the sorted set stored at key.
*/
final def zRank[K: Schema, M: Schema](key: K, member: M): IO[RedisError, Option[Long]] = {
val command =
Expand All @@ -735,6 +735,27 @@ trait SortedSets extends RedisEnvironment {
command.run((key, member))
}

/**
* Determine the index and score of a member in a sorted set, with scores ordered from low to high.
*
* @param key
* Key of a sorted set
* @param member
* Member of sorted set
* @return
* The rank of member along with the score in the sorted set stored at key.
*/
final def zRankWithScore[K: Schema, M: Schema](key: K, member: M): IO[RedisError, Option[RankScore]] = {
val command =
RedisCommand(
ZRank,
Tuple3(ArbitraryKeyInput[K](), ArbitraryValueInput[M](), WithScoreInput),
OptionalOutput(Tuple2Output(LongOutput, DoubleOutput).map { case (r, s) => RankScore(s, r) }),
executor
)
command.run((key, member, WithScore))
}

/**
* Remove one or more members from a sorted set.
*
Expand Down Expand Up @@ -846,12 +867,12 @@ trait SortedSets extends RedisEnvironment {
def returning[M: Schema]: IO[RedisError, Chunk[MemberScore[M]]] = {
val command = RedisCommand(
ZRevRange,
Tuple3(ArbitraryKeyInput[K](), RangeInput, ArbitraryValueInput[String]()),
Tuple3(ArbitraryKeyInput[K](), RangeInput, WithScoresInput),
ChunkTuple2Output(ArbitraryOutput[M](), DoubleOutput)
.map(_.map { case (m, s) => MemberScore(s, m) }),
executor
)
command.run((key, range, WithScores.asString))
command.run((key, range, WithScores))
}
}

Expand Down Expand Up @@ -947,14 +968,14 @@ trait SortedSets extends RedisEnvironment {
ArbitraryKeyInput[K](),
ArbitraryValueInput[String](),
ArbitraryValueInput[String](),
ArbitraryValueInput[String](),
WithScoresInput,
OptionalInput(LimitInput)
),
ChunkTuple2Output(ArbitraryOutput[M](), DoubleOutput)
.map(_.map { case (m, s) => MemberScore(s, m) }),
executor
)
command.run((key, scoreRange.max.asString, scoreRange.min.asString, WithScores.asString, limit))
command.run((key, scoreRange.max.asString, scoreRange.min.asString, WithScores, limit))
}
}

Expand All @@ -966,7 +987,7 @@ trait SortedSets extends RedisEnvironment {
* @param member
* Member of sorted set
* @return
* The rank of member.
* The rank of member in the sorted set stored at key.
*/
final def zRevRank[K: Schema, M: Schema](key: K, member: M): IO[RedisError, Option[Long]] = {
val command = RedisCommand(
Expand All @@ -978,6 +999,26 @@ trait SortedSets extends RedisEnvironment {
command.run((key, member))
}

/**
* Determine the index and score of a member in a sorted set, with scores ordered from high to low.
*
* @param key
* Key of a sorted set
* @param member
* Member of sorted set
* @return
* The rank of member along with the score in the sorted set stored at key.
*/
final def zRevRankWithScore[K: Schema, M: Schema](key: K, member: M): IO[RedisError, Option[RankScore]] = {
val command = RedisCommand(
ZRevRank,
Tuple3(ArbitraryKeyInput[K](), ArbitraryValueInput[M](), WithScoreInput),
OptionalOutput(Tuple2Output(LongOutput, DoubleOutput).map { case (r, s) => RankScore(s, r) }),
executor
)
command.run((key, member, WithScore))
}

/**
* Incrementally iterate sorted sets elements and associated scores.
*
Expand Down Expand Up @@ -1100,13 +1141,13 @@ trait SortedSets extends RedisEnvironment {
NonEmptyList(ArbitraryKeyInput[K]()),
OptionalInput(WeightsInput),
OptionalInput(AggregateInput),
ArbitraryValueInput[String]()
WithScoresInput
),
ChunkTuple2Output(ArbitraryOutput[M](), DoubleOutput)
.map(_.map { case (m, s) => MemberScore(s, m) }),
executor
)
command.run((keys.size + 1, (key, keys.toList), weights, aggregate, WithScores.asString))
command.run((keys.size + 1, (key, keys.toList), weights, aggregate, WithScores))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ trait SortedSets {

type MemberScores[+M] = Chunk[MemberScore[M]]

sealed case class RankScore(score: Double, rank: Long)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
sealed case class RankScore(score: Double, rank: Long)
sealed case class RankScore(rank: Long, score: Double)

We should consider introducing newtypes (maybe through zio-prelude) to represent things like Rank consistently and "cost-free". That's a topic for a separate PR, though.


sealed trait ScoreMaximum { self =>
private[redis] final def asString: String =
self match {
Expand Down Expand Up @@ -114,6 +116,12 @@ trait SortedSets {

sealed case class ScoreRange(min: ScoreMinimum, max: ScoreMaximum)

case object WithScore {
private[redis] def asString: String = "WITHSCORE"
}

type WithScore = WithScore.type

case object WithScores {
private[redis] def asString: String = "WITHSCORES"
}
Expand Down
7 changes: 7 additions & 0 deletions modules/redis/src/test/scala/zio/redis/InputSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,13 @@ object InputSpec extends BaseSpec {
} yield assert(result.args)(isEmpty)
}
),
suite("WithScore")(
test("valid value") {
for {
result <- ZIO.attempt(WithScoreInput.encode(WithScore))
} yield assert(result)(equalTo(RespCommand(Literal("WITHSCORE"))))
}
),
suite("WithScores")(
test("valid value") {
for {
Expand Down
40 changes: 40 additions & 0 deletions modules/redis/src/test/scala/zio/redis/SortedSetsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,23 @@ trait SortedSetsSpec extends BaseSpec {
} yield assert(rank)(isNone)
}
),
suite("zRankWithScore")(
test("existing elements from non-empty set") {
for {
redis <- ZIO.service[Redis]
key <- uuid
_ <- redis.zAdd(key)(MemberScore(1d, "a"), MemberScore(2d, "b"), MemberScore(3d, "c"))
rank <- redis.zRankWithScore(key, "c")
} yield assert(rank)(isSome(equalTo(RankScore(3d, 2L))))
},
test("empty set") {
for {
redis <- ZIO.service[Redis]
key <- uuid
rank <- redis.zRankWithScore(key, "c")
} yield assert(rank)(isNone)
}
),
suite("zRem")(
test("existing elements from non-empty set") {
for {
Expand Down Expand Up @@ -1346,6 +1363,29 @@ trait SortedSetsSpec extends BaseSpec {
} yield assert(result)(isNone)
}
),
suite("zRevRankWithScore")(
test("non-empty set") {
for {
redis <- ZIO.service[Redis]
key <- uuid
_ <- redis.zAdd(key)(
MemberScore(10d, "Delhi"),
MemberScore(20d, "Mumbai"),
MemberScore(30d, "Hyderabad"),
MemberScore(40d, "Kolkata"),
MemberScore(50d, "Chennai")
)
result <- redis.zRevRankWithScore(key, "Kolkata")
} yield assert(result)(isSome(equalTo(RankScore(40d, 1L))))
},
test("empty set") {
for {
redis <- ZIO.service[Redis]
key <- uuid
result <- redis.zRevRankWithScore(key, "Hyderabad")
} yield assert(result)(isNone)
}
),
suite("zScan")(
test("non-empty set") {
for {
Expand Down