Class: SSDB

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/ssdb.rb,
lib/ssdb/client.rb,
lib/ssdb/errors.rb,
lib/ssdb/version.rb,
lib/ssdb/constants.rb

Defined Under Namespace

Classes: Batch, Client, Future

Constant Summary collapse

Error =
Class.new(RuntimeError)
ConnectionError =
Class.new(Error)
TimeoutError =
Class.new(Error)
CommandError =
Class.new(Error)
FutureNotReady =
Class.new(Error)
VERSION =
"0.1.3"
T_BOOL =
->r { r == "1" }
T_INT =
->r { r.to_i }
T_CINT =
->r { r.to_i if r }
T_VBOOL =
->r { r.each_slice(2).map {|_, v| v == "1" }}
T_VINT =
->r { r.each_slice(2).map {|_, v| v.to_i }}
T_STRSTR =
->r { r.each_slice(2).to_a }
T_STRINT =
->r { r.each_slice(2).map {|v, s| [v, s.to_i] } }
T_MAPINT =
->r,n { h = {}; r.each_slice(2) {|k, v| h[k] = v }; n.map {|k| h[k].to_i } }
T_MAPSTR =
->r,n { h = {}; r.each_slice(2) {|k, v| h[k] = v }; n.map {|k| h[k] } }
T_HASHSTR =
->r { h = {}; r.each_slice(2) {|k, v| h[k] = v }; h }
T_HASHINT =
->r { h = {}; r.each_slice(2) {|k, v| h[k] = v.to_i }; h }
BLANK =
"".freeze
DB_STATS =
["compactions", "level", "size", "time", "read", "written"].freeze
T_INFO =
->rows {
  res = {}
  rows.shift # skip first
  rows.each_slice(2) do |key, val|
    res[key] = case key
    when "leveldb.stats"
      stats = {}
      val.lines.to_a.last.strip.split(/\s+/).each_with_index do |v, i|
        stats[DB_STATS[i]] = v.to_i
      end
      stats
    when /^cmd\./
      val.split("\t").inject({}) do |stats, i|
        k, v = i.split(": ", 2)
        stats.update k => v.to_i
      end
    else
      val.to_i
    end
  end
  res
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*a) ⇒ SSDB

Returns a new instance of SSDB.


20
21
22
23
# File 'lib/ssdb.rb', line 20

def initialize(*a)
  @client = Client.new(*a)
  super() # Monitor#initialize
end

Instance Attribute Details

#clientObject (readonly)


7
8
9
# File 'lib/ssdb.rb', line 7

def client
  @client
end

Class Method Details

.currentSSDB

Returns the current/global SSDB connection.

Returns:

  • (SSDB)

    the current/global SSDB connection


10
11
12
# File 'lib/ssdb.rb', line 10

def self.current
  @current ||= SSDB.new
end

.current=(ssdb) ⇒ Object

Parameters:

  • ssdb (SSDB)

    the current/global SSDB connection


15
16
17
# File 'lib/ssdb.rb', line 15

def self.current=(ssdb)
  @current = ssdb
end

Instance Method Details

#batchObject

Execute a batch operation

Examples:

Simple batch


ssdb.batch do
  ssdb.set "foo", "5"
  ssdb.get "foo"
  ssdb.incr "foo"
end
# => [true, "5", 6]

Using futures


ssdb.batch do
  v = ssdb.set "foo", "5"
  w = ssdb.incr "foo"
end

v.value
# => true
w.value
# => 6

48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ssdb.rb', line 48

def batch
  mon_synchronize do
    begin
      original, @client = @client, SSDB::Batch.new
      yield(self)
      @client.values = original.perform(@client)
    ensure
      @client = original
    end
  end
end

#decr(key, value = 1) ⇒ Object

Decrements a `key` by value

Examples:

ssdb.decr("foo") # => -1

Parameters:

  • key (String)

    the key

  • value (Integer) (defaults to: 1)

    the decrement


134
135
136
137
138
# File 'lib/ssdb.rb', line 134

def decr(key, value = 1)
  mon_synchronize do
    perform ["decr", key, value], proc: T_INT
  end
end

#del(key) ⇒ Object

Delete `key`.

Examples:

ssdb.del("foo") # => nil

Parameters:

  • key (String)

    the key


160
161
162
163
164
# File 'lib/ssdb.rb', line 160

def del(key)
  mon_synchronize do
    perform ["del", key]
  end
end

#eval(script, *args) ⇒ Array

