pgtk 0.21.3 → 0.21.4
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/lib/pgtk/stash.rb +18 -17
- 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: 35204d3374563b14809ba4c71c020294c14f0914967e62157a8aa5b005816eb4
|
|
4
|
+
data.tar.gz: ba2972020329f362a4c6ef818cb938bff243a64ea0d19c31d5028c5c3808a07d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 53afb6021dccbd51901eadb93ee5ac8dfce38d8a40f45f400e5b3f5994bc9f2a3a23e6dcb694b7bf7211a00f7195c1fda8871e22c06b56bea050c766d59d25f1
|
|
7
|
+
data.tar.gz: 4973efc117e972436f4e447c730974918159a58d2120aeb94ad620ef163ad95756deeb957549c727facdc15a75a36e5c3632f1182bdfa0779c921ec50dc0e7fe
|
data/lib/pgtk/stash.rb
CHANGED
|
@@ -38,28 +38,24 @@ class Pgtk::Stash
|
|
|
38
38
|
# @param [Hash] stash Optional existing stash to use (default: new empty stash)
|
|
39
39
|
# @option [Hash] queries Internal cache data (default: {})
|
|
40
40
|
# @option [Hash] tables Internal cache data (default: {})
|
|
41
|
-
# @option [Concurrent::ReentrantReadWriteLock] entrance Lock for write internal state
|
|
42
|
-
# @option [Concurrent::AtomicBoolean] launched Latch for start timers once
|
|
43
41
|
# @param [Integer] refill_interval Interval in seconds for recalculate stale queries
|
|
44
42
|
# @param [Integer] top Number of queries to recalculate
|
|
45
43
|
# @param [Integer] threads Number of threads in threadpool
|
|
46
44
|
# @param [Loog] loog Logger for debugging (default: null logger)
|
|
47
45
|
def initialize(
|
|
48
46
|
pool,
|
|
49
|
-
stash
|
|
50
|
-
queries: {},
|
|
51
|
-
tables: {},
|
|
52
|
-
entrance: Concurrent::ReentrantReadWriteLock.new,
|
|
53
|
-
launched: Concurrent::AtomicBoolean.new(false)
|
|
54
|
-
},
|
|
47
|
+
stash: { queries: {}, tables: {} },
|
|
55
48
|
refill_interval: 5,
|
|
56
49
|
top: 100,
|
|
57
50
|
threads: 5,
|
|
58
|
-
loog: Loog::NULL
|
|
51
|
+
loog: Loog::NULL,
|
|
52
|
+
entrance: Concurrent::ReentrantReadWriteLock.new,
|
|
53
|
+
launched: Concurrent::AtomicBoolean.new(false)
|
|
59
54
|
)
|
|
60
55
|
@pool = pool
|
|
61
56
|
@stash = stash
|
|
62
|
-
@
|
|
57
|
+
@launched = launched
|
|
58
|
+
@entrance = entrance
|
|
63
59
|
@refill_interval = refill_interval
|
|
64
60
|
@top = top
|
|
65
61
|
@threads = threads
|
|
@@ -93,7 +89,7 @@ class Pgtk::Stash
|
|
|
93
89
|
@pool.dump,
|
|
94
90
|
'',
|
|
95
91
|
"Pgtk::Stash (refill_interval=#{@refill_interval}s, top=#{@top}q, threads=#{@threads}t):",
|
|
96
|
-
" #{'not ' if @
|
|
92
|
+
" #{'not ' if @launched.false?}launched",
|
|
97
93
|
" #{@stash[:tables].count} tables in cache",
|
|
98
94
|
" #{@stash[:queries].count} queries in cache:",
|
|
99
95
|
qq.sort_by { -_1[2] }.take(20).map { |a| " #{a[1]}/#{a[2]}p/#{a[3]}s: #{a[0]}" }
|
|
@@ -128,9 +124,9 @@ class Pgtk::Stash
|
|
|
128
124
|
if ret.nil? || @stash.dig(:queries, pure, key, :stale)
|
|
129
125
|
ret = @pool.exec(pure, params, result)
|
|
130
126
|
unless pure.include?(' NOW() ')
|
|
127
|
+
tables = pure.scan(/(?<=^|\s)(?:FROM|JOIN) ([a-z_]+)(?=\s|$)/).map(&:first).uniq
|
|
128
|
+
raise "No tables at #{pure.inspect}" if tables.empty?
|
|
131
129
|
@entrance.with_write_lock do
|
|
132
|
-
tables = pure.scan(/(?<=^|\s)(?:FROM|JOIN) ([a-z_]+)(?=\s|$)/).map(&:first).uniq
|
|
133
|
-
raise "No tables at #{pure.inspect}" if tables.empty?
|
|
134
130
|
tables.each do |t|
|
|
135
131
|
@stash[:tables][t] = [] if @stash[:tables][t].nil?
|
|
136
132
|
@stash[:tables][t].append(pure).uniq!
|
|
@@ -159,11 +155,14 @@ class Pgtk::Stash
|
|
|
159
155
|
def transaction
|
|
160
156
|
@pool.transaction do |t|
|
|
161
157
|
yield Pgtk::Stash.new(
|
|
162
|
-
t,
|
|
158
|
+
t,
|
|
159
|
+
stash: @stash,
|
|
163
160
|
refill_interval: @refill_interval,
|
|
164
161
|
top: @top,
|
|
165
162
|
threads: @threads,
|
|
166
|
-
loog: @loog
|
|
163
|
+
loog: @loog,
|
|
164
|
+
entrance: @entrance,
|
|
165
|
+
launched: @launched
|
|
167
166
|
)
|
|
168
167
|
end
|
|
169
168
|
end
|
|
@@ -171,7 +170,7 @@ class Pgtk::Stash
|
|
|
171
170
|
private
|
|
172
171
|
|
|
173
172
|
def launch!
|
|
174
|
-
raise 'Cannot launch multiple times on same cache data' unless @
|
|
173
|
+
raise 'Cannot launch multiple times on same cache data' unless @launched.make_true
|
|
175
174
|
Concurrent::FixedThreadPool.new(@threads).then do |threadpool|
|
|
176
175
|
Concurrent::TimerTask.execute(execution_interval: 60 * 60, executor: threadpool) do
|
|
177
176
|
@entrance.with_write_lock do
|
|
@@ -193,10 +192,12 @@ class Pgtk::Stash
|
|
|
193
192
|
@stash[:queries][q].each_key do |k|
|
|
194
193
|
next unless @stash[:queries][q][k][:stale]
|
|
195
194
|
threadpool.post do
|
|
195
|
+
h = @stash[:queries][q][k]
|
|
196
|
+
ret = @pool.exec(q, h[:params], h[:result])
|
|
196
197
|
@entrance.with_write_lock do
|
|
197
198
|
h = @stash[:queries][q][k]
|
|
198
199
|
h[:stale] = false
|
|
199
|
-
h[:ret] =
|
|
200
|
+
h[:ret] = ret
|
|
200
201
|
end
|
|
201
202
|
end
|
|
202
203
|
end
|
data/lib/pgtk/version.rb
CHANGED