pgtk 0.31.1 → 0.31.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -2
- data/lib/pgtk/stash.rb +33 -16
- data/lib/pgtk/version.rb +1 -1
- 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: b7db2cc332dfccb4c405f590cc7aa64ffc55d1aa916aea3c0ea68379208c2568
|
|
4
|
+
data.tar.gz: c7a03b2c8b6ccf2b931b1ab4d16bf83f352edeb0099c1d5fcc8f21a5f274a789
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d0e7ca8dc2192a5acb274b9e4b02fda11fdfaf471fe607c7d5d8ac0cf85cd8b527bb35f3de93f271102ba4b483a285452df5d6e1f308df8f503e987b42b10197
|
|
7
|
+
data.tar.gz: d028000b4d1e869d9766105a7706f8b415f012a1c6f3254f2c2bdd6bb8578b97f27fd22f317cc06f7609ca3b79667814d60ca1713b21be3145a51b1b41e5d752
|
data/Gemfile.lock
CHANGED
data/lib/pgtk/stash.rb
CHANGED
|
@@ -57,7 +57,7 @@ class Pgtk::Stash
|
|
|
57
57
|
# @param [Concurrent::ReentrantReadWriteLock] entrance Read-write lock for thread-safe access
|
|
58
58
|
def initialize(
|
|
59
59
|
pool,
|
|
60
|
-
stash: { queries: {}, tables: {} },
|
|
60
|
+
stash: { queries: {}, tables: {}, table_mod: {} },
|
|
61
61
|
loog: Loog::NULL,
|
|
62
62
|
entrance: Concurrent::ReentrantReadWriteLock.new,
|
|
63
63
|
refill: 16,
|
|
@@ -71,6 +71,7 @@ class Pgtk::Stash
|
|
|
71
71
|
)
|
|
72
72
|
@pool = pool
|
|
73
73
|
@stash = stash
|
|
74
|
+
@stash[:table_mod] ||= {}
|
|
74
75
|
@loog = loog
|
|
75
76
|
@entrance = entrance
|
|
76
77
|
@refill = refill
|
|
@@ -216,11 +217,13 @@ class Pgtk::Stash
|
|
|
216
217
|
tables = pure.scan(ALTS_RE).flatten
|
|
217
218
|
tables.uniq!
|
|
218
219
|
ret = @pool.exec(pure, params, result)
|
|
220
|
+
now = Time.now
|
|
219
221
|
@entrance.with_write_lock do
|
|
220
222
|
tables.each do |t|
|
|
223
|
+
@stash[:table_mod][t] = now
|
|
221
224
|
@stash[:tables][t]&.each do |q|
|
|
222
225
|
@stash[:queries][q]&.each_key do |key|
|
|
223
|
-
@stash[:queries][q][key][:stale] =
|
|
226
|
+
@stash[:queries][q][key][:stale] = now
|
|
224
227
|
end
|
|
225
228
|
end
|
|
226
229
|
end
|
|
@@ -233,16 +236,17 @@ class Pgtk::Stash
|
|
|
233
236
|
ret = @stash.dig(:queries, pure, key, :ret)
|
|
234
237
|
if ret.nil? || @stash.dig(:queries, pure, key, :stale)
|
|
235
238
|
mark = @stash.dig(:queries, pure, key, :stale)
|
|
239
|
+
tables = pure.scan(/(?<=^|\s)(?:FROM|JOIN) ([a-z_]+)(?=\s|;|$)/).flatten
|
|
240
|
+
tables.uniq!
|
|
241
|
+
marks = tables.to_h { |t| [t, @stash[:table_mod][t]] }
|
|
236
242
|
ret = @pool.exec(pure, params, result)
|
|
237
|
-
cache(pure, key, params, result, ret, mark) unless pure.include?(' NOW() ')
|
|
243
|
+
cache(pure, key, params, result, ret, mark, tables, marks) unless pure.include?(' NOW() ')
|
|
238
244
|
end
|
|
239
245
|
bump(pure, key) if @stash.dig(:queries, pure, key)
|
|
240
246
|
ret
|
|
241
247
|
end
|
|
242
248
|
|
|
243
|
-
def cache(pure, key, params, result, ret, mark)
|
|
244
|
-
tables = pure.scan(/(?<=^|\s)(?:FROM|JOIN) ([a-z_]+)(?=\s|;|$)/).flatten
|
|
245
|
-
tables.uniq!
|
|
249
|
+
def cache(pure, key, params, result, ret, mark, tables, marks)
|
|
246
250
|
raise(ArgumentError, "No tables at #{pure.inspect}") if tables.empty?
|
|
247
251
|
@entrance.with_write_lock do
|
|
248
252
|
tables.each do |t|
|
|
@@ -252,8 +256,9 @@ class Pgtk::Stash
|
|
|
252
256
|
@stash[:queries][pure] ||= {}
|
|
253
257
|
existing = @stash[:queries][pure][key]
|
|
254
258
|
stale = existing && existing[:stale]
|
|
259
|
+
stillborn = tables.any? { |t| (cur = @stash[:table_mod][t]) && cur != marks[t] }
|
|
255
260
|
entry = { ret:, params:, result:, used: Time.now }
|
|
256
|
-
entry[:stale] = stale if stale && stale != mark
|
|
261
|
+
entry[:stale] = stale == mark ? Time.now : stale if (stale && stale != mark) || stillborn
|
|
257
262
|
@stash[:queries][pure][key] = entry
|
|
258
263
|
end
|
|
259
264
|
end
|
|
@@ -294,7 +299,7 @@ class Pgtk::Stash
|
|
|
294
299
|
m = @stash[:queries][q].values.map { |h| h[:used] }.min
|
|
295
300
|
next unless m
|
|
296
301
|
@stash[:queries][q].delete_if { |_, h| h[:used] == m }
|
|
297
|
-
@stash[:queries].
|
|
302
|
+
evict(q) if @stash[:queries][q].empty?
|
|
298
303
|
end
|
|
299
304
|
end
|
|
300
305
|
end
|
|
@@ -306,12 +311,18 @@ class Pgtk::Stash
|
|
|
306
311
|
@entrance.with_write_lock do
|
|
307
312
|
@stash[:queries].each_key do |q|
|
|
308
313
|
@stash[:queries][q].delete_if { |_, h| h[:used] < Time.now - @retire }
|
|
309
|
-
@stash[:queries].
|
|
314
|
+
evict(q) if @stash[:queries][q].empty?
|
|
310
315
|
end
|
|
311
316
|
end
|
|
312
317
|
end
|
|
313
318
|
end
|
|
314
319
|
|
|
320
|
+
def evict(query)
|
|
321
|
+
@stash[:queries].delete(query)
|
|
322
|
+
@stash[:tables].each_value { |list| list.delete(query) }
|
|
323
|
+
@stash[:tables].delete_if { |_, list| list.empty? }
|
|
324
|
+
end
|
|
325
|
+
|
|
315
326
|
def refiller!
|
|
316
327
|
Concurrent::TimerTask.execute(execution_interval: @refill, executor: @tpool) do
|
|
317
328
|
ranked.each { |q| replenish(q) }
|
|
@@ -328,16 +339,22 @@ class Pgtk::Stash
|
|
|
328
339
|
end
|
|
329
340
|
|
|
330
341
|
def replenish(query)
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
342
|
+
snapshot =
|
|
343
|
+
@entrance.with_read_lock do
|
|
344
|
+
@stash[:queries][query]&.filter_map do |k, h|
|
|
345
|
+
next unless h[:stale]
|
|
346
|
+
next if h[:stale] > Time.now - @delay
|
|
347
|
+
[k, h[:params], h[:result], h[:stale]]
|
|
348
|
+
end
|
|
349
|
+
end
|
|
350
|
+
return unless snapshot
|
|
351
|
+
snapshot.each do |k, params, result, mark|
|
|
334
352
|
next if @tpool.queue_length >= @maxqueue
|
|
335
353
|
@tpool.post do
|
|
336
|
-
|
|
337
|
-
mark = h[:stale]
|
|
338
|
-
ret = @pool.exec(query, h[:params], h[:result])
|
|
354
|
+
ret = @pool.exec(query, params, result)
|
|
339
355
|
@entrance.with_write_lock do
|
|
340
|
-
h = @stash[:queries][query]
|
|
356
|
+
h = @stash[:queries][query]&.dig(k)
|
|
357
|
+
next unless h
|
|
341
358
|
if h[:stale] == mark
|
|
342
359
|
h[:ret] = ret
|
|
343
360
|
h.delete(:stale)
|
data/lib/pgtk/version.rb
CHANGED