From 5e9d9a491c71f338c1ef1430647764e14fd7e2e4 Mon Sep 17 00:00:00 2001 From: Jeffrey Han Date: Sat, 7 Mar 2015 18:05:18 -0500 Subject: [PATCH] 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 --- src/itdelatrisu/opsu/OsuParser.java | 8 +++++--- src/itdelatrisu/opsu/db/OsuDB.java | 19 ++++++++++++++++--- src/itdelatrisu/opsu/db/ScoreDB.java | 3 ++- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/itdelatrisu/opsu/OsuParser.java b/src/itdelatrisu/opsu/OsuParser.java index d790a557..67d2f515 100644 --- a/src/itdelatrisu/opsu/OsuParser.java +++ b/src/itdelatrisu/opsu/OsuParser.java @@ -148,9 +148,11 @@ public class OsuParser { stringdb = new HashMap(); // add entries to database - updatingDatabase = true; - OsuDB.insert(parsedOsuFiles); - updatingDatabase = false; + if (!parsedOsuFiles.isEmpty()) { + updatingDatabase = true; + OsuDB.insert(parsedOsuFiles); + updatingDatabase = false; + } currentFile = null; currentDirectoryIndex = -1; diff --git a/src/itdelatrisu/opsu/db/OsuDB.java b/src/itdelatrisu/opsu/db/OsuDB.java index 4276c2b3..3ff3ccc7 100644 --- a/src/itdelatrisu/opsu/db/OsuDB.java +++ b/src/itdelatrisu/opsu/db/OsuDB.java @@ -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 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); diff --git a/src/itdelatrisu/opsu/db/ScoreDB.java b/src/itdelatrisu/opsu/db/ScoreDB.java index 7b40c8e2..00f77579 100644 --- a/src/itdelatrisu/opsu/db/ScoreDB.java +++ b/src/itdelatrisu/opsu/db/ScoreDB.java @@ -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);