Compare commits

...

72 Commits
0.1.1 ... 0.3.0

Author SHA1 Message Date
c914f8c44a fixes 2020-11-09 00:51:08 +06:00
abfda3627c repos rework 2020-11-09 00:46:26 +06:00
5b5bfa02db complete with major version change 0.3.0 2020-11-08 23:50:53 +06:00
0fdb072385 update: Replace channels by sharedflows in exposed CRUD realization 2020-11-08 20:45:45 +06:00
7b4c9d59b0 start 0.2.8 2020-11-08 20:44:40 +06:00
bd5923716f Merge pull request #11 from InsanusMokrassar/0.2.7
0.2.7
2020-11-07 02:28:38 +06:00
64cc42a23b upfill of changelog 2020-11-07 02:27:54 +06:00
325f178763 repos update 2020-11-07 02:25:50 +06:00
fc8d0e52ef start 0.2.7 2020-11-04 18:47:42 +06:00
e58348907e Merge pull request #10 from InsanusMokrassar/0.2.6
0.2.6
2020-11-03 20:08:06 +06:00
eaba9173ae more fixes:) 2020-11-03 20:07:33 +06:00
2f42b30f87 fix for paginate (again) and lastIndexExclusive 2020-11-03 19:43:45 +06:00
35913b95be fixes in pagintion 2020-11-03 18:37:50 +06:00
8023fa1b76 start 0.2.6 2020-11-03 18:35:22 +06:00
4cbe2d1d61 Merge pull request #9 from InsanusMokrassar/0.2.5
0.2.5
2020-11-02 21:44:53 +06:00
740036e8d9 link fix in ktor server 2020-11-02 21:44:12 +06:00
273a7aa9a4 optimize imports 2020-11-02 21:34:49 +06:00
dccb479c3c CoroutineScope#safeActor 2020-11-02 21:32:04 +06:00
6f5c6e5ebe safelyWithoutExceptions 2020-11-02 21:25:33 +06:00
8495cc6263 start 0.2.5 2020-11-02 21:24:20 +06:00
d242d8dcf4 Merge pull request #8 from InsanusMokrassar/0.2.4
0.2.4
2020-11-02 00:19:44 +06:00
864d576e70 fix add test for common diff utils 2020-11-02 00:19:23 +06:00
f0127b018e hotfix 2020-11-02 00:14:25 +06:00
5eb48a58bf diff utils rewriting 2020-11-02 00:12:47 +06:00
b690f68c7f add Flow#subscribe 2020-11-01 00:54:07 +06:00
7f19b83828 deprecate broadcast channel flow extensions 2020-11-01 00:43:47 +06:00
98a1ec82db update serialization version 2020-11-01 00:41:57 +06:00
63313ff964 start 0.2.4 2020-11-01 00:41:02 +06:00
210e32bed4 Merge pull request #7 from InsanusMokrassar/0.2.3
0.2.3
2020-10-28 14:47:30 +06:00
5b325a8ff9 add dokka 2020-10-28 14:47:12 +06:00
4582e0c817 small refactoring of BroadcastStateFlow 2020-10-28 14:19:57 +06:00
e2d1c5d6a1 corotoutines actualization 2020-10-28 14:17:39 +06:00
4019ad7d31 update exposed and coroutines versions 2020-10-28 13:58:23 +06:00
e7df21e91a common js visibility and on screen extensions 2020-10-28 13:57:24 +06:00
b2e30c9f6d start 0.2.3 2020-10-28 13:55:49 +06:00
0b27b5cc06 Revert "start add recycler view"
This reverts commit 347e9c32fe.
2020-10-28 13:51:24 +06:00
347e9c32fe start add recycler view 2020-10-28 13:51:16 +06:00
cc18f58e4c Merge pull request #6 from InsanusMokrassar/0.2.2
0.2.2
2020-10-25 22:26:18 +06:00
dbd2e963a1 change order of functions in read one to many interface 2020-10-25 22:23:24 +06:00
6928ca5329 optimize imports 2020-10-25 22:21:54 +06:00
9ece160aa8 add extensions for onetomany functions 2020-10-25 22:21:23 +06:00
6115c1bcac add extensions for OneToManyKeyValueRepo 2020-10-25 22:08:42 +06:00
e026e94cbf start 0.2.2 2020-10-25 21:52:36 +06:00
56cdd8d6af remove fixing of last index due to solving that pages size must be saved in reversing of pagination 2020-10-22 18:50:17 +06:00
899e6760e1 fix for lastIndex in reversing of pagination 2020-10-22 18:48:05 +06:00
864d0ffcc6 hope last fix 2020-10-22 18:37:56 +06:00
c6f417f8c8 fixes in pagination 2020-10-22 17:57:27 +06:00
9091fa5bd8 Merge pull request #4 from InsanusMokrassar/0.2.1
0.2.1
2020-10-22 15:50:30 +06:00
418af7874d update repos to use new reverse method 2020-10-22 15:47:28 +06:00
4972cc9daa several additions and tests for pagination reversing 2020-10-22 15:41:21 +06:00
ff905e1491 fixes in reverse extension 2020-10-22 14:54:27 +06:00
64b0184a17 Pagination#reverse 2020-10-22 14:39:39 +06:00
97cbe44fb5 start 0.2.1 2020-10-22 14:38:53 +06:00
452d8778c5 update changelog 2020-10-17 18:53:20 +06:00
02ad1a748e fix table name parameter in keyvalue and onetomany repos 2020-10-17 18:51:48 +06:00
c5a32e2b1b Merge pull request #3 from InsanusMokrassar/0.2.0
0.2.0
2020-10-17 18:09:36 +06:00
d34c5d845a remove onInit() 2020-10-17 18:01:22 +06:00
82b39baada fill CHANGELOG 2020-10-17 17:50:54 +06:00
712ed06b69 exposed tables changes 2020-10-17 17:41:56 +06:00
3365c63e07 start 0.2.0 2020-10-17 17:20:11 +06:00
702300f761 Update github_release.gradle 2020-10-16 21:39:52 +06:00
efcaa08b70 Update github_release.gradle 2020-10-16 21:38:31 +06:00
7f7369b3a8 Update and rename changelog_info_retriever to changelog_parser.sh 2020-10-16 21:37:24 +06:00
45a9a95888 Update github_release.gradle 2020-10-16 21:36:01 +06:00
90c1731bd1 Revert "more annotations to god of annotations"
This reverts commit f8a8808508.
2020-10-16 19:40:04 +06:00
893fd1ac07 Revert "less annotations to god of lessannotations"
This reverts commit 87230d010c.
2020-10-16 19:39:55 +06:00
9cac05f98b reorder repositories 2020-10-16 19:39:18 +06:00
3babc94842 update repos 2020-10-16 17:59:51 +06:00
87230d010c less annotations to god of lessannotations 2020-10-15 17:33:44 +06:00
f8a8808508 more annotations to god of annotations 2020-10-15 00:26:37 +06:00
f44174b5b3 update .gitignore 2020-10-14 22:58:35 +06:00
f6eb339cd0 Merge pull request #2 from InsanusMokrassar/0.1.1
0.1.1 (additions)
2020-10-14 22:31:25 +06:00
68 changed files with 1298 additions and 407 deletions

2
.gitignore vendored
View File

@@ -10,3 +10,5 @@ build/
out/
secret.gradle
publishing.sh

View File

@@ -1,10 +1,132 @@
# Changelog
## 0.1.0
## 0.3.0
Inited :)
All deprecations has been removed
### 0.1.1
* `Repos`:
* `Common`:
* `KeyValue` and `OneToMany` repos lost their deprecated methods
* `OneToMany` write repos got additional extensions for mutation of repo
* `KeyValue` write repos got additional extensions for mutation of repo
* New interface `MapperRepo` and new classes which are using this:
* `KeyValue`
* `MapperReadStandardKeyValueRepo`
* `MapperWriteStandardKeyValueRepo`
* `MapperStandardKeyValueRepo`
* `OneToMany`
* `MapperReadOneToManyKeyValueRepo`
* `MapperWriteOneToManyKeyValueRepo`
* `MapperOneToManyKeyValueRepo`
* `Exposed`:
* CRUD realizations replaced their channels to shared flows
## 0.2.7
* `Versions`:
* `Coroutines`: `1.4.0` -> `1.4.1`
* `Repos`:
* `WriteStandardKeyValueRepo` got new methods `set` and `unset` with collections
* All standard realizations of repos got collections methods realizations
* All old usages of `BroadcastFlow` and `BroadcastChannel` has been replaced with `MutableSharedFlow`
* `Ktor`:
* `Server`:
* Fixed incorrect answer for `keyvalue`
## 0.2.6
* `Pagination`
* Fixes in function `List#paginate`
* Extension property `Pagination#lastIndexExclusive`
## 0.2.5
* `Coroutines`
* Function `safelyWithoutExceptions`
* Extension `CoroutineScope#safeActor`
## 0.2.4
* `Versions`
* `Serialization`: `1.0.0` -> `1.0.1`
* `Common`
* Full rework of `DiffUtils`
* Data class `Diff` has been added
* Extension `Iterable#calculateDiff` has been added
* Extension `Iterable#calculateStrictDiff` as replacement for `Iterable#calculateDiff` with
`strictComparison` mode enabled
* Functions `Diff` (as analog of `Iterable#calculateDiff`) and `StrictDiff` (as analog of
`Iterable#calculateStrictDiff`)
* `Coroutines`
* `BroadcastFlow` now is deprecated
* `BroadcastStateFlow` now is deprecated
* New extensions for `Flow`s:
* `Flow#subscribe`
* `Flow#subscribeSafely`
* `Flow#subscribeSafelyWithoutExceptions`
## 0.2.3
* `Versions`
* `Coroutines`: `1.3.9` -> `1.4.0`
* `Exposed`: `0.27.1` -> `0.28.1`
* `Common`
* `K/JS`
* Add several extensions for `Element` objects to detect that object is on screen viewport
* Add several extensions for `Element` objects to detect object visibility
* `Coroutines`
* `BroadcastStateFlow` now use different strategy for getting of state and implements `replayCache`
## 0.2.2
* `Repos`
* `Common`
* Several new methods `ReadOneToManyKeyValueRepo#getAll`
* Several new method `WriteOneToManyKeyValueRepo#add` and several extensions
* Several new method `WriteOneToManyKeyValueRepo#remove` and several extensions
## 0.2.1
* `Pagination`
* `Common`:
* Extension `Pagination#reverse` has been added
* Factory `PaginationByIndexes`
* Shortcut `calculatePagesNumber` with reversed parameters
* Value `emptyPagination` for empty `SimplePagination` cases
## 0.2.0
* `Repos`
* `Exposed`
* Now this project depend on `micro_utils.coroutines`
* Typealias `ColumnAllocator` has been replaced to root exposed package
* Interface `ExposedRepo` has been added
* `ExposedCRUDRepo` now extends `ExposedRepo` instead of simple `Repo`
* New extension `initTable` for classes which are `Table` and `ExposedRepo` at the same time
* `KeyValue`:
* `tableName` parameter
* Class `AbstractExposedReadKeyValueRepo`
* Renamed to `ExposedReadKeyValueRepo`
* Changed incoming types to `ColumnAllocator`
* `open` instead of `abstract`
* Implements `ExposedRepo`
* Class `AbstractExposedKeyValueRepo`
* Renamed to `ExposedKeyValueRepo`
* Changed incoming types to `ColumnAllocator`
* `open` instead of `abstract`
* `OneToMany`:
* `tableName` parameter
* Class `AbstractExposedReadOneToManyKeyValueRepo`
* Renamed to `ExposedReadOneToManyKeyValueRepo`
* Changed incoming arguments order
* Implements `ExposedRepo`
* Class `AbstractExposedOneToManyKeyValueRepo`
* Renamed to `ExposedKeyValueRepo`
* Changed incoming arguments order
* `open` instead of `abstract`
* Release for every `Flow` in parent interfaces
## 0.1.1
* `Versions`:
* `kotlinx.serialization`: `1.0.0-RC2` -> `1.0.0`
@@ -39,3 +161,8 @@ Inited :)
* `MapCRUDRepo` class as implementation of `StandardCRUDRepo` on top of `MutableMap` has been added
* `MapKeyValueRepo` class as implementation of `StandardKeyValueRepo` on top of `MutableMap` has been added
* `MapOneToManyKeyValueRepo` class as implementation of `OneToManyKeyValueRepo` on top of `MutableMap` has been added
## 0.1.0
Inited :)

