container.insert: use Track interface instead of concrete implementation
This commit is contained in:
parent
5669e3f37d
commit
c49a8390d7
3 changed files with 44 additions and 49 deletions
|
@ -1,16 +0,0 @@
|
||||||
create table messages (
|
|
||||||
id bigserial not null primary key,
|
|
||||||
domain text,
|
|
||||||
action text,
|
|
||||||
class text,
|
|
||||||
item text,
|
|
||||||
sender text,
|
|
||||||
payload jsonb,
|
|
||||||
tstamp timestamptz default current_timestamp
|
|
||||||
);
|
|
||||||
|
|
||||||
-- indexes
|
|
||||||
|
|
||||||
create index idx_msg on messages using btree (domain, action, class, item);
|
|
||||||
create index idx_msg_item on messages using btree (domain, class, item);
|
|
||||||
|
|
|
@ -1,16 +0,0 @@
|
||||||
create table messages (
|
|
||||||
id integer not null primary key,
|
|
||||||
domain text,
|
|
||||||
action text,
|
|
||||||
class text,
|
|
||||||
item text,
|
|
||||||
sender text,
|
|
||||||
payload jsonb,
|
|
||||||
tstamp timestamptz default current_timestamp
|
|
||||||
);
|
|
||||||
|
|
||||||
-- indexes
|
|
||||||
|
|
||||||
create index idx_msg on messages (domain, action, class, item);
|
|
||||||
create index idx_msg_item on messages (domain, class, item);
|
|
||||||
|
|
|
@ -13,14 +13,14 @@ import (
|
||||||
sql "git.sr.ht/~cco/go-scopes/storage"
|
sql "git.sr.ht/~cco/go-scopes/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ItemFactory func(TrackTemplate) Track
|
type ItemFactory func(*TrackTemplate) Track
|
||||||
|
|
||||||
type Track interface {
|
type Track interface {
|
||||||
TrackId() lib.Ident
|
TrackId() lib.Ident
|
||||||
Head() lib.StrMap
|
Head() lib.StrMap
|
||||||
Data() lib.Map
|
Data() lib.Map
|
||||||
TimeStamp() *time.Time
|
TimeStamp() *time.Time
|
||||||
//Update(TrackTemplate)
|
Update(*TrackTemplate)
|
||||||
}
|
}
|
||||||
|
|
||||||
type BaseTrack = track
|
type BaseTrack = track
|
||||||
|
@ -43,6 +43,10 @@ type track struct {
|
||||||
container *Container
|
container *Container
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MakeTrack(t *TrackTemplate) Track {
|
||||||
|
return &track{t.TrackId, t.Head, t.TimeStamp, t.Data, t.Container}
|
||||||
|
}
|
||||||
|
|
||||||
func (tr *track) TrackId() lib.Ident {
|
func (tr *track) TrackId() lib.Ident {
|
||||||
return tr.trackId
|
return tr.trackId
|
||||||
}
|
}
|
||||||
|
@ -59,21 +63,41 @@ func (tr *track) TimeStamp() *time.Time {
|
||||||
return tr.timeStamp
|
return tr.timeStamp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tr *track) Update(t *TrackTemplate) {
|
||||||
|
if t.TrackId != 0 {
|
||||||
|
tr.trackId = t.TrackId
|
||||||
|
}
|
||||||
|
if t.Head != nil {
|
||||||
|
tr.head = t.Head // or update map with non-empty values?
|
||||||
|
}
|
||||||
|
if t.TimeStamp != nil {
|
||||||
|
tr.timeStamp = t.TimeStamp
|
||||||
|
}
|
||||||
|
if t.Data != nil {
|
||||||
|
tr.data = t.Data
|
||||||
|
}
|
||||||
|
if t.Container != nil {
|
||||||
|
tr.container = t.Container
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// basic container implementation
|
// basic container implementation
|
||||||
|
|
||||||
type Container struct {
|
type Container struct {
|
||||||
TableName string
|
ItemFactory ItemFactory
|
||||||
HeadFields []string
|
TableName string
|
||||||
Indexes [][]string
|
HeadFields []string
|
||||||
Storage *sql.Storage
|
Indexes [][]string
|
||||||
|
Storage *sql.Storage
|
||||||
}
|
}
|
||||||
|
|
||||||
func Tracks(db *sql.Storage) *Container {
|
func Tracks(db *sql.Storage) *Container {
|
||||||
return &Container{
|
return &Container{
|
||||||
TableName: "tracks",
|
ItemFactory: MakeTrack,
|
||||||
HeadFields: []string{"taskId", "userName"},
|
TableName: "tracks",
|
||||||
Indexes: [][]string{[]string{"taskId", "userName"}, []string{"userName"}},
|
HeadFields: []string{"taskId", "userName"},
|
||||||
Storage: db,
|
Indexes: [][]string{[]string{"taskId", "userName"}, []string{"userName"}},
|
||||||
|
Storage: db,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,20 +106,22 @@ func (cont *Container) New(headValues lib.StrSlice, data lib.Map) Track {
|
||||||
for i, k := range cont.HeadFields {
|
for i, k := range cont.HeadFields {
|
||||||
head[k] = headValues[i]
|
head[k] = headValues[i]
|
||||||
}
|
}
|
||||||
tr := &track{head: head, data: data}
|
//tr := &track{head: head, data: data}
|
||||||
|
tr := cont.ItemFactory(&TrackTemplate{Head: head, Data: data})
|
||||||
cont.insert(tr)
|
cont.insert(tr)
|
||||||
return tr
|
return tr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cont *Container) insert(tr *track) lib.Ident {
|
func (cont *Container) insert(tr Track) lib.Ident {
|
||||||
var columns []string
|
var columns []string
|
||||||
var values []any
|
var values []any
|
||||||
|
head := tr.Head()
|
||||||
for _, k := range cont.HeadFields {
|
for _, k := range cont.HeadFields {
|
||||||
columns = append(columns, k)
|
columns = append(columns, k)
|
||||||
values = append(values, tr.head[k])
|
values = append(values, head[k])
|
||||||
}
|
}
|
||||||
columns = append(columns, "Data")
|
columns = append(columns, "Data")
|
||||||
b, _ := json.Marshal(tr.data)
|
b, _ := json.Marshal(tr.Data())
|
||||||
values = append(values, b)
|
values = append(values, b)
|
||||||
db := cont.Storage
|
db := cont.Storage
|
||||||
data := lib.Map{
|
data := lib.Map{
|
||||||
|
@ -116,9 +142,10 @@ func (cont *Container) insert(tr *track) lib.Ident {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := db.Query(proc, sql, values...); err == nil {
|
if err := db.Query(proc, sql, values...); err == nil {
|
||||||
tr.trackId = trid
|
tr.Update(&TrackTemplate{TrackId: trid, TimeStamp: ts, Container: cont})
|
||||||
tr.timeStamp = ts
|
//tr.trackId = trid
|
||||||
tr.container = cont
|
//tr.timeStamp = ts
|
||||||
|
//tr.container = cont
|
||||||
return trid
|
return trid
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
|
|
Loading…
Add table
Reference in a new issue