Class: Ohm::ZSet

Inherits:
Struct show all
Includes:
Collection
Defined in:
lib/ohm-zset.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#keyObject

Returns the value of attribute key

Returns:

  • (Object)

    the current value of key



145
146
147
# File 'lib/ohm-zset.rb', line 145

def key
  @key
end

#modelObject

Returns the value of attribute model

Returns:

  • (Object)

    the current value of model



145
146
147
# File 'lib/ohm-zset.rb', line 145

def model
  @model
end

#namespaceObject

Returns the value of attribute namespace

Returns:

  • (Object)

    the current value of namespace



145
146
147
# File 'lib/ohm-zset.rb', line 145

def namespace
  @namespace
end

#score_fieldObject

Returns the value of attribute score_field

Returns:

  • (Object)

    the current value of score_field



145
146
147
# File 'lib/ohm-zset.rb', line 145

def score_field
  @score_field
end

Class Method Details

.generate_uuidObject



181
182
183
# File 'lib/ohm-zset.rb', line 181

def generate_uuid
  "ZSet:" + UUIDTools::UUID.random_create.to_s
end

.intersect_multiple(new_key, sets, weights = []) ⇒ Object



149
150
151
152
153
154
155
156
157
158
# File 'lib/ohm-zset.rb', line 149

def intersect_multiple(new_key, sets, weights = [])
  base_set = sets[0]
  weights = [1.0] * sets.length if weights = []

  new_set = Ohm::ZSet.new(new_key, base_set.model.key, base_set.model, base_set.score_field)
  sets = sets.map(&:key)

  Ohm.redis.zinterstore(new_key, sets, :weights => weights)
  new_set
end

.load_set(name) ⇒ Object



160
161
162
163
164
165
166
167
# File 'lib/ohm-zset.rb', line 160

def load_set(name)
  new_model, new_score_field = Ohm.redis.hmget("ZSet:" + name, "model", "score_field")
  return nil if new_model == nil && new_score_field == nil

  new_model = Ohm::Utils.const(self.class, new_model.to_sym)
  new_score_field = Ohm::Utils.string_to_score_field new_score_field
  return_instance = Ohm::ZSet.new(name, new_model.key, new_model, new_score_field)
end

.new_instance(name, model, score_field) ⇒ Object



189
190
191
# File 'lib/ohm-zset.rb', line 189

def new_instance(name, model, score_field)
  self.new(name, model.key, model, score_field)
end

.random_instance(model, score_field) ⇒ Object



185
186
187
# File 'lib/ohm-zset.rb', line 185

def random_instance(model, score_field)
  self.new_instance(Ohm::ZSet.generate_uuid, model, score_field)
end

.union_multiple(new_key, sets, weights = []) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/ohm-zset.rb', line 169

def union_multiple(new_key, sets, weights = [])
  base_set = sets[0]
  weights = [1.0] * sets.length if weights = []

  new_set = Ohm::ZSet.new(new_key, base_set.model.key, base_set.model, base_set.score_field)
  sets = sets.map(&:key)

  Ohm.redis.zunionstore(new_key, sets, :weights => weights)

  new_set
end

Instance Method Details

#add(model, custom_score = nil) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/ohm-zset.rb', line 245

def add(model, custom_score = nil)
  score_value = model

  lambda_function = score_field.each do |field|
    break field if field.is_a? Proc

    score_value = model.send(field) if custom_score.nil?

    break lambda{ |x| x.to_i } if field == score_field.last
  end

  if custom_score.nil?
    db.zadd(key, lambda_function.call(score_value), model.id)
  else
    db.zadd(key, lambda_function.call(custom_score), model.id)
  end
end

#add_list(*models) ⇒ Object



263
264
265
266
267
# File 'lib/ohm-zset.rb', line 263

def add_list(*models)
  models.each do |model|
    add(model)
  end
end

#clearObject



395
396
397
# File 'lib/ohm-zset.rb', line 395

def clear
  remrangebyrank(0, -1)
end

#count(a = "-inf", b = "+inf") ⇒ Object



198
199
200
201
202
# File 'lib/ohm-zset.rb', line 198

def count(a = "-inf", b = "+inf")
  return size if a == "-inf" && b == "+inf"

  db.zcount(key, a, b)
end

#delete(model) ⇒ Object



273
274
275
276
277
# File 'lib/ohm-zset.rb', line 273

def delete(model)
  db.zrem(key, model.id)

  model
