rediska 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5efa2877ed6be2aaac8746a197e0c0af6a82684a
4
- data.tar.gz: 30d0a4254f13357960664cf89e8737ec3ac05703
3
+ metadata.gz: ace76e76418b38e8b9b46fcdb5c4db0455e89c95
4
+ data.tar.gz: 902e598e4cd72baae2e0f376edb6c9215a3873c3
5
5
  SHA512:
6
- metadata.gz: 7c053f2ec1b3b5dfaeaffe3e3319d4c7d3644f3300ca868066ae9bc38f45012299ae14cd5b7b3189f5f2730b8adea503c33dce565b7cb925ec9e07c3ea0b5a79
7
- data.tar.gz: c0ce4d41e22a9215144ebaef7d714720b7567c7729b17c3d3b4305d9e0cbd6573db6f23aad7cf5a848d070e1009e648cd0204d88a6ab5daab39c1c012db1b6d9
6
+ metadata.gz: 19eb6136ff931a2cd42f2c71fd17ab6e83433822fcb7105c1993f30a1b39102e11fd04feded16da5a317abc294149f9c42c876461fe44b0f8bafda00856dd744
7
+ data.tar.gz: 4393f6c7a5f98b32c8bb0a6432ed3248c702344cac9221fcc4c44b138a1ff1f1d433045d0e7a7ee2a888a2a61dff215db94fc03296f822d9b8c294df93058f25
data/README.md CHANGED
@@ -182,6 +182,7 @@ end
182
182
  * ZREVRANGE
183
183
  * ZREVRANGEBYSCORE
184
184
  * ZREVRANK
185
+ * ZSCAN
185
186
  * ZSCORE
186
187
 
187
188
  ## Credits and Contributors
@@ -915,14 +915,8 @@ module Rediska
915
915
  data_type_check(key, ZSet)
916
916
  return [] unless data[key]
917
917
 
918
- # Sort by score, or if scores are equal, key alphanum
919
- results = data[key].sort do |(k1, v1), (k2, v2)|
920
- if v1 == v2
921
- k1 <=> k2
922
- else
923
- v1 <=> v2
924
- end
925
- end
918
+ results = sort_keys(data[key])
919
+
926
920
  # Select just the keys unless we want scores
927
921
  results = results.map(&:first) unless with_scores
928
922
  results[start..stop].flatten.map(&:to_s)
@@ -1008,6 +1002,58 @@ module Rediska
1008
1002
  data[out].size
1009
1003
  end
1010
1004
 
1005
+ def zscan(key, start_cursor, *args)
1006
+ data_type_check(key, ZSet)
1007
+ return [] unless data[key]
1008
+
1009
+ match = '*'
1010
+ count = 10
1011
+
1012
+ if args.size.odd?
1013
+ raise_argument_error('zscan')
1014
+ end
1015
+
1016
+ if idx = args.index('MATCH')
1017
+ match = args[idx + 1]
1018
+ end
1019
+
1020
+ if idx = args.index('COUNT')
1021
+ count = args[idx + 1]
1022
+ end
1023
+
1024
+ start_cursor = start_cursor.to_i
1025
+ data_type_check(start_cursor, Fixnum)
1026
+
1027
+ cursor = start_cursor
1028
+ next_keys = []
1029
+
1030
+ sorted_keys = sort_keys(data[key])
1031
+
1032
+ if start_cursor + count >= sorted_keys.length
1033
+ next_keys = sorted_keys.to_a.select { |k| File.fnmatch(match, k[0]) } [start_cursor..-1]
1034
+ cursor = 0
1035
+ else
1036
+ cursor = start_cursor + count
1037
+ next_keys = sorted_keys.to_a.select { |k| File.fnmatch(match, k[0]) } [start_cursor..cursor-1]
1038
+ end
1039
+
1040
+ return "#{cursor}", next_keys.flatten.map(&:to_s)
1041
+ end
1042
+
1043
+ # Originally from redis-rb
1044
+ def zscan_each(key, *args, &block)
1045
+ data_type_check(key, ZSet)
1046
+ return [] unless data[key]
1047
+
1048
+ return to_enum(:zscan_each, key, options) unless block_given?
1049
+ cursor = 0
1050
+ loop do
1051
+ cursor, values = zscan(key, cursor, options)
1052
+ values.each(&block)
1053
+ break if cursor == '0'
1054
+ end
1055
+ end
1056
+
1011
1057
  private
1012
1058
  def raise_argument_error(command, match_string = command)
1013
1059
  error_message = if %w(hmset mset_odd).include?(match_string.downcase)
@@ -1064,5 +1110,16 @@ module Rediska
1064
1110
  (1..-number).map { data[key].to_a[rand(data[key].size)] }.flatten
1065
1111
  end
1066
1112
  end
1113
+
1114
+ def sort_keys(arr)
1115
+ # Sort by score, or if scores are equal, key alphanum
1116
+ arr.sort do |(k1, v1), (k2, v2)|
1117
+ if v1 == v2
1118
+ k1 <=> k2
1119
+ else
1120
+ v1 <=> v2
1121
+ end
1122
+ end
1123
+ end
1067
1124
  end
1068
1125
  end
@@ -1,3 +1,3 @@
1
1
  module Rediska
2
- VERSION = '0.3.1'.freeze
2
+ VERSION = '0.4.0'.freeze
3
3
  end
@@ -448,6 +448,46 @@ shared_examples 'sorted sets' do
448
448
  expect(subject.zscore('key1', 3)).to be_nil
449
449
  end
450
450
 
451
+ describe '#zscan' do
452
+ before do
453
+ 50.times { |x| subject.zadd('key', x, "key #{x}") }
454
+ end
455
+
456
+ it 'with no arguments should return 10 numbers in ascending order' do
457
+ result = subject.zscan('key', 0)[1]
458
+ expect(result).to eq(result.sort { |x, y| x[1] <=> y[1] })
459
+ expect(result.count).to eq(10)
460
+ end
461
+
462
+ it 'with a count should return that number of members' do
463
+ expect(subject.zscan('key', 0, count: 2)).to eq(['2', [['key 0', 0.0], ['key 1', 1.0]]])
464
+ end
465
+
466
+ it 'with a count greater than the number of members, returns all the members in asc order' do
467
+ result = subject.zscan('key', 0, count: 1000)[1]
468
+ expect(result).to eq(result.sort { |x, y| x[1] <=> y[1] })
469
+ expect(result.size).to eq(50)
470
+ end
471
+
472
+ it 'with match, should return key-values where the key matches' do
473
+ subject.zadd('key', 1.0, 'blah')
474
+ subject.zadd('key', 2.0, 'bluh')
475
+ result = subject.zscan('key', 0, count: 100, match: 'key*')[1]
476
+ expect(result).to_not include(['blah', 1.0])
477
+ expect(result).to_not include(['bluh', 2.0])
478
+ end
479
+ end
480
+
481
+ describe '#zscan_each' do
482
+ before do
483
+ 50.times { |x| subject.zadd('key', x, "key #{x}") }
484
+ end
485
+
486
+ it 'enumerates over the items in the sorted set' do
487
+ expect(subject.zscan_each('key').to_a).to eq(subject.zscan('key', 0, count: 50)[1])
488
+ end
489
+ end
490
+
451
491
  #it 'should remove all members in a sorted set within the given indexes'
452
492
  #it 'should return a range of members in a sorted set, by index, with scores ordered from high to low'
453
493
  #it 'should return a range of members in a sorted set, by score, with scores ordered from high to low'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rediska
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leonid Beder
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-30 00:00:00.000000000 Z
11
+ date: 2015-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis