redis-structured-multi 0.0.1

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. data/LICENSE +9 -0
  2. data/README.md +33 -0
  3. data/lib/redis/structured-multi.rb +113 -0
  4. metadata +72 -0
data/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ Copyright (c) 2011 Sean Keith McAuley
2
+
3
+ This software is MIT-licensed.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ `redis-structured-multi` is a Ruby library for assembling Redis's multibulk replies (from the [redis-rb gem](http://github.com/ezmobius/redis-rb)'s `Redis#multi`) into objects, before they're actually returned, by using promises, thunks, and [fmap](http://github.com/derefr/fmap).
2
+
3
+ Just require `redis/structured-multi`, then you can do something like this:
4
+
5
+ records = [
6
+ {:name => 'bob', :height => 180, :likes => 'carrots'},
7
+ {:name => 'phil', :height => 145, :likes => 'apples'}]
8
+
9
+ REDIS = Redis.new
10
+
11
+ full_records = REDIS.structured_multi do
12
+ records.map do |record|
13
+ last_visit = REDIS.get("user:#{record[:name]}:lastvisittime").to_i
14
+
15
+ likes = (REDIS.smembers("user:#{record[:name]}:likes") + [record[:likes]]).to_set
16
+
17
+ left_handed = REDIS.get("user:#{record[:name]}:has:hand:left")
18
+ right_handed = REDIS.get("user:#{record[:name]}:has:hand:right")
19
+
20
+ record.merge(
21
+ :last_visit => last_visit,
22
+ :likes => likes,
23
+ :has_both_hands => (left_handed && right_handed))
24
+ end
25
+ end
26
+
27
+ Under the covers, this is a single Redis pipeline. Even though we're able to do things like `redis_bool && other_redis_bool`, or `redis_set_members + static_set_members`, we're really just transforming promises into other promises. Here's the strategy:
28
+
29
+ 1. Execute a bunch of Redis commands; receive promises in exchange.
30
+ 2. Build the structure you want out of data you have, plus these promises.
31
+ 3. Return this structure as the value of the `structured_multi` block.
32
+
33
+ After you do this, `structured-multi` will actually execute the pipeline, then `fmap` your structure into an equivalent one with all the same real data, but with the promises replaced with their actual Redis-retrieved values.
@@ -0,0 +1,113 @@
1
+ require 'fmap'
2
+ require 'redis'
3
+
4
+ class Redis
5
+ class ReceiptedPipeline < Pipeline
6
+ class Receipt
7
+ def initialize(pipeline, token, commands = nil)
8
+ @pipeline = pipeline
9
+ @token = token
10
+ @commands = (commands or [])
11
+ end
12
+
13
+ def ===( o )
14
+ if o.kind_of? Thunk
15
+ @pipeline.equal?(o.pipeline)
16
+ else
17
+ super
18
+ end
19
+ end
20
+
21
+ def method_missing(m, *args, &bl)
22
+ new_command = [m, args, bl]
23
+ self.class.new(@pipeline, @token, @commands + [new_command])
24
+ end
25
+ end
26
+
27
+ class Thunk
28
+ def initialize(pipeline, mbulk_reply)
29
+ @pipeline = pipeline
30
+ @mbulk_reply = mbulk_reply
31
+ @evaled_receipts = {}
32
+ end
33
+
34
+ attr :pipeline
35
+
36
+ def ===( o )
37
+ if o.kind_of? Receipt
38
+ @pipeline.equal?(o.pipeline)
39
+ else
40
+ super
41
+ end
42
+ end
43
+
44
+ def []( receipt )
45
+ receipt_token = receipt.instance_variable_get(:@token)
46
+ return @evaled_receipts[receipt_token] if @evaled_receipts.has_key?( receipt_token )
47
+
48
+ response = @mbulk_reply[ receipt_token ]
49
+ receipt.instance_variable_get(:@commands).inject(response) do |o, (m, args, bl)|
50
+ evaled_args = args.eqfmap(self){ |receipt| self[ receipt ] }
51
+ o.send(m, *evaled_args, &bl)
52
+ end
53
+ end
54
+ end
55
+
56
+
57
+ def initialize
58
+ super
59
+ @tokens_generated = 0
60
+ end
61
+
62
+ def generate_reply_receipt
63
+ token = (@tokens_generated += 1) - 1
64
+ Receipt.new( self, token )
65
+ end
66
+
67
+ def call(*args)
68
+ super
69
+ self.generate_reply_receipt
70
+ end
71
+
72
+ def call_pipelined(commands, options = {})
73
+ before = @commands.length
74
+ super
75
+ after = @commands.length
76
+
77
+ receipts = []
78
+ (after - before).times { receipts << self.generate_reply_receipt }
79
+ receipts
80
+ end
81
+ end
82
+
83
+ def pipelined_with_receipts(options = {})
84
+ synchronize do
85
+ begin
86
+ original, @client = @client, ReceiptedPipeline.new
87
+ yield
88
+
89
+ unless @client.commands.empty?
90
+ receipted_pipeline = @client
91
+ pipeline_reply = original.call_pipelined(@client.commands, options)
92
+ mbulk_reply = pipeline_reply.last
93
+ ReceiptedPipeline::Thunk.new(receipted_pipeline, mbulk_reply)
94
+ end
95
+ ensure
96
+ @client = original
97
+ end
98
+ end
99
+ end
100
+
101
+ def structured_multi
102
+ synchronize do
103
+ structure = nil
104
+ pipeline_thunk = pipelined_with_receipts(:raise => false) do
105
+ @client.call_without_reply([:multi])
106
+ structure = yield(self)
107
+ @client.call_without_reply([:exec])
108
+ end
109
+
110
+ structure.eqfmap(pipeline_thunk){ |receipt| pipeline_thunk[ receipt ] }
111
+ end
112
+ end
113
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis-structured-multi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sean Keith McAuley
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-22 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: redis
16
+ requirement: &70267549553720 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.2.2
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70267549553720
25
+ - !ruby/object:Gem::Dependency
26
+ name: fmap
27
+ requirement: &70267549553240 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 0.0.1
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70267549553240
36
+ description: ! 'redis-structured-multi is an extension to the redis gem''s Redis#multi
37
+ that allows data structures to be built from the as-yet-unreturned values of the
38
+ calls to Redis that occur within the #multi block.'
39
+ email: sean@zarokeanpie.com
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - README.md
45
+ - LICENSE
46
+ - lib/redis/structured-multi.rb
47
+ homepage: http://github.com/derefr/redis-structured-multi
48
+ licenses: []
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 1.8.8
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: redis-structured-multi extends the redis gem's Redis#multi to allow data
71
+ structures to be built from promises before data is fetched
72
+ test_files: []