redstruct 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b037a49dda3d7e960996e6fbc0c9bbc5ffe6445c
4
- data.tar.gz: 221f64d42c69a93802971c6e8ecf425dd598ac22
3
+ metadata.gz: 5f29df0c24d1d453a2a24b5e32cf563d284e08d2
4
+ data.tar.gz: 26f083074180af497290197bb09a956d98e6c683
5
5
  SHA512:
6
- metadata.gz: 94c3c5363a1f04b83cb6438fb07b22df54528fbc51d2f2820a8ffc46c59684bde7fd19ddd1aa83045fa109a7c7e7f94a6d3ce89339b535d751216142873b0987
7
- data.tar.gz: 1754834360f2ae46fc748ba2b74bbafd322765424ac93f0b58594dadd52974661810771ad22b2d5ceb1c59ea687ecc7fc8641e09212937ccfeca5f6c4ce5635e
6
+ metadata.gz: 0564b2207e6b03a6e7109db3c634dd74ce33d891bfbfa23f0ad8531482c7a9ca93a02935901ef8edb4c8abaa0a9c8b7a6ff5a277ff199a95dd2d79548d59374a
7
+ data.tar.gz: b629606788ae08144cba7d154d9ecebe87d885f04fc74ea2472564a0b1e63559a9a50a8d44e14e583f9852341f622fe839095d3e461bf6697731462888d725b9
@@ -32,9 +32,7 @@ module Redstruct
32
32
  end
33
33
 
34
34
  def pop(timeout: nil)
35
- options = {}
36
- options[:timeout] = timeout.to_i unless timeout.nil?
37
- return self.connection.blpop(@key, options)&.last
35
+ return timeout.nil? ? self.connection.lpop(@key) : self.connection.blpop(@key, timeout: timeout)&.last
38
36
  end
39
37
 
40
38
  def remove(value, count: 1)
@@ -1,14 +1,128 @@
1
+ # frozen_string_literal: true
1
2
  module Redstruct
2
3
  module Types
3
4
  class SortedSet < Redstruct::Types::Struct
4
- DEFAULT_SCORE = 1.0
5
+ # @param [Array<Array<#to_f, #to_s>>] pairs a list of pairs, where the first element is the score, and second the value
6
+ # @return [Integer] returns the amount of pairs inserted
7
+ def add(*pairs)
8
+ return self.connection.zadd(@key, pairs)
9
+ end
10
+
11
+ # @param [Array<#to_s>] values list of member values to remove from the set
12
+ # @return [Integer] the amount of elements removed
13
+ def remove(*values)
14
+ return self.connection.zrem(@key, values)
15
+ end
16
+
17
+ # Removes all items from the set. Does this by simply deleting the key
18
+ # @see Redstruct::Struct#delete
19
+ def clear
20
+ delete
21
+ end
22
+
23
+ # Returns the cardinality of the set
24
+ # @return [Integer] how many items are in the set
25
+ def size
26
+ return self.connection.zcard(@key)
27
+ end
28
+
29
+ # Returns the number of items between lower and upper bounds.
30
+ # By default lower and upper are inclusive. If you want to make them exclusive, prepend the value with "("
31
+ # @param [#to_s, #to_f] lower lower bound for the count range
32
+ # @param [#to_s, #to_f] upper upper bound for the count range
33
+ # @return [Integer] the number of items in the given range
34
+ def count(lower: nil, upper: nil)
35
+ return slice(lower: lower, upper: upper).size
36
+ end
37
+
38
+ # Returns a slice or partial selection of the set.
39
+ # @param [#to_s, #to_f] lower lower bound for the slice operation; it should be a simple float
40
+ # @param [#to_s, #to_f] upper upper bound for the slice operation; it should be a simple float
41
+ # @return [Redstruct::Types::SortedSet::Slice] sorted slice by given bounds, as list of pairs: (score, value)
42
+ def slice(lower: nil, upper: nil)
43
+ return self.class::Slice.new(self, lower: lower, upper: upper)
44
+ end
45
+
46
+ # Checks if the set contains any items.
47
+ # @return [Boolean] true if the key exists (meaning it contains at least 1 item), false otherwise
48
+ def empty?
49
+ return !self.connection.exists?
50
+ end
5
51
 
