Skip to content

Commit

Permalink
Use immutable library for List/Set/Map (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
05nelsonm authored Jan 18, 2024
1 parent 08dc9bc commit 8ff46e6
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 47 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[![badge-sqlite]][url-sqlite]
[![badge-coroutines]][url-coroutines]
[![badge-encoding]][url-encoding]
[![badge-immutable]][url-immutable]
[![badge-sqldelight]][url-sqldelight]
[![badge-sqlitemc]][url-sqlitemc]
[![badge-sqliter]][url-sqliter]
Expand Down Expand Up @@ -404,6 +405,7 @@ I'm guessing this is not really a factor on iOS."
[badge-kotlin]: https://img.shields.io/badge/kotlin-1.9.21-blue.svg?logo=kotlin
[badge-coroutines]: https://img.shields.io/badge/coroutines-1.7.3-blue.svg?logo=kotlin
[badge-encoding]: https://img.shields.io/badge/encoding-2.1.0-blue.svg?style=flat
[badge-immutable]: https://img.shields.io/badge/immutable-0.1.0--alpha01-blue.svg?style=flat
[badge-sqldelight]: https://img.shields.io/badge/SQLDelight-2.0.0-blue.svg?style=flat
[badge-sqlite]: https://img.shields.io/badge/SQLite3-3.43.2-blue.svg?style=flat
[badge-sqlitemc]: https://img.shields.io/badge/SQLite3MultipleCiphers-1.7.2-blue.svg?style=flat
Expand Down Expand Up @@ -433,6 +435,7 @@ I'm guessing this is not really a factor on iOS."
[url-kotlin]: https://kotlinlang.org
[url-coroutines]: https://github.com/Kotlin/kotlinx.coroutines
[url-encoding]: https://github.com/05nelsonm/encoding
[url-immutable]: https://github.com/05nelsonm/immutable
[url-sqldelight]: https://github.com/cashapp/sqldelight
[url-sqlite]: https://sqlite.org
[url-sqlitemc]: https://github.com/utelle/SQLite3MultipleCiphers
Expand Down
6 changes: 5 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ gradle-android = "8.1.4"
gradle-binary-compat = "0.13.2"
gradle-build-config = "4.1.2"
gradle-cklib = "0.3.0"
gradle-kmp-configuration = "0.1.5"
gradle-kmp-configuration = "0.1.7"
gradle-kotlin = "1.9.21"
gradle-maven-publish = "0.25.3"

immutable = "0.1.0-alpha01"

kotlinx-coroutines = "1.7.3"

okio = "3.5.0"
Expand All @@ -36,6 +38,8 @@ gradle-kotlin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.
gradle-maven-publish = { module = "com.vanniktech:gradle-maven-publish-plugin", version.ref = "gradle-maven-publish" }
gradle-sql-delight = { module = "app.cash.sqldelight:gradle-plugin", version.ref = "sql-delight" }

immutable-collections = { module = "io.matthewnelson.immutable:collections", version.ref = "immutable" }

kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" }
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx-coroutines" }

Expand Down
2 changes: 1 addition & 1 deletion library/driver/api/android/driver.api
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public final class io/toxicity/sqlite/mc/driver/config/FactoryConfig {
public final field platformConfig Lio/toxicity/sqlite/mc/driver/config/PlatformConfig;
public final field pragmaConfig Lio/toxicity/sqlite/mc/driver/config/PragmaConfig;
public final field schema Lapp/cash/sqldelight/db/SqlSchema;
public synthetic fun <init> (Ljava/lang/String;Lapp/cash/sqldelight/db/SqlSchema;Lio/toxicity/sqlite/mc/driver/config/FilesystemConfig;Lio/toxicity/sqlite/mc/driver/config/PlatformConfig;Lio/toxicity/sqlite/mc/driver/config/PragmaConfig;Lkotlinx/coroutines/CoroutineDispatcher;[Lapp/cash/sqldelight/db/AfterVersion;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public synthetic fun <init> (Ljava/lang/String;Lapp/cash/sqldelight/db/SqlSchema;Lio/toxicity/sqlite/mc/driver/config/FilesystemConfig;Lio/toxicity/sqlite/mc/driver/config/PlatformConfig;Lio/toxicity/sqlite/mc/driver/config/PragmaConfig;Lkotlinx/coroutines/CoroutineDispatcher;Ljava/util/List;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
}

public final class io/toxicity/sqlite/mc/driver/config/FactoryConfig$Builder {
Expand Down
2 changes: 1 addition & 1 deletion library/driver/api/jvm/driver.api
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public final class io/toxicity/sqlite/mc/driver/config/FactoryConfig {
public final field platformConfig Lio/toxicity/sqlite/mc/driver/config/PlatformConfig;
public final field pragmaConfig Lio/toxicity/sqlite/mc/driver/config/PragmaConfig;
public final field schema Lapp/cash/sqldelight/db/SqlSchema;
public synthetic fun <init> (Ljava/lang/String;Lapp/cash/sqldelight/db/SqlSchema;Lio/toxicity/sqlite/mc/driver/config/FilesystemConfig;Lio/toxicity/sqlite/mc/driver/config/PlatformConfig;Lio/toxicity/sqlite/mc/driver/config/PragmaConfig;Lkotlinx/coroutines/CoroutineDispatcher;[Lapp/cash/sqldelight/db/AfterVersion;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public synthetic fun <init> (Ljava/lang/String;Lapp/cash/sqldelight/db/SqlSchema;Lio/toxicity/sqlite/mc/driver/config/FilesystemConfig;Lio/toxicity/sqlite/mc/driver/config/PlatformConfig;Lio/toxicity/sqlite/mc/driver/config/PragmaConfig;Lkotlinx/coroutines/CoroutineDispatcher;Ljava/util/List;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
}

public final class io/toxicity/sqlite/mc/driver/config/FactoryConfig$Builder {
Expand Down
1 change: 1 addition & 0 deletions library/driver/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ kmpConfiguration {
dependencies {
api(libs.sql.delight.runtime)
implementation(libs.encoding.base16)
implementation(libs.immutable.collections)
implementation(libs.kotlinx.coroutines.core)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import app.cash.sqldelight.db.QueryResult
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.db.SqlSchema
import app.cash.sqldelight.logs.LogSqliteDriver
import io.matthewnelson.immutable.collections.toImmutableList
import io.toxicity.sqlite.mc.driver.MCConfigDsl
import io.toxicity.sqlite.mc.driver.SQLiteMCDriver
import kotlinx.coroutines.CoroutineDispatcher
Expand Down Expand Up @@ -50,7 +51,7 @@ public class FactoryConfig private constructor(
@JvmSynthetic
internal val dispatcher: CoroutineDispatcher,
@JvmSynthetic
internal val afterVersions: Array<AfterVersion>,
internal val afterVersions: List<AfterVersion>,
@JvmSynthetic
internal val logger: ((String) -> Unit)?,
) {
Expand Down Expand Up @@ -226,7 +227,7 @@ public class FactoryConfig private constructor(
dispatcher
}
},
afterVersions = afterVersions.toTypedArray(),
afterVersions = afterVersions.toImmutableList(),
logger = logger.toRedactedLoggerOrNull(redactLogs, dbName),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
package io.toxicity.sqlite.mc.driver.config

import app.cash.sqldelight.db.SqlCursor
import io.matthewnelson.immutable.collections.immutableSetOf
import io.matthewnelson.immutable.collections.toImmutableList
import io.matthewnelson.immutable.collections.toImmutableSet
import io.toxicity.sqlite.mc.driver.MCConfigDsl
import io.toxicity.sqlite.mc.driver.config.encryption.*
import io.toxicity.sqlite.mc.driver.internal.ext.buildMCConfigSQL
Expand Down Expand Up @@ -130,23 +133,23 @@ public abstract class MCPragma<FieldType: Any> private constructor(
@JvmStatic
@get:JvmSynthetic
internal val ALL: Set<MCPragma<*>> by lazy {
buildSet(capacity = 16) {
add(CIPHER)
add(HMAC_CHECK)
add(MC_LEGACY_WAL)
add(LEGACY)
add(LEGACY_PAGE_SIZE)
add(KDF_ITER)
add(FAST_KDF_ITER)
add(HMAC_USE)
add(HMAC_PNGO)
add(HMAC_SALT_MASK)
add(KDF_ALGORITHM)
add(HMAC_ALGORITHM)
add(PLAIN_TEXT_HEADER_SIZE)
add(KEY)
add(RE_KEY)
}
immutableSetOf(
CIPHER,
HMAC_CHECK,
MC_LEGACY_WAL,
LEGACY,
LEGACY_PAGE_SIZE,
KDF_ITER,
FAST_KDF_ITER,
HMAC_USE,
HMAC_PNGO,
HMAC_SALT_MASK,
KDF_ALGORITHM,
HMAC_ALGORITHM,
PLAIN_TEXT_HEADER_SIZE,
KEY,
RE_KEY,
)
}

private val TransformAny: Transformer<Any> = Transformer { it.toString() }
Expand Down Expand Up @@ -258,5 +261,5 @@ internal fun MCPragmas.toMCSQLStatements(): List<String> {
}

addAll(rekeyNonTransient)
}
}.toImmutableList()
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
**/
package io.toxicity.sqlite.mc.driver.config

import io.matthewnelson.immutable.collections.toImmutableMap
import io.toxicity.sqlite.mc.driver.EphemeralOpt
import io.toxicity.sqlite.mc.driver.MCConfigDsl
import io.toxicity.sqlite.mc.driver.SQLiteMCDriver
Expand Down Expand Up @@ -157,8 +158,8 @@ public class PragmaConfig private constructor(
}

return PragmaConfig(
ephemeral = ImmutablePragma(ephemeral),
filesystem = ImmutablePragma(filesystem),
ephemeral = ephemeral.toImmutableMap(),
filesystem = filesystem.toImmutableMap(),
)
}

Expand Down Expand Up @@ -226,20 +227,3 @@ public class PragmaConfig private constructor(
}
}
}

private class ImmutablePragma(
map: MutableMap<String, String>,
): Map<String, String> {

private val delegate = map.toMap()

override val entries: Set<Map.Entry<String, String>> get() = delegate.entries
override val keys: Set<String> get() = delegate.keys
override val size: Int get() = delegate.size
override val values: Collection<String> get() = delegate.values

override fun isEmpty(): Boolean = delegate.isEmpty()
override operator fun get(key: String): String? = delegate[key]
override fun containsValue(value: String): Boolean = delegate.containsValue(value)
override fun containsKey(key: String): Boolean = delegate.containsKey(key)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
**/
package io.toxicity.sqlite.mc.driver.config.encryption

import io.matthewnelson.immutable.collections.toImmutableSet
import io.toxicity.sqlite.mc.driver.MCConfigDsl
import io.toxicity.sqlite.mc.driver.SQLiteMCDriver
import io.toxicity.sqlite.mc.driver.config.FilesystemConfig
Expand Down Expand Up @@ -238,7 +239,7 @@ public class EncryptionMigrationConfig private constructor(

@JvmSynthetic
internal fun build(): EncryptionMigrationConfig? {
val set = migrations.toSet()
val set = migrations.toImmutableSet()
if (set.isEmpty()) return null
return EncryptionMigrationConfig(set)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public actual sealed class PlatformDriver actual constructor(private val args: A
properties = properties,
schema = schema,
migrateEmptySchema = platformConfig.migrateEmptySchema,
callbacks = afterVersions,
callbacks = afterVersions.toTypedArray(),
)
} catch (t: Throwable) {
properties.clear()
Expand Down Expand Up @@ -160,7 +160,7 @@ public actual sealed class PlatformDriver actual constructor(private val args: A
properties = properties,
schema = schema,
migrateEmptySchema = platformConfig.migrateEmptySchema,
callbacks = afterVersions,
callbacks = afterVersions.toTypedArray(),
)
} catch (t: Throwable) {
if (t is IllegalStateException) throw t
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,10 @@ private fun SqlSchema<QueryResult.Value<Unit>>.create(): (DatabaseConnection) ->
}

private fun SqlSchema<QueryResult.Value<Unit>>.upgrade(
afterVersions: Array<AfterVersion>
afterVersions: List<AfterVersion>
): (DatabaseConnection, Int, Int) -> Unit = { connection, oldVersion, newVersion ->
wrapConnection(connection) {
migrate(it, oldVersion.toLong(), newVersion.toLong(), callbacks = afterVersions)
migrate(it, oldVersion.toLong(), newVersion.toLong(), callbacks = afterVersions.toTypedArray())
}
}

Expand Down

0 comments on commit 8ff46e6

Please sign in to comment.