• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jcmellado / dartis / 53

pending completion
53

Pull #25

travis-ci

web-flow
- Fixed coverage configuration
Pull Request #25: Release 0.4.0

2 of 2 new or added lines in 1 file covered. (100.0%)

1238 of 1434 relevant lines covered (86.33%)

1.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.36
/lib/src/commands/sortedset.dart
1
// Copyright (c) 2018, Juan Mellado. All rights reserved. Use of this source
2
// is governed by a MIT-style license that can be found in the LICENSE file.
3

4
import 'dart:async' show Future;
5
import 'dart:collection' show LinkedHashMap;
6

7
import '../command.dart';
8
import '../protocol.dart';
9

10
/// Redis sorted sets commands.
11
abstract class SortedSetCommands<K, V> {
12
  /// Removes and returns the member with the highest score from one or more
13
  /// sorted sets, or blocks until one is available.
14
  ///
15
  /// See https://redis.io/commands/bzpopmax
16
  Future<SortedSetPopResult<K, V>> bzpopmax(
17
      {K key, Iterable<K> keys = const [], int timeout = 0});
18

19
  /// Removes and returns the member with the lowest score from one or more
20
  /// sorted sets, or blocks until one is available.
21
  ///
22
  /// See https://redis.io/commands/bzpopmin
23
  Future<SortedSetPopResult<K, V>> bzpopmin(
24
      {K key, Iterable<K> keys = const [], int timeout = 0});
25

26
  /// Adds one or more members to a sorted set, or updates its score if it
27
  /// already exists.
28
  ///
29
  /// Returns the number of elements added to the sorted sets.
30
  ///
31
  /// See [zaddIncr].
32
  ///
33
  /// See https://redis.io/commands/zadd
34
  Future<int> zadd(K key,
35
      {SortedSetExistMode mode,
36
      bool changed = false,
37
      double score,
38
      V member,
39
      Map<V, double> set = const {}});
40

41
  /// Increments the score of a member of a sorted set.
42
  ///
43
  /// Returns the new score of the member.
44
  ///
45
  /// See [zadd] and [zincrby].
46
  ///
47
  /// See https://redis.io/commands/zadd
48
  Future<double> zaddIncr(K key, double score, V member,
49
      {SortedSetExistMode mode});
50

51
  /// Returns the cardinality (number of elements) of the sorted set stored
52
  /// at [key].
53
  ///
54
  /// See https://redis.io/commands/zcard
55
  Future<int> zcard(K key);
56

57
  /// Returns the number of elements in the sorted set at [key] with a
58
  /// score between [min] and [max].
59
  ///
60
  /// See https://redis.io/commands/zcount
61
  Future<int> zcount(K key, String min, String max);
62

63
  /// Increments the score of [member] in the sorted set stored at [key]
64
  /// by [increment].
65
  ///
66
  /// Returns the new score of [member].
67
  ///
68
  /// See https://redis.io/commands/zincrby
69
  Future<double> zincrby(K key, double increment, V member);
70

71
  /// Computes the intersection of numkeys sorted sets given by the
72
  /// specified keys, and stores the result in [destination].
73
  ///
74
  /// Returns the number of elements in the resulting sorted set
75
  /// at [destination].
76
  ///
77
  /// See https://redis.io/commands/zinterstore
78
  Future<int> zinterstore(K destination, List<K> keys,
79
      {Iterable<double> weights = const [], AggregateMode mode});
80

81
  /// Counts the number of members in a sorted set between a given
82
  /// lexicographical range.
83
  ///
84
  /// Returns the number of elements in the specified score range.
85
  ///
86
  /// See https://redis.io/commands/zlexcount
87
  Future<int> zlexcount(K key, V min, V max);
88

89
  /// Removes and returns up to [count] members with the highest scores in
90
  /// the sorted set stored at [key].
91
  ///
92
  /// Returns the list of popped scores and elements.
93
  ///
94
  /// See https://redis.io/commands/zpopmax
95
  Future<Map<V, double>> zpopmax(K key, {int count});
96

97
  /// Removes and returns up to [count] members with the lowest scores in
98
  /// the sorted set stored at [key].
99
  ///
100
  /// Returns the list of popped scores and elements.
101
  ///
102
  /// See https://redis.io/commands/zpopmin
103
  Future<Map<V, double>> zpopmin(K key, {int count});
104

105
  /// Returns the specified range of elements in the sorted set stored at [key].
106
  ///
107
  /// See https://redis.io/commands/zrange
108
  Future<Map<V, double>> zrange(K key, int start, int stop,
109
      {bool withScores = false});
110

111
  /// Returns a range of members in a sorted set, by lexicographical range.
112
  ///
113
  /// See https://redis.io/commands/zrangebylex
114
  Future<List<V>> zrangebylex(K key, V min, V max, {int offset, int count});
115

116
  /// Returns a range of members in a sorted set, by score.
117
  ///
118
  /// See https://redis.io/commands/zrangebyscore
119
  Future<Map<V, double>> zrangebyscore(K key, String min, String max,
120
      {bool withScores = false, int offset, int count});
121

122
  /// Returns the rank of member in the sorted set stored at [key], with
123
  /// the scores ordered from low to high.
124
  ///
125
  /// See https://redis.io/commands/zrank
126
  Future<int> zrank(K key, V member);
127

128
  /// Removes the specified members from the sorted set stored at [key].
129
  ///
130
  /// Returns the number of members removed from the sorted set.
131
  ///
132
  /// See https://redis.io/commands/zrem
133
  Future<int> zrem(K key, {V member, Iterable<V> members = const []});
134

135
  /// Removes all members in a sorted set between the given lexicographical
136
  /// range.
137
  ///
138
  /// Returns the number of elements removed.
139
  ///
140
  /// See https://redis.io/commands/zremrangebylex
141
  Future<int> zremrangebylex(K key, V min, V max);
142

143
  /// Removes all members in a sorted set within the given indexes.
144
  ///
145
  /// Returns the number of elements removed.
146
  ///
147
  /// See https://redis.io/commands/zremrangebyrank
148
  Future<int> zremrangebyrank(K key, int start, int stop);
149

150
  /// Removes all members in a sorted set within the given scores.
151
  ///
152
  /// Returns the number of elements removed.
153
  ///
154
  /// See https://redis.io/commands/zremrangebyscore
155
  Future<int> zremrangebyscore(K key, String min, String max);
156

157
  /// Returns the specified range of elements in the sorted set stored at [key].
158
  ///
159
  /// See https://redis.io/commands/zrevrange
160
  Future<Map<V, double>> zrevrange(K key, int start, int stop,
161
      {bool withScores = false});
162

163
  /// Returns a range of members in a sorted set, by lexicographical range,
164
  /// ordered from higher to lower strings.
165
  ///
166
  /// See https://redis.io/commands/zrevrangebylex
167
  Future<List<V>> zrevrangebylex(K key, V max, V min, {int offset, int count});
168

169
  /// Returns a range of members in a sorted set, by score, with scores
170
  /// ordered from high to low.
171
  ///
172
  /// See https://redis.io/commands/zrevrangebyscore
173
  Future<Map<V, double>> zrevrangebyscore(K key, String max, String min,
174
      {bool withScores = false, int offset, int count});
175

176
  /// Returns the rank of member in the sorted set stored at [key], with
177
  /// the scores ordered from high to low.
178
  ///
179
  /// See https://redis.io/commands/zrevrank
180
  Future<int> zrevrank(K key, V member);
181

182
  /// Incrementally iterates members and scores of a sorted set stored at [key].
183
  ///
184
  /// See https://redis.io/commands/zscan
185
  Future<SortedSetScanResult<K>> zscan(K key, int cursor,
186
      {K pattern, int count});
187

188
  /// Returns the score of [member] in the sorted set at [key].
189
  ///
190
  /// See https://redis.io/commands/zscore
191
  Future<double> zscore(K key, V member);
192

193
  /// Adds multiple sorted sets and stores the resulting sorted set in a
194
  /// new key.
195
  ///
196
  /// Returns the number of elements in the resulting sorted set at destination.
197
  ///
198
  /// See https://redis.io/commands/zunionstore
199
  Future<int> zunionstore(K destination, List<K> keys,
200
      {Iterable<double> weights = const [], AggregateMode mode});
201
}
202