6
- def add(options = {}, *items)
7
- defaults = { nx: false, xx: false, ch: false }
52
+ # @param [#to_s] item the item to check for
53
+ # @return [Boolean] true if the item is in the set, false otherwise
54
+ def contain?(item)
55
+ return !index(item).nil?
8
56
  end
57
+ alias include? contain?
58
+
59
+ # Returns the index of the item in the set, sorted ascending by score
60
+ # @param [#to_s] item the item to check for
61
+ # @return [Integer, nil] the index of the item, or nil if not found
62
+ # @see Redis#zrank
63
+ def index(item)
64
+ return self.connection.zrank(@key, item)
65
+ end
66
+
67
+ # Returns the index of the item in the set, sorted descending by score
68
+ # @param [#to_s] item the item to check for
69
+ # @return [Integer, nil] the index of the item, or nil if not found
70
+ # @see Redis#zrevrank
71
+ def rindex(item)
72
+ return self.connection.zrevrank(@key, item)
73
+ end
74
+
75
+ # Returns an array representation of the set, sorted by score ascending
76
+ # NOTE: It pulls the whole set into memory, so use each if that's a concern
77
+ # @return [Array<Redstruct::Utils::ScoredValue>] all the items in the set, sorted by score ascending
78
+ # @see Redis#zrange
79
+ def to_a
80
+ return slice.to_a
81
+ end
82
+
83
+ # Utility class to allow operations on portions of the set only
84
+ class Slice
85
+ include Redstruct::Utils::Inspectable
86
+
87
+ # @param [String, Float] lower lower bound for the slice operation
88
+ # @param [String, Float] upper upper bound for the slice operation
89
+ def initialize(set, lower: nil, upper: nil)
90
+ @set = set
91
+ @lower = parse_bound(lower || '-inf')
92
+ @upper = parse_bound(upper || '+inf')
93
+ end
94
+
95
+ # @return [Array<String>] returns an array of values for the given scores
96
+ def to_a
97
+ @set.connection.zrangebyscore(@set.key, @lower, @upper)
98
+ end
99
+
100
+ # @return [Integer] the number of elements removed
101
+ def remove
102
+ @set.connection.zremrangebyscore(@set.key, @lower, @upper)
103
+ end
104
+
105
+ # @return [Integer] number of elements in the slice
106
+ def size
107
+ @set.connection.zcount(@set.key, @lower, @upper)
108
+ end
109
+
110
+ def inspectable_attributes
111
+ { lower: @lower, upper: @upper, set: @set }
112
+ end
113
+
114
+ private
9
115
 
10
- def <<(item)
11
- return self.connection.zadd(@key, DEFAULT_SCORE, item)
116
+ def parse_bound(bound)
117
+ case bound
118
+ when -Float::INFINITY
119
+ '-inf'
120
+ when Float::INFINITY
121
+ '+inf'
122
+ else
123
+ bound
124
+ end
125
+ end
12
126
  end
13
127
  end
14
128
  end
@@ -1,3 +1,3 @@
1
1
  module Redstruct
2
- VERSION = '0.1.6'.freeze
2
+ VERSION = '0.1.7'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redstruct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Pepin-Perreault
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-16 00:00:00.000000000 Z
11
+ date: 2017-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -145,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
145
145
  version: '0'
146
146
  requirements: []
147
147
  rubyforge_project:
148
- rubygems_version: 2.5.1
148
+ rubygems_version: 2.6.12
149
149
  signing_key:
150
150
  specification_version: 4
151
151
  summary: Higher level data structures for Redis.