gosora/common/meta_store.go
Azareal f10fdcb40c Added the active memory analytics pane. More to come here.
Added the meta store.

Added the panel_menu_statistics_active_memory phrase.
Added the panel_statistics_active_memory_head phrase.

Added the panel_analytics_time_range_month template.

Added Go 1.12 to the Travis list.

Added the stack and heap columns to the memchunks table.
Added the createdAt column to the activity_stream table.
Added the meta table.

You will need to run the patcher / updater for this commit.
2019-05-09 16:58:55 +10:00

46 lines
1.1 KiB
Go

package common
import "database/sql"
import "github.com/Azareal/Gosora/query_gen"
var Meta MetaStore
// MetaStore is a simple key-value store for the system to stash things in when needed
type MetaStore interface {
Get(name string) (val string, err error)
Set(name string, val string) error
}
type DefaultMetaStore struct {
get *sql.Stmt
set *sql.Stmt
add *sql.Stmt
}
func NewDefaultMetaStore(acc *qgen.Accumulator) (*DefaultMetaStore, error) {
m := &DefaultMetaStore{
get: acc.Select("meta").Columns("value").Where("name = ?").Prepare(),
set: acc.Update("meta").Set("value = ?").Where("name = ?").Prepare(),
add: acc.Insert("meta").Columns("name,value").Fields("?,''").Prepare(),
}
return m, acc.FirstError()
}
func (s *DefaultMetaStore) Get(name string) (val string, err error) {
err = s.get.QueryRow(name).Scan(&val)
return val, err
}
// TODO: Use timestamped rows as a more robust method of ensuring data integrity
func (s *DefaultMetaStore) Set(name string, val string) error {
_, err := s.Get(name)
if err == sql.ErrNoRows {
_, err := s.add.Exec(name)
if err != nil {
return err
}
}
_, err = s.set.Exec(val, name)
return err
}