diff --git a/src/github.com/matrix-org/dendrite/roomserver/storage/event_types_table.go b/src/github.com/matrix-org/dendrite/roomserver/storage/event_types_table.go new file mode 100644 index 000000000..d8f132d6c --- /dev/null +++ b/src/github.com/matrix-org/dendrite/roomserver/storage/event_types_table.go @@ -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 +} diff --git a/src/github.com/matrix-org/dendrite/roomserver/storage/sql.go b/src/github.com/matrix-org/dendrite/roomserver/storage/sql.go index bfd61a450..f2c4ec07b 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/storage/sql.go +++ b/src/github.com/matrix-org/dendrite/roomserver/storage/sql.go @@ -9,8 +9,7 @@ import ( type statements struct { partitionOffsetStatements - insertEventTypeNIDStmt *sql.Stmt - selectEventTypeNIDStmt *sql.Stmt + eventTypeStatements insertEventStateKeyNIDStmt *sql.Stmt selectEventStateKeyNIDStmt *sql.Stmt bulkSelectEventStateKeyNIDStmt *sql.Stmt @@ -36,7 +35,7 @@ func (s *statements) prepare(db *sql.DB) error { return err } - if err = s.prepareEventTypes(db); err != nil { + if err = s.eventTypeStatements.prepare(db); err != nil { return err } @@ -59,89 +58,6 @@ func (s *statements) prepare(db *sql.DB) error { 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) { _, err = db.Exec(eventStateKeysSchema) if err != nil {