end

#destroy!Object



391
392
393
# File 'lib/ohm-zset.rb', line 391

def destroy!
  db.del(key)
end

#duplicateObject



407
408
409
# File 'lib/ohm-zset.rb', line 407

def duplicate
  intersect(self, 1.0, 0.0)
end

#eachObject



237
238
239
# File 'lib/ohm-zset.rb', line 237

def each
  to_a.each { |element| yield element }
end

#empty?Boolean

Returns:

  • (Boolean)


241
242
243
# File 'lib/ohm-zset.rb', line 241

def empty?
  size == 0
end

#expire(seconds) ⇒ Object



418
419
420
# File 'lib/ohm-zset.rb', line 418

def expire(seconds)
  db.expire(self.key, seconds)
end

#firstObject



383
384
385
# File 'lib/ohm-zset.rb', line 383

def first
  range(0, 1)[0] rescue nil
end

#generate_uuidObject



399
400
401
# File 'lib/ohm-zset.rb', line 399

def generate_uuid
  ZSet.generate_uuid
end

#get(i) ⇒ Object



233
234
235
# File 'lib/ohm-zset.rb', line 233

def get(i)
  range(i, i)[0] rescue nil
end

#get_hmset_attrsObject



411
412
413
414
415
416
# File 'lib/ohm-zset.rb', line 411

def get_hmset_attrs
  return_list = []
  return_list << "model" << model.to_s
  return_list << "score_field" << Utils.score_field_to_string(score_field)
  return_list
end

#ids(a = 0, b = -1)) ⇒ Object



204
205
206
# File 'lib/ohm-zset.rb', line 204

def ids(a = 0, b = -1)
  execute { |key| db.zrange(key, a, b) }
end

#include?(model) ⇒ Boolean

Returns:

  • (Boolean)


229
230
231
# File 'lib/ohm-zset.rb', line 229

def include?(model)
  !rank(model).nil?
end

#intersect(set, w1 = 1.0, w2 = 1.0) ⇒ Object



287
288
289
290
291
292
293
# File 'lib/ohm-zset.rb', line 287

def intersect(set, w1=1.0, w2=1.0)
  new_key = generate_uuid
  new_set = Ohm::ZSet.new(new_key, model.key, model, score_field)

  db.zinterstore(new_set.key, [key, set.key], :weights => [w1, w2])      
  new_set
end

#intersect_multiple(sets, weights = []) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/ohm-zset.rb', line 295

def intersect_multiple(sets, weights = [])
  sets = sets.map(&:key)
  sets.push(key)
  weights = [1.0] * sets.length if weights = []

  new_key = generate_uuid
  new_set = Ohm::ZSet.new(new_key, model.key, model, score_field)

  db.zinterstore(new_set.key, sets, :weights => weights)

  new_set
end

#intersect_multiple!(sets, weights = []) ⇒ Object



308
309
310
311
312
# File 'lib/ohm-zset.rb', line 308

def intersect_multiple!(sets, weights = [])
  weights = [1.0] * sets.length if weights = []
  db.zinterstore(key, sets.map(&:key), :weights => weights)
  self
end

#lastObject



387
388
389
# File 'lib/ohm-zset.rb', line 387

def last
  revrange(0, 1)[0] rescue nil
end

#range(a = 0, b = -1)) ⇒ Object



208
209
210
# File 'lib/ohm-zset.rb', line 208

def range(a = 0, b = -1)
  fetch(ids(a, b))
end

#range_with_score(a = 0, b = -1)) ⇒ Object



212
213
214
# File 'lib/ohm-zset.rb', line 212

def range_with_score(a = 0, b = -1)
  _return = process_range_with_score { db.zrange(key, a, b, with_scores: true) }
end

#rangebyscore(a = "-inf", b = "+inf", limit = {}) ⇒ Object



348
349
350
351
352
353
# File 'lib/ohm-zset.rb', line 348

def rangebyscore(a = "-inf", b = "+inf", limit = {})
  limit[:offset] ||= 0
  limit[:count] ||= -1

  fetch(execute { |key| db.zrangebyscore(key, a, b, :limit => [limit[:offset], limit[:count]]) })
end

#rangebyscore_with_score(a = "-inf", b = "+inf", limit = {}) ⇒ Object



355
356
357
358
359
360
# File 'lib/ohm-zset.rb', line 355

