lib/github/kv.rb in github-ds-0.3.0 vs lib/github/kv.rb in github-ds-0.4.0

- old
+ new

@@ -1,5 +1,6 @@ +require_relative "kv/config" require_relative "result" require_relative "sql" # GitHub::KV is a key/value data store backed by MySQL (however, the backing # store used should be regarded as an implementation detail). @@ -51,11 +52,24 @@ InvalidValueError = Class.new(StandardError) class MissingConnectionError < StandardError; end attr_accessor :use_local_time + attr_writer :config + def self.config + @config ||= Config.new + end + + def self.reset + @config = Config.new + end + + def self.configure + yield(config) + end + # initialize :: [Exception], Boolean, Proc -> nil # # Initialize a new KV instance. # # encapsulated_errors - An Array of Exception subclasses that, when raised, @@ -65,13 +79,14 @@ # where time needs to be modified (eg. Timecop). # Default false. # &conn_block - A block to call to open a new database connection. # # Returns nothing. - def initialize(encapsulated_errors = [SystemCallError], use_local_time: false, &conn_block) - @encapsulated_errors = encapsulated_errors - @use_local_time = use_local_time + def initialize(config: GitHub::KV.config, &conn_block) + @encapsulated_errors = config.encapsulated_errors + @use_local_time = config.use_local_time + @table_name = config.table_name @conn_block = conn_block end def connection @conn_block.try(:call) || (raise MissingConnectionError, "KV must be initialized with a block that returns a connection") @@ -109,11 +124,11 @@ def mget(keys) validate_key_array(keys) Result.new { kvs = GitHub::SQL.results(<<-SQL, :keys => keys, :now => now, :connection => connection).to_h - SELECT `key`, value FROM key_values WHERE `key` IN :keys AND (`expires_at` IS NULL OR `expires_at` > :now) + SELECT `key`, value FROM #{@table_name} WHERE `key` IN :keys AND (`expires_at` IS NULL OR `expires_at` > :now) SQL keys.map { |key| kvs[key] } } end @@ -157,11 +172,11 @@ [key, value, now, now, expires || GitHub::SQL::NULL] } encapsulate_error do GitHub::SQL.run(<<-SQL, :rows => GitHub::SQL::ROWS(rows), :connection => connection) - INSERT INTO key_values (`key`, value, created_at, updated_at, expires_at) + INSERT INTO #{@table_name} (`key`, value, created_at, updated_at, expires_at) VALUES :rows ON DUPLICATE KEY UPDATE value = VALUES(value), updated_at = VALUES(updated_at), expires_at = VALUES(expires_at) @@ -202,11 +217,11 @@ def mexists(keys) validate_key_array(keys) Result.new { existing_keys = GitHub::SQL.values(<<-SQL, :keys => keys, :now => now, :connection => connection).to_set - SELECT `key` FROM key_values WHERE `key` IN :keys AND (`expires_at` IS NULL OR `expires_at` > :now) + SELECT `key` FROM #{@table_name} WHERE `key` IN :keys AND (`expires_at` IS NULL OR `expires_at` > :now) SQL keys.map { |key| existing_keys.include?(key) } } end @@ -238,16 +253,16 @@ # if the key already exists but has expired, prune it first. We could # achieve the same thing with the right INSERT ... ON DUPLICATE KEY UPDATE # query, but then we would not be able to rely on affected_rows GitHub::SQL.run(<<-SQL, :key => key, :now => now, :connection => connection) - DELETE FROM key_values WHERE `key` = :key AND expires_at <= :now + DELETE FROM #{@table_name} WHERE `key` = :key AND expires_at <= :now SQL value = value.is_a?(GitHub::SQL::Literal) ? value : GitHub::SQL::BINARY(value) sql = GitHub::SQL.run(<<-SQL, :key => key, :value => value, :now => now, :expires => expires || GitHub::SQL::NULL, :connection => connection) - INSERT IGNORE INTO key_values (`key`, value, created_at, updated_at, expires_at) + INSERT IGNORE INTO #{@table_name} (`key`, value, created_at, updated_at, expires_at) VALUES (:key, :value, :now, :now, :expires) SQL sql.affected_rows > 0 } @@ -291,11 +306,11 @@ # Lastly we only do these tricks when the value at the key is an integer. # If the value is not an integer the update ensures the values remain the # same and we raise an error. encapsulate_error { sql = GitHub::SQL.run(<<-SQL, key: key, amount: amount, now: now, expires: expires, touch: !touch_on_insert, connection: connection) - INSERT INTO key_values (`key`, `value`, `created_at`, `updated_at`, `expires_at`) + INSERT INTO #{@table_name} (`key`, `value`, `created_at`, `updated_at`, `expires_at`) VALUES(:key, :amount, :now, :now, :expires) ON DUPLICATE KEY UPDATE `value`=IF( concat('',`value`*1) = `value`, LAST_INSERT_ID(IF( @@ -376,11 +391,11 @@ def mdel(keys) validate_key_array(keys) encapsulate_error do GitHub::SQL.run(<<-SQL, :keys => keys, :connection => connection) - DELETE FROM key_values WHERE `key` IN :keys + DELETE FROM #{@table_name} WHERE `key` IN :keys SQL end nil end @@ -400,11 +415,11 @@ def ttl(key) validate_key(key) Result.new { GitHub::SQL.value(<<-SQL, :key => key, :now => now, :connection => connection) - SELECT expires_at FROM key_values + SELECT expires_at FROM #{@table_name} WHERE `key` = :key AND (expires_at IS NULL OR expires_at > :now) SQL } end @@ -420,10 +435,10 @@ def mttl(keys) validate_key_array(keys) Result.new { kvs = GitHub::SQL.results(<<-SQL, :keys => keys, :now => now, :connection => connection).to_h - SELECT `key`, expires_at FROM key_values + SELECT `key`, expires_at FROM #{@table_name} WHERE `key` in :keys AND (expires_at IS NULL OR expires_at > :now) SQL keys.map { |key| kvs[key] } }