83 lines
No EOL
2.2 KiB
Ruby
83 lines
No EOL
2.2 KiB
Ruby
|
|
require_relative 'Table.rb'
|
|
|
|
require 'time'
|
|
require 'json'
|
|
|
|
module Timeseries
|
|
module Hoarder
|
|
class CachingTable < Table
|
|
def initialize(db, name, content_name = 'tags', tag_access_update_delay: 60)
|
|
@content_name = content_name
|
|
@id_column = content_name + '_id'
|
|
|
|
super(db, name, 'ts_hoarder')
|
|
|
|
@known_tags = {}
|
|
@tag_access_times = {}
|
|
@tag_access_updates = {}
|
|
|
|
@tag_access_update_delay = tag_access_update_delay
|
|
end
|
|
|
|
def table_creation
|
|
@pg.exec("CREATE TABLE ts_hoarder.#{@table_name} ( #{@id_column} SERIAL PRIMARY KEY, #{@content_name} JSONB, created_at TIMESTAMPTZ, last_used TIMESTAMPTZ )")
|
|
|
|
@pg.exec("CREATE INDEX ON ts_hoarder.#{@table_name} USING GIN ( #{@content_name} )")
|
|
end
|
|
|
|
def load_cache_content
|
|
@pg.exec("SELECT * FROM ts_hoarder.#{@table_name}") do |results|
|
|
results.each do |tuple|
|
|
|
|
tags = JSON.parse(tuple[@content_name])
|
|
|
|
@known_tags[tags] = tuple[@id_column]
|
|
@tag_access_times[tags] = Time.parse(tuple['last_used'])
|
|
end
|
|
end
|
|
|
|
true
|
|
end
|
|
|
|
def create_entry(tags)
|
|
return @known_tags[tags] if @known_tags.include? tags
|
|
|
|
returned_id = nil
|
|
|
|
@pg.transaction do
|
|
@pg.exec("LOCK TABLE ts_hoarder.#{@table_name}")
|
|
|
|
res = @pg.exec_params("SELECT * FROM ts_hoarder.#{@table_name} WHERE #{@content_name} = $1::jsonb", [tags.to_json])
|
|
|
|
if(res.num_tuples >= 1)
|
|
returned_id = res[0][@id_column]
|
|
@known_tags[tags] = returned_id
|
|
@tag_access_times[tags] = Time.parse(res[0]['last_used'])
|
|
else
|
|
res = @pg.exec_params("INSERT INTO ts_hoarder.#{@table_name} (#{@content_name}, created_at, last_used) VALUES ($1::jsonb, NOW(), NOW()) RETURNING #{@id_column}", [tags.to_json])
|
|
|
|
returned_id = res[0][@id_column]
|
|
@known_tags[tags] = returned_id
|
|
@tag_access_times[tags] = Time.now()
|
|
end
|
|
end
|
|
|
|
returned_id
|
|
end
|
|
|
|
def [](tags)
|
|
access_time = Time.now()
|
|
if(((@tag_access_times[tags] || Time.at(0)) - Time.now()) > @tag_access_update_delay)
|
|
@tag_access_times[tags] = access_time
|
|
@tag_access_updates[tags] = true
|
|
end
|
|
|
|
known_id = @known_tags[tags]
|
|
return known_id unless known_id.nil?
|
|
|
|
return create_entry(tags)
|
|
end
|
|
end
|
|
end
|
|
end |