Class: KT
- Inherits:
-
Object
- Object
- KT
- Defined in:
- lib/kt.rb,
lib/kt/kv.rb,
lib/kt/errors.rb,
lib/kt/version.rb
Defined Under Namespace
Classes: CASFailed, Error, KV, RecordNotFound
Constant Summary collapse
- IDENTITY_ENCODING =
"text/tab-separated-values"
- BASE64_ENCODING =
"text/tab-separated-values; colenc=B"
- IDENTITY_HEADERS =
{"Content-Type" => IDENTITY_ENCODING}
- BASE64_HEADERS =
{"Content-Type" => BASE64_ENCODING}
- EMPTY_HEADERS =
{}
- VERSION =
"0.4.0"
Instance Method Summary collapse
-
#cas(key, oval = nil, nval = nil) ⇒ Object
cas executes a compare and swap operation if both old and new provided it sets to new value if previous value is old value if no old value provided it will set to new value if key is not present in db if no new value provided it will remove the record if it exists it returns true if it succeded or false otherwise.
-
#cas!(key, oval = nil, nval = nil) ⇒ Object
cas! works the same as cas but it raises error on failure.
-
#clear ⇒ Object
clear removes all records in the database.
-
#count ⇒ Object
count returns the number of records in the database.
-
#fetch(key, &block) ⇒ Object
fetch retrives the keys from cache if key is found it returns the unmarshaled value if key is not found it runs the block sends the value and returns it.
-
#get(key) ⇒ Object
get retrieves the data stored at key.
-
#get!(key) ⇒ Object
get! retrieves the data stored at key.
-
#get_bulk(keys) ⇒ Object
get_bulk retrieves the keys in the list It returns a hash of key => value.
-
#initialize(options) ⇒ KT
constructor
A new instance of KT.
-
#match_prefix(prefix, max_records = -1)) ⇒ Object
match_prefix performs the match_prefix operation against the server It returns a sorted list of keys.
-
#pttl(key) ⇒ Object
pttl returns the time to live for a key in milliseconds if key does not exist, it returns -2 if key exists but no ttl is set, it returns -1 if key exists and it has a ttl, it returns the number of milliseconds remaining.
-
#remove(key) ⇒ Object
remove deletes the data at key in the database.
-
#remove!(key) ⇒ Object
remove! deletes the data at key in the database it raises KT::RecordNotFound if key was not found.
-
#remove_bulk(keys) ⇒ Object
remove_bulk deletes multiple keys.
-
#set(key, value, expire: nil) ⇒ Object
set stores the data at key.
-
#set_bulk(values) ⇒ Object
set_bulk sets multiple keys to multiple values.
-
#ttl(key) ⇒ Object
ttl returns the time to live for a key in seconds if key does not exist, it returns -2 if key exists but no ttl is set, it returns -1 if key exists and it has a ttl, it returns the number of seconds remaining.
-
#vacuum ⇒ Object
vacuum triggers garbage collection of expired records.
Constructor Details
#initialize(options) ⇒ KT
Returns a new instance of KT.
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/kt.rb', line 16 def initialize() @host = .fetch(:host, "127.0.0.1") @port = .fetch(:port, 1978) @poolsize = .fetch(:poolsize, 5) @timeout = .fetch(:timeout, 5.0) @connect_timeout = .fetch(:connect_timeout, 0.5) @read_timeout = .fetch(:read_timeout, 0.5) @write_timeout = .fetch(:write_timeout, 0.5) @pool = ConnectionPool.new(size: @poolsize, timeout: @timeout) do Excon.new("http://#{@host}:#{@port}", :connect_timeout => @connect_timeout) end end |
Instance Method Details
#cas(key, oval = nil, nval = nil) ⇒ Object
cas executes a compare and swap operation if both old and new provided it sets to new value if previous value is old value if no old value provided it will set to new value if key is not present in db if no new value provided it will remove the record if it exists it returns true if it succeded or false otherwise
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/kt.rb', line 282 def cas(key, oval = nil, nval = nil) req = [KT::KV.new("key", key)] if oval != nil req << KT::KV.new("oval", oval) end if nval != nil req << KT::KV.new("nval", nval) end status, body = do_rpc("/rpc/cas", req) if status == 450 return false end if status != 200 raise_error(body) end return true end |
#cas!(key, oval = nil, nval = nil) ⇒ Object
cas! works the same as cas but it raises error on failure
305 306 307 308 309 |
# File 'lib/kt.rb', line 305 def cas!(key, oval = nil, nval = nil) if !cas(key, oval, nval) raise KT::CASFailed.new("Failed compare and swap for #{key}") end end |
#clear ⇒ Object
clear removes all records in the database
42 43 44 45 46 47 48 |
# File 'lib/kt.rb', line 42 def clear status, m = do_rpc("/rpc/clear") if status != 200 raise_error(m) end end |
#count ⇒ Object
count returns the number of records in the database
31 32 33 34 35 36 37 38 39 |
# File 'lib/kt.rb', line 31 def count status, m = do_rpc("/rpc/status") if status != 200 raise_error(m) end find_rec(m, "count").value.to_i end |
#fetch(key, &block) ⇒ Object
fetch retrives the keys from cache if key is found it returns the unmarshaled value if key is not found it runs the block sends the value and returns it
111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/kt.rb', line 111 def fetch(key, &block) value = get(key) if value Marshal::load(value) else block.call.tap do |value| unless value.nil? set(key, Marshal::dump(value)) end end end end |
#get(key) ⇒ Object
get retrieves the data stored at key. It returns nil if no such data is found
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/kt.rb', line 61 def get(key) status, body = do_rest("GET", key, nil) case status when 200 body when 404 nil end end |
#get!(key) ⇒ Object
get! retrieves the data stored at key. KT::RecordNotFound is raised if not such data is found
74 75 76 77 78 79 80 81 |
# File 'lib/kt.rb', line 74 def get!(key) value = get(key) if value != nil value else raise KT::RecordNotFound.new("Key: #{key} not found") end end |
#get_bulk(keys) ⇒ Object
get_bulk retrieves the keys in the list It returns a hash of key => value. If a key was not found in the database, the value in return hash will be nil
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/kt.rb', line 86 def get_bulk(keys) req = keys.map do |key| KT::KV.new("_#{key}", "") end status, res_body = do_rpc("/rpc/get_bulk", req) if status != 200 raise_error(res_body) end res = {} res_body.each do |kv| if kv.key.start_with?('_') res[kv.key[1, kv.key.size - 1]] = kv.value end end return res end |
#match_prefix(prefix, max_records = -1)) ⇒ Object
match_prefix performs the match_prefix operation against the server It returns a sorted list of keys. max_records defines the number of results to be returned if negative, it means unlimited
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/kt.rb', line 254 def match_prefix(prefix, max_records = -1) req = [ KT::KV.new("prefix", prefix), KT::KV.new("max", max_records.to_s) ] status, body = do_rpc("/rpc/match_prefix", req) if status != 200 raise_error(body) end res = [] body.each do |kv| if kv.key.start_with?('_') res << kv.key[1, kv.key.size - 1] end end return res end |
#pttl(key) ⇒ Object
pttl returns the time to live for a key in milliseconds if key does not exist, it returns -2 if key exists but no ttl is set, it returns -1 if key exists and it has a ttl, it returns the number of milliseconds remaining
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/kt.rb', line 155 def pttl(key) req = [ KT::KV.new("key", key), ] status, res_body = do_rpc("/rpc/check", req) case status when 200 xt_pos = res_body.index{|kv| kv.key == "xt"} if xt_pos != nil expire_time = res_body[xt_pos].value.to_f [expire_time - Time.now.to_f, 0].max else -1 end when 450 -2 else raise_error(res_body) end end |
#remove(key) ⇒ Object
remove deletes the data at key in the database.
212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/kt.rb', line 212 def remove(key) status, body = do_rest("DELETE", key, nil) if status == 404 return false end if status != 204 raise KT::Error.new(body) end return true end |
#remove!(key) ⇒ Object
remove! deletes the data at key in the database it raises KT::RecordNotFound if key was not found
228 229 230 231 232 |
# File 'lib/kt.rb', line 228 def remove!(key) unless remove(key) raise KT::RecordNotFound.new("key #{key} was not found") end end |
#remove_bulk(keys) ⇒ Object
remove_bulk deletes multiple keys. it returnes the number of keys deleted
236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/kt.rb', line 236 def remove_bulk(keys) req = keys.map do |key| KV.new("_#{key}", "") end status, body = do_rpc("/rpc/remove_bulk", req) if status != 200 raise_error(body) end find_rec(body, "num").value.to_i end |
#set(key, value, expire: nil) ⇒ Object
set stores the data at key
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/kt.rb', line 179 def set(key, value, expire: nil) req = [ KT::KV.new("key", key), KT::KV.new("value", value), ] if expire req << KT::KV.new("xt", expire.to_s) end status, body = do_rpc("/rpc/set", req) if status != 200 raise_error(body) end end |
#set_bulk(values) ⇒ Object
set_bulk sets multiple keys to multiple values
197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/kt.rb', line 197 def set_bulk(values) req = values.map do |key, value| KT::KV.new("_#{key}", value) end status, body = do_rpc("/rpc/set_bulk", req) if status != 200 raise_error(body) end find_rec(body, "num").value.to_i end |
#ttl(key) ⇒ Object
ttl returns the time to live for a key in seconds if key does not exist, it returns -2 if key exists but no ttl is set, it returns -1 if key exists and it has a ttl, it returns the number of seconds remaining
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/kt.rb', line 128 def ttl(key) req = [ KT::KV.new("key", key), ] status, res_body = do_rpc("/rpc/check", req) case status when 200 xt_pos = res_body.index{|kv| kv.key == "xt"} if xt_pos != nil expire_time = res_body[xt_pos].value.to_f [(expire_time - Time.now.to_f).to_i, 0].max else -1 end when 450 -2 else raise_error(res_body) end end |
#vacuum ⇒ Object
vacuum triggers garbage collection of expired records
51 52 53 54 55 56 57 |
# File 'lib/kt.rb', line 51 def vacuum status, m = do_rpc("/rpc/vacuum") if status != 200 raise_error(m) end end |