redcord 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/redcord/redis.rb +52 -77
- data/lib/redcord/relation.rb +15 -10
- data/lib/redcord/vacuum_helper.rb +6 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 525c939f4cee5b8adbb11b0530a2b2f44f6a26a8bb1a55e4dc837d725b3f71fb
|
4
|
+
data.tar.gz: f7e1747cc025310b20ab5e8d675fefe6d951ec60c5c09056454e11c8c366008e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6e2ff5ff1008d0a9e59e02438010e78324ec86c3ce03c470f28175498c7b29fbd277f97509f59dead80e258609ed033c9d686119726553b206996c064d4c7827
|
7
|
+
data.tar.gz: bba7b07d1e6b222614667a271b25973f5d1693d2090cb533dcbbfaf092f6200cce75ce5a7cf76356277bc1d51ae5011405465caae64ad295b85faa63d016c191
|
data/lib/redcord/redis.rb
CHANGED
@@ -18,24 +18,19 @@ class Redcord::Redis < Redis
|
|
18
18
|
).returns(String)
|
19
19
|
end
|
20
20
|
def create_hash_returning_id(key, args, ttl:, index_attrs:, range_index_attrs:, custom_index_attrs:, hash_tag: nil)
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
custom_index_attrs_flat = custom_index_attrs.inject([]) do |result, (index_name, attrs)|
|
27
|
-
result << index_name
|
28
|
-
result << attrs.size
|
29
|
-
result + attrs
|
30
|
-
end
|
31
|
-
run_script(
|
32
|
-
:create_hash,
|
33
|
-
keys: [id, hash_tag],
|
34
|
-
argv: [key, ttl, index_attrs.size, range_index_attrs.size, custom_index_attrs_flat.size] +
|
35
|
-
index_attrs + range_index_attrs + custom_index_attrs_flat + args.to_a.flatten,
|
36
|
-
)
|
37
|
-
id
|
21
|
+
id = "#{SecureRandom.uuid}#{hash_tag}"
|
22
|
+
custom_index_attrs_flat = custom_index_attrs.inject([]) do |result, (index_name, attrs)|
|
23
|
+
result << index_name
|
24
|
+
result << attrs.size
|
25
|
+
result + attrs
|
38
26
|
end
|
27
|
+
run_script(
|
28
|
+
:create_hash,
|
29
|
+
keys: [id, hash_tag],
|
30
|
+
argv: [key, ttl, index_attrs.size, range_index_attrs.size, custom_index_attrs_flat.size] +
|
31
|
+
index_attrs + range_index_attrs + custom_index_attrs_flat + args.to_a.flatten,
|
32
|
+
)
|
33
|
+
id
|
39
34
|
end
|
40
35
|
|
41
36
|
sig do
|
@@ -51,26 +46,21 @@ class Redcord::Redis < Redis
|
|
51
46
|
).void
|
52
47
|
end
|
53
48
|
def update_hash(model, id, args, ttl:, index_attrs:, range_index_attrs:, custom_index_attrs:, hash_tag:)
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
result << attrs.size
|
62
|
-
result + attrs
|
63
|
-
else
|
64
|
-
result
|
65
|
-
end
|
49
|
+
custom_index_attrs_flat = custom_index_attrs.inject([]) do |result, (index_name, attrs)|
|
50
|
+
if !(args.keys.to_set & attrs.to_set).empty?
|
51
|
+
result << index_name
|
52
|
+
result << attrs.size
|
53
|
+
result + attrs
|
54
|
+
else
|
55
|
+
result
|
66
56
|
end
|
67
|
-
run_script(
|
68
|
-
:update_hash,
|
69
|
-
keys: [id, hash_tag],
|
70
|
-
argv: [model, ttl, index_attrs.size, range_index_attrs.size, custom_index_attrs_flat.size] +
|
71
|
-
index_attrs + range_index_attrs + custom_index_attrs_flat + args.to_a.flatten,
|
72
|
-
)
|
73
57
|
end
|
58
|
+
run_script(
|
59
|
+
:update_hash,
|
60
|
+
keys: [id, hash_tag],
|
61
|
+
argv: [model, ttl, index_attrs.size, range_index_attrs.size, custom_index_attrs_flat.size] +
|
62
|
+
index_attrs + range_index_attrs + custom_index_attrs_flat + args.to_a.flatten,
|
63
|
+
)
|
74
64
|
end
|
75
65
|
|
76
66
|
sig do
|
@@ -83,17 +73,12 @@ class Redcord::Redis < Redis
|
|
83
73
|
).returns(Integer)
|
84
74
|
end
|
85
75
|
def delete_hash(model, id, index_attrs:, range_index_attrs:, custom_index_attrs:)
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
:delete_hash,
|
93
|
-
keys: [id, id.match(/\{.*\}$/)&.send(:[], 0)],
|
94
|
-
argv: [model, index_attrs.size, range_index_attrs.size] + index_attrs + range_index_attrs + custom_index_names,
|
95
|
-
)
|
96
|
-
end
|
76
|
+
custom_index_names = custom_index_attrs.keys
|
77
|
+
run_script(
|
78
|
+
:delete_hash,
|
79
|
+
keys: [id, id.match(/\{.*\}$/)&.send(:[], 0)],
|
80
|
+
argv: [model, index_attrs.size, range_index_attrs.size] + index_attrs + range_index_attrs + custom_index_names,
|
81
|
+
)
|
97
82
|
end
|
98
83
|
|
99
84
|
sig do
|
@@ -118,22 +103,17 @@ class Redcord::Redis < Redis
|
|
118
103
|
hash_tag: nil,
|
119
104
|
custom_index_name: nil
|
120
105
|
)
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
# The Lua script will return this as a flattened array.
|
133
|
-
# Convert the result into a hash of {id -> model hash}
|
134
|
-
res_hash = res.each_slice(2)
|
135
|
-
res_hash.map { |key, val| [key, val.each_slice(2).to_h] }.to_h
|
136
|
-
end
|
106
|
+
conditions = flatten_with_partial_sort(query_conditions.clone, custom_index_attrs)
|
107
|
+
res = run_script(
|
108
|
+
:find_by_attr,
|
109
|
+
keys: [hash_tag],
|
110
|
+
argv: [model, custom_index_name, index_attrs.size, range_index_attrs.size, custom_index_attrs.size, conditions.size] +
|
111
|
+
index_attrs + range_index_attrs + custom_index_attrs + conditions + select_attrs.to_a.flatten
|
112
|
+
)
|
113
|
+
# The Lua script will return this as a flattened array.
|
114
|
+
# Convert the result into a hash of {id -> model hash}
|
115
|
+
res_hash = res.each_slice(2)
|
116
|
+
res_hash.map { |key, val| [key, val.each_slice(2).to_h] }.to_h
|
137
117
|
end
|
138
118
|
|
139
119
|
sig do
|
@@ -156,33 +136,28 @@ class Redcord::Redis < Redis
|
|
156
136
|
hash_tag: nil,
|
157
137
|
custom_index_name: nil
|
158
138
|
)
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
keys: [hash_tag],
|
167
|
-
argv: [model, custom_index_name, index_attrs.size, range_index_attrs.size, custom_index_attrs.size] +
|
168
|
-
index_attrs + range_index_attrs + custom_index_attrs + conditions
|
169
|
-
)
|
170
|
-
end
|
139
|
+
conditions = flatten_with_partial_sort(query_conditions.clone, custom_index_attrs)
|
140
|
+
run_script(
|
141
|
+
:find_by_attr_count,
|
142
|
+
keys: [hash_tag],
|
143
|
+
argv: [model, custom_index_name, index_attrs.size, range_index_attrs.size, custom_index_attrs.size] +
|
144
|
+
index_attrs + range_index_attrs + custom_index_attrs + conditions
|
145
|
+
)
|
171
146
|
end
|
172
147
|
|
173
|
-
def scan_each_shard(key, &blk)
|
148
|
+
def scan_each_shard(key, count: 1000, &blk)
|
174
149
|
clients = instance_variable_get(:@client)
|
175
150
|
&.instance_variable_get(:@node)
|
176
151
|
&.instance_variable_get(:@clients)
|
177
152
|
&.values
|
178
153
|
|
179
154
|
if clients.nil?
|
180
|
-
scan_each(match: key, &blk)
|
155
|
+
scan_each(match: key, count: count, &blk)
|
181
156
|
else
|
182
157
|
clients.each do |client|
|
183
158
|
cursor = 0
|
184
159
|
loop do
|
185
|
-
cursor, keys = client.call([:scan, cursor, 'match', key])
|
160
|
+
cursor, keys = client.call([:scan, cursor, 'match', key, 'count', count])
|
186
161
|
keys.each(&blk)
|
187
162
|
break if cursor == "0"
|
188
163
|
end
|
data/lib/redcord/relation.rb
CHANGED
@@ -88,16 +88,21 @@ class Redcord::Relation
|
|
88
88
|
|
89
89
|
sig { returns(Integer) }
|
90
90
|
def count
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
91
|
+
Redcord::Base.trace(
|
92
|
+
'redcord_relation_count',
|
93
|
+
model_name: model.name,
|
94
|
+
) do
|
95
|
+
model.validate_index_attributes(query_conditions.keys, custom_index_name: custom_index_name)
|
96
|
+
redis.find_by_attr_count(
|
97
|
+
model.model_key,
|
98
|
+
extract_query_conditions!,
|
99
|
+
index_attrs: model._script_arg_index_attrs,
|
100
|
+
range_index_attrs: model._script_arg_range_index_attrs,
|
101
|
+
custom_index_attrs: model._script_arg_custom_index_attrs[custom_index_name],
|
102
|
+
hash_tag: extract_hash_tag!,
|
103
|
+
custom_index_name: custom_index_name
|
104
|
+
)
|
105
|
+
end
|
101
106
|
end
|
102
107
|
|
103
108
|
sig { params(index_name: T.nilable(Symbol)).returns(Redcord::Relation) }
|
@@ -32,24 +32,27 @@ module Redcord::VacuumHelper
|
|
32
32
|
|
33
33
|
sig { params(model: T.class_of(Redcord::Base), range_index_attr: Symbol).void }
|
34
34
|
def self._vacuum_range_index_attribute(model, range_index_attr)
|
35
|
+
key_suffix = model.shard_by_attribute.nil? ? nil : '{*}'
|
35
36
|
range_index_set_key = "#{model.model_key}:#{range_index_attr}"
|
36
37
|
range_index_set_nil_key = "#{range_index_set_key}:"
|
37
38
|
|
38
39
|
# Handle nil values for range index attributes, which are stored in a normal
|
39
40
|
# set at Redcord:Model:range_index_attr:
|
40
|
-
model.redis.scan_each_shard("#{range_index_set_nil_key}
|
41
|
+
model.redis.scan_each_shard("#{range_index_set_nil_key}#{key_suffix}") do |key|
|
41
42
|
_remove_stale_ids_from_set(model, key)
|
42
43
|
end
|
43
44
|
|
44
|
-
model.redis.scan_each_shard("#{range_index_set_key}
|
45
|
+
model.redis.scan_each_shard("#{range_index_set_key}#{key_suffix}") do |key|
|
45
46
|
_remove_stale_ids_from_sorted_set(model, key)
|
46
47
|
end
|
47
48
|
end
|
48
49
|
|
49
50
|
sig { params(model: T.class_of(Redcord::Base), index_name: Symbol).void }
|
50
51
|
def self._vacuum_custom_index(model, index_name)
|
52
|
+
key_suffix = model.shard_by_attribute.nil? ? nil : '{*}'
|
51
53
|
custom_index_content_key = "#{model.model_key}:custom_index:#{index_name}_content"
|
52
|
-
|
54
|
+
|
55
|
+
model.redis.scan_each_shard("#{custom_index_content_key}#{key_suffix}") do |key|
|
53
56
|
hash_tag = key.split(custom_index_content_key)[1] || ""
|
54
57
|
_remove_stale_records_from_custom_index(model, hash_tag, index_name)
|
55
58
|
end
|