From 3aabb9e6ce6479ddbf471051052a9d60ac45ceb0 Mon Sep 17 00:00:00 2001 From: hannah-smartbear Date: Tue, 17 Sep 2024 10:14:09 +0100 Subject: [PATCH] Simplifying discardOldestFileIfNeeded function --- .../java/com/bugsnag/android/FileStoreTest.kt | 7 ++-- .../java/com/bugsnag/android/EventStore.kt | 1 - .../java/com/bugsnag/android/FileStore.kt | 35 ++++++++----------- .../java/com/bugsnag/android/SessionStore.kt | 1 - 4 files changed, 17 insertions(+), 27 deletions(-) diff --git a/bugsnag-android-core/src/androidTest/java/com/bugsnag/android/FileStoreTest.kt b/bugsnag-android-core/src/androidTest/java/com/bugsnag/android/FileStoreTest.kt index d2c87b053d..6c4733ac54 100644 --- a/bugsnag-android-core/src/androidTest/java/com/bugsnag/android/FileStoreTest.kt +++ b/bugsnag-android-core/src/androidTest/java/com/bugsnag/android/FileStoreTest.kt @@ -2,13 +2,11 @@ package com.bugsnag.android import android.app.Application import androidx.test.core.app.ApplicationProvider -import com.bugsnag.android.EventStore.Companion.EVENT_COMPARATOR import org.junit.Assert.assertEquals import org.junit.Test import org.junit.runner.RunWith import org.mockito.junit.MockitoJUnitRunner import java.io.File -import java.util.Comparator @RunWith(MockitoJUnitRunner::class) class FileStoreTest { @@ -18,7 +16,7 @@ class FileStoreTest { val delegate = CustomDelegate() val dir = File(ApplicationProvider.getApplicationContext().cacheDir, "tmp") - val store = CustomFileStore(dir, 1, EVENT_COMPARATOR, delegate) + val store = CustomFileStore(dir, 1, delegate) val exc = RuntimeException("Whoops") store.write(CustomStreamable(exc)) @@ -49,8 +47,7 @@ class CustomStreamable(private val exc: Throwable) : JsonStream.Streamable { internal class CustomFileStore( folder: File, maxStoreCount: Int, - comparator: Comparator, delegate: Delegate? -) : FileStore(folder, maxStoreCount, comparator, NoopLogger, delegate) { +) : FileStore(folder, maxStoreCount, NoopLogger, delegate) { override fun getFilename(obj: Any?) = "foo.json" } diff --git a/bugsnag-android-core/src/main/java/com/bugsnag/android/EventStore.kt b/bugsnag-android-core/src/main/java/com/bugsnag/android/EventStore.kt index 497f971e01..1ed219d95b 100644 --- a/bugsnag-android-core/src/main/java/com/bugsnag/android/EventStore.kt +++ b/bugsnag-android-core/src/main/java/com/bugsnag/android/EventStore.kt @@ -33,7 +33,6 @@ internal class EventStore( ) : FileStore( File(config.persistenceDirectory.value, "bugsnag/errors"), config.maxPersistedEvents, - EVENT_COMPARATOR, logger, delegate ) { diff --git a/bugsnag-android-core/src/main/java/com/bugsnag/android/FileStore.kt b/bugsnag-android-core/src/main/java/com/bugsnag/android/FileStore.kt index 235ba699bf..34b5bb403e 100644 --- a/bugsnag-android-core/src/main/java/com/bugsnag/android/FileStore.kt +++ b/bugsnag-android-core/src/main/java/com/bugsnag/android/FileStore.kt @@ -7,8 +7,6 @@ import java.io.FileNotFoundException import java.io.FileOutputStream import java.io.OutputStreamWriter import java.io.Writer -import java.util.Collections -import java.util.Comparator import java.util.concurrent.ConcurrentSkipListSet import java.util.concurrent.locks.Lock import java.util.concurrent.locks.ReentrantLock @@ -16,7 +14,6 @@ import java.util.concurrent.locks.ReentrantLock internal abstract class FileStore( val storageDir: File, private val maxStoreCount: Int, - private val comparator: Comparator, protected open val logger: Logger, protected val delegate: Delegate? ) { @@ -115,23 +112,21 @@ internal abstract class FileStore( // Limit number of saved payloads to prevent disk space issues if (isStorageDirValid(storageDir)) { val listFiles = storageDir.listFiles() ?: return - val files: ArrayList = arrayListOf(*listFiles) - if (files.size >= maxStoreCount) { - // Sort files then delete the first one (oldest timestamp) - Collections.sort(files, comparator) - var k = 0 - while (k < files.size && files.size >= maxStoreCount) { - val oldestFile = files[k] - if (!queuedFiles.contains(oldestFile)) { - logger.w( - "Discarding oldest error as stored " + - "error limit reached: '" + oldestFile.path + '\'' - ) - deleteStoredFiles(setOf(oldestFile)) - files.removeAt(k) - k-- - } - k++ + if (listFiles.size < maxStoreCount) return + val sortedListFiles = listFiles.sortedBy { it.lastModified() } + // Number of files to discard takes into account that a new file may need to be written + val numberToDiscard = listFiles.size - maxStoreCount + 1 + var discardedCount = 0 + for (file in sortedListFiles) { + if (discardedCount == numberToDiscard) { + return + } else if (!queuedFiles.contains(file)) { + logger.w( + "Discarding oldest error as stored error limit reached: '" + + file.path + '\'' + ) + deleteStoredFiles(setOf(file)) + discardedCount++ } } } diff --git a/bugsnag-android-core/src/main/java/com/bugsnag/android/SessionStore.kt b/bugsnag-android-core/src/main/java/com/bugsnag/android/SessionStore.kt index 86519ac5af..094de50547 100644 --- a/bugsnag-android-core/src/main/java/com/bugsnag/android/SessionStore.kt +++ b/bugsnag-android-core/src/main/java/com/bugsnag/android/SessionStore.kt @@ -21,7 +21,6 @@ internal class SessionStore( config.persistenceDirectory.value, "bugsnag/sessions" ), config.maxPersistedSessions, - SESSION_COMPARATOR, logger, delegate ) {