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>(); stringdb = new HashMap<String, String>();
// add entries to database // add entries to database
updatingDatabase = true; if (!parsedOsuFiles.isEmpty()) {
OsuDB.insert(parsedOsuFiles); updatingDatabase = true;
updatingDatabase = false; OsuDB.insert(parsedOsuFiles);
updatingDatabase = false;
}
currentFile = null; currentFile = null;
currentDirectoryIndex = -1; currentDirectoryIndex = -1;

View File

@ -107,7 +107,12 @@ public class OsuDB {
"); " + "); " +
"CREATE TABLE IF NOT EXISTS info (" + "CREATE TABLE IF NOT EXISTS info (" +
"key TEXT NOT NULL UNIQUE, value TEXT" + "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); stmt.executeUpdate(sql);
} catch (SQLException e) { } catch (SQLException e) {
ErrorHandler.error("Could not create beatmap database.", e, true); ErrorHandler.error("Could not create beatmap database.", e, true);
@ -172,20 +177,28 @@ public class OsuDB {
* @param batch a list of OsuFile objects * @param batch a list of OsuFile objects
*/ */
public static void insert(List<OsuFile> batch) { public static void insert(List<OsuFile> batch) {
try { try (Statement stmt = connection.createStatement()) {
// turn off auto-commit mode // turn off auto-commit mode
boolean autoCommit = connection.getAutoCommit(); boolean autoCommit = connection.getAutoCommit();
connection.setAutoCommit(false); connection.setAutoCommit(false);
// drop indexes
String sql = "DROP INDEX IF EXISTS idx";
stmt.executeUpdate(sql);
// batch insert // batch insert
for (OsuFile osu : batch) { for (OsuFile osu : batch) {
setStatementFields(insertStmt, osu); setStatementFields(insertStmt, osu);
insertStmt.addBatch(); insertStmt.addBatch();
} }
insertStmt.executeBatch(); insertStmt.executeBatch();
connection.commit();
// re-create indexes
sql = "CREATE INDEX idx ON beatmaps (dir, file)";
stmt.executeUpdate(sql);
// restore previous auto-commit mode // restore previous auto-commit mode
connection.commit();
connection.setAutoCommit(autoCommit); connection.setAutoCommit(autoCommit);
} catch (SQLException e) { } catch (SQLException e) {
ErrorHandler.error("Failed to add beatmaps to database.", e, true); ErrorHandler.error("Failed to add beatmaps to database.", e, true);

View File

@ -114,7 +114,8 @@ public class ScoreDB {
"combo INTEGER, " + "combo INTEGER, " +
"perfect BOOLEAN, " + "perfect BOOLEAN, " +
"mods INTEGER" + "mods INTEGER" +
")"; ");" +
"CREATE INDEX IF NOT EXISTS idx ON scores (MID, MSID, title, artist, creator, version);";
stmt.executeUpdate(sql); stmt.executeUpdate(sql);
} catch (SQLException e) { } catch (SQLException e) {
ErrorHandler.error("Could not create score database.", e, true); ErrorHandler.error("Could not create score database.", e, true);