Skip to content

Commit

Permalink
Attempt to suppress fork warnings form SQlite3
Browse files Browse the repository at this point in the history
  • Loading branch information
malomalo committed Oct 10, 2024
1 parent 1f0a4ce commit 1794406
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions lib/bob_ross/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,26 @@ def initialize(path, cachefile, size: nil)
size = (dev_size * (size.to_i / 100.0)).round
end
@max_size = size || 1_073_741_824
end

def db
return @db if instance_variable_defined?(:@db)

@db = SQLite3::Database.new(cachefile)
@db.busy_timeout = 300
migrate
@db
end

def migrate
tables = @db.execute(<<-SQL).flatten
tables = db.execute(<<-SQL).flatten
SELECT name FROM sqlite_master
WHERE type='table'
ORDER BY name;
SQL

if !tables.include?('transformations')
@db.execute <<-SQL
db.execute <<-SQL
CREATE TABLE transformations (
hash VARCHAR,
transparent BOOLEAN,
Expand All @@ -50,33 +55,33 @@ def migrate
);
SQL

@db.execute <<-SQL
db.execute <<-SQL
CREATE UNIQUE INDEX thttm ON transformations (hash, transform, transformed_mime);
SQL

@db.execute <<-SQL
db.execute <<-SQL
CREATE INDEX tta ON transformations (transformed_at);
SQL

@db.execute <<-SQL
db.execute <<-SQL
CREATE TABLE stats (
key VARCHAR,
value INTEGER
);
SQL

@db.execute <<-SQL
db.execute <<-SQL
INSERT INTO stats (key, value) VALUES ('size', 0);
SQL

@db.execute <<-SQL
db.execute <<-SQL
CREATE TRIGGER stats_trigger_a AFTER INSERT ON transformations
FOR EACH ROW BEGIN
UPDATE stats SET value = (value + new.size) WHERE stats.key = 'size';
END
SQL

@db.execute <<-SQL
db.execute <<-SQL
CREATE TRIGGER stats_trigger_b AFTER DELETE ON transformations
FOR EACH ROW BEGIN
UPDATE stats SET value = (value - old.size) WHERE stats.key = 'size';
Expand All @@ -86,7 +91,7 @@ def migrate
end

def get(hash, transform)
@db.execute(<<-SQL, [hash, transform]).to_a
db.execute(<<-SQL, [hash, transform]).to_a
SELECT hash, transparent, transform, size, transformed_mime, transformed_at FROM transformations
WHERE hash = ? AND transform = ?
SQL
Expand All @@ -95,7 +100,7 @@ def get(hash, transform)

def use(hash, transform, mime)
file = File.open(destination(hash, transform, mime))
@db.execute(<<-SQL, [Time.now.to_i, hash, transform, mime])
db.execute(<<-SQL, [Time.now.to_i, hash, transform, mime])
UPDATE transformations
SET last_used_at = ?
WHERE hash = ? AND transform = ? AND transformed_mime = ?;
Expand All @@ -117,7 +122,7 @@ def set(hash, transparent, transform, mime, path)

FileUtils.mkdir_p(File.dirname(dest))
FileUtils.cp(path, dest)
@db.execute(<<-SQL, [hash, transparent ? 1 : 0, transform, stat.size, mime, Time.now.to_i, Time.now.to_i])
db.execute(<<-SQL, [hash, transparent ? 1 : 0, transform, stat.size, mime, Time.now.to_i, Time.now.to_i])
INSERT INTO transformations (hash, transparent, transform, size, transformed_mime, transformed_at, last_used_at)
VALUES (?, ?, ?, ?, ?, ?, ?)
SQL
Expand All @@ -134,13 +139,13 @@ def set(hash, transparent, transform, mime, path)
end

def del(hash)
entries = @db.execute(<<-SQL, [hash]).to_a
entries = db.execute(<<-SQL, [hash]).to_a
SELECT hash, transform, transformed_mime FROM transformations
WHERE hash = ?
SQL

entries.each do |entry|
@db.execute(<<-SQL, [entry[0], entry[1], entry[2]])
db.execute(<<-SQL, [entry[0], entry[1], entry[2]])
DELETE FROM transformations
WHERE hash = ? AND transform = ? AND transformed_mime = ?
SQL
Expand All @@ -149,7 +154,7 @@ def del(hash)
end

def size
@db.execute("SELECT value FROM stats WHERE stats.key = 'size'").first&.first || 0
db.execute("SELECT value FROM stats WHERE stats.key = 'size'").first&.first || 0
end

def purge!(buffer = 0)
Expand All @@ -161,11 +166,11 @@ def purge!(buffer = 0)
purged = 0
need_to_purge = new_size - @max_size
while purged < need_to_purge
r = @db.execute("SELECT hash, transform, transformed_mime, size FROM transformations ORDER BY last_used_at ASC LIMIT 1").first
r = db.execute("SELECT hash, transform, transformed_mime, size FROM transformations ORDER BY last_used_at ASC LIMIT 1").first
if r.nil?
return
else
@db.execute(<<-SQL, [r[0], r[1], r[2]])
db.execute(<<-SQL, [r[0], r[1], r[2]])
DELETE FROM transformations
WHERE hash = ? AND transform = ? AND transformed_mime = ?
SQL
Expand Down

0 comments on commit 1794406

Please sign in to comment.