203
/// Modes allowed for the ZADD command.
204
class SortedSetExistMode {
205
  /// The name of the mode.
206
  final String name;
207

208
  const SortedSetExistMode._(this.name);
×
209

210
  /// Only set the key if it does not already exist.
211
  static const SortedSetExistMode nx = SortedSetExistMode._(r'NX');
212

213
  /// Only set the key if it already exist.
214
  static const SortedSetExistMode xx = SortedSetExistMode._(r'XX');
215

216
  @override
1✔
217
  String toString() => 'SortedSetExistMode: $name';
2✔
218
}
219

220
/// Modes allowed for the ZINTERSTORE and ZUNIONSTORE commands.
221
class AggregateMode {
222
  /// The name of the mode.
223
  final String name;
224

225
  const AggregateMode._(this.name);
×
226

227
  /// The score of an element is summed across the inputs where it exists.
228
  static const AggregateMode sum = AggregateMode._(r'SUM');
229

230
  /// The resulting set will contain the minimum score of an element across
231
  /// the inputs where it exists.
232
  static const AggregateMode min = AggregateMode._(r'MIN');
233

234
  /// The resulting set will contain the maximum score of an element across
235
  /// the inputs where it exists.
236
  static const AggregateMode max = AggregateMode._(r'MAX');
237

238
  @override
1✔
239
  String toString() => 'AggregateMode: $name';
2✔
240
}
241

