diff --git a/Sources/MPDB.swift b/Sources/MPDB.swift index b5953e79..e5ab9e65 100644 --- a/Sources/MPDB.swift +++ b/Sources/MPDB.swift @@ -60,7 +60,24 @@ class MPDB { close() } else { Logger.info(message: "Successfully opened connection to database at path: \(dbPath)") - createTables() + if let db = connection { + let pragmaString = "PRAGMA journal_mode=WAL;" + var pragmaStatement: OpaquePointer? + if sqlite3_prepare_v2(db, pragmaString, -1, &pragmaStatement, nil) == SQLITE_OK { + if sqlite3_step(pragmaStatement) == SQLITE_ROW { + let res = String(cString: sqlite3_column_text(pragmaStatement, 0)) + Logger.info(message: "SQLite journal mode set to \(res)") + } else { + logSqlError(message: "Failed to enable journal_mode=WAL") + } + } else { + logSqlError(message: "PRAGMA journal_mode=WAL statement could not be prepared") + } + sqlite3_finalize(pragmaStatement) + } else { + reconnect() + } + createTablesAndIndexes() } } } @@ -108,10 +125,34 @@ class MPDB { } } - private func createTables() { + private func createIndexFor(_ persistenceType: PersistenceType) { + if let db = connection { + let tableName = tableNameFor(persistenceType) + let indexName = "idx_\(persistenceType)_time" + let createIndexString = "CREATE INDEX IF NOT EXISTS \(indexName) ON \(tableName) (time);" + var createIndexStatement: OpaquePointer? + if sqlite3_prepare_v2(db, createIndexString, -1, &createIndexStatement, nil) == SQLITE_OK { + if sqlite3_step(createIndexStatement) == SQLITE_DONE { + Logger.info(message: "\(indexName) index created") + } else { + logSqlError(message: "\(indexName) index creation failed") + } + } else { + logSqlError(message: "CREATE statement for index \(indexName) could not be prepared") + } + sqlite3_finalize(createIndexStatement) + } else { + reconnect() + } + } + + private func createTablesAndIndexes() { createTableFor(PersistenceType.events) + createIndexFor(PersistenceType.events) createTableFor(PersistenceType.people) + createIndexFor(PersistenceType.people) createTableFor(PersistenceType.groups) + createIndexFor(PersistenceType.groups) } func insertRow(_ persistenceType: PersistenceType, data: Data, flag: Bool = false) {