From fa616844241b84602be1b3307efb8757230935e7 Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Wed, 20 Apr 2016 20:44:32 +0800 Subject: [PATCH] fixed database trimming --- twidere/src/debug/res/values/defaults.xml | 4 +++ .../twidere/util/DataStoreUtils.java | 36 +++++++++---------- twidere/src/main/res/values/defaults.xml | 1 + .../src/main/res/xml/preferences_storage.xml | 2 +- 4 files changed, 24 insertions(+), 19 deletions(-) create mode 100644 twidere/src/debug/res/values/defaults.xml diff --git a/twidere/src/debug/res/values/defaults.xml b/twidere/src/debug/res/values/defaults.xml new file mode 100644 index 000000000..670c84887 --- /dev/null +++ b/twidere/src/debug/res/values/defaults.xml @@ -0,0 +1,4 @@ + + + 10 + \ No newline at end of file diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/DataStoreUtils.java b/twidere/src/main/java/org/mariotaku/twidere/util/DataStoreUtils.java index 596861701..f0700046f 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/DataStoreUtils.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/DataStoreUtils.java @@ -487,7 +487,7 @@ public class DataStoreUtils implements Constants { } public static int getActivitiesCount(final Context context, final Uri uri, final long compare, - String compareColumn, boolean greaterThan, UserKey... accountKeys) { + String compareColumn, boolean greaterThan, UserKey... accountKeys) { if (context == null) return 0; if (accountKeys == null) { accountKeys = getActivatedAccountKeys(context); @@ -783,46 +783,47 @@ public class DataStoreUtils implements Constants { continue; } final String table = getTableNameByUri(uri); - final Expression accountWhere = Expression.equalsArgs(AccountSupportColumns.ACCOUNT_KEY); final SQLSelectQuery.Builder qb = new SQLSelectQuery.Builder(); qb.select(new Column(Statuses._ID)) .from(new Tables(table)) - .where(accountWhere) - .orderBy(new OrderBy(Statuses.STATUS_ID, false)) + .where(Expression.equalsArgs(Statuses.ACCOUNT_KEY)) + .orderBy(new OrderBy(Statuses.POSITION_KEY, false)) .limit(itemLimit); - final Expression where = Expression.and(Expression.lesserThan(new Column(Statuses._ID), - SQLQueryBuilder.select(SQLFunctions.MIN(new Column(Statuses._ID))).from(qb.build()).build()), - accountWhere); + final Expression where = Expression.and( + Expression.notIn(new Column(Statuses._ID), qb.build()), + Expression.equalsArgs(Statuses.ACCOUNT_KEY) + ); final String[] whereArgs = {String.valueOf(accountKey), String.valueOf(accountKey)}; resolver.delete(uri, where.getSQL(), whereArgs); } for (final Uri uri : ACTIVITIES_URIS) { final String table = getTableNameByUri(uri); - final Expression accountWhere = Expression.equalsArgs(Accounts.ACCOUNT_KEY); final SQLSelectQuery.Builder qb = new SQLSelectQuery.Builder(); qb.select(new Column(Activities._ID)) .from(new Tables(table)) - .where(accountWhere) + .where(Expression.equalsArgs(Activities.ACCOUNT_KEY)) .orderBy(new OrderBy(Activities.TIMESTAMP, false)) .limit(itemLimit); - final Expression where = Expression.and(Expression.lesserThan(new Column(Activities._ID), - SQLQueryBuilder.select(SQLFunctions.MIN(new Column(Activities._ID))) - .from(qb.build()).build()), accountWhere); + final Expression where = Expression.and( + Expression.notIn(new Column(Activities._ID), qb.build()), + Expression.equalsArgs(Activities.ACCOUNT_KEY) + ); final String[] whereArgs = {String.valueOf(accountKey), String.valueOf(accountKey)}; resolver.delete(uri, where.getSQL(), whereArgs); } for (final Uri uri : DIRECT_MESSAGES_URIS) { final String table = getTableNameByUri(uri); - final Expression accountWhere = Expression.equalsArgs(Accounts.ACCOUNT_KEY); + final Expression accountWhere = Expression.equalsArgs(DirectMessages.ACCOUNT_KEY); final SQLSelectQuery.Builder qb = new SQLSelectQuery.Builder(); qb.select(new Column(DirectMessages._ID)) .from(new Tables(table)) .where(accountWhere) .orderBy(new OrderBy(DirectMessages.MESSAGE_ID, false)) .limit(itemLimit * 10); - final Expression where = Expression.and(Expression.lesserThan(new Column(DirectMessages._ID), - SQLQueryBuilder.select(SQLFunctions.MIN(new Column(DirectMessages._ID))) - .from(qb.build()).build()), accountWhere); + final Expression where = Expression.and( + Expression.notIn(new Column(DirectMessages._ID), qb.build()), + Expression.equalsArgs(DirectMessages.ACCOUNT_KEY) + ); final String[] whereArgs = {String.valueOf(accountKey), String.valueOf(accountKey)}; resolver.delete(uri, where.getSQL(), whereArgs); } @@ -836,8 +837,7 @@ public class DataStoreUtils implements Constants { .from(new Tables(table)) .orderBy(new OrderBy(BaseColumns._ID, false)) .limit(itemLimit * 20); - final Expression where = Expression.lesserThan(new Column(BaseColumns._ID), - SQLQueryBuilder.select(SQLFunctions.MIN(new Column(BaseColumns._ID))).from(qb.build()).build()); + final Expression where = Expression.notIn(new Column(BaseColumns._ID), qb.build()); resolver.delete(uri, where.getSQL(), null); } } diff --git a/twidere/src/main/res/values/defaults.xml b/twidere/src/main/res/values/defaults.xml index e1bd52ada..1c93a0dbc 100644 --- a/twidere/src/main/res/values/defaults.xml +++ b/twidere/src/main/res/values/defaults.xml @@ -5,5 +5,6 @@ normal round 1 + 50 \ No newline at end of file diff --git a/twidere/src/main/res/xml/preferences_storage.xml b/twidere/src/main/res/xml/preferences_storage.xml index cf49bcc8c..b5e80be4e 100644 --- a/twidere/src/main/res/xml/preferences_storage.xml +++ b/twidere/src/main/res/xml/preferences_storage.xml @@ -16,7 +16,7 @@ android:summary="@string/database_item_limit_summary" android:title="@string/database_item_limit" app:max="500" - app:min="50" + app:min="@integer/min_database_item_limit" app:step="10"/>