feat: ✨ begin work on everything :P
This commit is contained in:
parent
03b77c7395
commit
dca5944549
6 changed files with 445 additions and 6 deletions
83
lib/timeseries/hoarder/CachingTable.rb
Normal file
83
lib/timeseries/hoarder/CachingTable.rb
Normal file
|
@ -0,0 +1,83 @@
|
|||
|
||||
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
|
42
lib/timeseries/hoarder/Table.rb
Normal file
42
lib/timeseries/hoarder/Table.rb
Normal file
|
@ -0,0 +1,42 @@
|
|||
|
||||
|
||||
module Timeseries
|
||||
module Hoarder
|
||||
class Table
|
||||
def initialize(db, table_name, table_schema = "public")
|
||||
@table_name = table_name
|
||||
@table_schema = table_schema
|
||||
|
||||
@db = db
|
||||
@pg = @db.pg
|
||||
|
||||
@created = false
|
||||
|
||||
ensure_table_exists
|
||||
end
|
||||
|
||||
def ensure_table_exists
|
||||
return if @created
|
||||
|
||||
@pg.transaction do
|
||||
@pg.exec("SELECT pg_advisory_lock(0)")
|
||||
|
||||
r = @pg.exec_params("SELECT 1 FROM information_schema.tables WHERE table_name = $1 AND table_schema = $2", [@table_name, @schema_name])
|
||||
|
||||
if r.num_tuples >= 1
|
||||
@created = true
|
||||
return
|
||||
end
|
||||
|
||||
table_creation
|
||||
|
||||
@created = true
|
||||
end
|
||||
end
|
||||
|
||||
def table_creation
|
||||
raise "No table creation string method provided!"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
21
lib/timeseries/hoarder/TimeseriesDatabase.rb
Normal file
21
lib/timeseries/hoarder/TimeseriesDatabase.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
|
||||
require 'pg'
|
||||
|
||||
require_relative 'CachingTable.rb'
|
||||
|
||||
module Timeseries
|
||||
module Hoarder
|
||||
class Database
|
||||
attr_reader :pg
|
||||
attr_reader :data_sources
|
||||
|
||||
def initialize(pg)
|
||||
@pg = pg
|
||||
|
||||
@pg.exec("CREATE SCHEMA IF NOT EXISTS ts_hoarder")
|
||||
|
||||
@data_sources = CachingTable.new(self, 'sources', 'source')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue