Class: Findable::Query
- Inherits:
-
Object
show all
- Includes:
- Connection, Namespace
- Defined in:
- lib/findable/query.rb,
lib/findable/query/namespace.rb,
lib/findable/query/connection.rb
Defined Under Namespace
Modules: Connection, Namespace
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
#count ⇒ Object
17
18
19
|
# File 'lib/findable/query.rb', line 17
def count
redis.hlen(data_key)
end
|
#data ⇒ Object
9
10
11
|
# File 'lib/findable/query.rb', line 9
def data
redis.hvals(data_key)
end
|
#delete(id) ⇒ Object
48
49
50
|
# File 'lib/findable/query.rb', line 48
def delete(id)
redis.hdel data_key, id
end
|
#delete_all ⇒ Object
52
53
54
55
56
|
# File 'lib/findable/query.rb', line 52
def delete_all
redis.multi do
[data_key, info_key].each {|key| redis.del(key) }
end
end
|
#exists?(id) ⇒ Boolean
25
26
27
|
# File 'lib/findable/query.rb', line 25
def exists?(id)
redis.hexists(data_key, id)
end
|
#find_by_ids(ids) ⇒ Object
21
22
23
|
# File 'lib/findable/query.rb', line 21
def find_by_ids(ids)
redis.hmget(data_key, *Array(ids))
end
|
#ids ⇒ Object
13
14
15
|
# File 'lib/findable/query.rb', line 13
def ids
redis.hkeys(data_key).map(&:to_i)
end
|
#import(hashes) ⇒ Object
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/findable/query.rb', line 37
def import(hashes)
transaction 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
29
30
31
32
33
34
35
|
# File 'lib/findable/query.rb', line 29
def insert(hash)
transaction do
hash[:id] = auto_incremented_id(hash[:id])
redis.hset(data_key, hash[:id], Oj.dump(hash))
end
hash
end
|
#transaction ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/findable/query.rb', line 58
def transaction
raise ArgumentError, "Require block" unless block_given?
if Thread.current[thread_key]
yield
else
begin
Thread.current[thread_key] = true
Redis::Lock.new(lock_key).lock do
yield
end
rescue Redis::Lock::LockTimeout
raise
ensure
Thread.current[thread_key] = nil
end
end
end
|