performance improvements

This commit is contained in:
Mariotaku Lee 2017-06-08 18:45:21 +08:00
parent 1376910711
commit bbb14674c2
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
2 changed files with 21 additions and 14 deletions

View File

@ -470,6 +470,7 @@ class QuickSearchBarActivity : BaseActivity(), OnClickListener, LoaderCallbacks<
override fun swapCursor(newCursor: Cursor?): Cursor? { override fun swapCursor(newCursor: Cursor?): Cursor? {
indices = newCursor?.let(SuggestionItem::Indices) indices = newCursor?.let(SuggestionItem::Indices)
removedPositions.clear()
return super.swapCursor(newCursor) return super.swapCursor(newCursor)
} }

View File

@ -25,7 +25,10 @@ import android.database.MergeCursor
import android.net.Uri import android.net.Uri
import android.text.TextUtils import android.text.TextUtils
import org.mariotaku.ktextension.mapToArray import org.mariotaku.ktextension.mapToArray
import org.mariotaku.sqliteqb.library.* import org.mariotaku.sqliteqb.library.Columns
import org.mariotaku.sqliteqb.library.Expression
import org.mariotaku.sqliteqb.library.OrderBy
import org.mariotaku.sqliteqb.library.SQLConstants
import org.mariotaku.twidere.TwidereConstants.* import org.mariotaku.twidere.TwidereConstants.*
import org.mariotaku.twidere.model.UserKey import org.mariotaku.twidere.model.UserKey
import org.mariotaku.twidere.provider.TwidereDataStore.* import org.mariotaku.twidere.provider.TwidereDataStore.*
@ -107,15 +110,16 @@ object SuggestionsCursorCreator {
} else { } else {
val queryTrimmed = query.replace("_", "^_").substringAfter("@") val queryTrimmed = query.replace("_", "^_").substringAfter("@")
if (!hasName(db, queryTrimmed)) { val usersCursor = getUsersCursor(db, manager, nonNullProjection, accountKey, filterHost,
filterType, query, queryTrimmed, 10)
if (!usersCursor.hasName(queryTrimmed)) {
val m = PATTERN_SCREEN_NAME.matcher(query) val m = PATTERN_SCREEN_NAME.matcher(query)
if (m.matches()) { if (m.matches()) {
val screenName = m.group(1) val screenName = m.group(1)
cursors.add(getScreenNameCursor(nonNullProjection, screenName)) cursors.add(getScreenNameCursor(nonNullProjection, screenName))
} }
} }
cursors.add(getUsersCursor(db, manager, nonNullProjection, accountKey, filterHost, cursors.add(usersCursor)
filterType, query, queryTrimmed))
} }
return MergeCursor(cursors.toTypedArray()) return MergeCursor(cursors.toTypedArray())
} }
@ -159,7 +163,7 @@ object SuggestionsCursorCreator {
private fun getUsersCursor(db: SQLiteDatabaseWrapper, manager: UserColorNameManager, private fun getUsersCursor(db: SQLiteDatabaseWrapper, manager: UserColorNameManager,
projection: Array<String>, accountKey: UserKey, filterHost: String?, filterType: String?, projection: Array<String>, accountKey: UserKey, filterHost: String?, filterType: String?,
query: String, queryTrimmed: String): Cursor { query: String, queryTrimmed: String, limit: Int = 0): Cursor {
val nicknameKeys = Utils.getMatchedNicknameKeys(query, manager) val nicknameKeys = Utils.getMatchedNicknameKeys(query, manager)
val usersSelection = Expression.or( val usersSelection = Expression.or(
Expression.inArgs(Columns.Column(CachedUsers.USER_KEY), nicknameKeys.size), Expression.inArgs(Columns.Column(CachedUsers.USER_KEY), nicknameKeys.size),
@ -172,7 +176,8 @@ object SuggestionsCursorCreator {
val orderBy = OrderBy(order, ascending) val orderBy = OrderBy(order, ascending)
val usersProjection = projection.mapToArray { suggestionUsersProjectionMap[it]!! } val usersProjection = projection.mapToArray { suggestionUsersProjectionMap[it]!! }
val usersQuery = CachedUsersQueryBuilder.withScore(usersProjection, val usersQuery = CachedUsersQueryBuilder.withScore(usersProjection,
usersSelection, selectionArgs, orderBy.sql, accountKey, filterHost, filterType, 0) usersSelection, selectionArgs, orderBy.sql, accountKey, filterHost, filterType,
limit)
return db.rawQuery(usersQuery.first.sql, usersQuery.second) return db.rawQuery(usersQuery.first.sql, usersQuery.second)
} }
@ -209,14 +214,15 @@ object SuggestionsCursorCreator {
return cursor return cursor
} }
private fun hasName(db: SQLiteDatabaseWrapper, queryTrimmed: String): Boolean { private fun Cursor.hasName(queryTrimmed: String): Boolean {
val exactUserSelection = Expression.or(Expression.likeRaw(Columns.Column(CachedUsers.SCREEN_NAME), "?", "^")) val valueIdx = getColumnIndex(Suggestions.VALUE)
val exactUserCursor = db.query(CachedUsers.TABLE_NAME, arrayOf(SQLFunctions.COUNT()), moveToFirst()
exactUserSelection.sql, arrayOf(queryTrimmed), null, null, null, "1") while (!isAfterLast) {
try { if (queryTrimmed.equals(getString(valueIdx), true)) {
return exactUserCursor.moveToPosition(0) && exactUserCursor.getInt(0) > 0 return true
} finally { }
exactUserCursor.close() moveToNext()
} }
return false
} }
} }