redis-set 0.0.1 → 0.0.2

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +90 -8
  3. data/lib/redis/set.rb +73 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f92eb97fa5e6b6a92a3fd2626601b71fa3fd672c
4
- data.tar.gz: e4a4ef26258223dbf23648ad9451808ef531e7ff
3
+ metadata.gz: 85fb6b90aa4d2878c749b147389a183557967f5a
4
+ data.tar.gz: e6d89f6f66dd79108298f7d97cab63ca8e3a1e22
5
5
  SHA512:
6
- metadata.gz: 44445bd85204bacb5d94c8916d3ef040f32ec61ae87ea917f7071856d456cb145c9e5c41cd57d4cacf8b5c39edc69e1e8dddfbbfc8fc66658a10b6a32858c0d1
7
- data.tar.gz: cdf1eb9a32bf14bad861a68425411bf1696aace19872c7ca3276c02928545cca5dfee99d046da4c91db34776dfe5a7422b14e438a95892a2682ed04e040aa7c4
6
+ metadata.gz: 703d725c82cc116107ded6d3174dc2646ab419cbc0c11665523a0386e6688f104854a7c45c8feaaf18e61fdbcaf5a6e8c862ba3605461bb4b86962f0832275c5
7
+ data.tar.gz: a1ac7949c4cbf73eb0c9bd4f5d1c930e355860d1064a7a8dfd5c2c19d9b4d0cff4f189bc3b0f1b23705a6398fd4899e206c444195db9951d9282c225fbddf56a
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Redis::Set
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/redis/set`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ A unique set of unordered items. Lightweight wrapper over redis sets with some additional enumeration and atomic operations.
4
4
 
5
5
  TODO: Delete this and the text above, and describe your gem
6
6
 
@@ -20,17 +20,99 @@ Or install it yourself as:
20
20
 
21
21
  $ gem install redis-set
22
22
 
23
- ## Usage
23
+ ## Getting started
24
24
 
25
- TODO: Write usage instructions here
25
+ ```ruby
26
+ s = Redis::Set.new 'completed_customer_ids'
27
+ ```
28
+
29
+ Or you can pass in your own instance of the Redis class.
30
+
31
+ ```ruby
32
+ s = Redis::Set.new 'completed_customer_ids', Redis.new(:host => "10.0.1.1", :port => 6380, :db => 15)
33
+ ```
34
+
35
+ A third option is to instead pass your Redis configurations.
36
+
37
+ ```ruby
38
+ s = Redis::Set.new 'completed_customer_ids', :host => "10.0.1.1", :port => 6380, :db => 15
39
+ ```
40
+
41
+ ## Using the set
42
+
43
+ You can add data to the set using either the add or push methods.
44
+
45
+ ```ruby
46
+ s.add "hello"
47
+ s.add "world"
48
+ s.add "hello" # the item 'hello' will only exist once in the set since it is unique
49
+ ```
26
50
 
27
- ## Development
51
+ You can insert multiple items set using the add_multi or push_multi methods
28
52
 
29
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
53
+ ```ruby
54
+ s.add_multi ["one","two","three"]
55
+ s.add_multi "four","five","six"
56
+ # set should have items "one","two","three","four","five","six" now
57
+ ```
30
58
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
59
+ You can insert a new item into the set and get the resultant size of the set atomically
60
+ ```ruby
61
+ new_count = s.add_with_count "awesome"
62
+ ```
32
63
 
33
- ## Contributing
64
+ You can pop a random item from the set
65
+ ```ruby
66
+ result = s.pop
67
+ ```
68
+
69
+ You can pop multiple random items from the set
70
+ ```ruby
71
+ result = s.pop_multi 5 # pop 5 random items from set and return them
72
+ ```
34
73
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/redis-set.
74
+ You can remove a specific item from the set
75
+ ```ruby
76
+ s.remove 5 #remove the item 5 from the set if it exists
77
+ ```
78
+
79
+ You can atomically remove multiple items from the set.
80
+
81
+ ```ruby
82
+ s.remove_multi 3,4,5 #removes items 3,4, and 5 from the set if they exist
83
+ ```
84
+
85
+ You can get the size of the set.
86
+
87
+ ```ruby
88
+ s.size
89
+ ```
90
+
91
+ You can see if an item exists in the set.
92
+
93
+ ```ruby
94
+ s.include? "hello"
95
+ ```
96
+
97
+ You can get all items in the set.
98
+
99
+ ```ruby
100
+ s.all
101
+ ```
102
+
103
+ The set can be cleared of all items
104
+ ```ruby
105
+ s.clear
106
+ ```
107
+
108
+ The set can also be set to expire (in seconds).
109
+ ```ruby
110
+ # expire in five minutes
111
+ s.expire 60*5
112
+ ```
113
+
114
+ You can enumerate the set in batches.
115
+ ```ruby
116
+ s.enumerator(100).each{ |i| puts i } #enumerate through the set in batches of 100 items per redis op
117
+ ```
36
118
 
@@ -4,12 +4,14 @@ class Redis
4
4
  class Set
5
5
  attr_reader :name
6
6
 
7
- VERSION = "0.0.1"
7
+ VERSION = "0.0.2"
8
8
 
9
9
  class InvalidNameException < StandardError; end;
10
10
  class InvalidRedisConfigException < StandardError; end;
11
11
 
12
12
  def initialize(name, redis_or_options = {})
13
+ name = name.to_s if name.kind_of? Symbol
14
+
13
15
  raise InvalidNameException.new unless name.kind_of?(String) && name.size > 0
14
16
  @name = name
15
17
  @redis = if redis_or_options.kind_of? Redis
@@ -25,6 +27,8 @@ class Redis
25
27
  @redis.sadd name, value
26
28
  end
27
29
 
30
+ alias push add
31
+
28
32
  def add_multi *values
29
33
  if values.size > 0
30
34
  values = values.first if 1 == values.size && values.first.kind_of?(Array)
@@ -32,6 +36,14 @@ class Redis
32
36
  end
33
37
  end
34
38
 
39
+ alias push_multi add_multi
40
+
41
+ def add_with_count value
42
+ block_on_atomic_attempt{ attempt_atomic_add_read_count value }
43
+ end
44
+
45
+ alias push_with_count add_with_count
46
+
35
47
  def remove value
36
48
  @redis.srem name, value
37
49
  end
@@ -43,6 +55,14 @@ class Redis
43
55
  end
44
56
  end
45
57
 
58
+ def pop
59
+ @redis.pop name, 1
60
+ end
61
+
62
+ def pop_multi amount
63
+ @redis.pop name, amount
64
+ end
65
+
46
66
  def include? value
47
67
  @redis.sismember(name, value)
48
68
  end
@@ -51,17 +71,69 @@ class Redis
51
71
  @redis.scard name
52
72
  end
53
73
 
74
+ alias count size
75
+
54
76
  def all
55
77
  @redis.smembers name
56
78
  end
57
79
 
80
+ def scan cursor = 0, amount=10, match = "*"
81
+ @redis.sscan name, cursor, :count => amount, :match => match
82
+ end
83
+
84
+ def enumerator(slice_size = 10)
85
+ cursor = 0
86
+ Enumerator.new do |yielder|
87
+ loop do
88
+ cursor, items = scan cursor, slice_size
89
+ items.each do |item|
90
+ yielder << item
91
+ end
92
+ raise StopIteration if cursor.to_i.zero?
93
+ end
94
+ end
95
+ end
96
+
58
97
  def clear
59
98
  @redis.del name
60
99
  []
61
100
  end
62
101
 
102
+ alias flush clear
103
+
63
104
  def expire seconds
64
105
  @redis.expire name, seconds
65
106
  end
107
+
108
+ private
109
+
110
+ def attempt_atomic_add_read_count value
111
+ attempt_atomic_write_read lambda{ add value }, lambda{ |multi, read_result| multi.scard name}
112
+ end
113
+
114
+ def block_on_atomic_attempt
115
+ begin
116
+ success, result = yield
117
+ #puts "success is #{success} and result is #{result}"
118
+ end while !success && result
119
+ result.value
120
+ end
121
+
122
+ def attempt_atomic_write_read write_op, read_op
123
+
124
+ write_result, read_result = nil, nil
125
+ success = @redis.watch(name) do
126
+ write_result = write_op.call
127
+ #if write_result
128
+ @redis.multi do |multi|
129
+ read_result = read_op.call multi, write_result
130
+ end
131
+ #end
132
+ end
133
+
134
+ [success, read_result]
135
+ end
136
+
137
+
66
138
  end
67
139
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-set
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Misha Conway
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-14 00:00:00.000000000 Z
11
+ date: 2017-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler