From a216f82b3584dfc390e137d96143204502d0a500 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 3 Nov 2020 09:30:26 +0100 Subject: [PATCH] Avoid code duplication and log total count --- .../crypto/store/db/RealmCryptoStore.kt | 17 +---- .../database/tools/RealmDebugTools.kt | 64 ++++++++----------- .../sdk/internal/session/DefaultSession.kt | 19 +----- 3 files changed, 31 insertions(+), 69 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt index ec1a5d73a1..72274aa70a 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt @@ -87,6 +87,7 @@ import org.matrix.android.sdk.internal.crypto.store.db.query.get import org.matrix.android.sdk.internal.crypto.store.db.query.getById import org.matrix.android.sdk.internal.crypto.store.db.query.getOrCreate import org.matrix.android.sdk.internal.database.mapper.ContentMapper +import org.matrix.android.sdk.internal.database.tools.RealmDebugTools import org.matrix.android.sdk.internal.di.CryptoDatabase import org.matrix.android.sdk.internal.di.DeviceId import org.matrix.android.sdk.internal.di.MoshiProvider @@ -95,7 +96,6 @@ import org.matrix.android.sdk.internal.session.SessionScope import org.matrix.olm.OlmAccount import org.matrix.olm.OlmException import timber.log.Timber -import java.lang.StringBuilder import javax.inject.Inject import kotlin.collections.set @@ -1709,19 +1709,6 @@ internal class RealmCryptoStore @Inject constructor( * Prints out database info */ override fun logDbUsageInfo() { - Realm.getInstance(realmConfiguration).use { realm -> - val info = StringBuilder() - // Check if we have data - info.append("\n==============================================") - info.append("\n==============================================") - info.append("\nCrypto Realm is empty: ${realm.isEmpty}") - realmConfiguration.realmObjectClasses.forEach { modelClazz -> - val count = realm.where(modelClazz).count() - info.append("\nCrypto Realm - count ${modelClazz.simpleName}: $count") - } - info.append("\n==============================================") - info.append("\n==============================================") - Timber.i(info.toString()) - } + RealmDebugTools(realmConfiguration).logInfo("Crypto") } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/tools/RealmDebugTools.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/tools/RealmDebugTools.kt index 4e37ab14a8..ac60f88146 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/tools/RealmDebugTools.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/tools/RealmDebugTools.kt @@ -16,23 +16,9 @@ package org.matrix.android.sdk.internal.database.tools -import org.matrix.android.sdk.internal.crypto.store.db.model.CrossSigningInfoEntity -import org.matrix.android.sdk.internal.crypto.store.db.model.CryptoMetadataEntity -import org.matrix.android.sdk.internal.crypto.store.db.model.CryptoRoomEntity -import org.matrix.android.sdk.internal.crypto.store.db.model.DeviceInfoEntity -import org.matrix.android.sdk.internal.crypto.store.db.model.GossipingEventEntity -import org.matrix.android.sdk.internal.crypto.store.db.model.IncomingGossipingRequestEntity -import org.matrix.android.sdk.internal.crypto.store.db.model.KeyInfoEntity -import org.matrix.android.sdk.internal.crypto.store.db.model.KeysBackupDataEntity -import org.matrix.android.sdk.internal.crypto.store.db.model.MyDeviceLastSeenInfoEntity -import org.matrix.android.sdk.internal.crypto.store.db.model.OlmInboundGroupSessionEntity -import org.matrix.android.sdk.internal.crypto.store.db.model.OlmSessionEntity -import org.matrix.android.sdk.internal.crypto.store.db.model.OutgoingGossipingRequestEntity -import org.matrix.android.sdk.internal.crypto.store.db.model.TrustLevelEntity -import org.matrix.android.sdk.internal.crypto.store.db.model.UserEntity import io.realm.Realm import io.realm.RealmConfiguration -import io.realm.kotlin.where +import org.matrix.android.sdk.BuildConfig import timber.log.Timber internal class RealmDebugTools( @@ -41,30 +27,34 @@ internal class RealmDebugTools( /** * Log info about the DB */ - fun logInfo() { - Timber.d("Realm located at : ${realmConfiguration.realmDirectory}/${realmConfiguration.realmFileName}") + fun logInfo(baseName: String) { + buildString { + append("\n$baseName Realm located at : ${realmConfiguration.realmDirectory}/${realmConfiguration.realmFileName}") - val key = realmConfiguration.encryptionKey.joinToString("") { byte -> "%02x".format(byte) } - Timber.d("Realm encryption key : $key") + if (BuildConfig.LOG_PRIVATE_DATA) { + val key = realmConfiguration.encryptionKey.joinToString("") { byte -> "%02x".format(byte) } + append("\n$baseName Realm encryption key : $key") + } - Realm.getInstance(realmConfiguration).use { - // Check if we have data - Timber.e("Realm is empty: ${it.isEmpty}") - - Timber.d("Realm has CryptoMetadataEntity: ${it.where().count()}") - Timber.d("Realm has CryptoRoomEntity: ${it.where().count()}") - Timber.d("Realm has DeviceInfoEntity: ${it.where().count()}") - Timber.d("Realm has KeysBackupDataEntity: ${it.where().count()}") - Timber.d("Realm has OlmInboundGroupSessionEntity: ${it.where().count()}") - Timber.d("Realm has OlmSessionEntity: ${it.where().count()}") - Timber.d("Realm has UserEntity: ${it.where().count()}") - Timber.d("Realm has KeyInfoEntity: ${it.where().count()}") - Timber.d("Realm has CrossSigningInfoEntity: ${it.where().count()}") - Timber.d("Realm has TrustLevelEntity: ${it.where().count()}") - Timber.d("Realm has GossipingEventEntity: ${it.where().count()}") - Timber.d("Realm has IncomingGossipingRequestEntity: ${it.where().count()}") - Timber.d("Realm has OutgoingGossipingRequestEntity: ${it.where().count()}") - Timber.d("Realm has MyDeviceLastSeenInfoEntity: ${it.where().count()}") + Realm.getInstance(realmConfiguration).use { realm -> + // Check if we have data + separator() + separator() + append("\n$baseName Realm is empty: ${realm.isEmpty}") + var total = 0L + realmConfiguration.realmObjectClasses.forEach { modelClazz -> + val count = realm.where(modelClazz).count() + total += count + append("\n$baseName Realm - count ${modelClazz.simpleName}: $count") + } + separator() + append("\n$baseName Realm - total count: $total") + separator() + separator() + } } + .let { Timber.i(it) } } + + internal fun StringBuilder.separator() = append("\n==============================================") } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt index 2a6df81e72..25345e953c 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt @@ -18,9 +18,7 @@ package org.matrix.android.sdk.internal.session import androidx.annotation.MainThread import dagger.Lazy -import io.realm.Realm import io.realm.RealmConfiguration -import io.realm.kotlin.where import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import okhttp3.OkHttpClient @@ -61,6 +59,7 @@ import org.matrix.android.sdk.api.session.user.UserService import org.matrix.android.sdk.api.session.widgets.WidgetService import org.matrix.android.sdk.internal.auth.SessionParamsStore import org.matrix.android.sdk.internal.crypto.DefaultCryptoService +import org.matrix.android.sdk.internal.database.tools.RealmDebugTools import org.matrix.android.sdk.internal.di.SessionDatabase import org.matrix.android.sdk.internal.di.SessionId import org.matrix.android.sdk.internal.di.UnauthenticatedWithCertificate @@ -288,20 +287,6 @@ internal class DefaultSession @Inject constructor( } override fun logDbUsageInfo() { - Realm.getInstance(realmConfiguration).use { realm -> - val info = StringBuilder() - - // Check if we have data - info.append("\n==============================================") - info.append("\n==============================================") - info.append("\nSession Realm is empty: ${realm.isEmpty}") - realmConfiguration.realmObjectClasses.forEach { modelClazz -> - val count = realm.where(modelClazz).count() - info.append("\nSession Realm - count ${modelClazz.simpleName}: $count") - } - info.append("\n==============================================") - info.append("\n==============================================") - Timber.i(info.toString()) - } + RealmDebugTools(realmConfiguration).logInfo("Session") } }