diff options
author | Christian Schneppe <christian@pix-art.de> | 2019-06-22 15:38:54 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2019-06-22 15:38:54 +0200 |
commit | 9e3a701cb0d84f1314c51dd52b8cd2c311d8b873 (patch) | |
tree | 2557592ad523b867428f386fa49efeb8cca1baea /src/main | |
parent | c71aa059f9a344e052f6218132a156acf360be21 (diff) |
performance improvement of message expiry
Diffstat (limited to 'src/main')
3 files changed, 53 insertions, 5 deletions
diff --git a/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java b/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java index bef367b0a..243db470a 100644 --- a/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java +++ b/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java @@ -28,11 +28,14 @@ import java.security.cert.CertificateEncodingException; import java.security.cert.CertificateException; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.UUID; @@ -1052,16 +1055,55 @@ public class DatabaseBackend extends SQLiteOpenHelper { Log.d(Config.LOGTAG, "deleted " + num + " messages for " + conversation.getJid().asBareJid() + " in " + (SystemClock.elapsedRealtime() - start) + "ms"); } - public void expireOldMessages(long timestamp) { + public long countExpireOldMessages(long timestamp) { long start = SystemClock.elapsedRealtime(); final String[] args = {String.valueOf(timestamp)}; SQLiteDatabase db = this.getReadableDatabase(); db.beginTransaction(); + long num = DatabaseUtils.queryNumEntries(db, Message.TABLENAME, "timeSent<?", args); + db.setTransactionSuccessful(); + db.endTransaction(); + Log.d(Config.LOGTAG, "found " + num + " expired messages in " + (SystemClock.elapsedRealtime() - start) + "ms"); + return num; + } + + public long getOldestMessages() { + Cursor cursor = null; + try { + SQLiteDatabase db = this.getReadableDatabase(); + db.beginTransaction(); + cursor = db.rawQuery("select timeSent from " + Message.TABLENAME + " ORDER BY " + Message.TIME_SENT + " ASC limit 1", null); + db.setTransactionSuccessful(); + db.endTransaction(); + if (cursor.getCount() == 0) { + return 0; + } else { + cursor.moveToFirst(); + return cursor.getLong(0); + } + } catch (Exception e) { + return 0; + } finally { + if (cursor != null) { + cursor.close(); + } + } + } + + public int expireOldMessages(long timestamp) { + long start = SystemClock.elapsedRealtime(); + int num = 0; + if (countExpireOldMessages(timestamp) >= 1) { + final String[] args = {String.valueOf(timestamp)}; + SQLiteDatabase db = this.getReadableDatabase(); + db.beginTransaction(); db.delete("messages_index", "uuid in (select uuid from messages where timeSent<?)", args); - int num = db.delete(Message.TABLENAME, "timeSent<?", args); + num = db.delete(Message.TABLENAME, "timeSent<?", args); db.setTransactionSuccessful(); db.endTransaction(); + } Log.d(Config.LOGTAG, "deleted " + num + " expired messages in " + (SystemClock.elapsedRealtime() - start) + "ms"); + return num; } public MamReference getLastMessageReceived(Account account) { diff --git a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java index b57d9846f..c0c6c91d9 100644 --- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java +++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java @@ -79,6 +79,7 @@ import java.util.Set; import java.util.WeakHashMap; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; @@ -4871,16 +4872,21 @@ public class XmppConnectionService extends Service { private void expireOldMessages(long timestamp, boolean stepped) { if (stepped) { - final int year = 365; + final long expiredMessagesCount = databaseBackend.countExpireOldMessages(timestamp); + final long days = TimeUnit.MILLISECONDS.toDays(Calendar.getInstance().getTimeInMillis() - databaseBackend.getOldestMessages()); final long day = (long) 24 * 60 * 60 * 1000; int count = 0; - while (count <= year) { + int messagesCount = 0; + while (count <= days) { try { - databaseBackend.expireOldMessages(timestamp - ((year - count) * day)); + messagesCount += databaseBackend.expireOldMessages(timestamp - ((days - count) * day)); } catch (Exception e) { e.printStackTrace(); } count++; + if (expiredMessagesCount == messagesCount) { + break; + } } } else { databaseBackend.expireOldMessages(timestamp); diff --git a/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java b/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java index 4cab09d99..28ea68d4e 100644 --- a/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java +++ b/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java |