Skip to content

Commit

Permalink
feat(jvm): Add support for in-memory user storage (#2192)
Browse files Browse the repository at this point in the history
The user storage creation process is refactored to support both file-based and in-memory databases. This involves changes in the `PlatformUserStorageProvider`, `CoreLogic`, and `UserSessionScopeProviderImpl` classes. An optional flag is added to the `CoreLogic` constructor to indicate whether to use in-memory storage.
  • Loading branch information
vitorhugods authored Nov 6, 2023
1 parent 76b3f95 commit 43857ea
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
6 changes: 4 additions & 2 deletions logic/src/jvmMain/kotlin/com/wire/kalium/logic/CoreLogic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ import java.io.File
actual class CoreLogic(
rootPath: String,
kaliumConfigs: KaliumConfigs,
userAgent: String
userAgent: String,
useInMemoryStorage: Boolean = false,
) : CoreLogicCommon(
rootPath = rootPath, kaliumConfigs = kaliumConfigs, userAgent = userAgent
) {
Expand Down Expand Up @@ -75,7 +76,8 @@ actual class CoreLogic(
globalCallManager,
userStorageProvider,
networkStateObserver,
userAgent
userAgent,
useInMemoryStorage
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@

package com.wire.kalium.logic.di

import java.io.File

actual class PlatformUserStorageProperties internal constructor(
val rootPath: String,
val rootStoragePath: String
val databaseInfo: DatabaseStorageType
)

sealed interface DatabaseStorageType {
data class FiledBacked(val filePath: File) : DatabaseStorageType

data object InMemory : DatabaseStorageType
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,36 @@ package com.wire.kalium.logic.di
import com.wire.kalium.logic.data.id.toDao
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.persistence.db.PlatformDatabaseData
import com.wire.kalium.persistence.db.inMemoryDatabase
import com.wire.kalium.persistence.db.userDatabaseBuilder
import com.wire.kalium.persistence.kmmSettings.UserPrefBuilder
import com.wire.kalium.util.KaliumDispatcherImpl
import java.io.File

internal actual class PlatformUserStorageProvider : UserStorageProvider() {
override fun create(userId: UserId, shouldEncryptData: Boolean, platformProperties: PlatformUserStorageProperties): UserStorage {
override fun create(
userId: UserId,
shouldEncryptData: Boolean,
platformProperties: PlatformUserStorageProperties
): UserStorage {
val userIdEntity = userId.toDao()
val pref = UserPrefBuilder(userIdEntity, platformProperties.rootPath, shouldEncryptData)
val database = userDatabaseBuilder(
platformDatabaseData = PlatformDatabaseData(File(platformProperties.rootStoragePath)),
userId = userIdEntity,
passphrase = null,
dispatcher = KaliumDispatcherImpl.io,
enableWAL = false
)

val databaseInfo = platformProperties.databaseInfo
val database = when (databaseInfo) {
is DatabaseStorageType.FiledBacked -> {
userDatabaseBuilder(
platformDatabaseData = PlatformDatabaseData(databaseInfo.filePath),
userId = userIdEntity,
passphrase = null,
dispatcher = KaliumDispatcherImpl.io,
enableWAL = false
)
}

else -> {
inMemoryDatabase(userIdEntity, KaliumDispatcherImpl.io)
}
}
return UserStorage(database, pref)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ import com.wire.kalium.logic.data.asset.CacheFolder
import com.wire.kalium.logic.data.asset.DBFolder
import com.wire.kalium.logic.data.asset.DataStoragePaths
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.di.DatabaseStorageType
import com.wire.kalium.logic.di.PlatformUserStorageProperties
import com.wire.kalium.logic.di.RootPathsProvider
import com.wire.kalium.logic.di.UserStorageProvider
import com.wire.kalium.logic.feature.auth.AuthenticationScopeProvider
import com.wire.kalium.logic.feature.call.GlobalCallManager
import com.wire.kalium.logic.featureFlags.KaliumConfigs
import com.wire.kalium.network.NetworkStateObserver
import com.wire.kalium.logic.sync.UserSessionWorkSchedulerImpl
import com.wire.kalium.network.NetworkStateObserver
import com.wire.kalium.persistence.kmmSettings.GlobalPrefProvider
import java.io.File

@Suppress("LongParameterList")
internal actual class UserSessionScopeProviderImpl(
Expand All @@ -44,19 +46,27 @@ internal actual class UserSessionScopeProviderImpl(
private val globalCallManager: GlobalCallManager,
private val userStorageProvider: UserStorageProvider,
private val networkStateObserver: NetworkStateObserver,
userAgent: String
userAgent: String,
private val useInMemoryDatabase: Boolean
) : UserSessionScopeProviderCommon(globalCallManager, userStorageProvider, userAgent) {

override fun create(userId: UserId): UserSessionScope {
val rootAccountPath = rootPathsProvider.rootAccountPath(userId)
val rootStoragePath = "$rootAccountPath/storage"
val databaseInfo = if (useInMemoryDatabase) {
DatabaseStorageType.InMemory
} else {
DatabaseStorageType.FiledBacked(
File(rootStoragePath)
)
}
val rootFileSystemPath = AssetsStorageFolder("$rootStoragePath/files")
val rootCachePath = CacheFolder("$rootAccountPath/cache")
val dbPath = DBFolder("$rootAccountPath/database")
val dataStoragePaths = DataStoragePaths(rootFileSystemPath, rootCachePath, dbPath)
val userSessionWorkScheduler = UserSessionWorkSchedulerImpl(userId)
return UserSessionScope(
PlatformUserStorageProperties(rootPathsProvider.rootPath, rootStoragePath),
PlatformUserStorageProperties(rootPathsProvider.rootPath, databaseInfo),
userId,
globalScope,
globalCallManager,
Expand Down

0 comments on commit 43857ea

Please sign in to comment.