Add nodes table for event metadata

This commit is contained in:
Kegan Dougal 2020-11-03 15:31:10 +00:00
parent 33f03046a4
commit 21e61636e1

View file

@ -12,14 +12,18 @@ import (
type Database interface { type Database interface {
// StoreRelation stores the parent->child and child->parent relationship for later querying. // StoreRelation stores the parent->child and child->parent relationship for later querying.
// Also stores the event metadata e.g timestamp
StoreRelation(ctx context.Context, ev *gomatrixserverlib.HeaderedEvent) error StoreRelation(ctx context.Context, ev *gomatrixserverlib.HeaderedEvent) error
// ChildrenForParent returns the event IDs of events who have the given `eventID` as an m.relationship with the
// provided `relType`.
ChildrenForParent(ctx context.Context, eventID, relType string) ([]string, error) ChildrenForParent(ctx context.Context, eventID, relType string) ([]string, error)
} }
type DB struct { type DB struct {
db *sql.DB db *sql.DB
writer sqlutil.Writer writer sqlutil.Writer
insertRelationStmt *sql.Stmt insertEdgeStmt *sql.Stmt
insertNodeStmt *sql.Stmt
selectChildrenForParentStmt *sql.Stmt selectChildrenForParentStmt *sql.Stmt
} }
@ -46,15 +50,26 @@ func newPostgresDatabase(dbOpts *config.DatabaseOptions) (Database, error) {
rel_type TEXT NOT NULL, rel_type TEXT NOT NULL,
CONSTRAINT msc2836_edges UNIQUE (parent_event_id, child_event_id, rel_type) CONSTRAINT msc2836_edges UNIQUE (parent_event_id, child_event_id, rel_type)
); );
CREATE TABLE IF NOT EXISTS msc2836_nodes (
event_id TEXT PRIMARY KEY NOT NULL,
origin_server_ts BIGINT NOT NULL,
room_id TEXT NOT NULL
);
`) `)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if d.insertRelationStmt, err = d.db.Prepare(` if d.insertEdgeStmt, err = d.db.Prepare(`
INSERT INTO msc2836_edges(parent_event_id, child_event_id, rel_type) VALUES($1, $2, $3) ON CONFLICT DO NOTHING INSERT INTO msc2836_edges(parent_event_id, child_event_id, rel_type) VALUES($1, $2, $3) ON CONFLICT DO NOTHING
`); err != nil { `); err != nil {
return nil, err return nil, err
} }
if d.insertNodeStmt, err = d.db.Prepare(`
INSERT INTO msc2836_nodes(event_id, origin_server_ts, room_id) VALUES($1, $2, $3) ON CONFLICT DO NOTHING
`); err != nil {
return nil, err
}
if d.selectChildrenForParentStmt, err = d.db.Prepare(` if d.selectChildrenForParentStmt, err = d.db.Prepare(`
SELECT child_event_id FROM msc2836_edges WHERE parent_event_id = $1 AND rel_type = $2 SELECT child_event_id FROM msc2836_edges WHERE parent_event_id = $1 AND rel_type = $2
`); err != nil { `); err != nil {
@ -78,15 +93,26 @@ func newSQLiteDatabase(dbOpts *config.DatabaseOptions) (Database, error) {
rel_type TEXT NOT NULL, rel_type TEXT NOT NULL,
UNIQUE (parent_event_id, child_event_id, rel_type) UNIQUE (parent_event_id, child_event_id, rel_type)
); );
CREATE TABLE IF NOT EXISTS msc2836_nodes (
event_id TEXT PRIMARY KEY NOT NULL,
origin_server_ts BIGINT NOT NULL,
room_id TEXT NOT NULL
);
`) `)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if d.insertRelationStmt, err = d.db.Prepare(` if d.insertEdgeStmt, err = d.db.Prepare(`
INSERT INTO msc2836_edges(parent_event_id, child_event_id, rel_type) VALUES($1, $2, $3) ON CONFLICT (parent_event_id, child_event_id, rel_type) DO NOTHING INSERT INTO msc2836_edges(parent_event_id, child_event_id, rel_type) VALUES($1, $2, $3) ON CONFLICT (parent_event_id, child_event_id, rel_type) DO NOTHING
`); err != nil { `); err != nil {
return nil, err return nil, err
} }
if d.insertNodeStmt, err = d.db.Prepare(`
INSERT INTO msc2836_nodes(event_id, origin_server_ts, room_id) VALUES($1, $2, $3) ON CONFLICT DO NOTHING
`); err != nil {
return nil, err
}
if d.selectChildrenForParentStmt, err = d.db.Prepare(` if d.selectChildrenForParentStmt, err = d.db.Prepare(`
SELECT child_event_id FROM msc2836_edges WHERE parent_event_id = $1 AND rel_type = $2 SELECT child_event_id FROM msc2836_edges WHERE parent_event_id = $1 AND rel_type = $2
`); err != nil { `); err != nil {
@ -100,8 +126,14 @@ func (p *DB) StoreRelation(ctx context.Context, ev *gomatrixserverlib.HeaderedEv
if parent == "" || child == "" { if parent == "" || child == "" {
return nil return nil
} }
_, err := p.insertRelationStmt.ExecContext(ctx, parent, child, relType) return p.writer.Do(p.db, nil, func(txn *sql.Tx) error {
return err _, err := txn.Stmt(p.insertEdgeStmt).ExecContext(ctx, parent, child, relType)
if err != nil {
return err
}
_, err = txn.Stmt(p.insertNodeStmt).ExecContext(ctx, ev.EventID(), ev.OriginServerTS(), ev.RoomID())
return err
})
} }
func (p *DB) ChildrenForParent(ctx context.Context, eventID, relType string) ([]string, error) { func (p *DB) ChildrenForParent(ctx context.Context, eventID, relType string) ([]string, error) {