pgtk 0.21.3 → 0.22.0
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 +1 -1
- data/lib/pgtk/stash.rb +32 -31
- 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: 902941bbf98d37acfad72c9615282264148e6ad6ce1183f2fcd085d3a0cf288e
|
|
4
|
+
data.tar.gz: 00a5488c8dd99f98df1ebce8cef20207514964a1628df8489bd320d0c199ca77
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 47d6993b709ebce71a8cd1525f94230b8c554a8c2edd871ca67627ce6e6724d46ea3530827fa3cb653ae54b56cf4299cb4291197d3f870e602942ffc0b7b6134
|
|
7
|
+
data.tar.gz: cbe8bf7eebf1c038d71079845355d805bc647be88e62523db89180ce9f39e4299a779f104ee648a1f4399f18f90a0e4f3aab371f277df964dec843299b5b733d
|
data/Gemfile.lock
CHANGED
data/lib/pgtk/stash.rb
CHANGED
|
@@ -38,30 +38,26 @@ 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
|
-
# @param [Integer]
|
|
45
|
-
# @param [Integer] threads Number of threads in
|
|
42
|
+
# @param [Integer] max_queue_length Number of refilling tasks in the queue
|
|
43
|
+
# @param [Integer] threads Number of threads in tpool
|
|
46
44
|
# @param [Loog] loog Logger for debugging (default: null logger)
|
|
47
45
|
def initialize(
|
|
48
46
|
pool,
|
|
49
|
-
stash
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
top: 100,
|
|
57
|
-
threads: 5,
|
|
58
|
-
loog: Loog::NULL
|
|
47
|
+
stash: { queries: {}, tables: {} },
|
|
48
|
+
refill_interval: 16,
|
|
49
|
+
max_queue_length: 128,
|
|
50
|
+
threads: 4,
|
|
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
|
+
@max_queue_length = max_queue_length
|
|
65
61
|
@threads = threads
|
|
66
62
|
@loog = loog
|
|
67
63
|
end
|
|
@@ -92,11 +88,11 @@ class Pgtk::Stash
|
|
|
92
88
|
[
|
|
93
89
|
@pool.dump,
|
|
94
90
|
'',
|
|
95
|
-
"Pgtk::Stash (refill_interval=#{@refill_interval}s,
|
|
96
|
-
" #{'not ' if @
|
|
91
|
+
"Pgtk::Stash (refill_interval=#{@refill_interval}s, max_queue_length=#{@max_queue_length}, threads=#{@threads}):",
|
|
92
|
+
" #{'not ' if @launched.false?}launched",
|
|
97
93
|
" #{@stash[:tables].count} tables in cache",
|
|
98
94
|
" #{@stash[:queries].count} queries in cache:",
|
|
99
|
-
qq.sort_by { -_1[2] }.take(
|
|
95
|
+
qq.sort_by { -_1[2] }.take(64).map { |a| " #{a[1]}/#{a[2]}p/#{a[3]}s: #{a[0]}" }
|
|
100
96
|
].join("\n")
|
|
101
97
|
end
|
|
102
98
|
|
|
@@ -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
|
+
max_queue_length: @max_queue_length,
|
|
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,9 +170,9 @@ class Pgtk::Stash
|
|
|
171
170
|
private
|
|
172
171
|
|
|
173
172
|
def launch!
|
|
174
|
-
raise 'Cannot launch multiple times on same cache data' unless @
|
|
175
|
-
Concurrent::FixedThreadPool.new(@threads).then do |
|
|
176
|
-
Concurrent::TimerTask.execute(execution_interval: 60 * 60, executor:
|
|
173
|
+
raise 'Cannot launch multiple times on same cache data' unless @launched.make_true
|
|
174
|
+
Concurrent::FixedThreadPool.new(@threads).then do |tpool|
|
|
175
|
+
Concurrent::TimerTask.execute(execution_interval: 60 * 60, executor: tpool) do
|
|
177
176
|
@entrance.with_write_lock do
|
|
178
177
|
@stash[:queries].each_key do |q|
|
|
179
178
|
@stash[:queries][q].each_key do |k|
|
|
@@ -182,21 +181,23 @@ class Pgtk::Stash
|
|
|
182
181
|
end
|
|
183
182
|
end
|
|
184
183
|
end
|
|
185
|
-
Concurrent::TimerTask.execute(execution_interval: @refill_interval, executor:
|
|
184
|
+
Concurrent::TimerTask.execute(execution_interval: @refill_interval, executor: tpool) do
|
|
186
185
|
@stash[:queries]
|
|
187
186
|
.map { |k, v| [k, v.values.sum { |vv| vv[:popularity] }, v.values.any? { |vv| vv[:stale] }] }
|
|
188
187
|
.select { _1[2] }
|
|
189
188
|
.sort_by { -_1[1] }
|
|
190
|
-
.first(@top)
|
|
191
189
|
.each do |a|
|
|
192
190
|
q = a[0]
|
|
193
191
|
@stash[:queries][q].each_key do |k|
|
|
194
192
|
next unless @stash[:queries][q][k][:stale]
|
|
195
|
-
|
|
193
|
+
next if tpool.queue_length >= @max_queue_length
|
|
194
|
+
tpool.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