def rangebyscore_with_score(a = "-inf", b = "+inf", limit = {})
  limit[:offset] ||= 0
  limit[:count] ||= -1

  _return = process_range_with_score { db.zrangebyscore(key, a, b, with_scores: true, :limit => [limit[:offset], limit[:count]]) }
end

#rank(model) ⇒ Object



336
337
338
# File 'lib/ohm-zset.rb', line 336

def rank(model)
  db.zrank(key, model.id)
end

#remrangebyrank(a, b) ⇒ Object



279
280
281
# File 'lib/ohm-zset.rb', line 279

def remrangebyrank(a, b)
  db.zremrangebyrank(key, a, b)
end

#remrangebyscore(a, b) ⇒ Object



283
284
285
# File 'lib/ohm-zset.rb', line 283

def remrangebyscore(a, b)
  db.zremrangebyscore(key, a, b)
end

#revrange(a = 0, b = -1)) ⇒ Object



216
217
218
# File 'lib/ohm-zset.rb', line 216

def revrange(a = 0, b = -1)
  fetch(execute { |key| db.zrevrange(key, a, b) })
end

#revrange_with_score(a = 0, b = -1)) ⇒ Object



220
221
222
# File 'lib/ohm-zset.rb', line 220

def revrange_with_score(a = 0, b = -1)
  _return = process_range_with_score { db.zrevrange(key, a, b, with_scores: true) }
end

#revrangebyscore(a = "+inf", b = "-inf", limit = {}) ⇒ Object



362
363
364
365
366
367
# File 'lib/ohm-zset.rb', line 362

def revrangebyscore(a = "+inf", b = "-inf", limit = {})
  limit[:offset] ||= 0
  limit[:count] ||= -1

  fetch(execute { |key| db.zrevrangebyscore(key, a, b, :limit => [limit[:offset], limit[:count]]) })
end

#revrangebyscore_with_score(a = "+inf", b = "-inf", limit = {}) ⇒ Object



369
370
371
372
373
374
# File 'lib/ohm-zset.rb', line 369

def revrangebyscore_with_score(a = "+inf", b = "-inf", limit = {})
  limit[:offset] ||= 0
  limit[:count] ||= -1

  _return = process_range_with_score { db.zrevrangebyscore(key, a, b, with_scores: true, :limit => [limit[:offset], limit[:count]]) }
end

#revrank(model) ⇒ Object



340
341
342
# File 'lib/ohm-zset.rb', line 340

def revrank(model)
  db.zrevrank(key, model.id)
end

#save_setObject



403
404
405
# File 'lib/ohm-zset.rb', line 403

def save_set
  db.hmset("ZSet:" + key, *get_hmset_attrs)
end

#score(model) ⇒ Object



344
345
346
# File 'lib/ohm-zset.rb', line 344

def score(model)
  db.zscore(key, model.id).to_i
end

#sizeObject



194
195
196
# File 'lib/ohm-zset.rb', line 194

def size
  db.zcard(key)
end

#starts_with(query, limit = {}) ⇒ Object



376
377
378
379
380
381
# File 'lib/ohm-zset.rb', line 376

def starts_with(query, limit = {})
  start_query = ZScores.string(query)
  end_query = "(" + ZScores.string(query.succ).to_s

  rangebyscore(start_query, end_query, limit)
end

#to_aObject

Fetch data from Redis



225
226
227
# File 'lib/ohm-zset.rb', line 225

def to_a
  fetch(ids)
end

#union(set, w1 = 1.0, w2 = 1.0) ⇒ Object



314
315
316
317
318
319
320
321
# File 'lib/ohm-zset.rb', line 314

def union(set, w1=1.0, w2=1.0)
  new_key = generate_uuid
  new_set = Ohm::ZSet.new(new_key, model.key, model, score_field)

  db.zunionstore(new_set.key, [key, set.key], :weights => [w1, w2])

  new_set
end

#union_multiple(sets, weights = []) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/ohm-zset.rb', line 323

def union_multiple(sets, weights = [])
  sets = sets.map(&:key)
  sets.push(key)

  weights = [1.0] * sets.length if weights = []
  new_key = generate_uuid
  new_set = Ohm::ZSet.new(new_key, model.key, model, score_field)

  db.zunionstore(new_set.key, sets, :weights => weights)

  new_set
end

#update(model) ⇒ Object



269
270
271
# File 'lib/ohm-zset.rb', line 269

def update(model)
  add (model)
end