pgtk 0.23.0 → 0.24.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 57546e93aaf2c029a795f61302d943e4f64e7d1e03b77d339636bdfb73d271d5
4
- data.tar.gz: 467c0493e69e8402b83ea6b537ac69760d5e712ac088b856307d2418449878f9
3
+ metadata.gz: b16d736e3b32a69aabf5a87fff3d4611f0af14c6c87e72fb7dc3e84d2ee027ee
4
+ data.tar.gz: 1c989c0af1b8ce28e325b0b9e146295e3cd24f02bb2cec0fee7ed2ac714ed2d6
5
5
  SHA512:
6
- metadata.gz: 83b61249a1888eed186c7faa7814c9a07f28fa9268d8d7fad1dfc0b9d7f4b304d4c835116fce6e32b6e651d7a2fc12731417c52922daae8ad9759419b32d2344
7
- data.tar.gz: 5107f90f035a11e3ab17c8718f759c6869ea24d3c195dafb1845d546587244ad1e6f82dca7da5b505055cefdafa97f7b0e6390f17374e2458f773e54f7f9dd75
6
+ metadata.gz: a6dcaac89f9ea200f9030505df810fb4a0a2136387e98833960f830be10d19aa9f1b55f7c3f8e6fd447c8df30235233aafd8f7730bde99c8751cb910416e660f
7
+ data.tar.gz: 34e7b0682c858ce58397abf4c8694cdb849453f70d0d0fe1c220d96eb800a425602ae693a149b53b7b2a1fc1b8afafee3f9c72a494b1214393d21d2d1dbafebd
data/Gemfile.lock CHANGED
@@ -26,7 +26,7 @@ GEM
26
26
  loog (~> 0.6)
27
27
  tago (~> 0.1)
28
28
  joined (0.4.0)
29
- json (2.15.2)
29
+ json (2.16.0)
30
30
  language_server-protocol (3.17.0.5)
31
31
  lint_roller (1.1.0)
32
32
  logger (1.7.0)
@@ -52,7 +52,7 @@ GEM
52
52
  pg (1.6.2-x64-mingw-ucrt)
53
53
  pg (1.6.2-x86_64-linux)
54
54
  prism (1.6.0)
55
- qbash (0.4.5)
55
+ qbash (0.4.7)
56
56
  backtrace (> 0)
57
57
  elapsed (> 0)
58
58
  loog (> 0)
@@ -76,7 +76,7 @@ GEM
76
76
  rubocop-ast (>= 1.47.1, < 2.0)
77
77
  ruby-progressbar (~> 1.7)
78
78
  unicode-display_width (>= 2.4.0, < 4.0)
79
- rubocop-ast (1.47.1)
79
+ rubocop-ast (1.48.0)
80
80
  parser (>= 3.3.7.2)
81
81
  prism (~> 1.4)
82
82
  rubocop-minitest (0.38.2)
@@ -101,7 +101,7 @@ GEM
101
101
  simplecov-html (0.13.2)
102
102
  simplecov_json_formatter (0.1.4)
103
103
  slop (4.10.1)
104
- tago (0.3.0)
104
+ tago (0.4.0)
105
105
  threads (0.4.1)
106
106
  backtrace (~> 0)
107
107
  concurrent-ruby (~> 1.0)
data/lib/pgtk/pool.rb CHANGED
@@ -7,6 +7,7 @@ require 'pg'
7
7
  require 'loog'
8
8
  require 'tago'
9
9
  require_relative '../pgtk'
10
+ require_relative 'version'
10
11
  require_relative 'wire'
11
12
 
12
13
  # Pool provides a connection pool for PostgreSQL database connections.
@@ -74,6 +75,7 @@ class Pgtk::Pool
74
75
  def dump
