You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Sources/RediStack/Commands/SortedSetCommands.swift
+72-42Lines changed: 72 additions & 42 deletions
Original file line number
Diff line number
Diff line change
@@ -44,77 +44,106 @@ extension RedisClient {
44
44
}
45
45
}
46
46
47
-
// MARK: General
47
+
// MARK: Zadd
48
48
49
-
/// The supported options for the `zadd` command with Redis SortedSet types.
49
+
/// The supported insert behavior for a `zadd` command with Redis SortedSet types.
50
+
///
51
+
/// `zadd` normally inserts all elements (`.allElements`) provided into the SortedSet, updating the score of any element that already exist in the set.
52
+
///
53
+
/// However, it supports two other insert behaviors:
54
+
/// * `.onlyNewElements` will not update the score of any element already in the SortedSet
55
+
/// * `.onlyExistingElements` will not insert any new element into the SortedSet
50
56
///
51
57
/// See [https://redis.io/commands/zadd#zadd-options-redis-302-or-greater](https://redis.io/commands/zadd#zadd-options-redis-302-or-greater)
52
-
publicenumRedisSortedSetAddOption:String{
53
-
/// When adding elements, any that do not already exist in the SortedSet will be ignored and the score of the existing element will be updated.
54
-
case onlyUpdateExistingElements ="XX"
55
-
/// When adding elements, any that already exist in the SortedSet will be ignored and the score of the existing element will not be updated.
56
-
case onlyAddNewElements ="NX"
58
+
publicenumRedisZaddInsertBehavior{
59
+
/// Insert new elements and update the score of existing elements.
60
+
case allElements
61
+
/// Only insert new elements; do not update the score of existing elements.
62
+
case onlyNewElements
63
+
/// Only update the score of existing elements; do not insert new elements.
64
+
case onlyExistingElements
65
+
66
+
/// Redis representation of this option.
67
+
@usableFromInline
68
+
internalvarstring:String?{
69
+
switchself{
70
+
case.allElements:returnnil
71
+
case.onlyNewElements:return"NX"
72
+
case.onlyExistingElements:return"XX"
73
+
}
74
+
}
75
+
}
76
+
77
+
/// The supported behavior for what a `zadd` command return value should represent.
78
+
///
79
+
/// `zadd` normally returns the number of new elements inserted into the set (`.insertedElementsCount`),
80
+
/// but also supports the option (`.changedElementsCount`) to return the number of elements changed as a result of the command.
81
+
///
82
+
/// "Changed" in this context refers to both new elements that were inserted and existing elements that had their score updated.
83
+
///
84
+
/// See [https://redis.io/commands/zadd](https://redis.io/commands/zadd)
85
+
publicenumRedisZaddReturnBehavior{
86
+
/// Count both new elements that were inserted into the SortedSet and existing elements that had their score updated.
87
+
case changedElementsCount
88
+
/// Count only new elements that were inserted into the SortedSet.
89
+
case insertedElementsCount
90
+
91
+
/// Redis representation of this option.
92
+
@usableFromInline
93
+
internalvarstring:String?{
94
+
switchself{
95
+
case.changedElementsCount:return"CH"
96
+
case.insertedElementsCount:returnnil
97
+
}
98
+
}
57
99
}
58
100
59
101
extensionRedisClient{
60
102
/// Adds elements to a sorted set, assigning their score to the values provided.
61
-
/// - Note: `INCR` is not supported by this library in `zadd`. Use the `zincrby(:element:in:)` method instead.
62
103
///
63
104
/// See [https://redis.io/commands/zadd](https://redis.io/commands/zadd)
64
105
/// - Parameters:
65
106
/// - elements: A list of elements and their score to add to the sorted set.
66
107
/// - key: The key of the sorted set.
67
-
/// - option: An option for modifying the behavior of the command.
68
-
/// - returnChangedCount: `zadd` normally returns the number of new elements added to the set,
69
-
/// but setting this to `true` will instead have the command return the number of elements changed.
70
-
///
71
-
/// "Changed" in this context are new elements added, and elements that had their score updated.
72
-
/// - Returns: The number of elements added to the sorted set, unless `returnChangedCount` was set to `true`.
108
+
/// - insertBehavior: The desired behavior of handling new and existing elements in the SortedSet.
109
+
/// - returnBehavior: The desired behavior of what the return value should represent.
110
+
/// - Returns: If `returning` is `.changedElementsCount`, the number of elements inserted and that had their score updated. Otherwise, just the number of new elements inserted.
/// Adds elements to a sorted set, assigning their score to the values provided.
98
-
/// - Note: `INCR` is not supported by this library in `zadd`. Use the `zincrby(:element:in:)` method instead.
99
131
///
100
132
/// See [https://redis.io/commands/zadd](https://redis.io/commands/zadd)
101
133
/// - Parameters:
102
134
/// - elements: A list of elements and their score to add to the sorted set.
103
135
/// - key: The key of the sorted set.
104
-
/// - option: An option for modifying the behavior of the command.
105
-
/// - returnChangedCount: `zadd` normally returns the number of new elements added to the set,
106
-
/// but setting this to `true` will instead have the command return the number of elements changed.
107
-
///
108
-
/// "Changed" in this context are new elements added, and elements that had their score updated.
109
-
/// - Returns: The number of elements added to the sorted set, unless `returnChangedCount` was set to `true`.
136
+
/// - insertBehavior: The desired behavior of handling new and existing elements in the SortedSet.
137
+
/// - returnBehavior: The desired behavior of what the return value should represent.
138
+
/// - Returns: If `returning` is `.changedElementsCount`, the number of elements inserted and that had their score updated. Otherwise, just the number of new elements inserted.
returnself.zadd(elements, to: key,option: option, returnChangedCount: returnChangedCount)
146
+
returnself.zadd(elements, to: key,inserting: insertBehavior, returning: returnBehavior)
118
147
}
119
148
120
149
/// Adds an element to a sorted set, assigning their score to the value provided.
@@ -123,23 +152,24 @@ extension RedisClient {
123
152
/// - Parameters:
124
153
/// - element: The element and its score to add to the sorted set.
125
154
/// - key: The key of the sorted set.
126
-
/// - option: An option for modifying the behavior of the command.
127
-
/// - returnChangedCount: `zadd` normally returns the number of new elements added to the set,
128
-
/// but setting this to `true` will instead have the command return the number of elements changed.
129
-
///
130
-
/// "Changed" in this context are new elements added, and elements that had their score updated.
131
-
/// - Returns: `true` if the element was added or score was updated in the sorted set, depending on the `option` and `returnChangedCount` settings set.
155
+
/// - insertBehavior: The desired behavior of handling new and existing elements in the SortedSet.
156
+
/// - returnBehavior: The desired behavior of what the return value should represent.
157
+
/// - Returns: If `returning` is `.changedElementsCount`, the number of elements inserted and that had their score updated. Otherwise, just the number of new elements inserted.
0 commit comments