Class: KT

Inherits:
Object
  • Object
show all
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.2.0"

Instance Method Summary collapse

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(options)
  @host = options.fetch(:host, "127.0.0.1")
  @port = options.fetch(:port, 1978)
  @poolsize = options.fetch(:poolsize, 5)
  @timeout = options.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



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/kt.rb', line 263

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



286
287
288
289
290
# File 'lib/kt.rb', line 286

def cas!(key, oval = nil, nval = nil)
  if !cas(key, oval, nval)
    raise KT::CASFailed.new("Failed compare and swap for #{key}")
  end
end

#clearObject

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

#countObject

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

#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



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/kt.rb', line 235

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



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/kt.rb', line 136

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.



193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/kt.rb', line 193

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



209
210
211
212
213
# File 'lib/kt.rb', line 209

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



217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/kt.rb', line 217

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



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/kt.rb', line 160

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



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/kt.rb', line 178

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/kt.rb', line 109

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

#vacuumObject

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