Skip to content

Commit

Permalink
Reorganized cache-control to improve abstraction
Browse files Browse the repository at this point in the history
Moved "RandomUUIDSQLMappingFunction" from "MappingSupportSQLiteDatabase.kt" to new file "SQLiteCacheControl.kt"
Added extension function "SupportSQLiteOpenHelper.Factory.asCacheControlled" in "SQLiteCacheControl.kt" as wrapper for "asMapped" with "RandomUUIDSQLMappingFunction" as mapping function
Removed "RandomUUIDSQLMappingFunction" as default mapping function from extension function "SupportSQLiteOpenHelper.Factory.asMapped" in "MappingSupportSQLiteDatabase.kt"
Replaced call to "asMapped" with call to "asCacheControlled" in "DatabaseModule"

*This commit is related to issue #529 [1]*

[1] #529
  • Loading branch information
JaniruTEC committed Apr 13, 2024
1 parent 42f572a commit db2edda
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
4 changes: 2 additions & 2 deletions data/src/main/java/org/cryptomator/data/db/DatabaseModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import androidx.sqlite.db.SupportSQLiteOpenHelper
import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory
import org.cryptomator.data.db.sqlmapping.asMapped
import org.cryptomator.data.db.SQLiteCacheControl.asCacheControlled
import org.cryptomator.data.db.migrations.legacy.Upgrade0To1
import org.cryptomator.data.db.migrations.legacy.Upgrade10To11
import org.cryptomator.data.db.migrations.legacy.Upgrade11To12
Expand Down Expand Up @@ -51,7 +51,7 @@ class DatabaseModule {
.createFromInputStream(dbTemplateStreamCallable) //
.addMigrations(*migrations) //
.addCallback(DatabaseCallback) //
.openHelperFactory(DatabaseOpenHelperFactory().asMapped()) //
.openHelperFactory(DatabaseOpenHelperFactory().asCacheControlled()) //
.setJournalMode(RoomDatabase.JournalMode.TRUNCATE) //
.build() //Fails if no migration is found (especially when downgrading)
.also { //
Expand Down
21 changes: 21 additions & 0 deletions data/src/main/java/org/cryptomator/data/db/SQLiteCacheControl.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.cryptomator.data.db

import androidx.sqlite.db.SupportSQLiteOpenHelper
import org.cryptomator.data.db.sqlmapping.SQLMappingFunction
import org.cryptomator.data.db.sqlmapping.asMapped
import java.util.UUID

object SQLiteCacheControl {

object RandomUUIDSQLMappingFunction : SQLMappingFunction {

private val newIdentifier: String
get() = UUID.randomUUID().toString()

override fun invoke(sql: String): String {
return "$sql -- $newIdentifier"
}
}

fun SupportSQLiteOpenHelper.Factory.asCacheControlled(): SupportSQLiteOpenHelper.Factory = asMapped(RandomUUIDSQLMappingFunction)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import androidx.sqlite.db.SupportSQLiteOpenHelper
import androidx.sqlite.db.SupportSQLiteQuery
import androidx.sqlite.db.SupportSQLiteStatement
import java.util.Collections
import java.util.UUID

internal class MappingSupportSQLiteDatabase(
private val delegate: SupportSQLiteDatabase,
Expand Down Expand Up @@ -185,18 +184,8 @@ private class MappingSupportSQLiteOpenHelperFactory(
}
}

fun SupportSQLiteOpenHelper.Factory.asMapped(mappingFunction: SQLMappingFunction = RandomUUIDSQLMappingFunction): SupportSQLiteOpenHelper.Factory {
fun SupportSQLiteOpenHelper.Factory.asMapped(mappingFunction: SQLMappingFunction): SupportSQLiteOpenHelper.Factory {
return MappingSupportSQLiteOpenHelperFactory(this, mappingFunction)
}

interface SQLMappingFunction : (String) -> String

object RandomUUIDSQLMappingFunction : SQLMappingFunction {

private val newIdentifier: String
get() = UUID.randomUUID().toString()

override fun invoke(sql: String): String {
return "$sql -- $newIdentifier"
}
}
interface SQLMappingFunction : (String) -> String

0 comments on commit db2edda

Please sign in to comment.