Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
21 changes: 11 additions & 10 deletions Sources/RediStack/Commands/SortedSetCommands.swift
Original file line number Diff line number Diff line change
Expand Up @@ -561,17 +561,17 @@ extension RedisClient {
/// - key: The key identifying the sorted set in Redis.
/// - count: The max number of elements to pop from the set.
/// - Returns: A list of elements popped from the sorted set with their associated score.
public func zpopmin(from key: RedisKey, max count: Int) -> EventLoopFuture<[(RESPValue, Double)]> {
_zpop(command: "ZPOPMIN", count, key)
public func zpopmin(from key: RedisKey, max count: Int, scoreIsFirst: Bool = false) -> EventLoopFuture<[(RESPValue, Double)]> {
_zpop(command: "ZPOPMIN", count, key, scoreIsFirst: scoreIsFirst)
}

/// Removes the element from a sorted set with the lowest score.
///
/// See [https://redis.io/commands/zpopmin](https://redis.io/commands/zpopmin)
/// - Parameter key: The key identifying the sorted set in Redis.
/// - Returns: The element and its associated score that was popped from the sorted set, or `nil` if set was empty.
public func zpopmin(from key: RedisKey) -> EventLoopFuture<(RESPValue, Double)?> {
_zpop(command: "ZPOPMIN", nil, key)
public func zpopmin(from key: RedisKey, scoreIsFirst: Bool = false) -> EventLoopFuture<(RESPValue, Double)?> {
_zpop(command: "ZPOPMIN", nil, key, scoreIsFirst: scoreIsFirst)
.map { $0.count > 0 ? $0[0] : nil }
}

Expand All @@ -582,24 +582,25 @@ extension RedisClient {
/// - key: The key identifying the sorted set in Redis.
/// - count: The max number of elements to pop from the set.
/// - Returns: A list of elements popped from the sorted set with their associated score.
public func zpopmax(from key: RedisKey, max count: Int) -> EventLoopFuture<[(RESPValue, Double)]> {
_zpop(command: "ZPOPMAX", count, key)
public func zpopmax(from key: RedisKey, max count: Int, scoreIsFirst: Bool = false) -> EventLoopFuture<[(RESPValue, Double)]> {
_zpop(command: "ZPOPMAX", count, key, scoreIsFirst: scoreIsFirst)
}

/// Removes the element from a sorted set with the highest score.
///
/// See [https://redis.io/commands/zpopmax](https://redis.io/commands/zpopmax)
/// - Parameter key: The key identifying the sorted set in Redis.
/// - Returns: The element and its associated score that was popped from the sorted set, or `nil` if set was empty.
public func zpopmax(from key: RedisKey) -> EventLoopFuture<(RESPValue, Double)?> {
_zpop(command: "ZPOPMAX", nil, key)
public func zpopmax(from key: RedisKey, scoreIsFirst: Bool = false) -> EventLoopFuture<(RESPValue, Double)?> {
_zpop(command: "ZPOPMAX", nil, key, scoreIsFirst: scoreIsFirst)
.map { $0.count > 0 ? $0[0] : nil }
}

func _zpop(
command: String,
_ count: Int?,
_ key: RedisKey
_ key: RedisKey,
scoreIsFirst: Bool
) -> EventLoopFuture<[(RESPValue, Double)]> {
var args: [RESPValue] = [.init(from: key)]

Expand All @@ -611,7 +612,7 @@ extension RedisClient {

return send(command: command, with: args)
.tryConverting(to: [RESPValue].self)
.flatMapThrowing { try Self._mapSortedSetResponse($0, scoreIsFirst: true) }
.flatMapThrowing { try Self._mapSortedSetResponse($0, scoreIsFirst: scoreIsFirst) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,10 @@ extension SortedSetCommandsTests {

XCTAssertEqual(elements.count, 5)
XCTAssertEqual(elements, elements.sorted(by: <))

// test when score is not first
let value = try self.connection.zpopmin(from: #function, scoreIsFirst: false).wait()
XCTAssertEqual(value?.1, 1)
}

func test_zrevrange_realworld() throws {
Expand Down