Evaluates scripts

Examples:

script =<<-LUA
  local x = math.pi * 10
  return x
LUA
ssdb.eval(script)
# => ["31.425926"]

Parameters:

  • script (String)
  • args (multiple<String>)

Returns:

  • (Array)

    results


82
83
84
85
86
# File 'lib/ssdb.rb', line 82

def eval(script, *args)
  mon_synchronize do
    perform ["eval", script, *args]
  end
end

#exists(key) ⇒ Boolean Also known as: exists?

Checks existence of `key`.

Examples:

ssdb.exists("foo") # => true

Parameters:

  • key (String)

    the key

Returns:

  • (Boolean)

    true if exists


147
148
149
150
151
# File 'lib/ssdb.rb', line 147

def exists(key)
  mon_synchronize do
    perform ["exists", key], proc: T_BOOL
  end
end

#get(key) ⇒ String

Returns value at `key`.

Examples:

ssdb.get("foo") # => "val"

Parameters:

  • key (String)

    the key

Returns:

  • (String)

    the value


95
96
97
98
99
# File 'lib/ssdb.rb', line 95

def get(key)
  mon_synchronize do
    perform ["get", key]
  end
end

#incr(key, value = 1) ⇒ Object

Increments a `key` by value

Examples:

ssdb.incr("foo") # => 1

Parameters:

  • key (String)

    the key

  • value (Integer) (defaults to: 1)

    the increment


121
122
123
124
125
# File 'lib/ssdb.rb', line 121

def incr(key, value = 1)
  mon_synchronize do
    perform ["incr", key, value], proc: T_INT
  end
end

#infoHash

Returns info

Returns:

  • (Hash)

    info attributes


62
63
64
65
66
# File 'lib/ssdb.rb', line 62

def info
  mon_synchronize do
    perform ["info"], proc: T_INFO
  end
end

#keys(start, stop, opts = {}) ⇒ Array<String>

Scans keys between `start` and `stop`.

Examples:

ssdb.keys("a", "z", limit: 2) # => ["bar", "foo"]

Parameters:

  • start (String)

    start at this key

  • stop (String)

    stop at this key

  • opts (Hash) (defaults to: {})

    options

Options Hash (opts):

  • :limit (Integer)

    limit results

Returns:

  • (Array<String>)

    matching keys


176
177
178
179
180
181
# File 'lib/ssdb.rb', line 176

def keys(start, stop, opts = {})
  limit = opts[:limit] || -1
  mon_synchronize do
    perform ["keys", start, stop, limit], multi: true
  end
end

#mapped_multi_get(keys) ⇒ Array<String>

Retrieves multiple keys

Examples:

ssdb.mapped_multi_get(["bar", "foo"])
# => {"bar" => "val1", "foo" => val2"}

Parameters:

  • keys (Array<String>)

Returns:

  • (Array<String>)

    values


255
256
257
258
259
260
# File 'lib/ssdb.rb', line 255

def mapped_multi_get(keys)
  keys = Array(keys) unless keys.is_a?(Array)
  mon_synchronize do
    perform ["multi_get", *keys], multi: true, proc: T_HASHSTR
  end
end

#mapped_multi_zget(key, members) ⇒ Hash<String,Integer>

Retrieves multiple scores from `key`

Examples:

ssdb.mapped_multi_zget("visits", ["u1", "u2"])
# => {"u1" => 101, "u2" => 202}

Parameters:

  • key (String)

    the zset

  • members (Array<String>)

Returns:

  • (Hash<String,Integer>)

    members with scores


559
560
561
562
563
564
# File 'lib/ssdb.rb', line 559

def mapped_multi_zget(key, members)
  members = Array(members) unless members.is_a?(Array)
  mon_synchronize do
    perform ["multi_zget", key, *members], multi: true, proc: T_HASHINT
  end
end

#multi_del(keys) ⇒ Object

Deletes multiple keys

Examples:

ssdb.multi_del(["bar", "foo"])
# => 2

Parameters:

  • keys (Array<String>)

269
270
271
272
273
274
# File 'lib/ssdb.rb', line 269

def multi_del(keys)
  keys = Array(keys) unless keys.is_a?(Array)
  mon_synchronize do
    perform ["multi_del", *keys], proc: T_INT
  end
end

#multi_exists(keys) ⇒ Array<Boolean> Also known as: multi_exists?

Checks existence of multiple keys

Examples:

ssdb.multi_exists(["bar", "foo", "baz"])
# => [true, true, false]

Parameters:

  • keys (Array<String>)

Returns:

  • (Array<Boolean>)

    results


284
285
286
287
288
289
# File 'lib/ssdb.rb', line 284

def multi_exists(keys)
  keys = Array(keys) unless keys.is_a?(Array)
  mon_synchronize do
    perform ["multi_exists", *keys], multi: true, proc: T_VBOOL
  end
end

#multi_get(keys) ⇒ Array<String>

Retrieves multiple keys

Examples:

ssdb.multi_get(["bar", "foo"])
# => ["val1", "val2"]

Parameters:

  • keys (Array<String>)

Returns:

  • (Array<String>)

    values


240
241
242
243
244
245
# File 'lib/ssdb.rb', line 240

def multi_get(keys)
  keys = Array(keys) unless keys.is_a?(Array)
  mon_synchronize do
    perform ["multi_get", *keys], multi: true, proc: T_MAPSTR, args: [keys]
  end
end

#multi_set(pairs) ⇒ Object

Sets multiple keys

Examples:

ssdb.multi_set("bar" => "val1", "foo" => "val2")
# => 4

Parameters:

  • pairs (Hash)

    key/value pairs


226
227
228
229
230
# File 'lib/ssdb.rb', line 226

def multi_set(pairs)
  mon_synchronize do
    perform ["multi_set", *pairs.to_a].flatten, proc: T_INT
  end
end

#multi_zdel(key, members) ⇒ Object

Deletes multiple members from `key`

Examples:

ssdb.multi_zdel("visits", ["u1", "u2"])
# => 2

Parameters:

  • key (String)

    the zset

  • members (Array<String>)

574
575
576
577
578
579
# File 'lib/ssdb.rb', line 574

def multi_zdel(key, members)
  members = Array(members) unless members.is_a?(Array)
  mon_synchronize do
    perform ["multi_zdel", key, *members], proc: T_INT
  end
end

#multi_zexists(keys) ⇒ Array<Boolean> Also known as: multi_zexists?

Checks existence of multiple sets

Examples:

ssdb.multi_zexists("visits", "page_views", "baz")
# => [true, true, false]

Parameters:

  • keys (Array<String>)

Returns:

  • (Array<Boolean>)

    results


497
498
499
500
501
502
# File 'lib/ssdb.rb', line 497

def multi_zexists(keys)
  keys = Array(keys) unless keys.is_a?(Array)
  mon_synchronize do
    perform ["multi_zexists", *keys], multi: true, proc: T_VBOOL
  end
end

#multi_zget(key, members) ⇒ Array<Integer>

Retrieves multiple scores from `key`

Examples:

ssdb.multi_zget("visits", ["u1", "u2"])
# => [101, 202]

Parameters:

  • key (String)

    the zset

  • members (Array<String>)

Returns:

  • (Array<Integer>)

    scores


543
544
545
546
547
548
# File 'lib/ssdb.rb', line 543

def multi_zget(key, members)
  members = Array(members) unless members.is_a?(Array)
  mon_synchronize do
    perform ["multi_zget", key, *members], multi: true, proc: T_MAPINT, args: [members]
  end
end

#multi_zset(key, pairs) ⇒ Object

Sets multiple members of `key`

Examples:

ssdb.multi_zset("visits", "u1" => 102, "u3" => 303)
# => 2

Parameters:

  • key (String)

    the zset

  • pairs (Hash<String,Integer>)

    key/value pairs


528
529
530
531
532
# File 'lib/ssdb.rb', line 528

def multi_zset(key, pairs)
  mon_synchronize do
    perform ["multi_zset", key, *pairs.to_a].flatten, proc: T_INT
  end
end

#multi_zsize(keys) ⇒ Array<Boolean>

Returns cardinalities of multiple sets

Examples:

ssdb.multi_zsize("visits", "page_views", "baz")
# => [2, 1, 0]

Parameters:

  • keys (Array<String>)

Returns:

  • (Array<Boolean>)

    results


513
514
515
516
517
518
# File 'lib/ssdb.rb', line 513

def multi_zsize(keys)
  keys = Array(keys) unless keys.is_a?(Array)
  mon_synchronize do
    perform ["multi_zsize", *keys], multi: true, proc: T_VINT
  end
end

#rscan(start, stop, opts = {}) ⇒ Array<Array<String,String>>

Reverse-scans keys between `start` and `stop`.

Examples:

