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) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/ohm-zset.rb', line 237

def add(model)
  score_value = model

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

    score_value = model.send(field)

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

  db.zadd(key, lambda_function.call(score_value), model.id)
end

#add_list(*models) ⇒ Object



251
252
253
254
255
# File 'lib/ohm-zset.rb', line 251

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

#clearObject



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

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



261
262
263
264
265
# File 'lib/ohm-zset.rb', line 261

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

  model
end

#destroy!Object



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

def destroy!
  db.del(key)
end

#duplicateObject



381
382
383
# File 'lib/ohm-zset.rb', line 381

def duplicate
  intersect(self, 1.0, 0.0)
end

#eachObject



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  size == 0
end

#expire(seconds) ⇒ Object



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

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

#firstObject



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

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

#generate_uuidObject



373
374
375
# File 'lib/ohm-zset.rb', line 373

def generate_uuid
  ZSet.generate_uuid
end

#get(i) ⇒ Object



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

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

#get_hmset_attrsObject



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

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)


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

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

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



275
276
277
278
279
280
281
# File 'lib/ohm-zset.rb', line 275

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



283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/ohm-zset.rb', line 283

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



296
297
298
299
300
# File 'lib/ohm-zset.rb', line 296

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

#lastObject



361
362
363
# File 'lib/ohm-zset.rb', line 361

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

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



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

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

#rank(model) ⇒ Object



324
325
326
# File 'lib/ohm-zset.rb', line 324

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

#remrangebyrank(a, b) ⇒ Object



267
268
269
# File 'lib/ohm-zset.rb', line 267

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

#remrangebyscore(a, b) ⇒ Object



271
272
273
# File 'lib/ohm-zset.rb', line 271

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

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



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

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

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



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

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

#revrank(model) ⇒ Object



328
329
330
# File 'lib/ohm-zset.rb', line 328

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

#save_setObject



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

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

#score(model) ⇒ Object



332
333
334
# File 'lib/ohm-zset.rb', line 332

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



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

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



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

def to_a
  fetch(ids)
end

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



302
303
304
305
306
307
308
309
# File 'lib/ohm-zset.rb', line 302

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



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

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



257
258
259
# File 'lib/ohm-zset.rb', line 257

def update(model)
  add (model)
end