From 31151a9a7f798083da2f7435c46a8c1b1ddd8ee2 Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Tue, 25 Feb 2025 15:51:19 +0300 Subject: [PATCH] Some reducers --- .../db/serdha0/user_commons/reducers/Count.kt | 20 +++++++++++++ .../serdha0/user_commons/reducers/HasRows.kt | 21 ++++++++++++++ .../db/serdha0/user_commons/reducers/Max.kt | 28 +++++++++++++++++++ .../db/serdha0/user_commons/reducers/Min.kt | 28 +++++++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 src/commonMain/kotlin/ru/landgrafhomyak/db/serdha0/user_commons/reducers/Count.kt create mode 100644 src/commonMain/kotlin/ru/landgrafhomyak/db/serdha0/user_commons/reducers/HasRows.kt create mode 100644 src/commonMain/kotlin/ru/landgrafhomyak/db/serdha0/user_commons/reducers/Max.kt create mode 100644 src/commonMain/kotlin/ru/landgrafhomyak/db/serdha0/user_commons/reducers/Min.kt diff --git a/src/commonMain/kotlin/ru/landgrafhomyak/db/serdha0/user_commons/reducers/Count.kt b/src/commonMain/kotlin/ru/landgrafhomyak/db/serdha0/user_commons/reducers/Count.kt new file mode 100644 index 0000000..a30d9ea --- /dev/null +++ b/src/commonMain/kotlin/ru/landgrafhomyak/db/serdha0/user_commons/reducers/Count.kt @@ -0,0 +1,20 @@ +package ru.landgrafhomyak.db.serdha0.user_commons.reducers + +import ru.landgrafhomyak.db.serdha0.api.misc.Reducer +import ru.landgrafhomyak.db.serdha0.api.runtime.InputRow +import ru.landgrafhomyak.db.serdha0.api.runtime.OutputRow +import ru.landgrafhomyak.db.serdha0.user_commons.types.U_INT_64 + +public class Count private constructor(context: Reducer.Constructor.Scope) : Reducer.Action { + @Suppress("PropertyName", "MemberVisibilityCanBePrivate") + public val C_Counter: Reducer.OutputColumn = context.outputColumn("counter", context.types.U_INT_64, 0u) + + override fun calculate(acc: OutputRow, row: OutputRow, newAcc: InputRow) { + newAcc[this.C_Counter] = acc[this.C_Counter] + 1u + } + + public object Constructor : Reducer.Constructor { + override fun createReducer(context: Reducer.Constructor.Scope): Reducer.Action = + Count(context) + } +} \ No newline at end of file diff --git a/src/commonMain/kotlin/ru/landgrafhomyak/db/serdha0/user_commons/reducers/HasRows.kt b/src/commonMain/kotlin/ru/landgrafhomyak/db/serdha0/user_commons/reducers/HasRows.kt new file mode 100644 index 0000000..dafefeb --- /dev/null +++ b/src/commonMain/kotlin/ru/landgrafhomyak/db/serdha0/user_commons/reducers/HasRows.kt @@ -0,0 +1,21 @@ +package ru.landgrafhomyak.db.serdha0.user_commons.reducers + +import ru.landgrafhomyak.db.serdha0.api.misc.DatabaseType +import ru.landgrafhomyak.db.serdha0.api.misc.Reducer +import ru.landgrafhomyak.db.serdha0.api.runtime.InputRow +import ru.landgrafhomyak.db.serdha0.api.runtime.OutputRow + +public class HasRows private constructor(context: Reducer.Constructor.Scope) : Reducer.Action { + @Suppress("PropertyName", "MemberVisibilityCanBePrivate") + public val C_HasRows: Reducer.OutputColumn = + context.outputColumn("flag", context.types.BOOLEAN, false) + + override fun calculate(acc: OutputRow, row: OutputRow, newAcc: InputRow) { + newAcc[this.C_HasRows] = true + } + + public object Constructor : Reducer.Constructor { + override fun createReducer(context: Reducer.Constructor.Scope): Reducer.Action = + HasRows(context) + } +} \ No newline at end of file diff --git a/src/commonMain/kotlin/ru/landgrafhomyak/db/serdha0/user_commons/reducers/Max.kt b/src/commonMain/kotlin/ru/landgrafhomyak/db/serdha0/user_commons/reducers/Max.kt new file mode 100644 index 0000000..03ad1b3 --- /dev/null +++ b/src/commonMain/kotlin/ru/landgrafhomyak/db/serdha0/user_commons/reducers/Max.kt @@ -0,0 +1,28 @@ +package ru.landgrafhomyak.db.serdha0.user_commons.reducers + +import ru.landgrafhomyak.db.serdha0.api.misc.DatabaseType +import ru.landgrafhomyak.db.serdha0.api.misc.Reducer +import ru.landgrafhomyak.db.serdha0.api.runtime.InputRow +import ru.landgrafhomyak.db.serdha0.api.runtime.OutputRow + +public class Max> private constructor( + context: Reducer.Constructor.Scope>, + private val _type: DT +) : Reducer.Action> { + @Suppress("PropertyName", "MemberVisibilityCanBePrivate") + public val C_MaxValue: Reducer.BidirectionalColumn> = context.bidirectionalColumn("max_value", this._type) + + override fun calculate(acc: OutputRow>, row: OutputRow>, newAcc: InputRow>) { + val accumulatedValue = acc[this.C_MaxValue] + val newValue = row[this.C_MaxValue] + if (this._type._compare(accumulatedValue, newValue) < 0) + newAcc[this.C_MaxValue] = newValue + else + newAcc[this.C_MaxValue] = accumulatedValue + } + + public class Constructor>(private val type: DT) : Reducer.Constructor> { + override fun createReducer(context: Reducer.Constructor.Scope>): Reducer.Action> = + Max(context, this.type) + } +} \ No newline at end of file diff --git a/src/commonMain/kotlin/ru/landgrafhomyak/db/serdha0/user_commons/reducers/Min.kt b/src/commonMain/kotlin/ru/landgrafhomyak/db/serdha0/user_commons/reducers/Min.kt new file mode 100644 index 0000000..55ca1d5 --- /dev/null +++ b/src/commonMain/kotlin/ru/landgrafhomyak/db/serdha0/user_commons/reducers/Min.kt @@ -0,0 +1,28 @@ +package ru.landgrafhomyak.db.serdha0.user_commons.reducers + +import ru.landgrafhomyak.db.serdha0.api.misc.DatabaseType +import ru.landgrafhomyak.db.serdha0.api.misc.Reducer +import ru.landgrafhomyak.db.serdha0.api.runtime.InputRow +import ru.landgrafhomyak.db.serdha0.api.runtime.OutputRow + +public class Min> private constructor( + context: Reducer.Constructor.Scope>, + private val _type: DT +) : Reducer.Action> { + @Suppress("PropertyName", "MemberVisibilityCanBePrivate") + public val C_MinValue: Reducer.BidirectionalColumn> = context.bidirectionalColumn("min_value", this._type) + + override fun calculate(acc: OutputRow>, row: OutputRow>, newAcc: InputRow>) { + val accumulatedValue = acc[this.C_MinValue] + val newValue = row[this.C_MinValue] + if (this._type._compare(accumulatedValue, newValue) > 0) + newAcc[this.C_MinValue] = newValue + else + newAcc[this.C_MinValue] = accumulatedValue + } + + public class Constructor>(private val type: DT) : Reducer.Constructor> { + override fun createReducer(context: Reducer.Constructor.Scope>): Reducer.Action> = + Min(context, this.type) + } +} \ No newline at end of file