diff options
author | Dmitri Plotnikov <dplotnikov@google.com> | 2011-02-08 16:10:07 -0800 |
---|---|---|
committer | Dmitri Plotnikov <dplotnikov@google.com> | 2011-02-08 16:10:07 -0800 |
commit | 05e50fbf9809bf04eceec3d2a2753630dc4f9315 (patch) | |
tree | ce38cc3c7ad7a66bb9d1d80caeb15493f6e9825a /src/com/android/providers/contacts/SearchIndexManager.java | |
parent | 6d9702cec82fd27a1c3093c64df9dcc22744899a (diff) | |
download | packages_providers_ContactsProvider-05e50fbf9809bf04eceec3d2a2753630dc4f9315.zip packages_providers_ContactsProvider-05e50fbf9809bf04eceec3d2a2753630dc4f9315.tar.gz packages_providers_ContactsProvider-05e50fbf9809bf04eceec3d2a2753630dc4f9315.tar.bz2 |
Full text search: building full index for all contacts
Bug: 2078420
Change-Id: Ief6db4f4ecc0b6e5adb37a0654a72383099dc138
Diffstat (limited to 'src/com/android/providers/contacts/SearchIndexManager.java')
-rw-r--r-- | src/com/android/providers/contacts/SearchIndexManager.java | 82 |
1 files changed, 70 insertions, 12 deletions
diff --git a/src/com/android/providers/contacts/SearchIndexManager.java b/src/com/android/providers/contacts/SearchIndexManager.java index 8e238bc..80082a6 100644 --- a/src/com/android/providers/contacts/SearchIndexManager.java +++ b/src/com/android/providers/contacts/SearchIndexManager.java @@ -23,7 +23,11 @@ import com.android.providers.contacts.ContactsDatabaseHelper.Tables; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; +import android.os.SystemClock; +import android.provider.ContactsContract.Contacts; import android.provider.ContactsContract.Data; +import android.provider.ContactsContract.ProviderStatus; +import android.provider.ContactsContract.RawContacts; import android.text.TextUtils; import android.util.Log; @@ -34,9 +38,11 @@ import java.util.Set; * Maintains a search index for comprehensive contact search. */ public class SearchIndexManager { - private static final String TAG = "ContactsFTS"; + private static final String PROPERTY_SEARCH_INDEX_VERSION = "search_index"; + private static final int SEARCH_INDEX_VERSION = 1; + private static final class ContactIndexQuery { public static final String[] COLUMNS = { Data.CONTACT_ID, @@ -172,6 +178,40 @@ public class SearchIndexManager { mDbHelper = (ContactsDatabaseHelper) mContactsProvider.getDatabaseHelper(); } + public void updateIndex() { + if (getSearchIndexVersion() == SEARCH_INDEX_VERSION) { + return; + } + SQLiteDatabase db = mDbHelper.getWritableDatabase(); + db.beginTransaction(); + try { + if (getSearchIndexVersion() != SEARCH_INDEX_VERSION) { + rebuildIndex(db); + setSearchIndexVersion(SEARCH_INDEX_VERSION); + db.setTransactionSuccessful(); + } + } finally { + db.endTransaction(); + } + } + + private void rebuildIndex(SQLiteDatabase db) { + mContactsProvider.setProviderStatus(ProviderStatus.STATUS_UPGRADING); + long start = SystemClock.currentThreadTimeMillis(); + int count = 0; + try { + mDbHelper.createSearchIndexTable(db); + count = buildIndex(db, RawContacts.CONTACT_ID + " IN " + + "(SELECT " + Contacts._ID + " FROM " + Tables.DEFAULT_DIRECTORY + ")", false); + } finally { + mContactsProvider.setProviderStatus(ProviderStatus.STATUS_NORMAL); + + long end = SystemClock.currentThreadTimeMillis(); + Log.i(TAG, "Rebuild contact search index in " + (end - start) + "ms, " + + count + " contacts"); + } + } + public void updateIndexForRawContacts(Set<Long> rawContactIds) { mSb.setLength(0); mSb.append(Data.RAW_CONTACT_ID + " IN ("); @@ -181,9 +221,13 @@ public class SearchIndexManager { mSb.setLength(mSb.length() - 1); mSb.append(')'); - SQLiteDatabase db = mDbHelper.getWritableDatabase(); + buildIndex(mDbHelper.getWritableDatabase(), mSb.toString(), true); + } + + private int buildIndex(SQLiteDatabase db, String selection, boolean replace) { + int count = 0; Cursor cursor = db.query(Tables.DATA_JOIN_MIMETYPE_RAW_CONTACTS, - ContactIndexQuery.COLUMNS, mSb.toString(), null, null, null, + ContactIndexQuery.COLUMNS, selection, null, null, null, Data.CONTACT_ID + ", " + DataColumns.MIMETYPE_ID + ", " + Data.IS_SUPER_PRIMARY + ", " + DataColumns.CONCRETE_ID); mIndexBuilder.setCursor(cursor); @@ -194,7 +238,8 @@ public class SearchIndexManager { long contactId = cursor.getLong(0); if (contactId != currentContactId) { if (currentContactId != -1) { - saveContactIndex(db, currentContactId, mIndexBuilder); + saveContactIndex(db, currentContactId, mIndexBuilder, replace); + count++; } currentContactId = contactId; mIndexBuilder.reset(); @@ -207,25 +252,38 @@ public class SearchIndexManager { } } if (currentContactId != -1) { - saveContactIndex(db, currentContactId, mIndexBuilder); + saveContactIndex(db, currentContactId, mIndexBuilder, replace); + count++; } } finally { cursor.close(); } + return count; } - private void saveContactIndex(SQLiteDatabase db, long contactId, IndexBuilder builder) { - Log.d(TAG, "INDEX: " + contactId + ": " + builder.toString()); - + private void saveContactIndex( + SQLiteDatabase db, long contactId, IndexBuilder builder, boolean replace) { mValues.clear(); mValues.put(SearchIndexColumns.CONTENT, builder.getContent()); mValues.put(SearchIndexColumns.TOKENS, builder.getTokens()); - mSelectionArgs1[0] = String.valueOf(contactId); - int count = db.update(Tables.SEARCH_INDEX, mValues, - SearchIndexColumns.CONTACT_ID + "=CAST(? AS int)", mSelectionArgs1); - if (count == 0) { + if (replace) { + mSelectionArgs1[0] = String.valueOf(contactId); + int count = db.update(Tables.SEARCH_INDEX, mValues, + SearchIndexColumns.CONTACT_ID + "=CAST(? AS int)", mSelectionArgs1); + if (count == 0) { + mValues.put(SearchIndexColumns.CONTACT_ID, contactId); + db.insert(Tables.SEARCH_INDEX, null, mValues); + } + } else { mValues.put(SearchIndexColumns.CONTACT_ID, contactId); db.insert(Tables.SEARCH_INDEX, null, mValues); } } + private int getSearchIndexVersion() { + return Integer.parseInt(mDbHelper.getProperty(PROPERTY_SEARCH_INDEX_VERSION, "0")); + } + + private void setSearchIndexVersion(int version) { + mDbHelper.setProperty(PROPERTY_SEARCH_INDEX_VERSION, String.valueOf(version)); + } } |