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.3.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 |
# 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) @pool = ConnectionPool.new(size: @poolsize, timeout: @timeout) do Excon.new("http://#{@host}:#{@port}") 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
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/kt.rb', line 277 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
300 301 302 303 304 |
# File 'lib/kt.rb', line 300 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
39 40 41 42 43 44 45 |
# File 'lib/kt.rb', line 39 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
28 29 30 31 32 33 34 35 36 |
# File 'lib/kt.rb', line 28 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
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/kt.rb', line 108 def fetch(key, &block) value = get(key) if value Marshal::load(value) else block.call.tap do |value| set(key, Marshal::dump(value)) end end end |
#get(key) ⇒ Object
get retrieves the data stored at key. It returns nil if no such data is found
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/kt.rb', line 58 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
71 72 73 74 75 76 77 78 |
# File 'lib/kt.rb', line 71 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
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/kt.rb', line 83 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
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/kt.rb', line 249 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
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/kt.rb', line 150 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.
207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/kt.rb', line 207 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
223 224 225 226 227 |
# File 'lib/kt.rb', line 223 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
231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/kt.rb', line 231 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
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/kt.rb', line 174 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
192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/kt.rb', line 192 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
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/kt.rb', line 123 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
48 49 50 51 52 53 54 |
# File 'lib/kt.rb', line 48 def vacuum status, m = do_rpc("/rpc/vacuum") if status != 200 raise_error(m) end end |