From c5ed820590256cac847a0fa80b03d023f48af7f3 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Mon, 29 Oct 2012 17:41:06 -0700 Subject: [PATCH] Create DB indexes for optimized queries. --- .../securesms/database/DatabaseFactory.java | 29 +++++++++++++++++-- .../database/MmsAddressDatabase.java | 4 +++ .../securesms/database/MmsDatabase.java | 7 +++++ .../securesms/database/PartDatabase.java | 4 +++ .../securesms/database/SmsDatabase.java | 8 ++++- .../securesms/database/ThreadDatabase.java | 4 +++ 6 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/org/thoughtcrime/securesms/database/DatabaseFactory.java b/src/org/thoughtcrime/securesms/database/DatabaseFactory.java index 80ba841fd2..e4ff5238ae 100644 --- a/src/org/thoughtcrime/securesms/database/DatabaseFactory.java +++ b/src/org/thoughtcrime/securesms/database/DatabaseFactory.java @@ -26,7 +26,8 @@ import org.thoughtcrime.securesms.crypto.MasterSecret; public class DatabaseFactory { private static final int INTRODUCED_IDENTITIES_VERSION = 2; - private static final int DATABASE_VERSION = 2; + private static final int INTRODUCED_INDEXES_VERSION = 3; + private static final int DATABASE_VERSION = 3; private static final String DATABASE_NAME = "messages.db"; private static final Object lock = new Object(); @@ -140,20 +141,42 @@ public class DatabaseFactory { } @Override - public void onCreate(SQLiteDatabase db) { + public void onCreate(SQLiteDatabase db) { db.execSQL(SmsDatabase.CREATE_TABLE); db.execSQL(MmsDatabase.CREATE_TABLE); db.execSQL(PartDatabase.CREATE_TABLE); db.execSQL(ThreadDatabase.CREATE_TABLE); db.execSQL(MmsAddressDatabase.CREATE_TABLE); db.execSQL(IdentityDatabase.CREATE_TABLE); + + executeStatements(db, SmsDatabase.CREATE_INDEXS); + executeStatements(db, MmsDatabase.CREATE_INDEXS); + executeStatements(db, PartDatabase.CREATE_INDEXS); + executeStatements(db, ThreadDatabase.CREATE_INDEXS); + executeStatements(db, MmsAddressDatabase.CREATE_INDEXS); + // db.execSQL(CanonicalAddress.CREATE_TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { - if (oldVersion < INTRODUCED_IDENTITIES_VERSION) + if (oldVersion < INTRODUCED_IDENTITIES_VERSION) { db.execSQL(IdentityDatabase.CREATE_TABLE); + } + + if (oldVersion < INTRODUCED_INDEXES_VERSION) { + executeStatements(db, SmsDatabase.CREATE_INDEXS); + executeStatements(db, MmsDatabase.CREATE_INDEXS); + executeStatements(db, PartDatabase.CREATE_INDEXS); + executeStatements(db, ThreadDatabase.CREATE_INDEXS); + executeStatements(db, MmsAddressDatabase.CREATE_INDEXS); + } } + + private void executeStatements(SQLiteDatabase db, String[] statements) { + for (String statement : statements) + db.execSQL(statement); + } + } } diff --git a/src/org/thoughtcrime/securesms/database/MmsAddressDatabase.java b/src/org/thoughtcrime/securesms/database/MmsAddressDatabase.java index cf3fdef44d..32260d1b3d 100644 --- a/src/org/thoughtcrime/securesms/database/MmsAddressDatabase.java +++ b/src/org/thoughtcrime/securesms/database/MmsAddressDatabase.java @@ -42,6 +42,10 @@ public class MmsAddressDatabase extends Database { MMS_ID + " INTEGER, " + TYPE + " INTEGER, " + ADDRESS + " TEXT, " + ADDRESS_CHARSET + " INTEGER);"; + public static final String[] CREATE_INDEXS = { + "CREATE INDEX IF NOT EXISTS mms_addresses_mms_id_index ON " + TABLE_NAME + " (" + MMS_ID + ");", + }; + public MmsAddressDatabase(Context context, SQLiteOpenHelper databaseHelper) { super(context, databaseHelper); } diff --git a/src/org/thoughtcrime/securesms/database/MmsDatabase.java b/src/org/thoughtcrime/securesms/database/MmsDatabase.java index 049ad65f36..84e261be1c 100644 --- a/src/org/thoughtcrime/securesms/database/MmsDatabase.java +++ b/src/org/thoughtcrime/securesms/database/MmsDatabase.java @@ -89,6 +89,13 @@ public class MmsDatabase extends Database { CONTENT_CLASS + " INTEGER, " + RESPONSE_TEXT + " TEXT, " + DELIVERY_TIME + " INTEGER, " + DELIVERY_REPORT + " INTEGER);"; + public static final String[] CREATE_INDEXS = { + "CREATE INDEX IF NOT EXISTS mms_thread_id_index ON " + TABLE_NAME + " (" + THREAD_ID + ");", + "CREATE INDEX IF NOT EXISTS mms_read_index ON " + TABLE_NAME + " (" + READ + ");", + "CREATE INDEX IF NOT EXISTS mms_read_and_thread_id_index ON " + TABLE_NAME + "(" + READ + "," + THREAD_ID + ");", + "CREATE INDEX IF NOT EXISTS mms_message_box_index ON " + TABLE_NAME + " (" + MESSAGE_BOX + ");" + }; + public MmsDatabase(Context context, SQLiteOpenHelper databaseHelper) { super(context, databaseHelper); } diff --git a/src/org/thoughtcrime/securesms/database/PartDatabase.java b/src/org/thoughtcrime/securesms/database/PartDatabase.java index 0ccb434ad5..048758aeff 100644 --- a/src/org/thoughtcrime/securesms/database/PartDatabase.java +++ b/src/org/thoughtcrime/securesms/database/PartDatabase.java @@ -66,6 +66,10 @@ public class PartDatabase extends Database { CONTENT_LOCATION + " TEXT, " + CONTENT_TYPE_START + " INTEGER, " + CONTENT_TYPE_TYPE + " TEXT, " + ENCRYPTED + " INTEGER, " + DATA + " TEXT);"; + public static final String[] CREATE_INDEXS = { + "CREATE INDEX IF NOT EXISTS part_mms_id_index ON " + TABLE_NAME + " (" + MMS_ID + ");" + }; + public PartDatabase(Context context, SQLiteOpenHelper databaseHelper) { super(context, databaseHelper); } diff --git a/src/org/thoughtcrime/securesms/database/SmsDatabase.java b/src/org/thoughtcrime/securesms/database/SmsDatabase.java index b01688e650..64cbf9a8c1 100644 --- a/src/org/thoughtcrime/securesms/database/SmsDatabase.java +++ b/src/org/thoughtcrime/securesms/database/SmsDatabase.java @@ -39,7 +39,6 @@ import java.util.Set; */ public class SmsDatabase extends Database { - public static final String TRANSPORT = "transport_type"; public static final String TABLE_NAME = "sms"; public static final String ID = "_id"; @@ -62,6 +61,13 @@ public class SmsDatabase extends Database { TYPE + " INTEGER, " + REPLY_PATH_PRESENT + " INTEGER, " + SUBJECT + " TEXT, " + BODY + " TEXT, " + SERVICE_CENTER + " TEXT);"; + public static final String[] CREATE_INDEXS = { + "CREATE INDEX IF NOT EXISTS sms_thread_id_index ON " + TABLE_NAME + " (" + THREAD_ID + ");", + "CREATE INDEX IF NOT EXISTS sms_read_index ON " + TABLE_NAME + " (" + READ + ");", + "CREATE INDEX IF NOT EXISTS sms_read_and_thread_id_index ON " + TABLE_NAME + "(" + READ + "," + THREAD_ID + ");", + "CREATE INDEX IF NOT EXISTS sms_type_index ON " + TABLE_NAME + " (" + TYPE + ");" + }; + public SmsDatabase(Context context, SQLiteOpenHelper databaseHelper) { super(context, databaseHelper); } diff --git a/src/org/thoughtcrime/securesms/database/ThreadDatabase.java b/src/org/thoughtcrime/securesms/database/ThreadDatabase.java index 1303e4cb34..d0ab6fe769 100644 --- a/src/org/thoughtcrime/securesms/database/ThreadDatabase.java +++ b/src/org/thoughtcrime/securesms/database/ThreadDatabase.java @@ -50,6 +50,10 @@ public class ThreadDatabase extends Database { READ + " INTEGER DEFAULT 1, " + TYPE + " INTEGER DEFAULT 0, " + ERROR + " INTEGER DEFAULT 0, " + HAS_ATTACHMENT + " INTEGER DEFAULT 0);"; + public static final String[] CREATE_INDEXS = { + "CREATE INDEX IF NOT EXISTS thread_recipient_ids_index ON " + TABLE_NAME + " (" + RECIPIENT_IDS + ");", + }; + public ThreadDatabase(Context context, SQLiteOpenHelper databaseHelper) { super(context, databaseHelper); }