ssdb.rscan("z", "a", limit: 2)
# => [["foo", "val2"], ["bar", "val1"]]

Parameters:

  • start (String)

    start at this key

  • stop (String)

    stop at this key

  • opts (Hash) (defaults to: {})

    options

Options Hash (opts):

  • :limit (Integer)

    limit results

Returns:

  • (Array<Array<String,String>>)

    key/value pairs in reverse order


212
213
214
215
216
217
# File 'lib/ssdb.rb', line 212

def rscan(start, stop, opts = {})
  limit = opts[:limit] || -1
  mon_synchronize do
    perform ["rscan", start, stop, limit], multi: true, proc: T_STRSTR
  end
end

#scan(start, stop, opts = {}) ⇒ Array<Array<String,String>>

Scans keys between `start` and `stop`.

Examples:

ssdb.scan("a", "z", limit: 2)
# => [["bar", "val1"], ["foo", "val2"]]

Parameters:

  • start (String)

    start at this key

  • stop (String)

    stop at this key

  • opts (Hash) (defaults to: {})

    options

Options Hash (opts):

  • :limit (Integer)

    limit results

Returns:

  • (Array<Array<String,String>>)

    key/value pairs


194
195
196
197
198
199
# File 'lib/ssdb.rb', line 194

def scan(start, stop, opts = {})
  limit = opts[:limit] || -1
  mon_synchronize do
    perform ["scan", start, stop, limit], multi: true, proc: T_STRSTR
  end
end

#set(key, value) ⇒ Object

Sets `value` at `key`.

Examples:

ssdb.set("foo", "val") # => true

Parameters:

  • key (String)

    the key

  • value (String)

    the value


108
109
110
111
112
# File 'lib/ssdb.rb', line 108

def set(key, value)
  mon_synchronize do
    perform ["set", key, value], proc: T_BOOL
  end
end

#zadd(key, score, member) ⇒ Object

Redis 'compatibility'.

Examples:

ssdb.zadd("visits", 202, "u1")
# => true

Parameters:

  • key (String)

    the key

  • score (Integer)

    the score

  • member (String)

    the member


331
332
333
# File 'lib/ssdb.rb', line 331

def zadd(key, score, member)
  zset(key, member, score)
end

#zdecr(key, member, score = 1) ⇒ Object

Decrements the `member` in `key` by `score`

Examples:

ssdb.zdecr("visits", "u1")
# => 100
ssdb.zdecr("visits", "u1", 5)
# => 95

Parameters:

  • key (String)

    the key

  • member (String)

    the member

  • score (Integer) (defaults to: 1)

    the decrement


363
364
365
366
367
# File 'lib/ssdb.rb', line 363

def zdecr(key, member, score = 1)
  mon_synchronize do
    perform ["zdecr", key, member, score], proc: T_INT
  end
end

#zdel(key, member) ⇒ Object

Delete an `member` from a zset `key`.

Examples:

ssdb.zdel("visits", "u1")
# => true

Parameters:

  • key (String)

    the key

  • member (String)

    the member


405
406
407
408
409
# File 'lib/ssdb.rb', line 405

def zdel(key, member)
  mon_synchronize do
    perform ["zdel", key, member], proc: T_BOOL
  end
end

#zexists(key) ⇒ Boolean Also known as: zexists?

Checks existence of a zset at `key`.

Examples:

ssdb.zexists("visits")
# => true

Parameters:

  • key (String)

    the key

Returns:

  • (Boolean)

    true if exists


377
378
379
380
381
# File 'lib/ssdb.rb', line 377

def zexists(key)
  mon_synchronize do
    perform ["zexists", key], proc: T_BOOL
  end
end

#zget(key, member) ⇒ Integer

Returns the score of `member` at `key`.

Examples:

ssdb.zget("visits", "u1")
# => 101

Parameters:

  • key (String)

    the key

  • member (String)

    the member

Returns:

  • (Integer)

    the score


301
302
303
304
305
# File 'lib/ssdb.rb', line 301

def zget(key, member)
  mon_synchronize do
    perform ["zget", key, member], proc: T_CINT
  end
end

#zincr(key, member, score = 1) ⇒ Object

Increments the `member` in `key` by `score`

Examples:

ssdb.zincr("visits", "u1")
# => 102
ssdb.zincr("visits", "u1", 100)
# => 202

Parameters:

  • key (String)

    the key

  • member (String)

    the member

  • score (Integer) (defaults to: 1)

    the increment


