All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lightningkite.lightningdb.DelayedFieldCollection.kt Maven / Gradle / Ivy

The newest version!
package com.lightningkite.lightningdb

import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.onStart
import kotlin.reflect.KProperty1
import kotlin.reflect.KType

/**
 * Wraps a FieldCollection with the sole purpose of adding a delay in the response to every call.
 * @param wraps The actual underlying FieldCollection to retrieve data from.
 * @param milliseconds The amount of delay that will be added to every call.
 */
open class DelayedFieldCollection(
    override val wraps: FieldCollection,
    val milliseconds: Long
) : FieldCollection {
    override suspend fun fullCondition(condition: Condition): Condition = wraps.fullCondition(condition)
    override suspend fun mask(): Mask = wraps.mask()
    override suspend fun find(
        condition: Condition,
        orderBy: List>,
        skip: Int,
        limit: Int,
        maxQueryMs: Long,
    ): Flow = wraps.find(condition, orderBy, skip, limit, maxQueryMs).onStart { delay(milliseconds) }

    override suspend fun findPartial(
        fields: Set>,
        condition: Condition,
        orderBy: List>,
        skip: Int,
        limit: Int,
        maxQueryMs: Long
    ): Flow> = wraps.findPartial(fields, condition, orderBy, skip, limit, maxQueryMs)

    override suspend fun count(condition: Condition): Int {
        delay(milliseconds)
        return wraps.count(condition)
    }

    override suspend fun  groupCount(condition: Condition, groupBy: DataClassPath): Map {
        delay(milliseconds)
        return wraps.groupCount(condition, groupBy)
    }

    override suspend fun  aggregate(
        aggregate: Aggregate,
        condition: Condition,
        property: DataClassPath,
    ): Double? {
        delay(milliseconds)
        return wraps.aggregate(aggregate, condition, property)
    }

    override suspend fun  groupAggregate(
        aggregate: Aggregate,
        condition: Condition,
        groupBy: DataClassPath,
        property: DataClassPath,
    ): Map {
        delay(milliseconds)
        return wraps.groupAggregate(aggregate, condition, groupBy, property)
    }

    override suspend fun insert(models: Iterable): List {
        delay(milliseconds)
        return wraps.insert(models)
    }

    override suspend fun replaceOne(
        condition: Condition,
        model: Model,
        orderBy: List>,
    ): EntryChange {
        delay(milliseconds)
        return wraps.replaceOne(condition, model, orderBy)
    }

    override suspend fun replaceOneIgnoringResult(
        condition: Condition,
        model: Model,
        orderBy: List>,
    ): Boolean {
        delay(milliseconds)
        return wraps.replaceOneIgnoringResult(condition, model, orderBy)
    }

    override suspend fun upsertOne(
        condition: Condition,
        modification: Modification,
        model: Model,
    ): EntryChange {
        delay(milliseconds)
        return wraps.upsertOne(condition, modification, model)
    }

    override suspend fun upsertOneIgnoringResult(
        condition: Condition,
        modification: Modification,
        model: Model,
    ): Boolean {
        delay(milliseconds)
        return wraps.upsertOneIgnoringResult(condition, modification, model)
    }

    override suspend fun updateOne(
        condition: Condition,
        modification: Modification,
        orderBy: List>,
    ): EntryChange {
        delay(milliseconds)
        return wraps.updateOne(condition, modification, orderBy)
    }

    override suspend fun updateOneIgnoringResult(
        condition: Condition,
        modification: Modification,
        orderBy: List>,
    ): Boolean {
        delay(milliseconds)
        return wraps.updateOneIgnoringResult(condition, modification, orderBy)
    }

    override suspend fun updateMany(
        condition: Condition,
        modification: Modification,
    ): CollectionChanges {
        delay(milliseconds)
        return wraps.updateMany(condition, modification)
    }

    override suspend fun updateManyIgnoringResult(condition: Condition, modification: Modification): Int {
        delay(milliseconds)
        return wraps.updateManyIgnoringResult(condition, modification)
    }

    override suspend fun deleteOne(condition: Condition, orderBy: List>): Model? {
        delay(milliseconds)
        return wraps.deleteOne(condition, orderBy)
    }

    override suspend fun deleteOneIgnoringOld(condition: Condition, orderBy: List>): Boolean {
        delay(milliseconds)
        return wraps.deleteOneIgnoringOld(condition, orderBy)
    }

    override suspend fun deleteMany(condition: Condition): List {
        delay(milliseconds)
        return wraps.deleteMany(condition)
    }

    override suspend fun deleteManyIgnoringOld(condition: Condition): Int {
        delay(milliseconds)
        return wraps.deleteManyIgnoringOld(condition)
    }

    override fun registerRawSignal(callback: suspend (CollectionChanges) -> Unit) {
        return wraps.registerRawSignal(callback)
    }
}

fun  FieldCollection.delayed(milliseconds: Long): FieldCollection =
    DelayedFieldCollection(this, milliseconds)

fun Database.delayed(milliseconds: Long): Database = object : Database by this {
    override fun  collection(type: KType, name: String): FieldCollection {
        return [email protected](type, name).delayed(milliseconds)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy