Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: lookout/vanity
base: dda319f280
...
head fork: lookout/vanity
compare: adapters
  • 2 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
View
79 lib/vanity/mock_redis.rb
@@ -1,76 +1,7 @@
-module Vanity
- # The Redis you should never use in production.
- class MockRedis
- @@hash = {}
-
- def initialize(options = {})
- end
-
- def [](key)
- @@hash[key]
- end
-
- def []=(key, value)
- @@hash[key] = value.to_s
- end
-
- def del(*keys)
- keys.flatten.each do |key|
- @@hash.delete key
- end
- end
-
- def setnx(key, value)
- @@hash[key] = value.to_s unless @@hash.has_key?(key)
- end
-
- def incr(key)
- @@hash[key] = (@@hash[key].to_i + 1).to_s
- end
-
- def incrby(key, value)
- @@hash[key] = (@@hash[key].to_i + value).to_s
- end
+require "vanity/store/mock"
- def mget(keys)
- @@hash.values_at(*keys)
- end
-
- def exists(key)
- @@hash.has_key?(key)
- end
-
- def keys(pattern)
- regexp = Regexp.new(pattern.split("*").map { |r| Regexp.escape(r) }.join(".*"))
- @@hash.keys.select { |key| key =~ regexp }
- end
-
- def flushdb
- @@hash.clear
- end
-
- def sismember(key, value)
- case set = @@hash[key]
- when nil ; false
- when Set ; set.member?(value)
- else fail "Not a set"
- end
- end
-
- def sadd(key, value)
- case set = @@hash[key]
- when nil ; @@hash[key] = Set.new([value])
- when Set ; set.add value
- else fail "Not a set"
- end
- end
-
- def scard(key)
- case set = @@hash[key]
- when nil ; 0
- when Set ; set.size
- else fail "Not a set"
- end
- end
- end
+module Vanity
+ # @deprecated Please use Vanity::Store::Mock instead.
+ MockRedis = Vanity::Store::Mock
+ warn "Deprecated: please use Vanity::Store::Mock instead"
end
View
17 lib/vanity/playground.rb
@@ -11,14 +11,23 @@ class Playground
# Created new Playground. Unless you need to, use the global Vanity.playground.
def initialize(options = {})
- @host, @port, @db, @load_path = DEFAULTS.merge(options).values_at(:host, :port, :db, :load_path)
+ options = options.merge(DEFAULTS)
+ adapter = options[:adapter] || options[:redis] || :redis
+ if adapter.respond_to?(:mget)
+ @redis = adapter
+ elsif adapter == :redis
+ @host, @port, @db = options.values_at(:host, :port, :db)
+ else
+ require "vanity/store/#{adapter}"
+ @redis = Vanity::Store.const_get(adapter.to_s.capitalize).new
+ end
+ @load_path = options[:load_path]
@namespace = "vanity:#{Vanity::Version::MAJOR}"
@logger = options[:logger]
unless @logger
@logger = Logger.new(STDOUT)
@logger.level = Logger::ERROR
end
- @redis = options[:redis]
@loading = []
end
@@ -105,7 +114,7 @@ class << self ; self ; end.send(:define_method, :redis) { @redis }
@redis
end
- # Switches playground to use MockRedis instead of a live server.
+ # Switches playground to use Vanity::Store::Mock instead of a live server.
# Particularly useful for testing, e.g. if you can't access Redis on your CI
# server. This method has no affect after playground accesses live Redis
# server.
@@ -113,7 +122,7 @@ class << self ; self ; end.send(:define_method, :redis) { @redis }
# @example Put this in config/environments/test.rb
# config.after_initialize { Vanity.playground.mock! }
def mock!
- @redis ||= MockRedis.new
+ @redis ||= Vanity::Store::Mock.new
end
# Returns a metric (raises NameError if no metric with that identifier).
View
106 lib/vanity/store/activerecord.rb
@@ -0,0 +1,106 @@
+module Vanity
+ module Store
+ # @since 1.3.0
+ class ActiveRecord
+ def initialize(connection = nil)
+ @connection = connection
+ end
+
+ def connection
+ @connection ||= ::ActiveRecord::Base.connection
+ end
+
+ def table_name
+ @table_name ||= "vanity_hash"
+ end
+
+ def get(key)
+ select_value("SELECT value FROM #{table_name} WHERE key=?", key)
+ end
+ alias :[] :get
+
+ def setnx(key, value)
+ execute "INSERT INTO #{table_name} (key, value) VALUES (?,?)", key, value rescue nil
+ end
+
+ def set(key, value)
+ execute("INSERT INTO #{table_name} (key, value) VALUES (?,?)", key, value) rescue
+ execute("UPDATE #{table_name} SET value=? WHERE key=?", value, key)
+ end
+ alias :[]= :set
+
+ def del(*keys)
+ execute "DELETE FROM #{table_name} WHERE key IN (?)", keys.flatten
+ end
+
+ def incrby(key, incr)
+ if value = get(key)
+ execute("UPDATE #{table_name} SET value=? WHERE key=?", value.to_i + incr, key) or incrby(key, incr)
+ else
+ execute("INSERT INTO #{table_name} (key, value) VALUES (?,?)", key, incr) or incrby(key, incr)
+ end
+ end
+
+ def incr(key)
+ incrby key, 1
+ end
+
+ def mget(keys)
+ hash = select_rows("SELECT key, value FROM #{table_name} WHERE key IN (?)", keys).
+ inject({}) { |hash, (key, value)| hash.update(key=>value) }
+ keys.map { |key| hash[key] }
+ end
+
+ def exists(key)
+ select_value("SELECT 1 FROM #{table_name} WHERE key = ?", key) && true
+ end
+
+ def keys(pattern)
+ select_values("SELECT key FROM #{table_name} WHERE key LIKE ?", pattern.gsub("*", "%"))
+ end
+
+ def flushdb
+ execute "DELETE FROM #{table_name}"
+ end
+
+ def sismember(key, value)
+ y = get(key)
+ y ? YAML.load(y).member?(value.to_s) : false
+ end
+
+ def sadd(key, value)
+ y = get(key)
+ s = Set.new(y ? YAML.load(y) : [])
+ s.add value.to_s
+ set key, YAML.dump(s.to_a)
+ end
+
+ def scard(key)
+ y = get(key)
+ y ? YAML.load(y).size : 0
+ end
+ protected
+
+ def bind(statement, *args)
+ ::ActiveRecord::Base.send(:sanitize_sql_array, [statement] + args)
+ end
+
+ def select_value(statement, *args)
+ connection.select_value(bind(statement, *args))
+ end
+
+ def select_values(statement, *args)
+ connection.select_values(bind(statement, *args))
+ end
+
+ def select_rows(statement, *args)
+ connection.select_rows(bind(statement, *args))
+ end
+
+ def execute(statement, *args)
+ connection.execute bind(statement, *args)
+ end
+
+ end
+ end
+end
View
73 lib/vanity/store/mock.rb
@@ -0,0 +1,73 @@
+module Vanity
+ module Store
+ # @since 1.3.0
+ class Mock
+ @@hash = {}
+
+ def initialize(options = {})
+ end
+
+ def get(key)
+ @@hash[key]
+ end
+ alias :[] :get
+
+ def set(key, value)
+ @@hash[key] = value.to_s
+ end
+ alias :[]= :set
+
+ def del(*keys)
+ keys.flatten.each do |key|
+ @@hash.delete key
+ end
+ end
+
+ def setnx(key, value)
+ @@hash[key] = value.to_s unless @@hash.has_key?(key)
+ end
+
+ def incr(key)
+ @@hash[key] = (@@hash[key].to_i + 1).to_s
+ end
+
+ def incrby(key, value)
+ @@hash[key] = (@@hash[key].to_i + value).to_s
+ end
+
+ def mget(keys)
+ @@hash.values_at(*keys)
+ end
+
+ def exists(key)
+ @@hash.has_key?(key)
+ end
+
+ def keys(pattern)
+ regexp = Regexp.new(pattern.split("*").map { |r| Regexp.escape(r) }.join(".*"))
+ @@hash.keys.select { |key| key =~ regexp }
+ end
+
+ def flushdb
+ @@hash.clear
+ end
+
+ def sismember(key, value)
+ y = get(key)
+ y ? YAML.load(y).member?(value.to_s) : false
+ end
+
+ def sadd(key, value)
+ y = get(key)
+ s = Set.new(y ? YAML.load(y) : [])
+ s.add value.to_s
+ set key, YAML.dump(s.to_a)
+ end
+
+ def scard(key)
+ y = get(key)
+ y ? YAML.load(y).size : 0
+ end
+ end
+ end
+end
View
24 test/test_helper.rb
@@ -19,6 +19,27 @@
$logger = Logger.new("/dev/null")
end
+
+case store = ENV["STORE"]
+when "REDIS"
+ puts "Using real Redis server"
+ $adapter = :redis
+when "AR"
+ puts "Using ActiveRecord store"
+ require "vanity/store/activerecord"
+ $redis = Vanity::Store::ActiveRecord.new
+ $redis.connection.execute "CREATE TABLE vanity_hash (key VARCHAR(255) NOT NULL, value TEXT, PRIMARY KEY('key'))" rescue nil
+ $adapter = :activerecord
+when nil
+ require "vanity/store/mock"
+ $redis = Vanity::Store::Mock.new
+ $adapter = :mock
+else
+ puts "Acceptable values for STORE=[REDIS|AR]"
+ exit!
+end
+
+
class Test::Unit::TestCase
def setup
@@ -36,8 +57,7 @@ def nuke_playground
# Call this if you need a new playground, e.g. to re-define the same experiment,
# or reload an experiment (saved by the previous playground).
def new_playground
- Vanity.playground = Vanity::Playground.new(:logger=>$logger, :load_path=>"tmp/experiments", :db=>15)
- Vanity.playground.mock! unless ENV["REDIS"]
+ Vanity.playground = Vanity::Playground.new(:logger=>$logger, :load_path=>"tmp/experiments", :db=>15, :adapter=>$adapter)
end
# Defines the specified metrics (one or more names). Returns metric, or array

No commit comments for this range

Something went wrong with that request. Please try again.