346
347
348
349
350
# File 'lib/ssdb.rb', line 346

def zincr(key, member, score = 1)
  mon_synchronize do
    perform ["zincr", key, member, score], proc: T_INT
  end
end

#zkeys(key, start, stop, opts = {}) ⇒ Array<String>

Lists members at `key` starting at `start_member` between `start` and `stop` scores.

Examples:

ssdb.zkeys("visits", 0, 300, limit: 2)
# => ["u1", "u2"]

Parameters:

  • key (String)

    the zset

  • start (Integer)

    start at this score

  • stop (Integer)

    stop at this score

  • opts (Hash) (defaults to: {})

    options

Options Hash (opts):

  • :limit (Integer)

    limit results

Returns:

  • (Array<String>)

    matching members


442
443
444
445
446
447
# File 'lib/ssdb.rb', line 442

def zkeys(key, start, stop, opts = {})
  limit = opts[:limit] || -1
  mon_synchronize do
    perform ["zkeys", key, BLANK, start, stop, limit], multi: true
  end
end

#zlist(start, stop, opts = {}) ⇒ Array<String>

List zset keys between `start` and `stop`.

Examples:

ssdb.zlist("a", "z", limit: 2)
# => ["visits", "page_views"]

Parameters:

  • start (String)

    start at this key

  • stop (String)

    stop at this key

  • opts (Hash) (defaults to: {})

    options

Options Hash (opts):

  • :limit (Integer)

    limit results

Returns:

  • (Array<String>)

    matching zset keys


422
423
424
425
426
427
# File 'lib/ssdb.rb', line 422

def zlist(start, stop, opts = {})
  limit = opts[:limit] || -1
  mon_synchronize do
    perform ["zlist", start, stop, limit], multi: true
  end
end

#zrscan(key, start, stop, opts = {}) ⇒ Array<Array<String,Integer>>

Reverse scans for members at `key` starting at `start_member` between `start` and `stop` scores.

Examples:

ssdb.zrscan("visits", 300, 0, limit: 2)
# => [["u2", 202], ["u1", 101]]

Parameters:

  • key (String)

    the zset

  • start (Integer)

    start at this score

  • stop (Integer)

    stop at this score

  • opts (Hash) (defaults to: {})

    options

Options Hash (opts):

  • :limit (Integer)

    limit results

Returns:

  • (Array<Array<String,Integer>>)

    member/score pairs


482
483
484
485
486
487
# File 'lib/ssdb.rb', line 482

def zrscan(key, start, stop, opts = {})
  limit = opts[:limit] || -1
  mon_synchronize do
    perform ["zrscan", key, BLANK, start, stop, limit], multi: true, proc: T_STRINT
  end
end

#zscan(key, start, stop, opts = {}) ⇒ Array<Array<String,Integer>>

Scans for members at `key` starting at `start_member` between `start` and `stop` scores.

Examples:

ssdb.zscan("visits", 0, 300, limit: 2)
# => [["u1", 101], ["u2", 202]]

Parameters:

  • key (String)

    the zset

  • start (Integer)

    start at this score

  • stop (Integer)

    stop at this score

  • opts (Hash) (defaults to: {})

    options

Options Hash (opts):

  • :limit (Integer)

    limit results

Returns:

  • (Array<Array<String,Integer>>)

    member/score pairs


462
463
464
465
466
467
# File 'lib/ssdb.rb', line 462

def zscan(key, start, stop, opts = {})
  limit = opts[:limit] || -1
  mon_synchronize do
    perform ["zscan", key, BLANK, start, stop, limit], multi: true, proc: T_STRINT
  end
end

#zset(key, member, score) ⇒ Object

Sets the `score` of `member` at `key`.

Examples:

ssdb.zset("visits", "u1", 202)
# => true

Parameters:

  • key (String)

    the key

  • member (String)

    the member

  • score (Integer)

    the score


316
317
318
319
320
# File 'lib/ssdb.rb', line 316

def zset(key, member, score)
  mon_synchronize do
    perform ["zset", key, member, score], proc: T_BOOL
  end
end

#zsize(key) ⇒ Object

Returns the cardinality of a set `key`.

Examples:

ssdb.zsize("visits")
# => 2

Parameters:

  • key (String)

    the key


391
392
393
394
395
# File 'lib/ssdb.rb', line 391

def zsize(key)
  mon_synchronize do
    perform ["zsize", key], proc: T_INT
  end
end