242
/// Result of the BZPOPMIN and BZPOPMAX commands.
243
class SortedSetPopResult<K, V> {
244
  /// The key.
245
  final K key;
246

247
  /// The member.
248
  final MapEntry<V, double> member;
249

250
  /// Creates a [SortedSetMapper] instance.
251
  const SortedSetPopResult(this.key, this.member);
×
252

253
  @override
1✔
254
  String toString() => 'SortedSetPopResult<$K, $V>: {key=$key member=$member}';
3✔
255
}
256

257
/// Result of the ZSCAN command.
258
class SortedSetScanResult<K> {
259
  /// The cursor.
260
  final int cursor;
261

262
  /// The members with theirs scores.
263
  final Map<K, double> members;
264

265
  /// Creates a [SortedSetScanResult] instance.
266
  const SortedSetScanResult(this.cursor, this.members);
1✔
267

268
  @override
1✔
269
  String toString() =>
270
      'SortedSetScanResult<$K>: {cursor=$cursor, members=$members}';
3✔
271
}
272

273
/// A mapper for the BZPOPMIN and BZPOPMAX commands.
274
class SortedSetPopResultMapper<K, V>
275
    implements Mapper<SortedSetPopResult<K, V>> {
276
  @override
×
277
  SortedSetPopResult<K, V> map(covariant ArrayReply reply, RedisCodec codec) {
278
    final array = reply.array;
×
279

280
    if (array == null) {
281
      return null;
282
    }
283

284
    final key = codec.decode<K>(array[0]);
×
285
    final value = codec.decode<V>(array[1]);
×
286
    final score = codec.decode<double>(array[2]);
×
287
    final member = MapEntry<V, double>(value, score);
×
288

289
    return SortedSetPopResult<K, V>(key, member);
×
290
  }
291
}
292

293
/// A mapper for the ZSCAN command.
294
class SortedSetScanMapper<K> implements Mapper<SortedSetScanResult<K>> {
295
  @override
1✔
296
  SortedSetScanResult<K> map(covariant ArrayReply reply, RedisCodec codec) {
297
    final cursor = codec.decode<int>(reply.array[0]);
3✔
298
    // ignore: avoid_as
299
    final members = _mapSet(reply.array[1] as ArrayReply, codec);
3✔
300

301
    return SortedSetScanResult<K>(cursor, members);
1✔
302
  }
303

304
  /// Maps a [reply] to a Map<K, double>.
305
  Map<K, double> _mapSet(ArrayReply reply, RedisCodec codec) {
1✔
306
    // ignore: prefer_collection_literals
307
    final set = LinkedHashMap<K, double>();
1✔
308

309
    final array = reply.array;
1✔
310
    for (var i = 0; i < array.length; i += 2) {
3✔
311
      final member = codec.decode<K>(array[i]);
2✔
312
      final score = codec.decode<double>(array[i + 1]);
3✔
313

314
      set[member] = score;
1✔
315
    }
316

317
    return set;
318
  }
319
}
320

321
/// A mapper to be used with some sorted set commands.
322
class SortedSetMapper<V> implements Mapper<Map<V, double>> {
323
  /// Retrieves the scores.
324
  final bool withScores;
325

326
  /// Creates a [SortedSetMapper] instance.
327
  SortedSetMapper({this.withScores = false});
1✔
328

329
  @override
1✔
330
  Map<V, double> map(covariant ArrayReply reply, RedisCodec codec) {
331
    final array = reply.array;
1✔
332

333
    if (array == null) {
334
      return null;
335
    }
336

337
    // ignore: prefer_collection_literals
338
    final set = LinkedHashMap<V, double>();
1✔
339

340
    final incr = withScores ? 2 : 1;
1✔
341

342
    for (var i = 0; i < array.length; i += incr) {
3✔
343
      final member = codec.decode<V>(array[i]);
2✔
344
      final score = withScores ? codec.decode<double>(array[i + 1]) : null;
4✔
345

346
      set[member] = score;
1✔
347
    }
348

349
    return set;
350
  }
351
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2023 Coveralls, Inc