From 8084beb6f711549d74ef92d015202cc1bec14402 Mon Sep 17 00:00:00 2001 From: Mark Haines Date: Tue, 7 Mar 2017 10:37:41 +0000 Subject: [PATCH] Add helper method for preparing lists of statements. (#25) * Add helper method for preparing lists of statements. --- .../roomserver/storage/event_json_table.go | 11 ++++----- .../storage/event_state_keys_table.go | 15 ++++-------- .../roomserver/storage/event_types_table.go | 12 ++++------ .../roomserver/storage/events_table.go | 15 ++---------- .../storage/partition_offset_table.go | 12 ++++------ .../dendrite/roomserver/storage/prepare.go | 21 +++++++++++++++++ .../storage/previous_events_table.go | 12 ++++------ .../roomserver/storage/rooms_table.go | 23 ++++++------------- .../roomserver/storage/state_block_table.go | 16 +++++-------- .../storage/state_snapshot_table.go | 12 ++++------ 10 files changed, 65 insertions(+), 84 deletions(-) create mode 100644 src/github.com/matrix-org/dendrite/roomserver/storage/prepare.go diff --git a/src/github.com/matrix-org/dendrite/roomserver/storage/event_json_table.go b/src/github.com/matrix-org/dendrite/roomserver/storage/event_json_table.go index f7c8052c4..f585123f8 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/storage/event_json_table.go +++ b/src/github.com/matrix-org/dendrite/roomserver/storage/event_json_table.go @@ -44,13 +44,10 @@ func (s *eventJSONStatements) prepare(db *sql.DB) (err error) { if err != nil { return } - if s.insertEventJSONStmt, err = db.Prepare(insertEventJSONSQL); err != nil { - return - } - if s.bulkSelectEventJSONStmt, err = db.Prepare(bulkSelectEventJSONSQL); err != nil { - return - } - return + return statementList{ + {&s.insertEventJSONStmt, insertEventJSONSQL}, + {&s.bulkSelectEventJSONStmt, bulkSelectEventJSONSQL}, + }.prepare(db) } func (s *eventJSONStatements) insertEventJSON(eventNID types.EventNID, eventJSON []byte) error { diff --git a/src/github.com/matrix-org/dendrite/roomserver/storage/event_state_keys_table.go b/src/github.com/matrix-org/dendrite/roomserver/storage/event_state_keys_table.go index 88e1a5609..5d4363c55 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/storage/event_state_keys_table.go +++ b/src/github.com/matrix-org/dendrite/roomserver/storage/event_state_keys_table.go @@ -53,16 +53,11 @@ func (s *eventStateKeyStatements) prepare(db *sql.DB) (err error) { if err != nil { return } - if s.insertEventStateKeyNIDStmt, err = db.Prepare(insertEventStateKeyNIDSQL); err != nil { - return - } - if s.selectEventStateKeyNIDStmt, err = db.Prepare(selectEventStateKeyNIDSQL); err != nil { - return - } - if s.bulkSelectEventStateKeyNIDStmt, err = db.Prepare(bulkSelectEventStateKeyNIDSQL); err != nil { - return - } - return + return statementList{ + {&s.insertEventStateKeyNIDStmt, insertEventStateKeyNIDSQL}, + {&s.selectEventStateKeyNIDStmt, selectEventStateKeyNIDSQL}, + {&s.bulkSelectEventStateKeyNIDStmt, bulkSelectEventStateKeyNIDSQL}, + }.prepare(db) } func (s *eventStateKeyStatements) insertEventStateKeyNID(eventStateKey string) (types.EventStateKeyNID, error) { 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 index 8437747d6..26fe05388 100644 --- 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 @@ -76,13 +76,11 @@ func (s *eventTypeStatements) prepare(db *sql.DB) (err error) { 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 + + return statementList{ + {&s.insertEventTypeNIDStmt, insertEventTypeNIDSQL}, + {&s.selectEventTypeNIDStmt, selectEventTypeNIDSQL}, + }.prepare(db) } func (s *eventTypeStatements) insertEventTypeNID(eventType string) (types.EventTypeNID, error) { diff --git a/src/github.com/matrix-org/dendrite/roomserver/storage/events_table.go b/src/github.com/matrix-org/dendrite/roomserver/storage/events_table.go index 63cfa266b..f745b372f 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/storage/events_table.go +++ b/src/github.com/matrix-org/dendrite/roomserver/storage/events_table.go @@ -110,10 +110,7 @@ func (s *eventStatements) prepare(db *sql.DB) (err error) { return } - statements := []struct { - statement **sql.Stmt - sql string - }{ + return statementList{ {&s.insertEventStmt, insertEventSQL}, {&s.selectEventStmt, selectEventSQL}, {&s.bulkSelectStateEventByIDStmt, bulkSelectStateEventByIDSQL}, @@ -125,15 +122,7 @@ func (s *eventStatements) prepare(db *sql.DB) (err error) { {&s.bulkSelectStateAtEventAndReferenceStmt, bulkSelectStateAtEventAndReferenceSQL}, {&s.bulkSelectEventReferenceStmt, bulkSelectEventReferenceSQL}, {&s.bulkSelectEventIDStmt, bulkSelectEventIDSQL}, - } - - for _, statement := range statements { - if *statement.statement, err = db.Prepare(statement.sql); err != nil { - return - } - } - - return + }.prepare(db) } func (s *eventStatements) insertEvent( diff --git a/src/github.com/matrix-org/dendrite/roomserver/storage/partition_offset_table.go b/src/github.com/matrix-org/dendrite/roomserver/storage/partition_offset_table.go index 2dfe13287..a926f8777 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/storage/partition_offset_table.go +++ b/src/github.com/matrix-org/dendrite/roomserver/storage/partition_offset_table.go @@ -36,13 +36,11 @@ func (s *partitionOffsetStatements) prepare(db *sql.DB) (err error) { if err != nil { return } - if s.selectPartitionOffsetsStmt, err = db.Prepare(selectPartitionOffsetsSQL); err != nil { - return - } - if s.upsertPartitionOffsetStmt, err = db.Prepare(upsertPartitionOffsetsSQL); err != nil { - return - } - return + + return statementList{ + {&s.selectPartitionOffsetsStmt, selectPartitionOffsetsSQL}, + {&s.upsertPartitionOffsetStmt, upsertPartitionOffsetsSQL}, + }.prepare(db) } func (s *partitionOffsetStatements) selectPartitionOffsets(topic string) ([]types.PartitionOffset, error) { diff --git a/src/github.com/matrix-org/dendrite/roomserver/storage/prepare.go b/src/github.com/matrix-org/dendrite/roomserver/storage/prepare.go new file mode 100644 index 000000000..5db2b73f6 --- /dev/null +++ b/src/github.com/matrix-org/dendrite/roomserver/storage/prepare.go @@ -0,0 +1,21 @@ +package storage + +import ( + "database/sql" +) + +// a statementList is a list of SQL statements to prepare and a pointer to where to store the resulting prepared statement. +type statementList []struct { + statement **sql.Stmt + sql string +} + +// prepare the SQL for each statement in the list and assign the result to the prepared statement. +func (s statementList) prepare(db *sql.DB) (err error) { + for _, statement := range s { + if *statement.statement, err = db.Prepare(statement.sql); err != nil { + return + } + } + return +} diff --git a/src/github.com/matrix-org/dendrite/roomserver/storage/previous_events_table.go b/src/github.com/matrix-org/dendrite/roomserver/storage/previous_events_table.go index fe4601f00..f7d80861f 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/storage/previous_events_table.go +++ b/src/github.com/matrix-org/dendrite/roomserver/storage/previous_events_table.go @@ -50,13 +50,11 @@ func (s *previousEventStatements) prepare(db *sql.DB) (err error) { if err != nil { return } - if s.insertPreviousEventStmt, err = db.Prepare(insertPreviousEventSQL); err != nil { - return - } - if s.selectPreviousEventExistsStmt, err = db.Prepare(selectPreviousEventExistsSQL); err != nil { - return - } - return + + return statementList{ + {&s.insertPreviousEventStmt, insertPreviousEventSQL}, + {&s.selectPreviousEventExistsStmt, selectPreviousEventExistsSQL}, + }.prepare(db) } func (s *previousEventStatements) insertPreviousEvent(txn *sql.Tx, previousEventID string, previousEventReferenceSHA256 []byte, eventNID types.EventNID) error { diff --git a/src/github.com/matrix-org/dendrite/roomserver/storage/rooms_table.go b/src/github.com/matrix-org/dendrite/roomserver/storage/rooms_table.go index 93a65e473..f81715450 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/storage/rooms_table.go +++ b/src/github.com/matrix-org/dendrite/roomserver/storage/rooms_table.go @@ -56,22 +56,13 @@ func (s *roomStatements) prepare(db *sql.DB) (err error) { if err != nil { return } - if s.insertRoomNIDStmt, err = db.Prepare(insertRoomNIDSQL); err != nil { - return - } - if s.selectRoomNIDStmt, err = db.Prepare(selectRoomNIDSQL); err != nil { - return - } - if s.selectLatestEventNIDsStmt, err = db.Prepare(selectLatestEventNIDsSQL); err != nil { - return - } - if s.selectLatestEventNIDsForUpdateStmt, err = db.Prepare(selectLatestEventNIDsForUpdateSQL); err != nil { - return - } - if s.updateLatestEventNIDsStmt, err = db.Prepare(updateLatestEventNIDsSQL); err != nil { - return - } - return + return statementList{ + {&s.insertRoomNIDStmt, insertRoomNIDSQL}, + {&s.selectRoomNIDStmt, selectRoomNIDSQL}, + {&s.selectLatestEventNIDsStmt, selectLatestEventNIDsSQL}, + {&s.selectLatestEventNIDsForUpdateStmt, selectLatestEventNIDsForUpdateSQL}, + {&s.updateLatestEventNIDsStmt, updateLatestEventNIDsSQL}, + }.prepare(db) } func (s *roomStatements) insertRoomNID(roomID string) (types.RoomNID, error) { diff --git a/src/github.com/matrix-org/dendrite/roomserver/storage/state_block_table.go b/src/github.com/matrix-org/dendrite/roomserver/storage/state_block_table.go index 41cc210a8..440b539b1 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/storage/state_block_table.go +++ b/src/github.com/matrix-org/dendrite/roomserver/storage/state_block_table.go @@ -57,16 +57,12 @@ func (s *stateBlockStatements) prepare(db *sql.DB) (err error) { if err != nil { return } - if s.insertStateDataStmt, err = db.Prepare(insertStateDataSQL); err != nil { - return - } - if s.selectNextStateBlockNIDStmt, err = db.Prepare(selectNextStateBlockNIDSQL); err != nil { - return - } - if s.bulkSelectStateDataEntriesStmt, err = db.Prepare(bulkSelectStateDataEntriesSQL); err != nil { - return - } - return + + return statementList{ + {&s.insertStateDataStmt, insertStateDataSQL}, + {&s.selectNextStateBlockNIDStmt, selectNextStateBlockNIDSQL}, + {&s.bulkSelectStateDataEntriesStmt, bulkSelectStateDataEntriesSQL}, + }.prepare(db) } func (s *stateBlockStatements) bulkInsertStateData(stateBlockNID types.StateBlockNID, entries []types.StateEntry) error { diff --git a/src/github.com/matrix-org/dendrite/roomserver/storage/state_snapshot_table.go b/src/github.com/matrix-org/dendrite/roomserver/storage/state_snapshot_table.go index fec4bbbc1..0db9f4f5f 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/storage/state_snapshot_table.go +++ b/src/github.com/matrix-org/dendrite/roomserver/storage/state_snapshot_table.go @@ -52,13 +52,11 @@ func (s *stateSnapshotStatements) prepare(db *sql.DB) (err error) { if err != nil { return } - if s.insertStateStmt, err = db.Prepare(insertStateSQL); err != nil { - return - } - if s.bulkSelectStateBlockNIDsStmt, err = db.Prepare(bulkSelectStateBlockNIDsSQL); err != nil { - return - } - return + + return statementList{ + {&s.insertStateStmt, insertStateSQL}, + {&s.bulkSelectStateBlockNIDsStmt, bulkSelectStateBlockNIDsSQL}, + }.prepare(db) } func (s *stateSnapshotStatements) insertState(roomNID types.RoomNID, stateBlockNIDs []types.StateBlockNID) (stateNID types.StateSnapshotNID, err error) {