mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-07 23:13:11 -06:00
Move event types sql to a separate file
This commit is contained in:
parent
7f26b82433
commit
cb17fbfaa5
|
|
@ -0,0 +1,94 @@
|
||||||
|
package storage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"github.com/matrix-org/dendrite/roomserver/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
const eventTypesSchema = `
|
||||||
|
-- Numeric versions of the event "type"s. Event types tend to be taken from a
|
||||||
|
-- small common pool. Assigning each a numeric ID should reduce the amount of
|
||||||
|
-- data that needs to be stored and fetched from the database.
|
||||||
|
-- It also means that many operations can work with int64 arrays rather than
|
||||||
|
-- string arrays which may help reduce GC pressure.
|
||||||
|
-- Well known event types are pre-assigned numeric IDs:
|
||||||
|
-- 1 -> m.room.create
|
||||||
|
-- 2 -> m.room.power_levels
|
||||||
|
-- 3 -> m.room.join_rules
|
||||||
|
-- 4 -> m.room.third_party_invite
|
||||||
|
-- 5 -> m.room.member
|
||||||
|
-- 6 -> m.room.redaction
|
||||||
|
-- 7 -> m.room.history_visibility
|
||||||
|
-- Picking well-known numeric IDs for the events types that require special
|
||||||
|
-- attention during state conflict resolution means that we write that code
|
||||||
|
-- using numeric constants.
|
||||||
|
-- It also means that the numeric IDs for common event types should be
|
||||||
|
-- consistent between different instances which might make ad-hoc debugging
|
||||||
|
-- easier.
|
||||||
|
-- Other event types are automatically assigned numeric IDs starting from 2**16.
|
||||||
|
-- This leaves room to add more pre-assigned numeric IDs and clearly separates
|
||||||
|
-- the automatically assigned IDs from the pre-assigned IDs.
|
||||||
|
CREATE SEQUENCE IF NOT EXISTS event_type_nid_seq START 65536;
|
||||||
|
CREATE TABLE IF NOT EXISTS event_types (
|
||||||
|
-- Local numeric ID for the event type.
|
||||||
|
event_type_nid BIGINT PRIMARY KEY DEFAULT nextval('event_type_nid_seq'),
|
||||||
|
-- The string event_type.
|
||||||
|
event_type TEXT NOT NULL CONSTRAINT event_type_unique UNIQUE
|
||||||
|
);
|
||||||
|
INSERT INTO event_types (event_type_nid, event_type) VALUES
|
||||||
|
(1, 'm.room.create'),
|
||||||
|
(2, 'm.room.power_levels'),
|
||||||
|
(3, 'm.room.join_rules'),
|
||||||
|
(4, 'm.room.third_party_invite'),
|
||||||
|
(5, 'm.room.member'),
|
||||||
|
(6, 'm.room.redaction'),
|
||||||
|
(7, 'm.room.history_visibility') ON CONFLICT DO NOTHING;
|
||||||
|
`
|
||||||
|
|
||||||
|
// Assign a new numeric event type ID.
|
||||||
|
// The usual case is that the event type is not in the database.
|
||||||
|
// In that case the ID will be assigned using the next value from the sequence.
|
||||||
|
// We use `RETURNING` to tell postgres to return the assigned ID.
|
||||||
|
// But it's possible that the type was added in a query that raced with us.
|
||||||
|
// This will result in a conflict on the event_type_unique constraint.
|
||||||
|
// We peform a update that does nothing rather that doing nothing at all because
|
||||||
|
// postgres won't return anything unless we touch a row in the table.
|
||||||
|
const insertEventTypeNIDSQL = "" +
|
||||||
|
"INSERT INTO event_types (event_type) VALUES ($1)" +
|
||||||
|
" ON CONFLICT ON CONSTRAINT event_type_unique" +
|
||||||
|
" DO UPDATE SET event_type = $1" +
|
||||||
|
" RETURNING (event_type_nid)"
|
||||||
|
|
||||||
|
const selectEventTypeNIDSQL = "" +
|
||||||
|
"SELECT event_type_nid FROM event_types WHERE event_type = $1"
|
||||||
|
|
||||||
|
type eventTypeStatements struct {
|
||||||
|
insertEventTypeNIDStmt *sql.Stmt
|
||||||
|
selectEventTypeNIDStmt *sql.Stmt
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *eventTypeStatements) prepare(db *sql.DB) (err error) {
|
||||||
|
_, err = db.Exec(eventTypesSchema)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.insertEventTypeNIDStmt, err = db.Prepare(insertEventTypeNIDSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.selectEventTypeNIDStmt, err = db.Prepare(selectEventTypeNIDSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *eventTypeStatements) insertEventTypeNID(eventType string) (types.EventTypeNID, error) {
|
||||||
|
var eventTypeNID int64
|
||||||
|
err := s.insertEventTypeNIDStmt.QueryRow(eventType).Scan(&eventTypeNID)
|
||||||
|
return types.EventTypeNID(eventTypeNID), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *eventTypeStatements) selectEventTypeNID(eventType string) (types.EventTypeNID, error) {
|
||||||
|
var eventTypeNID int64
|
||||||
|
err := s.selectEventTypeNIDStmt.QueryRow(eventType).Scan(&eventTypeNID)
|
||||||
|
return types.EventTypeNID(eventTypeNID), err
|
||||||
|
}
|
||||||
|
|
@ -9,8 +9,7 @@ import (
|
||||||
|
|
||||||
type statements struct {
|
type statements struct {
|
||||||
partitionOffsetStatements
|
partitionOffsetStatements
|
||||||
insertEventTypeNIDStmt *sql.Stmt
|
eventTypeStatements
|
||||||
selectEventTypeNIDStmt *sql.Stmt
|
|
||||||
insertEventStateKeyNIDStmt *sql.Stmt
|
insertEventStateKeyNIDStmt *sql.Stmt
|
||||||
selectEventStateKeyNIDStmt *sql.Stmt
|
selectEventStateKeyNIDStmt *sql.Stmt
|
||||||
bulkSelectEventStateKeyNIDStmt *sql.Stmt
|
bulkSelectEventStateKeyNIDStmt *sql.Stmt
|
||||||
|
|
@ -36,7 +35,7 @@ func (s *statements) prepare(db *sql.DB) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = s.prepareEventTypes(db); err != nil {
|
if err = s.eventTypeStatements.prepare(db); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -59,89 +58,6 @@ func (s *statements) prepare(db *sql.DB) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *statements) prepareEventTypes(db *sql.DB) (err error) {
|
|
||||||
_, err = db.Exec(eventTypesSchema)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if s.insertEventTypeNIDStmt, err = db.Prepare(insertEventTypeNIDSQL); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if s.selectEventTypeNIDStmt, err = db.Prepare(selectEventTypeNIDSQL); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const eventTypesSchema = `
|
|
||||||
-- Numeric versions of the event "type"s. Event types tend to be taken from a
|
|
||||||
-- small common pool. Assigning each a numeric ID should reduce the amount of
|
|
||||||
-- data that needs to be stored and fetched from the database.
|
|
||||||
-- It also means that many operations can work with int64 arrays rather than
|
|
||||||
-- string arrays which may help reduce GC pressure.
|
|
||||||
-- Well known event types are pre-assigned numeric IDs:
|
|
||||||
-- 1 -> m.room.create
|
|
||||||
-- 2 -> m.room.power_levels
|
|
||||||
-- 3 -> m.room.join_rules
|
|
||||||
-- 4 -> m.room.third_party_invite
|
|
||||||
-- 5 -> m.room.member
|
|
||||||
-- 6 -> m.room.redaction
|
|
||||||
-- 7 -> m.room.history_visibility
|
|
||||||
-- Picking well-known numeric IDs for the events types that require special
|
|
||||||
-- attention during state conflict resolution means that we write that code
|
|
||||||
-- using numeric constants.
|
|
||||||
-- It also means that the numeric IDs for common event types should be
|
|
||||||
-- consistent between different instances which might make ad-hoc debugging
|
|
||||||
-- easier.
|
|
||||||
-- Other event types are automatically assigned numeric IDs starting from 2**16.
|
|
||||||
-- This leaves room to add more pre-assigned numeric IDs and clearly separates
|
|
||||||
-- the automatically assigned IDs from the pre-assigned IDs.
|
|
||||||
CREATE SEQUENCE IF NOT EXISTS event_type_nid_seq START 65536;
|
|
||||||
CREATE TABLE IF NOT EXISTS event_types (
|
|
||||||
-- Local numeric ID for the event type.
|
|
||||||
event_type_nid BIGINT PRIMARY KEY DEFAULT nextval('event_type_nid_seq'),
|
|
||||||
-- The string event_type.
|
|
||||||
event_type TEXT NOT NULL CONSTRAINT event_type_unique UNIQUE
|
|
||||||
);
|
|
||||||
INSERT INTO event_types (event_type_nid, event_type) VALUES
|
|
||||||
(1, 'm.room.create'),
|
|
||||||
(2, 'm.room.power_levels'),
|
|
||||||
(3, 'm.room.join_rules'),
|
|
||||||
(4, 'm.room.third_party_invite'),
|
|
||||||
(5, 'm.room.member'),
|
|
||||||
(6, 'm.room.redaction'),
|
|
||||||
(7, 'm.room.history_visibility') ON CONFLICT DO NOTHING;
|
|
||||||
`
|
|
||||||
|
|
||||||
// Assign a new numeric event type ID.
|
|
||||||
// The usual case is that the event type is not in the database.
|
|
||||||
// In that case the ID will be assigned using the next value from the sequence.
|
|
||||||
// We use `RETURNING` to tell postgres to return the assigned ID.
|
|
||||||
// But it's possible that the type was added in a query that raced with us.
|
|
||||||
// This will result in a conflict on the event_type_unique constraint.
|
|
||||||
// We peform a update that does nothing rather that doing nothing at all because
|
|
||||||
// postgres won't return anything unless we touch a row in the table.
|
|
||||||
const insertEventTypeNIDSQL = "" +
|
|
||||||
"INSERT INTO event_types (event_type) VALUES ($1)" +
|
|
||||||
" ON CONFLICT ON CONSTRAINT event_type_unique" +
|
|
||||||
" DO UPDATE SET event_type = $1" +
|
|
||||||
" RETURNING (event_type_nid)"
|
|
||||||
|
|
||||||
const selectEventTypeNIDSQL = "" +
|
|
||||||
"SELECT event_type_nid FROM event_types WHERE event_type = $1"
|
|
||||||
|
|
||||||
func (s *statements) insertEventTypeNID(eventType string) (types.EventTypeNID, error) {
|
|
||||||
var eventTypeNID int64
|
|
||||||
err := s.insertEventTypeNIDStmt.QueryRow(eventType).Scan(&eventTypeNID)
|
|
||||||
return types.EventTypeNID(eventTypeNID), err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *statements) selectEventTypeNID(eventType string) (types.EventTypeNID, error) {
|
|
||||||
var eventTypeNID int64
|
|
||||||
err := s.selectEventTypeNIDStmt.QueryRow(eventType).Scan(&eventTypeNID)
|
|
||||||
return types.EventTypeNID(eventTypeNID), err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *statements) prepareEventStateKeys(db *sql.DB) (err error) {
|
func (s *statements) prepareEventStateKeys(db *sql.DB) (err error) {
|
||||||
_, err = db.Exec(eventStateKeysSchema)
|
_, err = db.Exec(eventStateKeysSchema)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue