com.lightningkite.lightningdb.AbstractSignalFieldCollection.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of server-core Show documentation
Show all versions of server-core Show documentation
A set of tools to fill in/replace what Ktor is lacking in.
The newest version!
package com.lightningkite.lightningdb
import java.util.concurrent.ConcurrentLinkedQueue
/**
* Used as the root for handling raw signals.
* Raw signals are used primarily to handle websockets.
*/
abstract class AbstractSignalFieldCollection : FieldCollection {
val signals = ConcurrentLinkedQueue) -> Unit>()
override fun registerRawSignal(callback: suspend (CollectionChanges) -> Unit) {
signals.add(callback)
}
private suspend fun signal(change: CollectionChanges) {
signals.forEach { it(change) }
}
final override suspend fun insert(models: Iterable): List {
val result = insertImpl(models)
val change = CollectionChanges(changes = result.map { EntryChange(new = it) })
signal(change)
return result
}
final override suspend fun deleteMany(condition: Condition): List {
return deleteManyImpl(condition).also {
val change = CollectionChanges(changes = it.map { EntryChange(old = it) })
signal(change)
}
}
final override suspend fun deleteOne(condition: Condition, orderBy: List>): Model? {
return deleteOneImpl(condition, orderBy)?.also {
val change = CollectionChanges(old = it)
signal(change)
}
}
final override suspend fun replaceOne(
condition: Condition,
model: Model,
orderBy: List>
): EntryChange =
replaceOneImpl(condition, model, orderBy).also {
if (it.new == null) return@also
if (it.new == it.old) return@also
val change = CollectionChanges(it.old, it.new)
signal(change)
}
final override suspend fun updateOne(
condition: Condition,
modification: Modification,
orderBy: List>
): EntryChange =
updateOneImpl(condition, modification, orderBy).also {
if (it.new == null) return@also
if (it.new == it.old) return@also
val change = CollectionChanges(it.old, it.new)
signal(change)
}
final override suspend fun upsertOne(
condition: Condition,
modification: Modification,
model: Model
): EntryChange = upsertOneImpl(condition, modification, model).also {
val change = CollectionChanges(it.old, it.new)
if (it.new == it.old) return@also
signal(change)
}
final override suspend fun updateMany(
condition: Condition,
modification: Modification
): CollectionChanges = updateManyImpl(condition, modification).also { changes ->
signal(CollectionChanges(changes.changes.filter { it.old != it.new }))
}
final override suspend fun replaceOneIgnoringResult(
condition: Condition,
model: Model,
orderBy: List>
): Boolean =
if (signals.isEmpty()) replaceOneIgnoringResultImpl(condition, model, orderBy)
else replaceOne(condition, model, orderBy).new != null
final override suspend fun upsertOneIgnoringResult(
condition: Condition,
modification: Modification,
model: Model
): Boolean =
if (signals.isEmpty()) upsertOneIgnoringResultImpl(condition, modification, model)
else upsertOne(condition, modification, model).old != null
final override suspend fun updateOneIgnoringResult(
condition: Condition,
modification: Modification,
orderBy: List>
): Boolean =
if (signals.isEmpty()) updateOneIgnoringResultImpl(condition, modification, orderBy)
else updateOne(condition, modification, orderBy).new != null
final override suspend fun updateManyIgnoringResult(
condition: Condition,
modification: Modification
): Int =
if (signals.isEmpty()) updateManyIgnoringResultImpl(condition, modification)
else updateMany(condition, modification).changes.size
final override suspend fun deleteOneIgnoringOld(
condition: Condition,
orderBy: List>
): Boolean =
if (signals.isEmpty()) deleteOneIgnoringOldImpl(condition, orderBy)
else deleteOne(condition, orderBy) != null
final override suspend fun deleteManyIgnoringOld(condition: Condition): Int =
if (signals.isEmpty()) deleteManyIgnoringOldImpl(condition)
else deleteMany(condition).size
protected abstract suspend fun insertImpl(models: Iterable): List
protected abstract suspend fun replaceOneImpl(
condition: Condition,
model: Model,
orderBy: List> = listOf()
): EntryChange
protected abstract suspend fun replaceOneIgnoringResultImpl(
condition: Condition,
model: Model,
orderBy: List> = listOf()
): Boolean
protected abstract suspend fun upsertOneImpl(
condition: Condition,
modification: Modification,
model: Model
): EntryChange
protected abstract suspend fun upsertOneIgnoringResultImpl(
condition: Condition,
modification: Modification,
model: Model
): Boolean
protected abstract suspend fun updateOneImpl(
condition: Condition,
modification: Modification,
orderBy: List> = listOf()
): EntryChange
protected abstract suspend fun updateOneIgnoringResultImpl(
condition: Condition,
modification: Modification,
orderBy: List>
): Boolean
protected abstract suspend fun updateManyImpl(
condition: Condition,
modification: Modification
): CollectionChanges
protected abstract suspend fun updateManyIgnoringResultImpl(
condition: Condition,
modification: Modification
): Int
protected abstract suspend fun deleteOneImpl(condition: Condition, orderBy: List>): Model?
protected abstract suspend fun deleteOneIgnoringOldImpl(
condition: Condition,
orderBy: List> = listOf()
): Boolean
protected abstract suspend fun deleteManyImpl(condition: Condition): List
protected abstract suspend fun deleteManyIgnoringOldImpl(condition: Condition): Int
}