ohm-zset 0.4 → 0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +7 -0
- data/lib/ohm-zset.rb +44 -0
- data/lib/ohm-zset/version.rb +1 -1
- data/test/unit/ohm-zset.rb +70 -0
- metadata +1 -2
data/README.md
CHANGED
@@ -197,5 +197,12 @@ sorted_set_2.add(Little.create(name: 'X1',score: 29))
|
|
197
197
|
# sorted_set and sorted_set_2 are pointing to same ZSet instance
|
198
198
|
```
|
199
199
|
|
200
|
+
## Updating gem
|
201
|
+
```
|
202
|
+
|
203
|
+
gem build ohm-zset.gemspec
|
204
|
+
gem push ohm-zset-0.X.gem
|
205
|
+
```
|
206
|
+
|
200
207
|
## Copyright
|
201
208
|
Copyright (c) 2012 [Joshua Arvin Lat](http://www.joshualat.com). See LICENSE for more details.
|
data/lib/ohm-zset.rb
CHANGED
@@ -209,10 +209,18 @@ module Ohm
|
|
209
209
|
fetch(ids(a, b))
|
210
210
|
end
|
211
211
|
|
212
|
+
def range_with_score(a = 0, b = -1)
|
213
|
+
_return = process_range_with_score { db.zrange(key, a, b, with_scores: true) }
|
214
|
+
end
|
215
|
+
|
212
216
|
def revrange(a = 0, b = -1)
|
213
217
|
fetch(execute { |key| db.zrevrange(key, a, b) })
|
214
218
|
end
|
215
219
|
|
220
|
+
def revrange_with_score(a = 0, b = -1)
|
221
|
+
_return = process_range_with_score { db.zrevrange(key, a, b, with_scores: true) }
|
222
|
+
end
|
223
|
+
|
216
224
|
# Fetch data from Redis
|
217
225
|
def to_a
|
218
226
|
fetch(ids)
|
@@ -344,6 +352,13 @@ module Ohm
|
|
344
352
|
fetch(execute { |key| db.zrangebyscore(key, a, b, :limit => [limit[:offset], limit[:count]]) })
|
345
353
|
end
|
346
354
|
|
355
|
+
def rangebyscore_with_score(a = "-inf", b = "+inf", limit = {})
|
356
|
+
limit[:offset] ||= 0
|
357
|
+
limit[:count] ||= -1
|
358
|
+
|
359
|
+
_return = process_range_with_score { db.zrangebyscore(key, a, b, with_scores: true, :limit => [limit[:offset], limit[:count]]) }
|
360
|
+
end
|
361
|
+
|
347
362
|
def revrangebyscore(a = "+inf", b = "-inf", limit = {})
|
348
363
|
limit[:offset] ||= 0
|
349
364
|
limit[:count] ||= -1
|
@@ -351,6 +366,13 @@ module Ohm
|
|
351
366
|
fetch(execute { |key| db.zrevrangebyscore(key, a, b, :limit => [limit[:offset], limit[:count]]) })
|
352
367
|
end
|
353
368
|
|
369
|
+
def revrangebyscore_with_score(a = "+inf", b = "-inf", limit = {})
|
370
|
+
limit[:offset] ||= 0
|
371
|
+
limit[:count] ||= -1
|
372
|
+
|
373
|
+
_return = process_range_with_score { db.zrevrangebyscore(key, a, b, with_scores: true, :limit => [limit[:offset], limit[:count]]) }
|
374
|
+
end
|
375
|
+
|
354
376
|
def starts_with(query, limit = {})
|
355
377
|
start_query = ZScores.string(query)
|
356
378
|
end_query = "(" + ZScores.string(query.succ).to_s
|
@@ -405,5 +427,27 @@ module Ohm
|
|
405
427
|
def execute
|
406
428
|
yield key
|
407
429
|
end
|
430
|
+
|
431
|
+
def process_range_with_score
|
432
|
+
_result = execute { |key| yield }
|
433
|
+
|
434
|
+
_ids = []
|
435
|
+
_scores = []
|
436
|
+
|
437
|
+
_result.each do |_id, _score|
|
438
|
+
_ids << _id
|
439
|
+
_scores << _score
|
440
|
+
end
|
441
|
+
|
442
|
+
_objects = fetch(_ids)
|
443
|
+
|
444
|
+
_return = []
|
445
|
+
|
446
|
+
_objects.each_with_index do |_object, i|
|
447
|
+
_return << {item: _object, score: _scores[i]}
|
448
|
+
end
|
449
|
+
|
450
|
+
_return
|
451
|
+
end
|
408
452
|
end
|
409
453
|
end
|
data/lib/ohm-zset/version.rb
CHANGED
data/test/unit/ohm-zset.rb
CHANGED
@@ -286,8 +286,34 @@ describe Ohm do
|
|
286
286
|
end
|
287
287
|
end
|
288
288
|
|
289
|
+
it "can return the elements of a specified range of the set with scores" do
|
290
|
+
b = setup_1
|
291
|
+
|
292
|
+
expected_items = [{item: 'L2', score: 2.0},
|
293
|
+
{item: 'L3', score: 3.0},
|
294
|
+
{item: 'L4', score: 4.0}]
|
295
|
+
|
296
|
+
b.zlittles.range_with_score(1, 3).each_with_index do |e, i|
|
297
|
+
assert_equal expected_items[i][:item], e[:item].name
|
298
|
+
assert_equal expected_items[i][:score], e[:score]
|
299
|
+
end
|
300
|
+
end
|
301
|
+
|
289
302
|
it "can iterate over the elements of a specified range of the reverse sorted set" do
|
290
303
|
b = setup_1
|
304
|
+
|
305
|
+
expected_items = [{item: 'L3', score: 3.0},
|
306
|
+
{item: 'L2', score: 2.0},
|
307
|
+
{item: 'L1', score: 1.0}]
|
308
|
+
|
309
|
+
b.zlittles.revrange_with_score(1, 3).each_with_index do |e, i|
|
310
|
+
assert_equal expected_items[i][:item], e[:item].name
|
311
|
+
assert_equal expected_items[i][:score], e[:score]
|
312
|
+
end
|
313
|
+
end
|
314
|
+
|
315
|
+
it "can iterate over the elements of a specified range of the reverse sorted set with scores" do
|
316
|
+
b = setup_1
|
291
317
|
expected_items = ["L3", "L2", "L1"]
|
292
318
|
|
293
319
|
b.zlittles.revrange(1, 3).each_with_index do |e, i|
|
@@ -499,6 +525,28 @@ describe Ohm do
|
|
499
525
|
assert_equal ["L4"], b.zlittles.rangebyscore(2, "+inf", offset: 2, count: 2).to_a.map(&:name)
|
500
526
|
end
|
501
527
|
|
528
|
+
it "can get a range of elements by score with scores" do
|
529
|
+
b, = setup_2
|
530
|
+
|
531
|
+
_l1 = {item: 'L1', score: 1.0}
|
532
|
+
_l2 = {item: 'L2', score: 2.0}
|
533
|
+
_l3 = {item: 'L3', score: 3.0}
|
534
|
+
_l4 = {item: 'L4', score: 4.0}
|
535
|
+
|
536
|
+
assert_equal [_l1, _l2, _l3, _l4], b.zlittles.rangebyscore_with_score(0, 4).to_a.collect{|x| {item: x[:item].name, score: x[:score]}}
|
537
|
+
assert_equal [_l1, _l2], b.zlittles.rangebyscore_with_score(1, 2).to_a.collect{|x| {item: x[:item].name, score: x[:score]}}
|
538
|
+
assert_equal [_l3, _l4], b.zlittles.rangebyscore_with_score(3, 4).to_a.collect{|x| {item: x[:item].name, score: x[:score]}}
|
539
|
+
assert_equal [_l1, _l2, _l3], b.zlittles.rangebyscore_with_score(1, 3).to_a.collect{|x| {item: x[:item].name, score: x[:score]}}
|
540
|
+
assert_equal [_l1, _l2], b.zlittles.rangebyscore_with_score(0, 4, offset: 0, count: 2).to_a.collect{|x| {item: x[:item].name, score: x[:score]}}
|
541
|
+
assert_equal [_l3, _l4], b.zlittles.rangebyscore_with_score(0, 4, offset: 2, count: 2).to_a.collect{|x| {item: x[:item].name, score: x[:score]}}
|
542
|
+
assert_equal [_l2, _l3, _l4], b.zlittles.rangebyscore_with_score(0, 4, offset: 1, count: 3).to_a.collect{|x| {item: x[:item].name, score: x[:score]}}
|
543
|
+
assert_equal [_l3], b.zlittles.rangebyscore_with_score(1, 3, offset: 2, count: 4).to_a.collect{|x| {item: x[:item].name, score: x[:score]}}
|
544
|
+
assert_equal [_l1, _l2, _l3, _l4], b.zlittles.rangebyscore_with_score(0, "+inf").to_a.collect{|x| {item: x[:item].name, score: x[:score]}}
|
545
|
+
assert_equal [_l4], b.zlittles.rangebyscore_with_score(2, "+inf", offset: 2).to_a.collect{|x| {item: x[:item].name, score: x[:score]}}
|
546
|
+
assert_equal [_l1, _l2], b.zlittles.rangebyscore_with_score("-inf", 2).to_a.collect{|x| {item: x[:item].name, score: x[:score]}}
|
547
|
+
assert_equal [_l4], b.zlittles.rangebyscore_with_score(2, "+inf", offset: 2, count: 2).to_a.collect{|x| {item: x[:item].name, score: x[:score]}}
|
548
|
+
end
|
549
|
+
|
502
550
|
it "can get a range of elements by score in reverse" do
|
503
551
|
b, = setup_2
|
504
552
|
|
@@ -516,6 +564,28 @@ describe Ohm do
|
|
516
564
|
assert_equal ["L2"], b.zlittles.revrangebyscore("+inf", 2, offset: 2, count: 2).to_a.map(&:name)
|
517
565
|
end
|
518
566
|
|
567
|
+
it "can get a range of elements by score in reverse with scores" do
|
568
|
+
b, = setup_2
|
569
|
+
|
570
|
+
_l1 = {item: 'L1', score: 1.0}
|
571
|
+
_l2 = {item: 'L2', score: 2.0}
|
572
|
+
_l3 = {item: 'L3', score: 3.0}
|
573
|
+
_l4 = {item: 'L4', score: 4.0}
|
574
|
+
|
575
|
+
assert_equal [_l4, _l3, _l2, _l1], b.zlittles.revrangebyscore_with_score(4, 0).to_a.collect{|x| {item: x[:item].name, score: x[:score]}}
|
576
|
+
assert_equal [_l2, _l1], b.zlittles.revrangebyscore_with_score(2, 1).to_a.collect{|x| {item: x[:item].name, score: x[:score]}}
|
577
|
+
assert_equal [_l4, _l3], b.zlittles.revrangebyscore_with_score(4, 3).to_a.collect{|x| {item: x[:item].name, score: x[:score]}}
|
578
|
+
assert_equal [_l3, _l2, _l1], b.zlittles.revrangebyscore_with_score(3, 1).to_a.collect{|x| {item: x[:item].name, score: x[:score]}}
|
579
|
+
assert_equal [_l4, _l3], b.zlittles.revrangebyscore_with_score(4, 0, offset: 0, count: 2).to_a.collect{|x| {item: x[:item].name, score: x[:score]}}
|
580
|
+
assert_equal [_l2, _l1], b.zlittles.revrangebyscore_with_score(4, 0, offset: 2, count: 2).to_a.collect{|x| {item: x[:item].name, score: x[:score]}}
|
581
|
+
assert_equal [_l3, _l2, _l1], b.zlittles.revrangebyscore_with_score(4, 0, offset: 1, count: 3).to_a.collect{|x| {item: x[:item].name, score: x[:score]}}
|
582
|
+
assert_equal [_l1], b.zlittles.revrangebyscore_with_score(3, 1, offset: 2, count: 4).to_a.collect{|x| {item: x[:item].name, score: x[:score]}}
|
583
|
+
assert_equal [_l4, _l3, _l2, _l1], b.zlittles.revrangebyscore_with_score("+inf", 0).to_a.collect{|x| {item: x[:item].name, score: x[:score]}}
|
584
|
+
assert_equal [_l2], b.zlittles.revrangebyscore_with_score("+inf", 2, offset: 2).to_a.collect{|x| {item: x[:item].name, score: x[:score]}}
|
585
|
+
assert_equal [_l2, _l1], b.zlittles.revrangebyscore_with_score(2, "-inf").to_a.collect{|x| {item: x[:item].name, score: x[:score]}}
|
586
|
+
assert_equal [_l2], b.zlittles.revrangebyscore_with_score("+inf", 2, offset: 2, count: 2).to_a.collect{|x| {item: x[:item].name, score: x[:score]}}
|
587
|
+
end
|
588
|
+
|
519
589
|
it "can get the number of elements between 2 given scores" do
|
520
590
|
b, = setup_2
|
521
591
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ohm-zset
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.5'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -46,7 +46,6 @@ files:
|
|
46
46
|
- lib/ohm-zset.rb
|
47
47
|
- lib/ohm-zset/version.rb
|
48
48
|
- lib/suppress-warnings.rb
|
49
|
-
- ohm-zset-0.2.gem
|
50
49
|
- ohm-zset.gemspec
|
51
50
|
- test/.DS_Store
|
52
51
|
- test/helper.rb
|