View File

@@ -14,11 +14,13 @@ buildscript {
}
}
repositories {
mavenLocal()
jcenter()
mavenCentral()
maven { url "https://kotlin.bintray.com/kotlinx" }
allprojects {
repositories {
mavenLocal()
jcenter()
mavenCentral()
maven { url "https://kotlin.bintray.com/kotlinx" }
}
}
apply from: "./extensions.gradle"

View File

@@ -1,24 +0,0 @@
#!/bin/bash
function parse() {
version=$1
while IFS= read -r line && [ -z "`echo $line | grep -e "^#\+ $version"`" ]
do
: # do nothing
done
while IFS= read -r line && [ -z "`echo $line | grep -e "^#\+"`" ]
do
echo "$line"
done
}
version=$1
file=$2
if [ -n "$file" ]; then
parse $version < "$file"
else
parse $version
fi

24
changelog_parser.sh Executable file
View File

@@ -0,0 +1,24 @@
#!/bin/bash
function parse() {
version="$1"
while IFS= read -r line && [ -z "`echo "$line" | grep -e "^#\+ $version"`" ]
do
: # do nothing
done
while IFS= read -r line && [ -z "`echo "$line" | grep -e "^#\+"`" ]
do
echo "$line"
done
}
version="$1"
file="$2"
if [ -n "$file" ]; then
parse "$version" < "$file"
else
parse "$version"
fi

View File

@@ -1,10 +1,151 @@
@file:Suppress("NOTHING_TO_INLINE")
package dev.inmo.micro_utils.common
fun <T> Iterable<T>.syncWith(
other: Iterable<T>,
removed: (List<T>) -> Unit = {},
added: (List<T>) -> Unit = {}
) {
removed(filter { it !in other })
added(other.filter { it !in this })
private inline fun <T> getObject(
additional: MutableList<T>,
iterator: Iterator<T>
): T? = when {
additional.isNotEmpty() -> additional.removeFirst()
iterator.hasNext() -> iterator.next()
else -> null
}
/**
* Diff object which contains information about differences between two [Iterable]s
*
* @see calculateDiff
*/
data class Diff<T> internal constructor(
val removed: List<IndexedValue<T>>,
/**
* Old-New values pairs
*/
val replaced: List<Pair<IndexedValue<T>, IndexedValue<T>>>,
val added: List<IndexedValue<T>>
)
private inline fun <T> performChanges(
potentialChanges: MutableList<Pair<IndexedValue<T>?, IndexedValue<T>?>>,
additionalsInOld: MutableList<T>,
additionalsInNew: MutableList<T>,
changedList: MutableList<Pair<IndexedValue<T>, IndexedValue<T>>>,
removedList: MutableList<IndexedValue<T>>,
addedList: MutableList<IndexedValue<T>>,
strictComparison: Boolean
) {
var i = -1
val (oldObject, newObject) = potentialChanges.lastOrNull() ?: return
for ((old, new) in potentialChanges.take(potentialChanges.size - 1)) {
i++
val oldOneEqualToNewObject = old ?.value === newObject ?.value || (old ?.value == newObject ?.value && !strictComparison)
val newOneEqualToOldObject = new ?.value === oldObject ?.value || (new ?.value == oldObject ?.value && !strictComparison)
if (oldOneEqualToNewObject || newOneEqualToOldObject) {
changedList.addAll(
potentialChanges.take(i).mapNotNull {
if (it.first != null && it.second != null) it as Pair<IndexedValue<T>, IndexedValue<T>> else null
}
)
val newPotentials = potentialChanges.drop(i).take(potentialChanges.size - i)
when {
oldOneEqualToNewObject -> {
newPotentials.first().second ?.let { addedList.add(it) }
newPotentials.drop(1).take(newPotentials.size - 2).forEach { (oldOne, newOne) ->
addedList.add(newOne!!)
oldOne ?.let { additionalsInOld.add(oldOne.value) }
}
if (newPotentials.size > 1) {
newPotentials.last().first ?.value ?.let { additionalsInOld.add(it) }
}
}
newOneEqualToOldObject -> {
newPotentials.first().first ?.let { removedList.add(it) }
newPotentials.drop(1).take(newPotentials.size - 2).forEach { (oldOne, newOne) ->
removedList.add(oldOne!!)
newOne ?.let { additionalsInNew.add(newOne.value) }
}
if (newPotentials.size > 1) {
newPotentials.last().second ?.value ?.let { additionalsInNew.add(it) }
}
}
}
potentialChanges.clear()
return
}
}
if (potentialChanges.isNotEmpty() && potentialChanges.last().let { it.first == null && it.second == null }) {
potentialChanges.dropLast(1).forEach { (old, new) ->
when {
old != null && new != null -> changedList.add(old to new)
old != null -> removedList.add(old)
new != null -> addedList.add(new)
}
}
}
}
/**
* Calculating [Diff] object
*
* @param strictComparison If this parameter set to true, objects which are not equal by links will be used as different
* objects. For example, in case of two "Example" string they will be equal by value, but CAN be different by links
*/
fun <T> Iterable<T>.calculateDiff(
other: Iterable<T>,
strictComparison: Boolean = false
): Diff<T> {
var i = -1
var j = -1
val additionalInOld = mutableListOf<T>()
val additionalInNew = mutableListOf<T>()
val oldIterator = iterator()
val newIterator = other.iterator()
val potentiallyChangedObjects = mutableListOf<Pair<IndexedValue<T>?, IndexedValue<T>?>>()
val changedObjects = mutableListOf<Pair<IndexedValue<T>, IndexedValue<T>>>()
val addedObjects = mutableListOf<IndexedValue<T>>()
val removedObjects = mutableListOf<IndexedValue<T>>()
while (true) {
i++
j++
val oldObject = getObject(additionalInOld, oldIterator)
val newObject = getObject(additionalInNew, newIterator)
if (oldObject == null && newObject == null) {
break
}
when {
oldObject === newObject || (oldObject == newObject && !strictComparison) -> {
changedObjects.addAll(potentiallyChangedObjects.map { it as Pair<IndexedValue<T>, IndexedValue<T>> })
potentiallyChangedObjects.clear()
}
else -> {
potentiallyChangedObjects.add(oldObject ?.let { IndexedValue(i, oldObject) } to newObject ?.let { IndexedValue(j, newObject) })
val previousOldsAdditionsSize = additionalInOld.size
val previousNewsAdditionsSize = additionalInNew.size
performChanges(potentiallyChangedObjects, additionalInOld, additionalInNew, changedObjects, removedObjects, addedObjects, strictComparison)
i -= (additionalInOld.size - previousOldsAdditionsSize)
j -= (additionalInNew.size - previousNewsAdditionsSize)
}
}
}
potentiallyChangedObjects.add(null to null)
performChanges(potentiallyChangedObjects, additionalInOld, additionalInNew, changedObjects, removedObjects, addedObjects, strictComparison)
return Diff(removedObjects.toList(), changedObjects.toList(), addedObjects.toList())
}
inline fun <T> Diff(old: Iterable<T>, new: Iterable<T>) = old.calculateDiff(new)
inline fun <T> StrictDiff(old: Iterable<T>, new: Iterable<T>) = old.calculateDiff(new, true)
/**
* This method call [calculateDiff] with strict mode enabled
*/
inline fun <T> Iterable<T>.calculateStrictDiff(
other: Iterable<T>
) = calculateDiff(other, strictComparison = true)

View File

@@ -0,0 +1,76 @@
package dev.inmo.micro_utils.common
import kotlin.math.floor
import kotlin.test.Test
import kotlin.test.assertEquals
class DiffUtilsTests {
@Test
fun testThatSimpleRemoveWorks() {
val oldList = (0 until 10).toList()
val withIndex = oldList.withIndex()
for (count in 1 .. (floor(oldList.size.toFloat() / 2).toInt())) {
for ((i, v) in withIndex) {
if (i + count > oldList.lastIndex) {
continue
}
val removedSublist = oldList.subList(i, i + count)
oldList.calculateDiff(oldList - removedSublist).apply {
assertEquals(
removedSublist.mapIndexed { j, o -> IndexedValue(i + j, o) },
removed
)
}
}
}
}
@Test
fun testThatSimpleAddWorks() {
val oldList = (0 until 10).map { it.toString() }
val withIndex = oldList.withIndex()
for (count in 1 .. (floor(oldList.size.toFloat() / 2).toInt())) {
for ((i, v) in withIndex) {
if (i + count > oldList.lastIndex) {
continue
}
val addedSublist = oldList.subList(i, i + count).map { "added$it" }
val mutable = oldList.toMutableList()
mutable.addAll(i, addedSublist)
oldList.calculateDiff(mutable).apply {
assertEquals(
addedSublist.mapIndexed { j, o -> IndexedValue(i + j, o) },
added
)
}
}
}
}
@Test
fun testThatSimpleChangesWorks() {
val oldList = (0 until 10).map { it.toString() }
val withIndex = oldList.withIndex()
for (step in 0 until oldList.size) {
for ((i, v) in withIndex) {
val mutable = oldList.toMutableList()
val changes = (
if (step == 0) i until oldList.size else (i until oldList.size step step)
).map { index ->
IndexedValue(index, mutable[index]) to IndexedValue(index, "changed$index").also {
mutable[index] = it.value
}
}
oldList.calculateDiff(mutable).apply {
assertEquals(
changes,
replaced
)
}
}
}
}
}

View File

@@ -0,0 +1,43 @@
package dev.inmo.micro_utils.common
import kotlinx.browser.window
import org.w3c.dom.DOMRect
import org.w3c.dom.Element
val DOMRect.isOnScreenByLeftEdge: Boolean
get() = left >= 0 && left <= window.innerWidth
inline val Element.isOnScreenByLeftEdge
get() = getBoundingClientRect().isOnScreenByLeftEdge
val DOMRect.isOnScreenByRightEdge: Boolean
get() = right >= 0 && right <= window.innerWidth
inline val Element.isOnScreenByRightEdge
get() = getBoundingClientRect().isOnScreenByRightEdge
internal val DOMRect.isOnScreenHorizontally: Boolean
get() = isOnScreenByLeftEdge || isOnScreenByRightEdge
val DOMRect.isOnScreenByTopEdge: Boolean
get() = top >= 0 && top <= window.innerHeight
inline val Element.isOnScreenByTopEdge
get() = getBoundingClientRect().isOnScreenByTopEdge
val DOMRect.isOnScreenByBottomEdge: Boolean
get() = bottom >= 0 && bottom <= window.innerHeight
inline val Element.isOnScreenByBottomEdge
get() = getBoundingClientRect().isOnScreenByBottomEdge
internal val DOMRect.isOnScreenVertically: Boolean
get() = isOnScreenByLeftEdge || isOnScreenByRightEdge
val DOMRect.isOnScreenFully: Boolean
get() = isOnScreenByLeftEdge && isOnScreenByTopEdge && isOnScreenByRightEdge && isOnScreenByBottomEdge
val Element.isOnScreenFully: Boolean
get() = getBoundingClientRect().isOnScreenFully
val DOMRect.isOnScreen: Boolean
get() = isOnScreenFully || (isOnScreenHorizontally && isOnScreenVertically)
inline val Element.isOnScreen: Boolean
get() = getBoundingClientRect().isOnScreen

View File

@@ -0,0 +1,48 @@
package dev.inmo.micro_utils.common
import kotlinx.browser.window
import org.w3c.dom.Element
import org.w3c.dom.css.CSSStyleDeclaration
sealed class Visibility
object Visible : Visibility()
object Invisible : Visibility()
object Gone : Visibility()
var CSSStyleDeclaration.visibilityState: Visibility
get() = when {
display == "none" -> Gone
visibility == "hidden" -> Invisible
else -> Visible
}
set(value) {
when (value) {
Visible -> {
if (display == "none") {
display = "initial"
}
visibility = "visible"
}
Invisible -> {
if (display == "none") {
display = "initial"
}
visibility = "hidden"
}
Gone -> {
display = "none"
}
}
}
inline var Element.visibilityState: Visibility
get() = window.getComputedStyle(this).visibilityState
set(value) {
window.getComputedStyle(this).visibilityState = value
}
inline val Element.isVisible: Boolean
get() = visibilityState == Visible
inline val Element.isInvisible: Boolean
get() = visibilityState == Invisible
inline val Element.isGone: Boolean
get() = visibilityState == Gone

View File

@@ -17,3 +17,15 @@ fun <T> CoroutineScope.actor(
return channel
}
inline fun <T> CoroutineScope.safeActor(
channelCapacity: Int = Channel.UNLIMITED,
noinline onException: ExceptionHandler<Unit> = {},
crossinline block: suspend (T) -> Unit
): Channel<T> = actor(
channelCapacity
) {
safely(onException) {
block(it)
}
}

View File

@@ -1,21 +0,0 @@
package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.flow.*
@Suppress("FunctionName")
fun <T> BroadcastFlow(
internalChannelSize: Int = Channel.BUFFERED
): BroadcastFlow<T> {
val channel = BroadcastChannel<T>(internalChannelSize)
return BroadcastFlow(
channel,
channel.asFlow()
)
}
class BroadcastFlow<T> internal constructor(
private val channel: BroadcastChannel<T>,
private val flow: Flow<T>
): Flow<T> by flow, SendChannel<T> by channel

View File

@@ -1,33 +0,0 @@
package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.*
class BroadcastStateFlow<T> internal constructor(
parentFlow: Flow<T>,
private val stateGetter: () -> T
) : StateFlow<T>, Flow<T> by parentFlow {
override val value: T
get() = stateGetter()
}
fun <T> BroadcastChannel<T>.asStateFlow(value: T, scope: CoroutineScope): StateFlow<T> = asFlow().let {
var state: T = value
it.onEach { state = it }.launchIn(scope)
BroadcastStateFlow(it) {
state
}
}
fun <T> BroadcastChannel<T?>.asStateFlow(scope: CoroutineScope): StateFlow<T?> = asStateFlow(null, scope)
fun <T> broadcastStateFlow(initial: T, scope: CoroutineScope, channelSize: Int = Channel.BUFFERED) = BroadcastChannel<T>(
channelSize
).let {
it to it.asStateFlow(initial, scope)
}
fun <T> broadcastStateFlow(scope: CoroutineScope, channelSize: Int = Channel.BUFFERED) = broadcastStateFlow<T?>(null, scope, channelSize)

View File

@@ -0,0 +1,37 @@
@file:Suppress("NOTHING_TO_INLINE")
package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.*
/**
* Shortcut for chain if [Flow.onEach] and [Flow.launchIn]
*/
inline fun <T> Flow<T>.subscribe(scope: CoroutineScope, noinline block: suspend (T) -> Unit) = onEach(block).launchIn(scope)
/**
* Use [subscribe], but all [block]s will be called inside of [safely] function.
* Use [onException] to set up your reaction for [Throwable]s
*/
inline fun <T> Flow<T>.subscribeSafely(
scope: CoroutineScope,
noinline onException: ExceptionHandler<Unit> = { throw it },
noinline block: suspend (T) -> Unit
) = subscribe(scope) {
safely(onException) {
block(it)
}
}
/**
* Use [subscribeSafelyWithoutExceptions], but all exceptions inside of [safely] will be skipped
*/
inline fun <T> Flow<T>.subscribeSafelyWithoutExceptions(
scope: CoroutineScope,
noinline block: suspend (T) -> Unit
) = subscribeSafely(
scope,
{},
block
)

View File

@@ -21,3 +21,10 @@ suspend inline fun <T> safely(
onException(e)
}
}
/**
* Shortcut for [safely] without exception handler (instead of this you will receive null as a result)
*/
suspend inline fun <T> safelyWithoutExceptions(
noinline block: suspend CoroutineScope.() -> T
): T? = safely({ null }, block)

88
dokka/build.gradle Normal file
View File

@@ -0,0 +1,88 @@
buildscript {
repositories {
mavenLocal()
jcenter()
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
classpath "org.jetbrains.dokka:dokka-gradle-plugin:$dokka_version"
}
}
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
id "org.jetbrains.dokka" version "$dokka_version"
}
repositories {
mavenLocal()
jcenter()
mavenCentral()
}
kotlin {
jvm()
js(BOTH) {
browser()
nodejs()
}
sourceSets {
commonMain {
dependencies {
implementation kotlin('stdlib')
project.parent.subprojects.forEach {
if (
it != project
&& it.hasProperty("kotlin")
&& it.kotlin.sourceSets.any { it.name.contains("commonMain") }
) {
api it
}
}
}
}
}
}
private List<SourceDirectorySet> findSourcesWithName(String... approximateNames) {
return parent.subprojects
.findAll { it != project && it.hasProperty("kotlin") }
.collectMany { it.kotlin.sourceSets }
.findAll { sourceSet ->
approximateNames.any { nameToFilter ->
sourceSet.name.contains(nameToFilter)
}
}.collect { it.kotlin }
}
tasks.dokkaHtml {
dokkaSourceSets {
configureEach {
skipDeprecated.set(true)
sourceLink {
localDirectory.set(file("./"))
remoteUrl.set(new URL("https://github.com/InsanusMokrassar/MicroUtils/blob/master/"))
remoteLineSuffix.set("#L")
}
}
named("commonMain") {
sourceRoots.setFrom(findSourcesWithName("commonMain"))
}
named("jsMain") {
sourceRoots.setFrom(findSourcesWithName("jsMain", "commonMain"))
}
named("jvmMain") {
sourceRoots.setFrom(findSourcesWithName("jvmMain", "commonMain"))
}
}
}

3
dokka/gradle.properties Normal file
View File

@@ -0,0 +1,3 @@
dokka_version=1.4.0
org.gradle.jvmargs=-Xmx1024m

View File

@@ -1,8 +1,11 @@
private String getCurrentVersionChangelog() {
OutputStream changelogDataOS = new ByteArrayOutputStream()
exec {
commandLine 'chmod', "+x", './changelog_parser.sh'
}
exec {
standardOutput = changelogDataOS
commandLine './changelog_info_retriever', "$library_version", 'CHANGELOG.md'
commandLine './changelog_parser.sh', "${project.version}", 'CHANGELOG.md'
}
return changelogDataOS.toString().trim()
@@ -18,9 +21,9 @@ if (new File(projectDir, "secret.gradle").exists()) {
owner "InsanusMokrassar"
repo "MicroUtils"
tagName "$library_version"
releaseName "$library_version"
targetCommitish "$library_version"
tagName "${project.version}"
releaseName "${project.version}"
targetCommitish "${project.version}"
body getCurrentVersionChangelog()
}

View File

@@ -5,9 +5,9 @@ kotlin.incremental=true
kotlin.incremental.js=true
kotlin_version=1.4.10
kotlin_coroutines_version=1.3.9
kotlin_serialisation_core_version=1.0.0
kotlin_exposed_version=0.27.1
kotlin_coroutines_version=1.4.1
kotlin_serialisation_core_version=1.0.1
kotlin_exposed_version=0.28.1
ktor_version=1.4.1
@@ -19,4 +19,4 @@ github_release_plugin_version=2.2.12
uuidVersion=0.2.2
group=dev.inmo
version=0.1.1
version=0.3.0

View File

@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@@ -4,7 +4,8 @@ import dev.inmo.micro_utils.coroutines.safely
import dev.inmo.micro_utils.ktor.common.*
import io.ktor.client.HttpClient
import io.ktor.client.features.websocket.ws
import io.ktor.http.cio.websocket.*
import io.ktor.http.cio.websocket.Frame
import io.ktor.http.cio.websocket.readBytes
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.channelFlow
import kotlinx.serialization.DeserializationStrategy

View File

@@ -4,7 +4,8 @@ import dev.inmo.micro_utils.ktor.common.*
import io.ktor.client.HttpClient
import io.ktor.client.request.get
import io.ktor.client.request.post
import kotlinx.serialization.*
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.SerializationStrategy
typealias BodyPair<T> = Pair<SerializationStrategy<T>, T>

View File

@@ -22,13 +22,11 @@ fun <T> Route.includeWebsocketHandling(
converter: (T) -> StandardKtorSerialInputData
) {
webSocket(suburl) {
// println("connected")
safely {
flow.collect {
send(converter(it))
}
}
// println("disconnected")
}
}

View File

@@ -7,8 +7,8 @@ import io.ktor.http.HttpStatusCode
import io.ktor.request.receive
import io.ktor.response.respond
import io.ktor.response.respondBytes
import io.ktor.util.toByteArray
import kotlinx.serialization.*
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.SerializationStrategy
suspend fun <T> ApplicationCall.unianswer(
answerSerializer: SerializationStrategy<T>,

View File

@@ -3,13 +3,6 @@ project.group = "$group"
apply from: "$publishGradlePath"
repositories {
jcenter()
mavenCentral()
maven { url "https://kotlin.bintray.com/kotlinx" }
mavenLocal()
}
kotlin {
jvm()

View File

@@ -3,13 +3,6 @@ project.group = "$group"
apply from: "$publishGradlePath"
repositories {
jcenter()
mavenCentral()
maven { url "https://kotlin.bintray.com/kotlinx" }
mavenLocal()
}
kotlin {
jvm()
js (BOTH) {

View File

@@ -28,6 +28,15 @@ interface Pagination {
val Pagination.firstIndex: Int
get() = page * size
/**
* Last number in index of objects. In fact, one [Pagination] object represent data in next range:
*
* [[firstIndex], [lastIndex]]; That means, that for [Pagination] with [Pagination.size] == 10 and [Pagination.page] == 1
* you will retrieve [Pagination.firstIndex] == 10 and [Pagination.lastIndex] == 19. Here [Pagination.lastIndexExclusive] == 20
*/
val Pagination.lastIndexExclusive: Int
get() = firstIndex + size
/**
* Last number in index of objects. In fact, one [Pagination] object represent data in next range:
*
@@ -35,7 +44,7 @@ val Pagination.firstIndex: Int
* you will retrieve [Pagination.firstIndex] == 10 and [Pagination.lastIndex] == 19.
*/
val Pagination.lastIndex: Int
get() = firstIndex + size - 1
get() = lastIndexExclusive - 1
/**
* Calculates pages count for given [datasetSize]
@@ -43,6 +52,11 @@ val Pagination.lastIndex: Int
fun calculatePagesNumber(datasetSize: Long, pageSize: Int): Int {
return ceil(datasetSize.toDouble() / pageSize).toInt()
}
/**
* Calculates pages count for given [datasetSize]. As a fact, it is shortcut for [calculatePagesNumber]
* @return calculated page number which can be correctly used in [PaginationResult] as [PaginationResult.page] value
*/
fun calculatePagesNumber(pageSize: Int, datasetSize: Long): Int = calculatePagesNumber(datasetSize, pageSize)
/**
* Calculates pages count for given [datasetSize]
*/

View File

@@ -14,6 +14,8 @@ inline fun FirstPagePagination(size: Int = defaultMediumPageSize) =
size = size
)
val emptyPagination = Pagination(0, 0)
@Suppress("NOTHING_TO_INLINE")
inline fun Pagination.nextPage() =
SimplePagination(
@@ -27,7 +29,21 @@ data class SimplePagination(
override val size: Int
) : Pagination
/**
* Factory for [SimplePagination]
*/
fun Pagination(
page: Int,
size: Int
) = SimplePagination(page, size)
/**
* @param firstIndex Inclusive first index of pagination
* @param lastIndex INCLUSIVE last index of pagination (last index of object covered by result [SimplePagination])
*/
fun PaginationByIndexes(
firstIndex: Int,
lastIndex: Int
) = maxOf(0, (lastIndex - firstIndex + 1)).let { size ->
Pagination(calculatePage(firstIndex, size), size)
}

View File

@@ -21,7 +21,12 @@ fun <T> Iterable<T>.paginate(with: Pagination): PaginationResult<T> {
}
fun <T> List<T>.paginate(with: Pagination): PaginationResult<T> {
return subList(with.firstIndex, with.lastIndex + 1).createPaginationResult(
val firstIndex = maxOf(with.firstIndex, 0)
val lastIndex = minOf(with.lastIndexExclusive, size)
if (firstIndex > lastIndex) {
return emptyPaginationResult()
}
return subList(firstIndex, lastIndex).createPaginationResult(
with,
size.toLong()
)

View File

@@ -0,0 +1,28 @@
package dev.inmo.micro_utils.pagination.utils
import dev.inmo.micro_utils.pagination.*
/**
* Example:
*
* * `|__f__l_______________________|` will be transformed to `|_______________________f__l__|`
* * `|__f__l_|` will be transformed to `|__f__l_|`
*
* @return Reversed version of this [Pagination]
*/
fun Pagination.reverse(datasetSize: Long): SimplePagination {
val pagesNumber = calculatePagesNumber(size, datasetSize)
val newPage = pagesNumber - page - 1
return when {
page < 0 || page >= pagesNumber -> emptyPagination
else -> Pagination(
newPage,
size
)
}
}
/**
* Shortcut for [reverse]
*/
fun Pagination.reverse(objectsCount: Int) = reverse(objectsCount.toLong())

View File

@@ -0,0 +1,24 @@
package dev.inmo.micro_utils.pagination.utils
import dev.inmo.micro_utils.pagination.*
import kotlin.test.Test
import kotlin.test.assertEquals
class PaginationReversingTests {
@Test
fun testThatCommonCaseWorksOk() {
val pageSize = 3
val collectionSize = 9
assertEquals(Pagination(-1, pageSize).reverse(collectionSize), Pagination(0, 0))
val middleFirstIndex = collectionSize / 2 - pageSize / 2
val middleLastIndex = middleFirstIndex + pageSize - 1
assertEquals(
PaginationByIndexes(middleFirstIndex, middleLastIndex).reverse(collectionSize),
PaginationByIndexes(middleFirstIndex, middleLastIndex)
)
assertEquals(Pagination(calculatePagesNumber(collectionSize, pageSize), pageSize).reverse(collectionSize), Pagination(0, 0))
}
}

View File

@@ -0,0 +1,9 @@
package dev.inmo.micro_utils.repos
interface MapperRepo<FromKey, FromValue, ToKey, ToValue> {
suspend fun FromKey.toOutKey() = this as ToKey
suspend fun FromValue.toOutValue() = this as ToValue
suspend fun ToKey.toInnerKey() = this as FromKey
suspend fun ToValue.toInnerValue() = this as FromValue
}

View File

@@ -1,7 +1,6 @@
package dev.inmo.micro_utils.repos
import dev.inmo.micro_utils.pagination.Pagination
import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.pagination.*
import kotlinx.coroutines.flow.Flow
interface ReadOneToManyKeyValueRepo<Key, Value> : Repo {
@@ -11,20 +10,76 @@ interface ReadOneToManyKeyValueRepo<Key, Value> : Repo {
suspend fun contains(k: Key, v: Value): Boolean
suspend fun count(k: Key): Long
suspend fun count(): Long
suspend fun getAll(k: Key, reversed: Boolean = false): List<Value> = mutableListOf<Value>().also { list ->
doWithPagination {
get(k, it).also {
list.addAll(it.results)
}.nextPageIfNotEmpty()
}
if (reversed) {
list.reverse()
}
}
/**
* WARNING!!! THIS METHOD PROBABLY IS NOT EFFICIENT, USE WITH CAUTION
*/
suspend fun getAll(reverseLists: Boolean = false): Map<Key, List<Value>> = mutableMapOf<Key, List<Value>>().also { map ->
doWithPagination {
keys(it).also { paginationResult ->
paginationResult.results.forEach { k ->
map[k] = getAll(k, reverseLists)
}
}.nextPageIfNotEmpty()
}
}
}
@Deprecated("Renamed", ReplaceWith("ReadOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo"))
typealias OneToManyReadKeyValueRepo<Key, Value> = ReadOneToManyKeyValueRepo<Key, Value>
interface WriteOneToManyKeyValueRepo<Key, Value> : Repo {
val onNewValue: Flow<Pair<Key, Value>>
val onValueRemoved: Flow<Pair<Key, Value>>
val onDataCleared: Flow<Key>
suspend fun add(k: Key, v: Value)
suspend fun remove(k: Key, v: Value)
suspend fun add(toAdd: Map<Key, List<Value>>)
suspend fun remove(toRemove: Map<Key, List<Value>>)
suspend fun clear(k: Key)
}
@Deprecated("Renamed", ReplaceWith("WriteOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo"))
typealias OneToManyWriteKeyValueRepo<Key, Value> = WriteOneToManyKeyValueRepo<Key, Value>
interface OneToManyKeyValueRepo<Key, Value> : ReadOneToManyKeyValueRepo<Key, Value>, WriteOneToManyKeyValueRepo<Key, Value>
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.add(
keysAndValues: List<Pair<Key, List<Value>>>
) = add(keysAndValues.toMap())
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.add(
vararg keysAndValues: Pair<Key, List<Value>>
) = add(keysAndValues.toMap())
suspend inline fun <Key, Value> WriteOneToManyKeyValueRepo<Key, Value>.add(
k: Key, v: List<Value>
) = add(mapOf(k to v))
suspend inline fun <Key, Value> WriteOneToManyKeyValueRepo<Key, Value>.add(
k: Key, vararg v: Value
) = add(k, v.toList())
interface OneToManyKeyValueRepo<Key, Value> : ReadOneToManyKeyValueRepo<Key, Value>, WriteOneToManyKeyValueRepo<Key, Value>
suspend inline fun <Key, Value> WriteOneToManyKeyValueRepo<Key, Value>.remove(
keysAndValues: List<Pair<Key, List<Value>>>
) = remove(keysAndValues.toMap())
suspend inline fun <Key, Value> WriteOneToManyKeyValueRepo<Key, Value>.remove(
vararg keysAndValues: Pair<Key, List<Value>>
) = remove(keysAndValues.toMap())
suspend inline fun <Key, Value> WriteOneToManyKeyValueRepo<Key, Value>.remove(
k: Key,
v: List<Value>
) = remove(mapOf(k to v))
suspend inline fun <Key, Value> WriteOneToManyKeyValueRepo<Key, Value>.remove(
k: Key,
vararg v: Value
) = remove(k, v.toList())

View File

@@ -16,8 +16,20 @@ interface WriteStandardKeyValueRepo<Key, Value> : Repo {
val onNewValue: Flow<Pair<Key, Value>>
val onValueRemoved: Flow<Key>
suspend fun set(k: Key, v: Value)
suspend fun unset(k: Key)
suspend fun set(toSet: Map<Key, Value>)
suspend fun unset(toUnset: List<Key>)
}
suspend inline fun <Key, Value> WriteStandardKeyValueRepo<Key, Value>.set(
vararg toSet: Pair<Key, Value>
) = set(toSet.toMap())
suspend inline fun <Key, Value> WriteStandardKeyValueRepo<Key, Value>.set(
k: Key, v: Value
) = set(k to v)
suspend inline fun <Key, Value> WriteStandardKeyValueRepo<Key, Value>.unset(
vararg k: Key
) = unset(k.toList())
interface StandardKeyValueRepo<Key, Value> : ReadStandardKeyValueRepo<Key, Value>, WriteStandardKeyValueRepo<Key, Value>

View File

@@ -0,0 +1,84 @@
package dev.inmo.micro_utils.repos.mappers
import dev.inmo.micro_utils.pagination.Pagination
import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.repos.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
open class MapperReadStandardKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
private val to: ReadStandardKeyValueRepo<ToKey, ToValue>,
mapper: MapperRepo<FromKey, FromValue, ToKey, ToValue>
) : ReadStandardKeyValueRepo<FromKey, FromValue>, MapperRepo<FromKey, FromValue, ToKey, ToValue> by mapper {
override suspend fun get(k: FromKey): FromValue? = to.get(
k.toOutKey()
) ?.toInnerValue()
override suspend fun values(
pagination: Pagination,
reversed: Boolean
): PaginationResult<FromValue> = to.values(
pagination,
reversed
).let {
PaginationResult(
it.page,
it.pagesNumber,
it.results.map { it.toInnerValue() },
it.size
)
}
override suspend fun keys(
pagination: Pagination,
reversed: Boolean
): PaginationResult<FromKey> = to.keys(
pagination,
reversed
).let {
PaginationResult(
it.page,
it.pagesNumber,
it.results.map { it.toInnerKey() },
it.size
)
}
override suspend fun contains(key: FromKey): Boolean = to.contains(
key.toOutKey()
)
override suspend fun count(): Long = to.count()
}
open class MapperWriteStandardKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
private val to: WriteStandardKeyValueRepo<ToKey, ToValue>,
mapper: MapperRepo<FromKey, FromValue, ToKey, ToValue>
) : WriteStandardKeyValueRepo<FromKey, FromValue>, MapperRepo<FromKey, FromValue, ToKey, ToValue> by mapper {
override val onNewValue: Flow<Pair<FromKey, FromValue>> = to.onNewValue.map { (k, v) ->
k.toInnerKey() to v.toInnerValue()
}
override val onValueRemoved: Flow<FromKey> = to.onValueRemoved.map { k ->
k.toInnerKey()
}
override suspend fun set(toSet: Map<FromKey, FromValue>) = to.set(
toSet.map { (k, v) ->
k.toOutKey() to v.toOutValue()
}.toMap()
)
override suspend fun unset(toUnset: List<FromKey>) = to.unset(
toUnset.map { k ->
k.toOutKey()
}
)
}
open class MapperStandardKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
private val to: StandardKeyValueRepo<ToKey, ToValue>,
mapper: MapperRepo<FromKey, FromValue, ToKey, ToValue>
) : StandardKeyValueRepo<FromKey, FromValue>,
MapperRepo<FromKey, FromValue, ToKey, ToValue> by mapper,
ReadStandardKeyValueRepo<FromKey, FromValue> by MapperReadStandardKeyValueRepo(to, mapper),
WriteStandardKeyValueRepo<FromKey, FromValue> by MapperWriteStandardKeyValueRepo(to, mapper)

View File

@@ -0,0 +1,87 @@
package dev.inmo.micro_utils.repos.mappers
import dev.inmo.micro_utils.pagination.Pagination
import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.repos.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
open class MapperReadOneToManyKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
private val to: ReadOneToManyKeyValueRepo<ToKey, ToValue>,
mapper: MapperRepo<FromKey, FromValue, ToKey, ToValue>
) : ReadOneToManyKeyValueRepo<FromKey, FromValue>, MapperRepo<FromKey, FromValue, ToKey, ToValue> by mapper {
override suspend fun get(
k: FromKey,
pagination: Pagination,
reversed: Boolean
): PaginationResult<FromValue> = to.get(
k.toOutKey(),
pagination,
reversed
).let {
PaginationResult(
it.page,
it.pagesNumber,
it.results.map { it.toInnerValue() },
it.size
)
}
override suspend fun keys(
pagination: Pagination,
reversed: Boolean
): PaginationResult<FromKey> = to.keys(
pagination,
reversed
).let {
PaginationResult(
it.page,
it.pagesNumber,
it.results.map { it.toInnerKey() },
it.size
)
}
override suspend fun contains(k: FromKey): Boolean = to.contains(k.toOutKey())
override suspend fun contains(k: FromKey, v: FromValue): Boolean = to.contains(k.toOutKey(), v.toOutValue())
override suspend fun count(): Long = to.count()
override suspend fun count(k: FromKey): Long = to.count(k.toOutKey())
}
open class MapperWriteOneToManyKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
private val to: WriteOneToManyKeyValueRepo<ToKey, ToValue>,
mapper: MapperRepo<FromKey, FromValue, ToKey, ToValue>
) : WriteOneToManyKeyValueRepo<FromKey, FromValue>, MapperRepo<FromKey, FromValue, ToKey, ToValue> by mapper {
override val onNewValue: Flow<Pair<FromKey, FromValue>> = to.onNewValue.map { (k, v) ->
k.toInnerKey() to v.toInnerValue()
}
override val onValueRemoved: Flow<Pair<FromKey, FromValue>> = to.onValueRemoved.map { (k, v) ->
k.toInnerKey() to v.toInnerValue()
}
override val onDataCleared: Flow<FromKey> = to.onDataCleared.map { k ->
k.toInnerKey()
}
override suspend fun add(toAdd: Map<FromKey, List<FromValue>>) = to.add(
toAdd.map { (k, v) ->
k.toOutKey() to v.map { it.toOutValue() }
}.toMap()
)
override suspend fun remove(toRemove: Map<FromKey, List<FromValue>>) = to.remove(
toRemove.map { (k, v) ->
k.toOutKey() to v.map { it.toOutValue() }
}.toMap()
)
override suspend fun clear(k: FromKey) = to.clear(k.toOutKey())
}
open class MapperOneToManyKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
private val to: OneToManyKeyValueRepo<ToKey, ToValue>,
mapper: MapperRepo<FromKey, FromValue, ToKey, ToValue>
) : OneToManyKeyValueRepo<FromKey, FromValue>,
MapperRepo<FromKey, FromValue, ToKey, ToValue> by mapper,
ReadOneToManyKeyValueRepo<FromKey, FromValue> by MapperReadOneToManyKeyValueRepo(to, mapper),
WriteOneToManyKeyValueRepo<FromKey, FromValue> by MapperWriteOneToManyKeyValueRepo(to, mapper)

View File

@@ -1,7 +1,7 @@
package dev.inmo.micro_utils.repos.pagination
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.*
import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo
suspend inline fun <Key, Value, REPO : ReadStandardKeyValueRepo<Key, Value>> REPO.doForAll(
@Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE")

View File

@@ -1,7 +1,7 @@
package dev.inmo.micro_utils.repos.pagination
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.*
import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo
suspend inline fun <Key, Value, REPO : ReadOneToManyKeyValueRepo<Key, Value>> REPO.doForAll(
@Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE")

View File

@@ -9,6 +9,7 @@ kotlin {
sourceSets {
commonMain {
dependencies {
api internalProject("micro_utils.coroutines")
api internalProject("micro_utils.repos.common")
api internalProject("micro_utils.pagination.exposed")
}

View File

@@ -1,9 +1,7 @@
package dev.inmo.micro_utils.repos.exposed
import kotlinx.coroutines.channels.Channel
abstract class AbstractExposedCRUDRepo<ObjectType, IdType, InputValueType>(
flowsChannelsSize: Int = Channel.BUFFERED,
flowsChannelsSize: Int = 0,
tableName: String = ""
) :
AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(

View File

@@ -3,28 +3,27 @@ package dev.inmo.micro_utils.repos.exposed
import dev.inmo.micro_utils.repos.UpdatedValuePair
import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.*
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.statements.InsertStatement
import org.jetbrains.exposed.sql.statements.UpdateStatement
import org.jetbrains.exposed.sql.transactions.transaction
abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
flowsChannelsSize: Int = 64,
flowsChannelsSize: Int = 0,
tableName: String = ""
) :
AbstractExposedReadCRUDRepo<ObjectType, IdType>(tableName),
ExposedCRUDRepo<ObjectType, IdType>,
WriteStandardCRUDRepo<ObjectType, IdType, InputValueType>
{
protected val newObjectsChannel = BroadcastChannel<ObjectType>(flowsChannelsSize)
protected val updateObjectsChannel = BroadcastChannel<ObjectType>(flowsChannelsSize)
protected val deleteObjectsIdsChannel = BroadcastChannel<IdType>(flowsChannelsSize)
protected val newObjectsChannel = MutableSharedFlow<ObjectType>(flowsChannelsSize)
protected val updateObjectsChannel = MutableSharedFlow<ObjectType>(flowsChannelsSize)
protected val deleteObjectsIdsChannel = MutableSharedFlow<IdType>(flowsChannelsSize)
override val newObjectsFlow: Flow<ObjectType> = newObjectsChannel.asFlow()
override val updatedObjectsFlow: Flow<ObjectType> = updateObjectsChannel.asFlow()
override val deletedObjectsIdsFlow: Flow<IdType> = deleteObjectsIdsChannel.asFlow()
override val newObjectsFlow: Flow<ObjectType> = newObjectsChannel.asSharedFlow()
override val updatedObjectsFlow: Flow<ObjectType> = updateObjectsChannel.asSharedFlow()
override val deletedObjectsIdsFlow: Flow<IdType> = deleteObjectsIdsChannel.asSharedFlow()
abstract val InsertStatement<Number>.asObject: ObjectType
abstract val selectByIds: SqlExpressionBuilder.(List<out IdType>) -> Op<Boolean>
@@ -45,7 +44,7 @@ abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
values.map { value -> createWithoutNotification(value) }
}.also {
it.forEach {
newObjectsChannel.send(it)
newObjectsChannel.emit(it)
}
}
}
@@ -75,7 +74,7 @@ abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
onBeforeUpdate(listOf(id to value))
return updateWithoutNotification(id, value).also {
if (it != null) {
updateObjectsChannel.send(it)
updateObjectsChannel.emit(it)
}
}
}
@@ -89,7 +88,7 @@ abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
} as List<ObjectType>
).also {
it.forEach {
updateObjectsChannel.send(it)
updateObjectsChannel.emit(it)
}
}
}

View File

@@ -0,0 +1,6 @@
package dev.inmo.micro_utils.repos.exposed
import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.Table
typealias ColumnAllocator<T> = Table.() -> Column<T>

View File

@@ -1,11 +1,8 @@
package dev.inmo.micro_utils.repos.exposed
import dev.inmo.micro_utils.repos.Repo
import org.jetbrains.exposed.sql.*
interface ExposedCRUDRepo<ObjectType, IdType> : Repo {
val database: Database
interface ExposedCRUDRepo<ObjectType, IdType> : ExposedRepo {
val ResultRow.asObject: ObjectType
val selectById: SqlExpressionBuilder.(IdType) -> Op<Boolean>
}

View File

@@ -0,0 +1,8 @@
package dev.inmo.micro_utils.repos.exposed
import dev.inmo.micro_utils.repos.Repo
import org.jetbrains.exposed.sql.Database
interface ExposedRepo : Repo {
val database: Database
}

View File

@@ -6,3 +6,5 @@ import org.jetbrains.exposed.sql.transactions.transaction
fun Table.initTable(database: Database) {
transaction(database) { SchemaUtils.createMissingTablesAndColumns(this@initTable) }
}
fun <T> T.initTable() where T: ExposedRepo, T: Table = initTable(database)

View File

@@ -1,48 +0,0 @@
package dev.inmo.micro_utils.repos.exposed.keyvalue
import dev.inmo.micro_utils.repos.StandardKeyValueRepo
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
abstract class AbstractExposedKeyValueRepo<Key, Value>(
database: Database,
keyColumn: Column<Key>,
valueColumn: Column<Value>
) : StandardKeyValueRepo<Key, Value>, AbstractExposedReadKeyValueRepo<Key, Value>(
database,
keyColumn,
valueColumn
) {
private val onNewValueChannel = BroadcastChannel<Pair<Key, Value>>(Channel.BUFFERED)
private val onValueRemovedChannel = BroadcastChannel<Key>(Channel.BUFFERED)
override val onNewValue: Flow<Pair<Key, Value>> = onNewValueChannel.asFlow()
override val onValueRemoved: Flow<Key> = onValueRemovedChannel.asFlow()
override suspend fun set(k: Key, v: Value) {
transaction(database) {
if (select { keyColumn.eq(k) }.limit(1).any()) {
update({ keyColumn.eq(k) }) {
it[valueColumn] = v
}
} else {
insert {
it[keyColumn] = k
it[valueColumn] = v
}
}
}
onNewValueChannel.send(k to v)
}
override suspend fun unset(k: Key) {
transaction(database) {
deleteWhere { keyColumn.eq(k) }
}
onValueRemovedChannel.send(k)
}
}

View File

@@ -0,0 +1,62 @@
package dev.inmo.micro_utils.repos.exposed.keyvalue
import dev.inmo.micro_utils.repos.StandardKeyValueRepo
import dev.inmo.micro_utils.repos.exposed.ColumnAllocator
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.*
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
open class ExposedKeyValueRepo<Key, Value>(
database: Database,
keyColumnAllocator: ColumnAllocator<Key>,
valueColumnAllocator: ColumnAllocator<Value>,
tableName: String? = null
) : StandardKeyValueRepo<Key, Value>, ExposedReadKeyValueRepo<Key, Value>(
database,
keyColumnAllocator,
valueColumnAllocator,
tableName
) {
private val _onNewValue = MutableSharedFlow<Pair<Key, Value>>(Channel.BUFFERED)
private val _onValueRemoved = MutableSharedFlow<Key>(Channel.BUFFERED)
override val onNewValue: Flow<Pair<Key, Value>> = _onNewValue.asSharedFlow()
override val onValueRemoved: Flow<Key> = _onValueRemoved.asSharedFlow()
override suspend fun set(toSet: Map<Key, Value>) {
transaction(database) {
toSet.mapNotNull { (k, v) ->
if (update({ keyColumn.eq(k) }) { it[valueColumn] = v } > 0) {
k to v
} else {
val inserted = insert {
it[keyColumn] = k
it[valueColumn] = v
}.getOrNull(keyColumn) != null
if (inserted) {
k to v
} else {
null
}
}
}
}.forEach {
_onNewValue.emit(it)
}
}
override suspend fun unset(toUnset: List<Key>) {
transaction(database) {
toUnset.mapNotNull {
if (deleteWhere { keyColumn.eq(it) } > 0) {
it
} else {
null
}
}
}.forEach {
_onValueRemoved.emit(it)
}
}
}

View File

@@ -2,14 +2,19 @@ package dev.inmo.micro_utils.repos.exposed.keyvalue
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo
import dev.inmo.micro_utils.repos.exposed.ColumnAllocator
import dev.inmo.micro_utils.repos.exposed.ExposedRepo
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
abstract class AbstractExposedReadKeyValueRepo<Key, Value>(
protected val database: Database,
protected val keyColumn: Column<Key>,
protected val valueColumn: Column<Value>
) : ReadStandardKeyValueRepo<Key, Value>, Table() {
open class ExposedReadKeyValueRepo<Key, Value>(
override val database: Database,
keyColumnAllocator: ColumnAllocator<Key>,
valueColumnAllocator: ColumnAllocator<Value>,
tableName: String? = null
) : ReadStandardKeyValueRepo<Key, Value>, ExposedRepo, Table(tableName ?: "") {
protected val keyColumn: Column<Key> = keyColumnAllocator()
protected val valueColumn: Column<Value> = valueColumnAllocator()
override val primaryKey: PrimaryKey = PrimaryKey(keyColumn, valueColumn)
override suspend fun get(k: Key): Value? = transaction(database) {

View File

@@ -1,35 +0,0 @@
package dev.inmo.micro_utils.repos.exposed.onetomany
import dev.inmo.micro_utils.repos.OneToManyKeyValueRepo
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
abstract class AbstractExposedOneToManyKeyValueRepo<Key, Value>(
keyColumnAllocator: ColumnAllocator<Key>,
valueColumnAllocator: ColumnAllocator<Value>,
database: Database
) : OneToManyKeyValueRepo<Key, Value>, AbstractExposedReadOneToManyKeyValueRepo<Key, Value>(
keyColumnAllocator,
valueColumnAllocator,
database
) {
override suspend fun add(k: Key, v: Value) {
transaction(database) {
insert {
it[keyColumn] = k
it[valueColumn] = v
}
}
}
override suspend fun remove(k: Key, v: Value) {
transaction(database) { deleteWhere { keyColumn.eq(k).and(valueColumn.eq(v)) } }
}
override suspend fun clear(k: Key) {
transaction(database) { deleteWhere { keyColumn.eq(k) } }
}
}
@Deprecated("Renamed", ReplaceWith("AbstractExposedOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.exposed.onetomany.AbstractExposedOneToManyKeyValueRepo"))
typealias AbstractOneToManyExposedKeyValueRepo<Key, Value> = AbstractExposedOneToManyKeyValueRepo<Key, Value>

View File

@@ -0,0 +1,64 @@
package dev.inmo.micro_utils.repos.exposed.onetomany
import dev.inmo.micro_utils.repos.OneToManyKeyValueRepo
import dev.inmo.micro_utils.repos.exposed.ColumnAllocator
import kotlinx.coroutines.flow.*
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
open class ExposedOneToManyKeyValueRepo<Key, Value>(
database: Database,
keyColumnAllocator: ColumnAllocator<Key>,
valueColumnAllocator: ColumnAllocator<Value>,
tableName: String? = null
) : OneToManyKeyValueRepo<Key, Value>, ExposedReadOneToManyKeyValueRepo<Key, Value>(
database,
keyColumnAllocator,
valueColumnAllocator,
tableName
) {
protected val _onNewValue: MutableSharedFlow<Pair<Key, Value>> = MutableSharedFlow()
override val onNewValue: Flow<Pair<Key, Value>>
get() = _onNewValue
protected val _onValueRemoved: MutableSharedFlow<Pair<Key, Value>> = MutableSharedFlow()
override val onValueRemoved: Flow<Pair<Key, Value>>
get() = _onValueRemoved
protected val _onDataCleared: MutableSharedFlow<Key> = MutableSharedFlow()
override val onDataCleared: Flow<Key>
get() = _onDataCleared
override suspend fun add(toAdd: Map<Key, List<Value>>) {
transaction(database) {
toAdd.keys.flatMap { k ->
toAdd[k] ?.mapNotNull { v ->
insertIgnore {
it[keyColumn] = k
it[valueColumn] = v
}.getOrNull(keyColumn) ?.let { k to v }
} ?: emptyList()
}
}.forEach { _onNewValue.emit(it) }
}
override suspend fun remove(toRemove: Map<Key, List<Value>>) {
transaction(database) {
toRemove.keys.flatMap { k ->
toRemove[k] ?.mapNotNull { v ->
if (deleteIgnoreWhere { keyColumn.eq(k).and(valueColumn.eq(v)) } > 0 ) {
k to v
} else {
null
}
} ?: emptyList()
}
}.forEach {
_onValueRemoved.emit(it)
}
}
override suspend fun clear(k: Key) {
transaction(database) {
deleteWhere { keyColumn.eq(k) }
}.also { _onDataCleared.emit(k) }
}
}

View File

@@ -2,19 +2,21 @@ package dev.inmo.micro_utils.repos.exposed.onetomany
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo
import dev.inmo.micro_utils.repos.exposed.*
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
typealias ColumnAllocator<T> = Table.() -> Column<T>
abstract class AbstractExposedReadOneToManyKeyValueRepo<Key, Value>(
open class ExposedReadOneToManyKeyValueRepo<Key, Value>(
override val database: Database,
keyColumnAllocator: ColumnAllocator<Key>,
valueColumnAllocator: ColumnAllocator<Value>,
protected val database: Database
) : ReadOneToManyKeyValueRepo<Key, Value>, Table() {
tableName: String? = null
) : ReadOneToManyKeyValueRepo<Key, Value>, ExposedRepo, Table(tableName ?: "") {
protected val keyColumn: Column<Key> = keyColumnAllocator()
protected val valueColumn: Column<Value> = valueColumnAllocator()
init { initTable() }
override suspend fun count(k: Key): Long = transaction(database) { select { keyColumn.eq(k) }.count() }
override suspend fun count(): Long = transaction(database) { selectAll().count() }
@@ -45,6 +47,3 @@ abstract class AbstractExposedReadOneToManyKeyValueRepo<Key, Value>(
select { keyColumn.eq(k).and(valueColumn.eq(v)) }.limit(1).any()
}
}
@Deprecated("Renamed", ReplaceWith("AbstractExposedReadOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.exposed.onetomany.AbstractExposedReadOneToManyKeyValueRepo"))
typealias AbstractOneToManyExposedReadKeyValueRepo<Key, Value> = AbstractExposedReadOneToManyKeyValueRepo<Key, Value>

View File

@@ -1,8 +1,7 @@
package dev.inmo.micro_utils.repos
import dev.inmo.micro_utils.coroutines.BroadcastFlow
import dev.inmo.micro_utils.pagination.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.*
class ReadMapCRUDRepo<ObjectType, IdType>(
private val map: Map<IdType, ObjectType> = emptyMap()
@@ -26,15 +25,12 @@ class ReadMapCRUDRepo<ObjectType, IdType>(
abstract class WriteMapCRUDRepo<ObjectType, IdType, InputValueType>(
private val map: MutableMap<IdType, ObjectType> = mutableMapOf()
) : WriteStandardCRUDRepo<ObjectType, IdType, InputValueType> {
private val _newObjectsFlow: BroadcastFlow<ObjectType> = BroadcastFlow()
override val newObjectsFlow: Flow<ObjectType>
get() = _newObjectsFlow
private val _updatedObjectsFlow: BroadcastFlow<ObjectType> = BroadcastFlow()
override val updatedObjectsFlow: Flow<ObjectType>
get() = _updatedObjectsFlow
private val _deletedObjectsIdsFlow: BroadcastFlow<IdType> = BroadcastFlow()
override val deletedObjectsIdsFlow: Flow<IdType>
get() = _deletedObjectsIdsFlow
private val _newObjectsFlow: MutableSharedFlow<ObjectType> = MutableSharedFlow()
override val newObjectsFlow: Flow<ObjectType> = _newObjectsFlow.asSharedFlow()
private val _updatedObjectsFlow: MutableSharedFlow<ObjectType> = MutableSharedFlow()
override val updatedObjectsFlow: Flow<ObjectType> = _updatedObjectsFlow.asSharedFlow()
private val _deletedObjectsIdsFlow: MutableSharedFlow<IdType> = MutableSharedFlow()
override val deletedObjectsIdsFlow: Flow<IdType> = _deletedObjectsIdsFlow.asSharedFlow()
protected abstract suspend fun updateObject(newValue: InputValueType, id: IdType, old: ObjectType): ObjectType
protected abstract suspend fun createObject(newValue: InputValueType): Pair<IdType, ObjectType>
@@ -44,7 +40,7 @@ abstract class WriteMapCRUDRepo<ObjectType, IdType, InputValueType>(
val (id, newObject) = createObject(it)
map[id] = newObject
newObject.also { _ ->
_newObjectsFlow.send(newObject)
_newObjectsFlow.emit(newObject)
}
}
}
@@ -54,7 +50,7 @@ abstract class WriteMapCRUDRepo<ObjectType, IdType, InputValueType>(
return newValue.also {
map[id] = it
_updatedObjectsFlow.send(it)
_updatedObjectsFlow.emit(it)
}
}
@@ -64,7 +60,7 @@ abstract class WriteMapCRUDRepo<ObjectType, IdType, InputValueType>(
override suspend fun deleteById(ids: List<IdType>) {
ids.forEach {
map.remove(it) ?.also { _ -> _deletedObjectsIdsFlow.send(it) }
map.remove(it) ?.also { _ -> _deletedObjectsIdsFlow.emit(it) }
}
}

View File

@@ -1,8 +1,11 @@
package dev.inmo.micro_utils.repos
import dev.inmo.micro_utils.coroutines.BroadcastFlow
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.pagination.Pagination
import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.pagination.utils.paginate
import dev.inmo.micro_utils.pagination.utils.reverse
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
class ReadMapKeyValueRepo<Key, Value>(
private val map: Map<Key, Value> = emptyMap()
@@ -13,31 +16,27 @@ class ReadMapKeyValueRepo<Key, Value>(
pagination: Pagination,
reversed: Boolean
): PaginationResult<Value> {
val firstIndex: Int = if (reversed) {
val size = map.size
(size - pagination.lastIndex).let { if (it < 0) 0 else it }
} else {
pagination.firstIndex
val values = map.values
val actualPagination = if (reversed) pagination.reverse(values.size) else pagination
return values.paginate(actualPagination).let {
if (reversed) {
it.copy(results = it.results.reversed())
} else {
it
}
}
return map.keys.drop(firstIndex).take(pagination.size).mapNotNull { map[it] }.createPaginationResult(
firstIndex,
count()
)
}
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> {
val firstIndex: Int = if (reversed) {
val size = map.size
(size - pagination.lastIndex).let { if (it < 0) 0 else it }
} else {
pagination.firstIndex
val keys = map.keys
val actualPagination = if (reversed) pagination.reverse(keys.size) else pagination
return keys.paginate(actualPagination).let {
if (reversed) {
it.copy(results = it.results.reversed())
} else {
it
}
}
return map.keys.drop(firstIndex).take(pagination.size).createPaginationResult(
firstIndex,
count()
)
}
override suspend fun contains(key: Key): Boolean = map.containsKey(key)
@@ -48,20 +47,22 @@ class ReadMapKeyValueRepo<Key, Value>(
class WriteMapKeyValueRepo<Key, Value>(
private val map: MutableMap<Key, Value> = mutableMapOf()
) : WriteStandardKeyValueRepo<Key, Value> {
private val _onNewValue: BroadcastFlow<Pair<Key, Value>> = BroadcastFlow()
private val _onNewValue: MutableSharedFlow<Pair<Key, Value>> = MutableSharedFlow()
override val onNewValue: Flow<Pair<Key, Value>>
get() = _onNewValue
private val _onValueRemoved: BroadcastFlow<Key> = BroadcastFlow()
private val _onValueRemoved: MutableSharedFlow<Key> = MutableSharedFlow()
override val onValueRemoved: Flow<Key>
get() = _onValueRemoved
override suspend fun set(k: Key, v: Value) {
map[k] = v
_onNewValue.send(k to v)
override suspend fun set(toSet: Map<Key, Value>) {
map.putAll(toSet)
toSet.forEach { (k, v) -> _onNewValue.emit(k to v) }
}
override suspend fun unset(k: Key) {
map.remove(k) ?.also { _onValueRemoved.send(k) }
override suspend fun unset(toUnset: List<Key>) {
toUnset.forEach { k ->
map.remove(k) ?.also { _ -> _onValueRemoved.emit(k) }
}
}
}

View File

@@ -1,9 +1,9 @@
package dev.inmo.micro_utils.repos
import dev.inmo.micro_utils.coroutines.BroadcastFlow
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.pagination.utils.paginate
import kotlinx.coroutines.flow.Flow
import dev.inmo.micro_utils.pagination.utils.reverse
import kotlinx.coroutines.flow.*
class MapReadOneToManyKeyValueRepo<Key, Value>(
private val map: Map<Key, List<Value>> = emptyMap()
@@ -13,8 +13,7 @@ class MapReadOneToManyKeyValueRepo<Key, Value>(
return list.paginate(
if (reversed) {
val firstIndex = (map.size - pagination.lastIndex).let { if (it < 0) 0 else it }
SimplePagination(firstIndex, pagination.size)
pagination.reverse(list.size)
} else {
pagination
}
@@ -22,17 +21,15 @@ class MapReadOneToManyKeyValueRepo<Key, Value>(
}
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> {
val firstIndex: Int = if (reversed) {
val size = map.size
(size - pagination.lastIndex).let { if (it < 0) 0 else it }
} else {
pagination.firstIndex
val keys = map.keys
val actualPagination = if (reversed) pagination.reverse(keys.size) else pagination
return keys.paginate(actualPagination).let {
if (reversed) {
it.copy(results = it.results.reversed())
} else {
it
}
}
return map.keys.drop(firstIndex).take(pagination.size).createPaginationResult(
firstIndex,
count()
)
}
override suspend fun contains(k: Key): Boolean = map.containsKey(k)
@@ -47,27 +44,35 @@ class MapReadOneToManyKeyValueRepo<Key, Value>(
class MapWriteOneToManyKeyValueRepo<Key, Value>(
private val map: MutableMap<Key, MutableList<Value>> = mutableMapOf()
) : WriteOneToManyKeyValueRepo<Key, Value> {
private val _onNewValue: BroadcastFlow<Pair<Key, Value>> = BroadcastFlow()
override val onNewValue: Flow<Pair<Key, Value>>
get() = _onNewValue
private val _onValueRemoved: BroadcastFlow<Pair<Key, Value>> = BroadcastFlow()
override val onValueRemoved: Flow<Pair<Key, Value>>
get() = _onValueRemoved
private val _onDataCleared: BroadcastFlow<Key> = BroadcastFlow()
override val onDataCleared: Flow<Key>
get() = _onDataCleared
private val _onNewValue: MutableSharedFlow<Pair<Key, Value>> = MutableSharedFlow()
override val onNewValue: Flow<Pair<Key, Value>> = _onNewValue.asSharedFlow()
private val _onValueRemoved: MutableSharedFlow<Pair<Key, Value>> = MutableSharedFlow()
override val onValueRemoved: Flow<Pair<Key, Value>> = _onValueRemoved.asSharedFlow()
private val _onDataCleared: MutableSharedFlow<Key> = MutableSharedFlow()
override val onDataCleared: Flow<Key> = _onDataCleared.asSharedFlow()
override suspend fun add(k: Key, v: Value) {
map.getOrPut(k) { mutableListOf() }.add(v)
_onNewValue.send(k to v)
override suspend fun add(toAdd: Map<Key, List<Value>>) {
toAdd.keys.forEach { k ->
if (map.getOrPut(k) { mutableListOf() }.addAll(toAdd[k] ?: return@forEach)) {
toAdd[k] ?.forEach { v ->
_onNewValue.emit(k to v)
}
}
}
}
override suspend fun remove(k: Key, v: Value) {
map[k] ?.remove(v) ?.also { _onValueRemoved.send(k to v) }
override suspend fun remove(toRemove: Map<Key, List<Value>>) {
toRemove.keys.forEach { k ->
if (map[k] ?.removeAll(toRemove[k] ?: return@forEach) == true) {
toRemove[k] ?.forEach { v ->
_onValueRemoved.emit(k to v)
}
}
}
}
override suspend fun clear(k: Key) {
map.remove(k) ?.also { _onDataCleared.send(k) }
map.remove(k) ?.also { _onDataCleared.emit(k) }
}
}

View File

@@ -3,9 +3,7 @@ package dev.inmo.micro_utils.repos.ktor.client.crud
import dev.inmo.micro_utils.ktor.client.encodeUrlQueryValue
import dev.inmo.micro_utils.ktor.client.uniget
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
import dev.inmo.micro_utils.pagination.Pagination
import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.pagination.asUrlQueryParts
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo
import dev.inmo.micro_utils.repos.ktor.common.crud.*
import io.ktor.client.HttpClient

View File

@@ -1,8 +1,6 @@
package dev.inmo.micro_utils.repos.ktor.client.crud
import dev.inmo.micro_utils.ktor.client.BodyPair
import dev.inmo.micro_utils.ktor.client.createStandardWebsocketFlow
import dev.inmo.micro_utils.ktor.client.unipost
import dev.inmo.micro_utils.ktor.client.*
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
import dev.inmo.micro_utils.repos.UpdatedValuePair
import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo

View File

@@ -3,12 +3,10 @@ package dev.inmo.micro_utils.repos.ktor.client.key_value
import dev.inmo.micro_utils.ktor.client.encodeUrlQueryValue
import dev.inmo.micro_utils.ktor.client.uniget
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
import dev.inmo.micro_utils.pagination.Pagination
import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.pagination.asUrlQueryParts
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo
import dev.inmo.micro_utils.repos.ktor.common.key_value.*
import io.ktor.client.*
import io.ktor.client.HttpClient
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer
@@ -71,6 +69,3 @@ class KtorReadStandardKeyValueRepo<Key, Value> (
Long.serializer()
)
}
@Deprecated("Renamed", ReplaceWith("KtorReadStandardKeyValueRepo", "dev.inmo.micro_utils.repos.ktor.client.key_value.KtorReadStandardKeyValueRepo"))
typealias KtorStandartReadKeyValueRepo<Key, Value> = KtorReadStandardKeyValueRepo<Key, Value>

View File

@@ -1,9 +1,7 @@
package dev.inmo.micro_utils.repos.ktor.client.key_value
import dev.inmo.micro_utils.repos.StandardKeyValueRepo
import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo
import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo
import io.ktor.client.*
import dev.inmo.micro_utils.repos.*
import io.ktor.client.HttpClient
import kotlinx.serialization.KSerializer
class KtorStandartKeyValueRepo<K, V> (

View File

@@ -1,16 +1,13 @@
package dev.inmo.micro_utils.repos.ktor.client.key_value
import dev.inmo.micro_utils.ktor.client.BodyPair
import dev.inmo.micro_utils.ktor.client.createStandardWebsocketFlow
import dev.inmo.micro_utils.ktor.client.unipost
import dev.inmo.micro_utils.ktor.client.*
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo
import dev.inmo.micro_utils.repos.ktor.common.key_value.*
import io.ktor.client.*
import io.ktor.client.HttpClient
import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.PairSerializer
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.builtins.*
class KtorWriteStandardKeyValueRepo<K, V> (
private var baseUrl: String,
@@ -18,6 +15,8 @@ class KtorWriteStandardKeyValueRepo<K, V> (
private var keySerializer: KSerializer<K>,
private var valueSerializer: KSerializer<V>,
) : WriteStandardKeyValueRepo<K, V> {
private val keyValueMapSerializer = MapSerializer(keySerializer, valueSerializer)
private val keysListSerializer = ListSerializer(keySerializer)
override val onNewValue: Flow<Pair<K, V>> = client.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, onNewValueRoute),
deserializer = PairSerializer(keySerializer, valueSerializer)
@@ -28,24 +27,21 @@ class KtorWriteStandardKeyValueRepo<K, V> (
deserializer = keySerializer
)
override suspend fun set(k: K, v: V) = client.unipost(
override suspend fun set(toSet: Map<K, V>) = client.unipost(
buildStandardUrl(
baseUrl,
setRoute
),
BodyPair(KeyValuePostObject.serializer(keySerializer, valueSerializer), KeyValuePostObject(k, v)),
BodyPair(keyValueMapSerializer, toSet),
Unit.serializer()
)
override suspend fun unset(k: K) = client.unipost(
override suspend fun unset(toUnset: List<K>) = client.unipost(
buildStandardUrl(
baseUrl,
unsetRoute,
),
BodyPair(keySerializer, k),
BodyPair(keysListSerializer, toUnset),
Unit.serializer()
)
}
@Deprecated("Renamed", ReplaceWith("KtorWriteStandardKeyValueRepo", "dev.inmo.micro_utils.repos.ktor.client.key_value.KtorWriteStandardKeyValueRepo"))
typealias KtorStandartWriteKeyValueRepo<K, V> = KtorWriteStandardKeyValueRepo<K, V>

View File

@@ -1,8 +1,6 @@
package dev.inmo.micro_utils.repos.ktor.client.one_to_many
import dev.inmo.micro_utils.repos.OneToManyKeyValueRepo
import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo
import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo
import dev.inmo.micro_utils.repos.*
import io.ktor.client.HttpClient
import kotlinx.serialization.KSerializer

View File

@@ -3,9 +3,7 @@ package dev.inmo.micro_utils.repos.ktor.client.one_to_many
import dev.inmo.micro_utils.ktor.client.encodeUrlQueryValue
import dev.inmo.micro_utils.ktor.client.uniget
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
import dev.inmo.micro_utils.pagination.Pagination
import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.pagination.asUrlQueryParts
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo
import dev.inmo.micro_utils.repos.ktor.common.one_to_many.*
import io.ktor.client.HttpClient

View File

@@ -1,16 +1,13 @@
package dev.inmo.micro_utils.repos.ktor.client.one_to_many
import dev.inmo.micro_utils.coroutines.broadcastStateFlow
import dev.inmo.micro_utils.ktor.client.*
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo
import dev.inmo.micro_utils.repos.ktor.common.one_to_many.*
import io.ktor.client.HttpClient
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.PairSerializer
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.builtins.*
class KtorWriteOneToManyKeyValueRepo<Key, Value> (
private val baseUrl: String,
@@ -19,6 +16,7 @@ class KtorWriteOneToManyKeyValueRepo<Key, Value> (
private val valueSerializer: KSerializer<Value>
) : WriteOneToManyKeyValueRepo<Key, Value> {
private val keyValueSerializer = PairSerializer(keySerializer, valueSerializer)
private val keyValueMapSerializer = MapSerializer(keySerializer, ListSerializer(valueSerializer))
override val onNewValue: Flow<Pair<Key, Value>> = client.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, onNewValueRoute),
deserializer = keyValueSerializer
@@ -32,24 +30,23 @@ class KtorWriteOneToManyKeyValueRepo<Key, Value> (
deserializer = keySerializer
)
override suspend fun add(k: Key, v: Value) = client.unipost(
buildStandardUrl(
baseUrl,
addRoute,
),
BodyPair(keyValueSerializer, k to v),
Unit.serializer(),
)
override suspend fun remove(k: Key, v: Value) = client.unipost(
override suspend fun remove(toRemove: Map<Key, List<Value>>) = client.unipost(
buildStandardUrl(
baseUrl,
removeRoute,
),
BodyPair(keyValueSerializer, k to v),
BodyPair(keyValueMapSerializer, toRemove),
Unit.serializer(),
)
override suspend fun add(toAdd: Map<Key, List<Value>>) = client.unipost(
buildStandardUrl(
baseUrl,
clearRoute,
),
BodyPair(keyValueMapSerializer, toAdd),
Unit.serializer(),
)
override suspend fun clear(k: Key) = client.unipost(
buildStandardUrl(
baseUrl,

View File

@@ -1,8 +1,6 @@
package dev.inmo.micro_utils.repos.ktor.server.crud
import dev.inmo.micro_utils.ktor.server.includeWebsocketHandling
import dev.inmo.micro_utils.ktor.server.unianswer
import dev.inmo.micro_utils.ktor.server.uniload
import dev.inmo.micro_utils.ktor.server.*
import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo
import dev.inmo.micro_utils.repos.ktor.common.crud.*
import io.ktor.application.call

View File

@@ -1,7 +1,8 @@
package dev.inmo.micro_utils.repos.ktor.server.key_value
import dev.inmo.micro_utils.repos.StandardKeyValueRepo
import io.ktor.routing.*
import io.ktor.routing.Route
import io.ktor.routing.route
import kotlinx.serialization.KSerializer
fun <K, V> Route.configureStandartKeyValueRepoRoutes(

View File

@@ -6,8 +6,9 @@ import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.pagination.extractPagination
import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo
import dev.inmo.micro_utils.repos.ktor.common.key_value.*
import io.ktor.application.*
import io.ktor.routing.*
import io.ktor.application.call
import io.ktor.routing.Route
import io.ktor.routing.get
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer

View File

@@ -1,19 +1,21 @@
package dev.inmo.micro_utils.repos.ktor.server.key_value
import dev.inmo.micro_utils.ktor.server.includeWebsocketHandling
import dev.inmo.micro_utils.ktor.server.uniload
import dev.inmo.micro_utils.ktor.server.*
import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo
import dev.inmo.micro_utils.repos.ktor.common.key_value.*
import io.ktor.application.*
import io.ktor.routing.*
import io.ktor.application.call
import io.ktor.routing.Route
import io.ktor.routing.post
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.PairSerializer
import kotlinx.serialization.builtins.*
fun <K, V> Route.configureWriteStandartKeyValueRepoRoutes (
originalRepo: WriteStandardKeyValueRepo<K, V>,
keySerializer: KSerializer<K>,
valueSerializer: KSerializer<V>,
) {
val keyValueMapSerializer = MapSerializer(keySerializer, valueSerializer)
val keysListSerializer = ListSerializer(keySerializer)
includeWebsocketHandling(
onNewValueRoute,
originalRepo.onNewValue,
@@ -27,18 +29,16 @@ fun <K, V> Route.configureWriteStandartKeyValueRepoRoutes (
)
post(setRoute) {
val (key, value) = call.uniload(
KeyValuePostObject.serializer(keySerializer, valueSerializer)
val toSet = call.uniload(
keyValueMapSerializer
)
originalRepo.set(key, value)
call.unianswer(Unit.serializer(), originalRepo.set(toSet))
}
post(unsetRoute) {
val key = call.uniload(
keySerializer
)
val toUnset = call.uniload(keysListSerializer)
originalRepo.unset(key)
call.unianswer(Unit.serializer(), originalRepo.unset(toUnset))
}
}

View File

@@ -1,8 +1,6 @@
package dev.inmo.micro_utils.repos.ktor.server.one_to_many
import dev.inmo.micro_utils.ktor.server.decodeUrlQueryValue
import dev.inmo.micro_utils.ktor.server.decodeUrlQueryValueOrSendError
import dev.inmo.micro_utils.ktor.server.unianswer
import dev.inmo.micro_utils.ktor.server.*
import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.pagination.extractPagination
import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo

View File

@@ -7,15 +7,15 @@ import io.ktor.application.call
import io.ktor.routing.Route
import io.ktor.routing.post
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.PairSerializer
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.builtins.*
fun <Key, Value> Route.configureOneToManyWriteKeyValueRepoRoutes(
originalRepo: WriteOneToManyKeyValueRepo<Key, Value>,
keySerializer: KSerializer<Key>,
valueSealizer: KSerializer<Value>,
valueSerializer: KSerializer<Value>,
) {
val keyValueSerializer = PairSerializer(keySerializer, valueSealizer)
val keyValueSerializer = PairSerializer(keySerializer, valueSerializer)
val keyValueMapSerializer = MapSerializer(keySerializer, ListSerializer(valueSerializer))
includeWebsocketHandling(
onNewValueRoute,
@@ -34,24 +34,22 @@ fun <Key, Value> Route.configureOneToManyWriteKeyValueRepoRoutes(
)
post(addRoute) {
val obj = call.uniload(
keyValueSerializer
)
val obj = call.uniload(keyValueMapSerializer)
call.unianswer(
Unit.serializer(),
originalRepo.add(obj.first, obj.second)
originalRepo.add(obj)
)
}
post(removeRoute) {
val obj = call.uniload(
keyValueSerializer
keyValueMapSerializer
)
call.unianswer(
Unit.serializer(),
originalRepo.remove(obj.first, obj.second),
originalRepo.remove(obj),
)
}

View File

@@ -16,7 +16,9 @@ String[] includes = [
":ktor:server",
":ktor:common",
":ktor:client",
":coroutines"
":coroutines",
":dokka"
]