Replace couchbase counters with drb version

This commit is contained in:
2014-08-05 17:49:16 +02:00
parent c0c25673bf
commit 99a9536c68
20 changed files with 400 additions and 282 deletions
+43
View File
@@ -0,0 +1,43 @@
# This is a non thread safe replacement for the
# couchbase counter mechanism since every test needs
# a clean start and Hash#clear is soooo much faster than
# a couchbase bucket flush
class InMemoryQCounter
attr_reader :store
def initialize
@store = {}
end
def get(key, options = {})
store[key]
end
def set(key, value)
store[key] = value
end
def incr(key, options = {})
# store[key] ||= options[:initial].to_i
# store[key] += 1
if store[key]
store[key] += 1
else
store[key] = options[:initial].to_i
end
end
def decr(key, options = {})
# store[key] ||= options[:initial].to_i
# store[key] -= 1
if store[key]
store[key] -= 1
else
store[key] = options[:initial].to_i
end
end
def flush
store.clear
end
end
+1 -1
View File
@@ -7,6 +7,7 @@ module Qwaiter
autoload :Counter
autoload :Broadcaster
autoload :Couchbase
autoload :DrbCounter
def self.broadcast_user(uid, event, data)
message = {channel: "/user/#{uid}", data: {event: event, data: data}}
@@ -18,4 +19,3 @@ module Qwaiter
broadcaster.broadcast message
end
end
+7
View File
@@ -5,13 +5,20 @@ module Qwaiter
end
def self.load_design_docs!
return unless connection.present?
Dir.glob(Rails.root.join('config/couchbase/design_docs', "*.json")).each do |design_doc|
connection.save_design_doc File.open(design_doc)
end
end
def self.design_doc(name)
return unless connection.present?
connection.design_docs[name]
end
def self.flush_counters!
return unless connection.present?
design_doc('supplier').counters(reduce: false).each{|counter| Qwaiter::Counter.set counter.key, 0}
end
end
end
-5
View File
@@ -22,8 +22,3 @@ module Qwaiter
end
end
end
# use the connection from couchbase-structures/documents
# will be overwritten in the specs since flushing the real
# thing is difficult
Qwaiter::Counter.connection = $cb unless Rails.env.test?
+8
View File
@@ -0,0 +1,8 @@
module Qwaiter
module DrbCounter
def self.object
require 'drb'
DRbObject.new_with_uri('druby://localhost:9022')
end
end
end