Class: SSDB

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/ssdb.rb,
lib/ssdb/client.rb,
lib/ssdb/version.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)
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] } }
BLANK =
"".freeze
VERSION =
"0.1.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*a) ⇒ SSDB

Returns a new instance of SSDB.



37
38
39
40
# File 'lib/ssdb.rb', line 37

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

Instance Attribute Details

#clientObject (readonly)



24
25
26
# File 'lib/ssdb.rb', line 24

def client
  @client
end

Class Method Details

.currentSSDB

Returns the current/global SSDB connection.

Returns:

  • (SSDB)

    the current/global SSDB connection



27
28
29
# File 'lib/ssdb.rb', line 27

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

.current=(ssdb) ⇒ Object

Parameters:

  • ssdb (SSDB)

    the current/global SSDB connection



32
33
34
# File 'lib/ssdb.rb', line 32

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]

batch with futures


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

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


65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ssdb.rb', line 65

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

Parameters:

  • key (String)

    the key

  • value (Integer) (defaults to: 1)

    the decrement



111
112
113
114
115
# File 'lib/ssdb.rb', line 111

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

#del(key) ⇒ Object

Delete ‘key`.

Parameters:

  • key (String)

    the key



131
132
133
134
135
# File 'lib/ssdb.rb', line 131

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

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

Checks existence of ‘key`.

Parameters:

  • key (String)

    the key

Returns:

  • (Boolean)

    true if exists



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

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

#get(key) ⇒ String

Returns value at ‘key`.

Parameters:

  • key (String)

    the key

Returns:

  • (String)

    the value



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

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

#incr(key, value = 1) ⇒ Object

Increments a ‘key` by value

Parameters:

  • key (String)

    the key

  • value (Integer) (defaults to: 1)

    the increment



101
102
103
104
105
# File 'lib/ssdb.rb', line 101

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

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

Scans keys between ‘start` and `stop`.

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



144
145
146
147
148
149
# File 'lib/ssdb.rb', line 144

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

#multi_del(keys) ⇒ Object

Deletes multiple keys

Parameters:

  • keys (Array<String>)


202
203
204
205
206
207
# File 'lib/ssdb.rb', line 202

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>

Checks existence of multiple keys

Parameters:

  • keys (Array<String>)

Returns:

  • (Array<Boolean>)

    results



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

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

Parameters:

  • keys (Array<String>)

Returns:

  • (Array<String>)

    values



192
193
194
195
196
197
# File 'lib/ssdb.rb', line 192

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

Parameters:

  • pairs (Hash)

    key/value pairs



182
183
184
185
186
# File 'lib/ssdb.rb', line 182

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`

Parameters:

  • key (String)

    the zset

  • members (Array<String>)


413
414
415
416
417
418
# File 'lib/ssdb.rb', line 413

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>

Checks existence of multiple sets

Parameters:

  • keys (Array<String>)

Returns:

  • (Array<Boolean>)

    results



369
370
371
372
373
374
# File 'lib/ssdb.rb', line 369

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<Float>

Retrieves multiple scores from ‘key`

Parameters:

  • key (String)

    the zset

  • members (Array<String>)

Returns:

  • (Array<Float>)

    scores



402
403
404
405
406
407
# File 'lib/ssdb.rb', line 402

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`

Parameters:

  • key (String)

    the zset

  • pairs (Hash)

    key/value pairs



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

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

Parameters:

  • keys (Array<String>)

Returns:

  • (Array<Boolean>)

    results



380
381
382
383
384
385
# File 'lib/ssdb.rb', line 380

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`.

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



172
173
174
175
176
177
# File 'lib/ssdb.rb', line 172

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`.

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



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

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`.

Parameters:

  • key (String)

    the key

  • value (String)

    the value



91
92
93
94
95
# File 'lib/ssdb.rb', line 91

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

#zadd(key, score, member) ⇒ Object

Redis ‘compatibility’.

Parameters:

  • key (String)

    the key

  • score (Numeric)

    the score

  • member (String)

    the member



247
248
249
# File 'lib/ssdb.rb', line 247

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

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

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

Parameters:

  • key (String)

    the key

  • member (String)

    the member

  • score (Integer) (defaults to: 1)

    the decrement



267
268
269
270
271
# File 'lib/ssdb.rb', line 267

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`.

Parameters:

  • key (String)

    the key

  • member (String)

    the member



297
298
299
300
301
# File 'lib/ssdb.rb', line 297

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`.

Parameters:

  • key (String)

    the key

Returns:

  • (Boolean)

    true if exists



277
278
279
280
281
# File 'lib/ssdb.rb', line 277

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

#zget(key, member) ⇒ Float

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

Parameters:

  • key (String)

    the key

  • member (String)

    the member

Returns:

  • (Float)

    the score



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

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`

Parameters:

  • key (String)

    the key

  • member (String)

    the member

  • score (Integer) (defaults to: 1)

    the increment



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

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.

Parameters:

  • key (String)

    the zset

  • start (Float)

    start at this score

  • stop (Float)

    stop at this score

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

    options

Options Hash (opts):

  • :limit (Integer)

    limit results

Returns:

  • (Array<String>)

    matching members



326
327
328
329
330
331
# File 'lib/ssdb.rb', line 326

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`.

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



310
311
312
313
314
315
# File 'lib/ssdb.rb', line 310

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,Float>>

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

Parameters:

  • key (String)

    the zset

  • start (Float)

    start at this score

  • stop (Float)

    stop at this score

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

    options

Options Hash (opts):

  • :limit (Integer)

    limit results

Returns:

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

    member/score pairs



358
359
360
361
362
363
# File 'lib/ssdb.rb', line 358

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,Float>>

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

Parameters:

  • key (String)

    the zset

  • start (Float)

    start at this score

  • stop (Float)

    stop at this score

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

    options

Options Hash (opts):

  • :limit (Integer)

    limit results

Returns:

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

    member/score pairs



342
343
344
345
346
347
# File 'lib/ssdb.rb', line 342

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`.

Parameters:

  • key (String)

    the key

  • member (String)

    the member

  • score (Numeric)

    the score



236
237
238
239
240
# File 'lib/ssdb.rb', line 236

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`.

Parameters:

  • key (String)

    the key



287
288
289
290
291
# File 'lib/ssdb.rb', line 287

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