From ca75d56ab2e83f24e0c73dd63c917445cc2e0473 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Sun, 18 Aug 2019 00:32:03 +1200 Subject: [PATCH] Rework & simplify contexts. --- lib/async/redis/client.rb | 8 ++--- lib/async/redis/context/nested.rb | 32 ------------------- lib/async/redis/context/pipeline.rb | 4 ++- lib/async/redis/context/subscribe.rb | 4 +-- .../context/{multi.rb => transaction.rb} | 20 +++++++----- .../{multi_spec.rb => transaction_spec.rb} | 10 +++--- spec/async/redis/performance_spec.rb | 12 ------- 7 files changed, 27 insertions(+), 63 deletions(-) delete mode 100644 lib/async/redis/context/nested.rb rename lib/async/redis/context/{multi.rb => transaction.rb} (85%) rename spec/async/redis/context/{multi_spec.rb => transaction_spec.rb} (87%) diff --git a/lib/async/redis/client.rb b/lib/async/redis/client.rb index c09fbc5..6970d19 100755 --- a/lib/async/redis/client.rb +++ b/lib/async/redis/client.rb @@ -19,9 +19,9 @@ # THE SOFTWARE. require_relative 'pool' -require_relative 'context/multi' -require_relative 'context/subscribe' require_relative 'context/pipeline' +require_relative 'context/transaction' +require_relative 'context/subscribe' require_relative 'protocol/resp2' @@ -100,8 +100,8 @@ def multi(&block) end end - def nested(&block) - context = Context::Nested.new(@pool) + def transaction(&block) + context = Context::Transaction.new(@pool) return context unless block_given? diff --git a/lib/async/redis/context/nested.rb b/lib/async/redis/context/nested.rb deleted file mode 100644 index ab74e6a..0000000 --- a/lib/async/redis/context/nested.rb +++ /dev/null @@ -1,32 +0,0 @@ -# Copyright, 2018, by Samuel G. D. Williams. -# Copyright, 2018, by Huba Nagy. -# -# 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: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# 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. - -require_relative 'generic' - -module Async - module Redis - module Context - class Nested < Generic - include ::Protocol::Redis::Methods - end - end - end -end diff --git a/lib/async/redis/context/pipeline.rb b/lib/async/redis/context/pipeline.rb index 5d1d986..452924f 100644 --- a/lib/async/redis/context/pipeline.rb +++ b/lib/async/redis/context/pipeline.rb @@ -25,7 +25,9 @@ module Async module Redis module Context # Send multiple commands without waiting for the response, instead of sending them one by one. - class Pipeline < Nested + class Pipeline < Generic + include ::Protocol::Redis::Methods + class Sync include ::Protocol::Redis::Methods diff --git a/lib/async/redis/context/subscribe.rb b/lib/async/redis/context/subscribe.rb index 73560af..4afd06a 100644 --- a/lib/async/redis/context/subscribe.rb +++ b/lib/async/redis/context/subscribe.rb @@ -19,12 +19,12 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. -require_relative 'nested' +require_relative 'generic' module Async module Redis module Context - class Subscribe < Nested + class Subscribe < Generic def initialize(pool, channels) super(pool) diff --git a/lib/async/redis/context/multi.rb b/lib/async/redis/context/transaction.rb similarity index 85% rename from lib/async/redis/context/multi.rb rename to lib/async/redis/context/transaction.rb index 2c62880..f1a3350 100644 --- a/lib/async/redis/context/multi.rb +++ b/lib/async/redis/context/transaction.rb @@ -19,26 +19,30 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. -require_relative 'nested' +require_relative 'pipeline' module Async module Redis module Context - class Multi < Nested + class Transaction < Pipeline def initialize(pool, *args) super(pool) - - @connection.write_request(['MULTI']) - @connection.flush - @connection.read_response + end + + def multi + sync.call('MULTI') + end + + def watch(*keys) + sync.call('WATCH', *keys) end def execute - call 'EXEC' + sync.call('EXEC') end def discard - call 'DISCARD' + sync.call('DISCARD') end end end diff --git a/spec/async/redis/context/multi_spec.rb b/spec/async/redis/context/transaction_spec.rb similarity index 87% rename from spec/async/redis/context/multi_spec.rb rename to spec/async/redis/context/transaction_spec.rb index c5ed62d..ba82bbc 100644 --- a/spec/async/redis/context/multi_spec.rb +++ b/spec/async/redis/context/transaction_spec.rb @@ -20,7 +20,7 @@ require 'async/redis/client' -RSpec.describe Async::Redis::Context::Multi, timeout: 5 do +RSpec.describe Async::Redis::Context::Transaction, timeout: 5 do include_context Async::RSpec::Reactor let(:endpoint) {Async::Redis.local_endpoint} @@ -28,12 +28,14 @@ let (:multi_key_base) {"async-redis:test:multi"} - it "can atomically execute commands in a multi" do + it "can atomically execute commands" do response = nil - client.multi do |context| + client.transaction do |context| + context.multi + (0..5).each do |id| - response = context.set "#{multi_key_base}:#{id}", "multi-test 6" + response = context.sync.set "#{multi_key_base}:#{id}", "multi-test 6" expect(response).to be == "QUEUED" end diff --git a/spec/async/redis/performance_spec.rb b/spec/async/redis/performance_spec.rb index b590630..eea57c0 100644 --- a/spec/async/redis/performance_spec.rb +++ b/spec/async/redis/performance_spec.rb @@ -45,18 +45,6 @@ end end - benchmark.report("async-redis (nested)") do |times| - key = keys.sample - value = times.to_s - - async_client.nested do |nested| - i = 0; while i < times; i += 1 - nested.set(key, value) - expect(nested.get(key)).to be == value - end - end - end - benchmark.report("async-redis (pipeline)") do |times| key = keys.sample value = times.to_s