Skip to content

Commit

Permalink
Simplifying discardOldestFileIfNeeded function
Browse files Browse the repository at this point in the history
  • Loading branch information
hannah-smartbear authored and lemnik committed Sep 20, 2024
1 parent 395e3f7 commit 3aabb9e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -18,7 +16,7 @@ class FileStoreTest {

val delegate = CustomDelegate()
val dir = File(ApplicationProvider.getApplicationContext<Application>().cacheDir, "tmp")
val store = CustomFileStore(dir, 1, EVENT_COMPARATOR, delegate)
val store = CustomFileStore(dir, 1, delegate)
val exc = RuntimeException("Whoops")
store.write(CustomStreamable(exc))

Expand Down Expand Up @@ -49,8 +47,7 @@ class CustomStreamable(private val exc: Throwable) : JsonStream.Streamable {
internal class CustomFileStore(
folder: File,
maxStoreCount: Int,
comparator: Comparator<in File?>,
delegate: Delegate?
) : FileStore(folder, maxStoreCount, comparator, NoopLogger, delegate) {
) : FileStore(folder, maxStoreCount, NoopLogger, delegate) {
override fun getFilename(obj: Any?) = "foo.json"
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ internal class EventStore(
) : FileStore(
File(config.persistenceDirectory.value, "bugsnag/errors"),
config.maxPersistedEvents,
EVENT_COMPARATOR,
logger,
delegate
) {
Expand Down
35 changes: 15 additions & 20 deletions bugsnag-android-core/src/main/java/com/bugsnag/android/FileStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@ 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

internal abstract class FileStore(
val storageDir: File,
private val maxStoreCount: Int,
private val comparator: Comparator<in File?>,
protected open val logger: Logger,
protected val delegate: Delegate?
) {
Expand Down Expand Up @@ -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<File> = 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++
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ internal class SessionStore(
config.persistenceDirectory.value, "bugsnag/sessions"
),
config.maxPersistedSessions,
SESSION_COMPARATOR,
logger,
delegate
) {
Expand Down

0 comments on commit 3aabb9e

Please sign in to comment.