Skip to content

Commit edf91f0

Browse files
authored
Merge pull request #2 from scuml/sorted-set-floats
Adds double support sortedsets. Adds withscores to ZRANGE
2 parents 023a4db + e749701 commit edf91f0

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

Sources/SwiftRedis/Redis+SortedSet.swift

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ extension Redis {
3333
public func zadd(_ key: String, tuples: (Int, String)..., callback: (Int?, NSError?) -> Void) {
3434
zaddArrayOfScoreMembers(key, tuples: tuples, callback: callback)
3535
}
36+
37+
public func zadd(_ key: String, tuples: (Double, String)..., callback: (Int?, NSError?) -> Void) {
38+
zaddArrayOfScoreMembers(key, tuples: tuples, callback: callback)
39+
}
3640

3741
/// Add elements to a sorted set.
3842
///
@@ -44,6 +48,10 @@ extension Redis {
4448
public func zadd(_ key: String, tuples: (Int, RedisString)..., callback: (Int?, NSError?) -> Void) {
4549
zaddArrayOfScoreMembers(key, tuples: tuples, callback: callback)
4650
}
51+
52+
public func zadd(_ key: String, tuples: (Double, RedisString)..., callback: (Int?, NSError?) -> Void) {
53+
zaddArrayOfScoreMembers(key, tuples: tuples, callback: callback)
54+
}
4755

4856
/// Add elements to a sorted set.
4957
///
@@ -53,6 +61,10 @@ extension Redis {
5361
/// number of elements added to the sorted set.
5462
/// NSError will be non-nil if an error occurred.
5563
public func zaddArrayOfScoreMembers(_ key: String, tuples: [(Int, String)], callback: (Int?, NSError?) -> Void) {
64+
zaddArrayOfScoreMembers(key, tuples: tuples.map{(Double($0.0), $0.1)}, callback: callback)
65+
}
66+
67+
public func zaddArrayOfScoreMembers(_ key: String, tuples: [(Double, String)], callback: (Int?, NSError?) -> Void) {
5668
var command = ["ZADD"]
5769
command.append(key)
5870
for tuple in tuples {
@@ -72,6 +84,9 @@ extension Redis {
7284
/// number of elements added to the sorted set.
7385
/// NSError will be non-nil if an error occurred.
7486
public func zaddArrayOfScoreMembers(_ key: String, tuples: [(Int, RedisString)], callback: (Int?, NSError?) -> Void) {
87+
zaddArrayOfScoreMembers(key, tuples: tuples.map{(Double($0.0), $0.1)}, callback: callback)
88+
}
89+
public func zaddArrayOfScoreMembers(_ key: String, tuples: [(Double, RedisString)], callback: (Int?, NSError?) -> Void) {
7590
var command = [RedisString("ZADD")]
7691
command.append(RedisString(key))
7792
for tuple in tuples {
@@ -239,12 +254,19 @@ extension Redis {
239254
/// - Parameter callback: The callback function, the Array<RedisString> will contain the
240255
/// elements fetched from the sorted set.
241256
/// NSError will be non-nil if an error occurred.
242-
public func zrange(_ key: String, start: Int, stop: Int, callback: ([RedisString?]?, NSError?) -> Void) {
243-
issueCommand("ZRANGE", key, String(start), String(stop)) { (response: RedisResponse) in
244-
self.redisStringArrayResponseHandler(response, callback: callback)
257+
public func zrange(_ key: String, start: Int, stop: Int, withscores: Bool = false, callback: ([RedisString?]?, NSError?) -> Void) {
258+
if !withscores {
259+
issueCommand("ZRANGE", key, String(start), String(stop)) { (response: RedisResponse) in
260+
self.redisStringArrayResponseHandler(response, callback: callback)
261+
}
262+
} else {
263+
issueCommand("ZRANGE", key, String(start), String(stop), "WITHSCORES") { (response: RedisResponse) in
264+
self.redisStringArrayResponseHandler(response, callback: callback)
265+
}
245266
}
246267
}
247268

269+
248270
/// Return a range of members in a sorted set, by lexicographical range.
249271
///
250272
/// - Parameter key: The key.

Sources/SwiftRedis/RedisMulti+SortedSet.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ extension RedisMulti {
2929
public func zadd(_ key: String, tuples: (Int, String)...) -> RedisMulti {
3030
return zaddArrayOfScoreMembers(key, tuples: tuples)
3131
}
32+
@discardableResult
33+
public func zadd(_ key: String, tuples: (Double, String)...) -> RedisMulti {
34+
return zaddArrayOfScoreMembers(key, tuples: tuples)
35+
}
3236

3337
/// Add an ZADD command to the "transaction"
3438
///
@@ -38,6 +42,10 @@ extension RedisMulti {
3842
/// - Returns: The `RedisMulti` object being added to.
3943
@discardableResult
4044
public func zaddArrayOfScoreMembers(_ key: String, tuples: [(Int, String)]) -> RedisMulti {
45+
return zaddArrayOfScoreMembers(key, tuples: tuples.map{(Double($0.0), $0.1)})
46+
}
47+
@discardableResult
48+
public func zaddArrayOfScoreMembers(_ key: String, tuples: [(Double, String)]) -> RedisMulti {
4149
var command = [RedisString("ZADD"), RedisString(key)]
4250
for tuple in tuples {
4351
command.append(RedisString(tuple.0))
@@ -57,6 +65,10 @@ extension RedisMulti {
5765
public func zadd(_ key: String, tuples: (Int, RedisString)...) -> RedisMulti {
5866
return zaddArrayOfScoreMembers(key, tuples: tuples)
5967
}
68+
@discardableResult
69+
public func zadd(_ key: String, tuples: (Double, RedisString)...) -> RedisMulti {
70+
return zaddArrayOfScoreMembers(key, tuples: tuples)
71+
}
6072

6173
/// Add an ZADD command to the "transaction"
6274
///
@@ -66,6 +78,10 @@ extension RedisMulti {
6678
/// - Returns: The `RedisMulti` object being added to.
6779
@discardableResult
6880
public func zaddArrayOfScoreMembers(_ key: String, tuples: [(Int, RedisString)]) -> RedisMulti {
81+
return zaddArrayOfScoreMembers(key, tuples: tuples.map{(Double($0.0), $0.1)})
82+
}
83+
@discardableResult
84+
public func zaddArrayOfScoreMembers(_ key: String, tuples: [(Double, RedisString)]) -> RedisMulti {
6985
var command = [RedisString("ZADD"), RedisString(key)]
7086
for tuple in tuples {
7187
command.append(RedisString(tuple.0))

Tests/SwiftRedisTests/TestSetCommands.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class TestSetCommands: XCTestCase {
2323
static var allTests: [(String, (TestSetCommands) -> () throws -> Void)] {
2424
return [
2525
("test_ZAdd", test_ZAdd),
26+
("test_ZAddFloat", test_ZAddFloat),
2627
("test_ZCard", test_ZCard),
2728
("test_ZCount", test_ZCount),
2829
("test_ZIncrby", test_ZIncrby),
@@ -92,6 +93,39 @@ public class TestSetCommands: XCTestCase {
9293
waitForExpectations(timeout: 5, handler: { error in XCTAssertNil(error, "Timeout") })
9394
}
9495

96+
func test_ZAddFloat() {
97+
let expectation1 = expectation(description: "Add score(s) and member(s) to the set")
98+
99+
setupTests {
100+
redis.zadd(self.key1, tuples: (-1.5, "one"), (2.33333333, "two"), (3.14159, "three")) {
101+
(result: Int?, error: NSError?) in
102+
103+
XCTAssertNil(error)
104+
XCTAssertNotNil(result)
105+
106+
redis.zrange(self.key1, start: 0, stop: 2, withscores: true, callback: {
107+
(resultList: [RedisString?]?, zRangeError: NSError?) in
108+
109+
XCTAssertNil(zRangeError)
110+
111+
XCTAssertEqual(resultList?[0], RedisString("one"), "The first element of the list should be \(RedisString("one")). It was \(String(describing: resultList?[0]))")
112+
XCTAssertEqual(resultList?[1], RedisString("-1.5"), "The first score of the list should be \(RedisString("-1.5")). It was \(String(describing: resultList?[0]))")
113+
114+
XCTAssertEqual(resultList?[2], RedisString("two"), "The first element of the list should be \(RedisString("two")). It was \(String(describing: resultList?[1]))")
115+
XCTAssertEqual(resultList?[3], RedisString("2.33333333"), "The first score of the list should be \(RedisString("2.33333333")). It was \(String(describing: resultList?[1]))")
116+
117+
XCTAssertEqual(resultList?[4], RedisString("three"), "The first element of the list should be \(RedisString("three")). It was \(String(describing: resultList?[2]))")
118+
XCTAssertEqual(resultList?[5], RedisString("3.14159"), "The first score of the list should be \(RedisString("3.14159")). It was \(String(describing: resultList?[2]))")
119+
120+
XCTAssertEqual(resultList?.count, 6, "The size of the list should be 6. It was \(String(describing: resultList?.count))")
121+
expectation1.fulfill()
122+
})
123+
124+
}
125+
}
126+
waitForExpectations(timeout: 5, handler: { error in XCTAssertNil(error, "Timeout") })
127+
}
128+
95129
func test_ZCard() {
96130
let expectation1 = expectation(description: "Returns the sorted set cardinality of the sorted set ")
97131
setupTests {

0 commit comments

Comments
 (0)