Class: Findable::Query
- Inherits:
-
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,
lib/findable/query/serializer.rb
Defined Under Namespace
Modules: Connection, Namespace
Classes: Lock, Serializer
Constant Summary
Constants included
from Namespace
Namespace::AUTO_INCREMENT_KEY, Namespace::KEY_NAMES, Namespace::PREFIX
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Namespace
#thread_key
Methods included from Connection
#redis
Constructor Details
#initialize(model, serializer = nil) ⇒ Query
Returns a new instance of Query.
13
14
15
16
|
# File 'lib/findable/query.rb', line 13
def initialize(model, serializer = nil)
@model = model
@serializer = serializer || Serializer.new
end
|
Instance Attribute Details
#model ⇒ Object
Returns the value of attribute model.
11
12
13
|
# File 'lib/findable/query.rb', line 11
def model
@model
end
|
Instance Method Details
#all ⇒ Object
18
19
20
|
# File 'lib/findable/query.rb', line 18
def all
@serializer.deserialize(redis.hvals(data_key), model)
end
|
#count ⇒ Object
26
27
28
|
# File 'lib/findable/query.rb', line 26
def count
redis.hlen(data_key)
end
|
#delete(object) ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/findable/query.rb', line 81
def delete(object)
if model.index_defined?
model.indexes.each do |name|
next if name == :id
if value = object.public_send("#{name}_was") || object.public_send(name)
redis.hdel(index_key, value)
end
end
end
redis.hdel(data_key, object.id)
end
|
#delete_all ⇒ Object
94
95
96
97
98
|
# File 'lib/findable/query.rb', line 94
def delete_all
redis.multi do
[data_key, info_key, index_key].each {|key| redis.del(key) }
end
end
|
#exists?(id) ⇒ Boolean
40
41
42
|
# File 'lib/findable/query.rb', line 40
def exists?(id)
redis.hexists(data_key, id)
end
|
#find_by_ids(ids) ⇒ Object
30
31
32
|
# File 'lib/findable/query.rb', line 30
def find_by_ids(ids)
@serializer.deserialize(redis.hmget(data_key, *Array(ids)), model)
end
|
#find_by_index(index, value) ⇒ Object
34
35
36
37
38
|
# File 'lib/findable/query.rb', line 34
def find_by_index(index, value)
if ids = ids_from_index([index, value].join(":"))
find_by_ids(ids)
end
end
|
#ids ⇒ Object
22
23
24
|
# File 'lib/findable/query.rb', line 22
def ids
redis.hkeys(data_key).map(&:to_i)
end
|
#import(hashes) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/findable/query.rb', line 57
def import(hashes)
lock do
indexes = Hash.new {|h, k| h[k] = [] }
values = hashes.each_with_object([]) do |hash, obj|
hash = hash.with_indifferent_access
hash["id"] = auto_incremented_id(hash["id"])
obj << hash["id"]
obj << @serializer.serialize(hash)
if model.index_defined?
model.indexes.each_with_object([]) do |name, obj|
next if name == :id
indexes[[name, hash[name]].join(":")] << hash["id"]
end
end
end
redis.hmset(data_key, *values)
if indexes.present?
attrs = indexes.map {|k, v| [k, @serializer.serialize(v)] }.flatten
redis.hmset(index_key, *attrs)
end
end
end
|
#insert(object) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/findable/query.rb', line 44
def insert(object)
lock do
object.id = auto_incremented_id(object.id)
redis.hset(
data_key,
object.id,
@serializer.serialize(object.attributes)
)
update_index(object)
end
object
end
|
#lock ⇒ Object
100
101
102
103
|
# File 'lib/findable/query.rb', line 100
def lock
raise ArgumentError, "Require block" unless block_given?
Lock.new(lock_key, thread_key).lock { yield }
end
|
#update_index(object) ⇒ Object
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/findable/query.rb', line 105
def update_index(object)
if model.index_defined?
indexes = model.indexes.each_with_object([]) {|name, obj|
next if name == :id || object.public_send("#{name}_changed?")
if old_value = object.public_send("#{name}_was")
old_index_key = [name, old_value].join(":")
if (old_ids = ids_from_index(old_index_key)).present?
new_ids = old_ids.reject {|id| id == object.id }
if new_ids.empty?
redis.hdel(index_key, old_index_key)
else
obj << old_index_key
obj << @serializer.serialize(new_ids)
end
end
end
obj << [name, object.public_send(name)].join(":")
obj << object.id
}
redis.hmset(index_key, *indexes)
end
end
|