SQLite optimizations. (addressing #34)
- Added indexes. - Only call OsuDB.insert() if necessary. - Drop/recreate indexes for batch inserts. - Added pragmas for "locking_mode" and "journal_mode". Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
@@ -107,7 +107,12 @@ public class OsuDB {
|
||||
"); " +
|
||||
"CREATE TABLE IF NOT EXISTS info (" +
|
||||
"key TEXT NOT NULL UNIQUE, value TEXT" +
|
||||
")";
|
||||
"); " +
|
||||
"CREATE INDEX IF NOT EXISTS idx ON beatmaps (dir, file); " +
|
||||
|
||||
// extra optimizations
|
||||
"PRAGMA locking_mode = EXCLUSIVE; " +
|
||||
"PRAGMA journal_mode = WAL;";
|
||||
stmt.executeUpdate(sql);
|
||||
} catch (SQLException e) {
|
||||
ErrorHandler.error("Could not create beatmap database.", e, true);
|
||||
@@ -172,20 +177,28 @@ public class OsuDB {
|
||||
* @param batch a list of OsuFile objects
|
||||
*/
|
||||
public static void insert(List<OsuFile> batch) {
|
||||
try {
|
||||
try (Statement stmt = connection.createStatement()) {
|
||||
// turn off auto-commit mode
|
||||
boolean autoCommit = connection.getAutoCommit();
|
||||
connection.setAutoCommit(false);
|
||||
|
||||
// drop indexes
|
||||
String sql = "DROP INDEX IF EXISTS idx";
|
||||
stmt.executeUpdate(sql);
|
||||
|
||||
// batch insert
|
||||
for (OsuFile osu : batch) {
|
||||
setStatementFields(insertStmt, osu);
|
||||
insertStmt.addBatch();
|
||||
}
|
||||
insertStmt.executeBatch();
|
||||
connection.commit();
|
||||
|
||||
// re-create indexes
|
||||
sql = "CREATE INDEX idx ON beatmaps (dir, file)";
|
||||
stmt.executeUpdate(sql);
|
||||
|
||||
// restore previous auto-commit mode
|
||||
connection.commit();
|
||||
connection.setAutoCommit(autoCommit);
|
||||
} catch (SQLException e) {
|
||||
ErrorHandler.error("Failed to add beatmaps to database.", e, true);
|
||||
|
||||
@@ -114,7 +114,8 @@ public class ScoreDB {
|
||||
"combo INTEGER, " +
|
||||
"perfect BOOLEAN, " +
|
||||
"mods INTEGER" +
|
||||
")";
|
||||
");" +
|
||||
"CREATE INDEX IF NOT EXISTS idx ON scores (MID, MSID, title, artist, creator, version);";
|
||||
stmt.executeUpdate(sql);
|
||||
} catch (SQLException e) {
|
||||
ErrorHandler.error("Could not create score database.", e, true);
|
||||
|
||||
Reference in New Issue
Block a user