Class: ZeevexCluster::Coordinator::Mysql
- Inherits:
-
BaseKeyValStore
- Object
- BaseKeyValStore
- ZeevexCluster::Coordinator::Mysql
- Defined in:
- lib/zeevex_cluster/coordinator/mysql.rb
Defined Under Namespace
Classes: Literal
Constant Summary collapse
- ERR_DUPLICATE_KEY =
1062
Class Method Summary collapse
Instance Method Summary collapse
-
#add(key, value, options = {}) ⇒ Object
Add the value for a key to the DB if there is no existing entry Serializes unless :raw => true.
-
#append(key, str, options = {}) ⇒ Object
append string vlaue to an entry does NOT serialize.
-
#cas(key, options = {}) ⇒ Object
Block is passed the current value, and returns the updated value.
-
#delete(key, options = {}) ⇒ Object
Delete key from database; true if key existed beforehand.
-
#get(key, options = {}) ⇒ Object
Fetch the value for a key, deserializing unless :raw => true.
-
#initialize(options = {}) ⇒ Mysql
constructor
A new instance of Mysql.
-
#prepend(key, str, options = {}) ⇒ Object
prepend string value to an entry does NOT serialize.
-
#set(key, value, options = {}) ⇒ Object
Set the value for a key, serializing unless :raw => true.
Methods inherited from BaseKeyValStore
Methods included from Util::Logging
Constructor Details
#initialize(options = {}) ⇒ Mysql
Returns a new instance of Mysql.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/zeevex_cluster/coordinator/mysql.rb', line 38 def initialize( = {}) super @table = @options[:table] || 'kvstore' @logger = @options[:logger] || Logger.new(STDOUT) @namespace = @options.fetch(:namespace, '') @client ||= Mysql2::Client.new(:host => [:server] || 'localhost', :port => [:port] | 3306, :database => [:database] || 'zcluster', :username => [:username], :password => [:password], :reconnect => true, :symbolize_keys => true, :cache_rows => false, :application_timezone => :utc, :database_timezone => :utc) end |
Class Method Details
.setup ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/zeevex_cluster/coordinator/mysql.rb', line 29 def self.setup unless @setup require 'mysql2' BaseKeyValStore.setup @setup = true end end |
Instance Method Details
#add(key, value, options = {}) ⇒ Object
Add the value for a key to the DB if there is no existing entry Serializes unless :raw => true
Returns true if key was added, false if key already had a value
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/zeevex_cluster/coordinator/mysql.rb', line 61 def add(key, value, = {}) key = to_key(key) value = serialize_value(value, is_raw?()) res = do_insert_row({:keyname => key, :value => value, :namespace => @namespace}, :expiration => .fetch(:expiration, @expiration)) res[:affected_rows] == 1 rescue Mysql2::Error => e case e.error_number # duplicate key # see http://www.briandunning.com/error-codes/?source=MySQL when ERR_DUPLICATE_KEY false else raise ZeevexCluster::Coordinator::ConnectionError.new, "Unhandled mysql error: #{e.errno} #{e.}", e end rescue raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $! end |
#append(key, str, options = {}) ⇒ Object
append string vlaue to an entry does NOT serialize
157 158 159 160 161 162 |
# File 'lib/zeevex_cluster/coordinator/mysql.rb', line 157 def append(key, str, = {}) newval = Literal.new %{CONCAT(value, #{qval str})} do_update_row({:keyname => to_key(key)}, {:value => newval}) rescue ::Mysql2::Error raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $! end |
#cas(key, options = {}) ⇒ Object
Block is passed the current value, and returns the updated value.
Block can raise DontChange to simply exit the block without updating.
returns nil for no value returns false for failure (somebody else set) returns true for success
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/zeevex_cluster/coordinator/mysql.rb', line 111 def cas(key, = {}) key = to_key(key) orig_row = do_get_first(key) return nil unless orig_row expiration = .fetch(:expiration, @expiration) newval = serialize_value(yield(deserialize_value(orig_row[:value], is_raw?())), is_raw?()) updates = {:value => newval} res = do_update_row(simple_cond(orig_row), updates, :expiration => .fetch(:expiration, @expiration)) case res when false then false when true then true else raise ZeevexCluster::Coordinator::ConnectionError, "Unhandled return value from do_update_row - #{res.inspect}" end rescue ZeevexCluster::Coordinator::DontChange => e false rescue ::Mysql2::Error logger.error "got error in cas: #{$!.inspect}" raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $! rescue logger.error "got general error in cas: #{$!.inspect}" raise end |
#delete(key, options = {}) ⇒ Object
Delete key from database; true if key existed beforehand
97 98 99 100 |
# File 'lib/zeevex_cluster/coordinator/mysql.rb', line 97 def delete(key, = {}) res = do_delete_row(:keyname => to_key(key)) res[:success] end |
#get(key, options = {}) ⇒ Object
Fetch the value for a key, deserializing unless :raw => true
141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/zeevex_cluster/coordinator/mysql.rb', line 141 def get(key, = {}) key = to_key(key) row = do_get_first key return nil if row.nil? if !is_raw?() deserialize_value(row[:value]) else row[:value] end rescue ::Mysql2::Error raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $! end |
#prepend(key, str, options = {}) ⇒ Object
prepend string value to an entry does NOT serialize
166 167 168 169 170 171 |
# File 'lib/zeevex_cluster/coordinator/mysql.rb', line 166 def prepend(key, str, = {}) newval = Literal.new %{CONCAT(#{qval str}, value)} do_update_row({:keyname => to_key(key)}, {:value => newval}) rescue ::Mysql2::Error raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $! end |
#set(key, value, options = {}) ⇒ Object
Set the value for a key, serializing unless :raw => true
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/zeevex_cluster/coordinator/mysql.rb', line 83 def set(key, value, = {}) key = to_key(key) value = serialize_value(value, is_raw?()) row = {:keyname => key, :value => value} res = do_upsert_row(row, :expiration => .fetch(:expiration, @expiration), :skip_locking => true) res[:success] rescue ::Mysql2::Error raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $! end |