Class: Findable::Query

Inherits:
Object
  • Object
show all
Includes:
Connection, Namespace
Defined in:
lib/findable/query.rb,
lib/findable/query/lock.rb,
lib/findable/query/namespace.rb,
lib/findable/query/connection.rb

Defined Under Namespace

Modules: Connection, Namespace Classes: Lock

Constant Summary

Constants included from Namespace

Namespace::AUTO_INCREMENT_KEY, Namespace::KEY_NAMES, Namespace::PREFIX

Instance Method Summary collapse

Methods included from Namespace

#initialize, #thread_key

Methods included from Connection

#redis

Instance Method Details

#countObject



18
19
20
# File 'lib/findable/query.rb', line 18

def count
  redis.hlen(data_key)
end

#dataObject



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

def data
  redis.hvals(data_key)
end

#delete(id) ⇒ Object



49
50
51
# File 'lib/findable/query.rb', line 49

def delete(id)
  redis.hdel data_key, id
end

#delete_allObject



53
54
55
56
57
# File 'lib/findable/query.rb', line 53

def delete_all
  redis.multi do
    [data_key, info_key].each {|key| redis.del(key) }
  end
end

#exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/findable/query.rb', line 26

def exists?(id)
  redis.hexists(data_key, id)
end

#find_by_ids(ids) ⇒ Object



22
23
24
# File 'lib/findable/query.rb', line 22

def find_by_ids(ids)
  redis.hmget(data_key, *Array(ids))
end

#idsObject



14
15
16
# File 'lib/findable/query.rb', line 14

def ids
  redis.hkeys(data_key).map(&:to_i)
end

#import(hashes) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/findable/query.rb', line 38

def import(hashes)
  lock do
    auto_incremented = hashes.each_with_object([]) do |hash, obj|
      hash["id"] = auto_incremented_id(hash["id"])
      obj << hash["id"]
      obj << Oj.dump(hash)
    end
    redis.hmset(data_key, *auto_incremented)
  end
end

#insert(hash) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/findable/query.rb', line 30

def insert(hash)
  lock do
    hash[:id] = auto_incremented_id(hash[:id])
    redis.hset(data_key, hash[:id], Oj.dump(hash))
  end
  hash
end

#lockObject

Raises:

  • (ArgumentError)


59
60
61
62
# File 'lib/findable/query.rb', line 59

def lock
  raise ArgumentError, "Require block" unless block_given?
  Lock.new(lock_key, thread_key).lock { yield }
end