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:
Jeffrey Han 2015-03-07 18:05:18 -05:00
parent 4718cb9aa5
commit 5e9d9a491c
3 changed files with 23 additions and 7 deletions

View File

@ -148,9 +148,11 @@ public class OsuParser {
stringdb = new HashMap<String, String>();
// 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;

View File

@ -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);

View File

@ -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);