1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-30 13:12:25 -05:00

Remove O(n) deep SQL queries per folderlist by cleaning up the folder "exists" check on Local message stores

* Replace the extra SQL query before each folder 'open' with an exception if the open fails. (Optimize for the common case, not the exception)
This commit is contained in:
Jesse Vincent 2008-11-01 21:36:23 +00:00
parent cd7a7a67fb
commit 046943b340

View File

@ -141,6 +141,8 @@ public class LocalStore extends Store {
public Folder[] getPersonalNamespaces() throws MessagingException { public Folder[] getPersonalNamespaces() throws MessagingException {
ArrayList<Folder> folders = new ArrayList<Folder>(); ArrayList<Folder> folders = new ArrayList<Folder>();
Cursor cursor = null; Cursor cursor = null;
try { try {
cursor = mDb.rawQuery("SELECT name FROM folders", null); cursor = mDb.rawQuery("SELECT name FROM folders", null);
while (cursor.moveToNext()) { while (cursor.moveToNext()) {
@ -334,7 +336,6 @@ public class LocalStore extends Store {
return; return;
} }
if (!exists()) { if (!exists()) {
create(FolderType.HOLDS_MESSAGES);
} }
Cursor cursor = null; Cursor cursor = null;
try { try {
@ -343,10 +344,18 @@ public class LocalStore extends Store {
new String[] { new String[] {
mName mName
}); });
cursor.moveToFirst(); if ( cursor.getCount() == 0 ) {
mFolderId = cursor.getInt(0); // Calling exists on open is a little expensive. Instead, just handle it when we don't find it.
mUnreadMessageCount = cursor.getInt(1); create(FolderType.HOLDS_MESSAGES);
mVisibleLimit = cursor.getInt(2); open(mode);
}
cursor.moveToFirst();
mFolderId = cursor.getInt(0);
mUnreadMessageCount = cursor.getInt(1);
mVisibleLimit = cursor.getInt(2);
} }
finally { finally {
if (cursor != null) { if (cursor != null) {
@ -372,7 +381,19 @@ public class LocalStore extends Store {
@Override @Override
public boolean exists() throws MessagingException { public boolean exists() throws MessagingException {
return Utility.arrayContains(getPersonalNamespaces(), this); Cursor cursor = null;
int mFolderId = 0 ;
try {
cursor = mDb.rawQuery("SELECT id FROM folders " + "where folders.name = ?", new String[] { this.getName() });
cursor.moveToFirst();
mFolderId = cursor.getInt(0);
}
finally {
if (cursor != null) {
cursor.close();
}
}
return (mFolderId > 0 )? true : false;
} }
@Override @Override