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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pgtk/stash.rb +18 -17
  3. data/lib/pgtk/version.rb +1 -1
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 96d8784e334eb788333eb79d26c99c47f129ea8de8e522732fa37fa333b99a35
4
- data.tar.gz: ec5caad7117ec1f22784c718316b2897effb907f03cd1043e7305a057d9412ee
3
+ metadata.gz: 35204d3374563b14809ba4c71c020294c14f0914967e62157a8aa5b005816eb4
4
+ data.tar.gz: ba2972020329f362a4c6ef818cb938bff243a64ea0d19c31d5028c5c3808a07d
5
5
  SHA512:
6
- metadata.gz: 2266b9797e6f49cecdd4f5e4b198f4d06d9060544a6e2eb89ca99c8f0a05a93e6674b6237af30569e6d474c08e13b02ac706cf9a96daf07938021cd61d518e51
7
- data.tar.gz: 1b723ca7237b8d1e5381481599fd0850e2685fa411a37551360d78548ca0b29ea38da50b849dbad37c8cac1d65422cdcb488b1edd45d9025ccf67336ea586048
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
- @entrance = stash[:entrance]
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 @stash[:launched].false?}launched",
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, @stash,
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 @stash[:launched].make_true
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] = @pool.exec(q, h[:params], h[:result])
200
+ h[:ret] = ret
200
201
  end
201
202
  end
202
203
  end
data/lib/pgtk/version.rb CHANGED
@@ -11,5 +11,5 @@ require_relative '../pgtk'
11
11
  # License:: MIT
12
12
  module Pgtk
13
13
  # Current version of the library.
14
- VERSION = '0.21.3'
14
+ VERSION = '0.21.4'
15
15
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pgtk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.21.3
4
+ version: 0.21.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko