r.0.9.1.source-code.TolrModule.kt Maven / Gradle / Ivy
The newest version!
package se.wollan.tolr
import kotlinx.coroutines.CoroutineScope
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import se.wollan.datascope.SqliteDatabase
import se.wollan.time.HybridLogicalClockSynchronizer
open class TolrModule(
resolveConfigurationProvider: () -> ConfigurationProvider,
private val resolveCoroutineScope: () -> CoroutineScope,
private val resolveRpcClient: () -> RPCClient,
private val resolveDatabase: () -> SqliteDatabase,
private val resolveHybridLogicalClockSynchronizer: () -> HybridLogicalClockSynchronizer,
) {
private fun resolveCachedConfigurationProvider(): ConfigurationProvider = _cachedConfigurationProvider
private val _cachedConfigurationProvider by lazy { CachedConfigurationProviderDecorator(resolveConfigurationProvider()) }
open fun resolveDatabaseMigrator(): DatabaseMigrator = DatabaseMigratorImpl(resolveDatabase())
private fun resolveRecordRepo(): LogRecordRepo = _recordRepo
private val _recordRepo by lazy {
SkipEmptyWritesRecordRepoDecorator(SqliteRecordRepo(resolveDatabase(), resolveCachedConfigurationProvider()))
}
private fun resolveServerServerAPI(): ServerServerAPI =
ServerServerAPIImpl(resolveRpcClient(), resolveHybridLogicalClockSynchronizer())
private fun resolveLogReplicator(): LogReplicator = _logReplicator
private val _logReplicator by lazy {
LogReplicatorImpl(
coroutineScope = resolveCoroutineScope(),
recordRepo = resolveRecordRepo(),
serverServerAPI = resolveServerServerAPI(),
logger = resolveLogger("Replicator"),
dataScope = resolveDatabase(),
configurationProvider = resolveCachedConfigurationProvider()
)
}
private fun resolveRpcServer(): RPCServer = RPCServerImpl(
resolveLogReplicator(),
resolveHybridLogicalClockSynchronizer(),
resolveServerServerAPI(),
resolveCachedConfigurationProvider()
)
private fun resolveProjectionFactory(): ProjectionFactory = _projectionFactory
private val _projectionFactory by lazy {
ProjectionFactoryImpl(resolveCoroutineScope(), resolveRecordRepo(), resolveLogger("Projector"))
}
open fun resolveTotalOrderedLog(): TotalOrderedLog = _totalOrderedLog
private val _totalOrderedLog by lazy {
TotalOrderedLogImpl(
resolveRecordRepo(),
resolveHybridLogicalClockSynchronizer(),
resolveLogReplicator(),
resolveRpcServer(),
resolveCachedConfigurationProvider(),
resolveDatabase(),
resolveProjectionFactory()
)
}
private fun resolveLogger(name: String): Logger = LoggerFactory.getLogger(name)
}