Skip to content

Commit

Permalink
Introduce 'UpdateByScore'
Browse files Browse the repository at this point in the history
  • Loading branch information
drmarjanovic committed Sep 16, 2024
1 parent f815857 commit 2292dd1
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 17 deletions.
12 changes: 9 additions & 3 deletions modules/redis-it/src/test/scala/zio/redis/SortedSetsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,10 @@ trait SortedSetsSpec extends IntegrationSpec {
key <- uuid
_ <- redis.zAdd(key)(MemberScore("v1", 3d))
_ <- redis.zAdd(key)(MemberScore("v2", 4d))
added <- redis.zAdd(key, update = Some(Update.SetLessThan))(MemberScore("v3", 1d), MemberScore("v1", 2d))
added <- redis.zAdd(key, updateByScore = Some(UpdateByScore.SetLessThan))(
MemberScore("v3", 1d),
MemberScore("v1", 2d)
)
result <- redis.zRange(key, 0 to -1).returning[String]
} yield assert(added)(equalTo(1L)) && assert(result.toList)(equalTo(List("v3", "v1", "v2")))
},
Expand All @@ -193,7 +196,10 @@ trait SortedSetsSpec extends IntegrationSpec {
key <- uuid
_ <- redis.zAdd(key)(MemberScore("v1", 1d))
_ <- redis.zAdd(key)(MemberScore("v2", 2d))
added <- redis.zAdd(key, update = Some(Update.SetGreaterThan))(MemberScore("v3", 1d), MemberScore("v1", 3d))
added <- redis.zAdd(key, updateByScore = Some(UpdateByScore.SetGreaterThan))(
MemberScore("v3", 1d),
MemberScore("v1", 3d)
)
result <- redis.zRange(key, 0 to -1).returning[String]
} yield assert(added)(equalTo(1L)) && assert(result.toList)(equalTo(List("v3", "v2", "v1")))
},
Expand All @@ -203,7 +209,7 @@ trait SortedSetsSpec extends IntegrationSpec {
key <- uuid
_ <- redis.zAdd(key)(MemberScore("v1", 1d))
_ <- redis.zAdd(key)(MemberScore("v2", 2d))
added <- redis.zAdd(key, update = Some(Update.SetGreaterThan), change = Some(Changed))(
added <- redis.zAdd(key, updateByScore = Some(UpdateByScore.SetGreaterThan), change = Some(Changed))(
MemberScore("v3", 1d),
MemberScore("v1", 3d)
)
Expand Down
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 @@ -584,6 +584,11 @@ object Input {
RespCommand(RespCommandArgument.Value(data.asString))
}

case object UpdateByScoreInput extends Input[UpdateByScore] {
def encode(data: UpdateByScore): RespCommand =
RespCommand(RespCommandArgument.Value(data.asString))
}

case object ValueInput extends Input[Chunk[Byte]] {
def encode(data: Chunk[Byte]): RespCommand =
RespCommand(RespCommandArgument.Value(data))
Expand Down
24 changes: 18 additions & 6 deletions modules/redis/src/main/scala/zio/redis/api/SortedSets.scala
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,27 @@ trait SortedSets[G[+_]] extends RedisEnvironment[G] {
* The number of elements added to the sorted set, not including elements already existing for which the score was
* updated.
*/
final def zAdd[K: Schema, M: Schema](key: K, update: Option[Update] = None, change: Option[Changed] = None)(
final def zAdd[K: Schema, M: Schema](
key: K,
update: Option[Update] = None,
updateByScore: Option[UpdateByScore] = None,
change: Option[Changed] = None
)(
memberScore: MemberScore[M],
memberScores: MemberScore[M]*
): G[Long] = {
val command = RedisCommand(
ZAdd,
Tuple4(
Tuple5(
ArbitraryKeyInput[K](),
OptionalInput(UpdateInput),
OptionalInput(UpdateByScoreInput),
OptionalInput(ChangedInput),
NonEmptyList(MemberScoreInput[M]())
),
LongOutput
)
command.run((key, update, change, (memberScore, memberScores.toList)))
command.run((key, update, updateByScore, change, (memberScore, memberScores.toList)))
}

/**
Expand All @@ -160,23 +166,29 @@ trait SortedSets[G[+_]] extends RedisEnvironment[G] {
* The new score of member (a double precision floating point number), or None if the operation was aborted (when
* called with either the XX or the NX option).
*/
final def zAddWithIncr[K: Schema, M: Schema](key: K, update: Option[Update] = None, change: Option[Changed] = None)(
final def zAddWithIncr[K: Schema, M: Schema](
key: K,
update: Option[Update] = None,
updateByScore: Option[UpdateByScore] = None,
change: Option[Changed] = None
)(
increment: Increment,
memberScore: MemberScore[M],
memberScores: MemberScore[M]*
): G[Option[Double]] = {
val command = RedisCommand(
ZAdd,
Tuple5(
Tuple6(
ArbitraryKeyInput[K](),
OptionalInput(UpdateInput),
OptionalInput(UpdateByScoreInput),
OptionalInput(ChangedInput),
IncrementInput,
NonEmptyList(MemberScoreInput[M]())
),
OptionalOutput(DoubleOutput)
)
command.run((key, update, change, increment, (memberScore, memberScores.toList)))
command.run((key, update, updateByScore, change, increment, (memberScore, memberScores.toList)))
}

/**
Expand Down
25 changes: 17 additions & 8 deletions modules/redis/src/main/scala/zio/redis/options/Shared.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,27 @@ trait Shared {
sealed trait Update { self =>
private[redis] final def asString: String =
self match {
case Update.SetExisting => "XX"
case Update.SetNew => "NX"
case Update.SetLessThan => "LT"
case Update.SetGreaterThan => "GT"
case Update.SetExisting => "XX"
case Update.SetNew => "NX"
}
}

object Update {
case object SetExisting extends Update
case object SetNew extends Update
case object SetLessThan extends Update
case object SetGreaterThan extends Update
case object SetExisting extends Update
case object SetNew extends Update
}

sealed trait UpdateByScore { self =>
private[redis] final def asString: String =
self match {
case UpdateByScore.SetLessThan => "LT"
case UpdateByScore.SetGreaterThan => "GT"
}
}

object UpdateByScore {
case object SetLessThan extends UpdateByScore
case object SetGreaterThan extends UpdateByScore
}

}
12 changes: 12 additions & 0 deletions modules/redis/src/test/scala/zio/redis/InputSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,18 @@ object InputSpec extends BaseSpec {
} yield assert(result)(equalTo(RespCommand(Value("NX"))))
}
),
suite("UpdateByScore")(
test("set greater than") {
for {
result <- ZIO.attempt(UpdateByScoreInput.encode(UpdateByScore.SetGreaterThan))
} yield assert(result)(equalTo(RespCommand(Value("XX"))))
},
test("set less than") {
for {
result <- ZIO.attempt(UpdateByScoreInput.encode(UpdateByScore.SetLessThan))
} yield assert(result)(equalTo(RespCommand(Value("NX"))))
}
),
suite("Id")(
test("valid value") {
for {
Expand Down

0 comments on commit 2292dd1

Please sign in to comment.