Skip to content

Commit

Permalink
Ensure LevelDB Iterator is closed (#5458)
Browse files Browse the repository at this point in the history
Task/Issue URL:
https://app.asana.com/0/1200204095367872/1209128100809324/f

### Description

- Closes the `DBIterator` after local storage has been cleared.
- Also keeps the DB open and updates the DB initialization to be `Lazy`.

### Steps to test this PR

_Filter logcat by WebLocalStorageManager_
- [x] Visit cnn.com
- [x] Use the fire button
- [x] Verify that cnn.com entries are deleted
  • Loading branch information
joshliebe authored Jan 12, 2025
1 parent 4b3fb20 commit d88e5ae
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import com.duckduckgo.app.pixels.remoteconfig.AndroidBrowserConfigFeature
import com.duckduckgo.di.scopes.AppScope
import com.squareup.anvil.annotations.ContributesBinding
import com.squareup.anvil.annotations.ContributesTo
import dagger.Lazy
import dagger.Module
import dagger.Provides
import dagger.SingleInstanceIn
import java.io.File
import java.nio.charset.StandardCharsets
import javax.inject.Inject
import javax.inject.Provider
import kotlinx.coroutines.runBlocking
import org.iq80.leveldb.DB
import org.iq80.leveldb.Options
Expand All @@ -40,7 +40,7 @@ interface WebLocalStorageManager {

@ContributesBinding(AppScope::class)
class DuckDuckGoWebLocalStorageManager @Inject constructor(
private val databaseProvider: Provider<DB>,
private val databaseProvider: Lazy<DB>,
private val androidBrowserConfigFeature: AndroidBrowserConfigFeature,
private val webLocalStorageSettingsJsonParser: WebLocalStorageSettingsJsonParser,
) : WebLocalStorageManager {
Expand All @@ -58,8 +58,8 @@ class DuckDuckGoWebLocalStorageManager @Inject constructor(
Timber.d("WebLocalStorageManager: Allowed domains: $domains")
Timber.d("WebLocalStorageManager: Matching regex: $matchingRegex")

databaseProvider.get().use { db ->
val iterator = db.iterator()
val db = databaseProvider.get()
db.iterator().use { iterator ->
iterator.seekToFirst()

while (iterator.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import com.duckduckgo.app.browser.weblocalstorage.WebLocalStorageSettings
import com.duckduckgo.app.browser.weblocalstorage.WebLocalStorageSettingsJsonParser
import com.duckduckgo.app.pixels.remoteconfig.AndroidBrowserConfigFeature
import com.duckduckgo.feature.toggles.api.Toggle
import javax.inject.Provider
import dagger.Lazy
import kotlinx.coroutines.test.runTest
import org.iq80.leveldb.DB
import org.iq80.leveldb.DBIterator
Expand All @@ -39,7 +39,7 @@ class DuckDuckGoWebLocalStorageManagerTest {

private val mockDB: DB = mock()
private val mockIterator: DBIterator = mock()
private val mockDatabaseProvider: Provider<DB> = mock()
private val mockDatabaseProvider: Lazy<DB> = mock()
private val mockWebLocalStorageSettingsJsonParser: WebLocalStorageSettingsJsonParser = mock()
private val mockAndroidBrowserConfigFeature: AndroidBrowserConfigFeature = mock()
private val mockToggle: Toggle = mock()
Expand Down Expand Up @@ -152,13 +152,13 @@ class DuckDuckGoWebLocalStorageManagerTest {
}

@Test
fun whenClearWebLocalStorageThenDBIsClosed() {
fun whenClearWebLocalStorageThenIteratorIsClosed() {
whenever(mockDB.iterator()).thenReturn(mockIterator)
whenever(mockIterator.hasNext()).thenReturn(false)

testee.clearWebLocalStorage()

verify(mockDB).close()
verify(mockIterator).close()
}

private fun createMockDBEntry(key: ByteArray): MutableMap.MutableEntry<ByteArray, ByteArray> {
Expand Down

0 comments on commit d88e5ae

Please sign in to comment.