75
76
  [
76
77
  'Pgtk::Pool',
78
+ " Pgtk version: #{Pgtk::VERSION}",
77
79
  " PgSQL version: #{version}",
78
80
  " #{@pool.size} connections:",
79
81
  @pool.map do |c|
data/lib/pgtk/stash.rb CHANGED
@@ -6,6 +6,7 @@
6
6
  require 'concurrent-ruby'
7
7
  require 'joined'
8
8
  require 'loog'
9
+ require 'tago'
9
10
  require_relative '../pgtk'
10
11
 
11
12
  # Database query cache implementation.
@@ -39,6 +40,8 @@ class Pgtk::Stash
39
40
  # @option [Hash] queries Internal cache data (default: {})
40
41
  # @option [Hash] tables Internal cache data (default: {})
41
42
  # @param [Integer] refill_interval Interval in seconds for recalculate stale queries
43
+ # @param [Integer] cap How many queries to keep in cache (if more, oldest ones are deleted)
44
+ # @param [Integer] cap_interval Interval in seconds for cap the cache (remove old queries)
42
45
  # @param [Integer] max_queue_length Number of refilling tasks in the queue
43
46
  # @param [Integer] threads Number of threads in tpool
44
47
  # @param [Loog] loog Logger for debugging (default: null logger)
@@ -48,6 +51,8 @@ class Pgtk::Stash
48
51
  refill_interval: 16,
49
52
  max_queue_length: 128,
50
53
  threads: 4,
54
+ cap: 10_000,
55
+ cap_interval: 60,
51
56
  loog: Loog::NULL,
52
57
  entrance: Concurrent::ReentrantReadWriteLock.new,
53
58
  launched: Concurrent::AtomicBoolean.new(false)
@@ -59,6 +64,8 @@ class Pgtk::Stash
59
64
  @refill_interval = refill_interval
60
65
  @max_queue_length = max_queue_length
61
66
  @threads = threads
67
+ @cap = cap
68
+ @cap_interval = cap_interval
62
69
  @loog = loog
63
70
  @tpool = Concurrent::FixedThreadPool.new(@threads)
64
71
  end
@@ -78,13 +85,14 @@ class Pgtk::Stash
78
85
  # Convert internal state into text.
79
86
  def dump
80
87
  qq =
81
- @stash[:queries].map do |k, v|
82
- [
83
- k.dup, # the query
84
- v.values.count, # how many keys?
85
- v.values.sum { |vv| vv[:popularity] }, # total popularity of all keys
86
- v.values.count { |vv| vv[:stale] } # how many stale keys?
87
- ]
88
+ @stash[:queries].map do |q, kk|
89
+ {
90
+ q: q.dup, # the query
91
+ c: kk.values.count, # how many keys?
92
+ p: kk.values.sum { |vv| vv[:popularity] }, # total popularity of all keys
93
+ s: kk.values.count { |vv| vv[:stale] }, # how many stale keys?
94
+ u: kk.values.map { |vv| vv[:used] }.max || Time.now # when was it used
95
+ }
88
96
  end
89
97
  [
90
98
  @pool.dump,
@@ -93,10 +101,14 @@ class Pgtk::Stash
93
101
  " #{'not ' if @launched.false?}launched",
94
102
  " #{@tpool.queue_length} task(s) in the thread pool",
95
103
  " #{@stash[:tables].count} table(s) in cache",
96
- " #{qq.sum { |a| a[3] }} stale quer(ies) in cache:",
97
- qq.select { |a| a[3].positive? }.sort_by { -_1[2] }.take(16).map { |a| " #{a[1]}/#{a[2]}p/#{a[3]}s: #{a[0]}" },
98
- " #{qq.count { |a| a[3].zero? }} other quer(ies) in cache:",
99
- qq.select { |a| a[3].zero? }.sort_by { -_1[2] }.take(8).map { |a| " #{a[1]}/#{a[2]}p/#{a[3]}s: #{a[0]}" }
104
+ " #{qq.sum { |a| a[:s] }} stale quer(ies) in cache:",
105
+ qq.select { |a| a[:s].positive? }.sort_by { -_1[:p] }.take(8).map do |a|
106
+ " #{a[:c]}/#{a[:p]}p/#{a[:s]}s/#{a[:u].ago}: #{a[:q]}"
107
+ end,
108
+ " #{qq.count { |a| a[:s].zero? }} other quer(ies) in cache:",
109
+ qq.select { |a| a[:s].zero? }.sort_by { -_1[:p] }.take(16).map do |a|
110
+ " #{a[:c]}/#{a[:p]}p/#{a[:s]}s/#{a[:u].ago}: #{a[:q]}"
111
+ end
100
112
  ].join("\n")
101
113
  end
102
114
 
@@ -176,11 +188,17 @@ class Pgtk::Stash
176
188
 
177
189
  def launch!
178
190
  raise 'Cannot launch multiple times on same cache data' unless @launched.make_true
179
- retire = 60 * 60
180
- Concurrent::TimerTask.execute(execution_interval: retire, executor: @tpool) do
181
- @entrance.with_write_lock do
182
- @stash[:queries].each_key do |q|
183
- @stash[:queries][q].delete_if { |_, h| h[:used] < Time.now - retire }
191
+ Concurrent::TimerTask.execute(execution_interval: @cap_interval, executor: @tpool) do
192
+ loop do
193
+ s = @stash[:queries].values.sum { |kk| kk.values.size }
194
+ break if s <= @cap
195
+ @entrance.with_write_lock do
196
+ @stash[:queries].each_key do |q|
197
+ m = @stash[:queries][q].values.map { |h| h[:used] }.min
198
+ next unless m
199
+ @stash[:queries][q].delete_if { |_, h| h[:used] == m }
200
+ @stash[:queries].delete_if { |_, kk| kk.empty? }
201
+ end
184
202
  end
185
203
  end
186
204
  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.23.0'
14
+ VERSION = '0.24.0'
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.23.0
4
+ version: 0.24.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko