Merge pull request #2 from InsanusMokrassar/0.1.1

0.1.1 (additions)
This commit is contained in:
InsanusMokrassar 2020-10-14 22:31:25 +06:00 committed by GitHub
commit f6eb339cd0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
32 changed files with 485 additions and 60 deletions

View File

@ -8,10 +8,34 @@ Inited :)
* `Versions`:
* `kotlinx.serialization`: `1.0.0-RC2` -> `1.0.0`
* `Pagination`
* `Common`
* Function `calculatePage` for calculating page based on size of page and index of first element
* Extension `List#createPaginationResult` which use index of first element to calculate current page and other
info in `PaginationResult` object
* Factory `emptyPaginationResult` for empty `PaginationResult`
* Extensions `paginate` for creating of `PaginationResult` which use as source one of next types:
* `Iterable`
* `List`
* `Set`
* `Repos`
* `Common`
* Interfaces related to `OneToManyKeyValueRepo` were renamed with convenience to `Read`/`Write` modifier before name
* All subclasses were renamed
* Interfaces related to `StandartKeyValueRepo` were renamed with convenience to `Read`/`Write` modifier before name
* All subclasses were renamed
* Extensions `doForAll` and `getAll` were added for all current types of repos:
* `ReadStandardCRUDRepo`
* `StandardReadKeyValueRepo`
* `OneToManyReadKeyValueRepo`
* `StandardReadKeyValueRepo` methods `values` and `keys` now have default value for `reversed` parameter `false`
* `ReadStandardKeyValueRepo`
* `ReadOneToManyKeyValueRepo`
* `ReadStandardKeyValueRepo` methods `values` and `keys` now have default value for `reversed` parameter `false`
* New `Flow`'s in `WriteOneToManyKeyValueRepo`:
* `onNewValue`
* `onValueRemoved`
* `onDataCleared`
* New function `ReadStandardCRUDRepo#count`
* `In Memory`
* Package has been created:) you can connect it via `implementation "dev.inmo:micro_utils.repos.inmemory"`
* `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

View File

@ -1,6 +1,7 @@
package dev.inmo.micro_utils.pagination
import kotlin.math.ceil
import kotlin.math.floor
/**
* Base interface of pagination
@ -50,3 +51,12 @@ fun calculatePagesNumber(datasetSize: Int, pageSize: Int): Int =
datasetSize.toLong(),
pageSize
)
/**
* @return calculated page number which can be correctly used in [PaginationResult] as [PaginationResult.page] value
*/
fun calculatePage(firstIndex: Int, resultsSize: Int): Int = if (resultsSize > 0) {
floor(firstIndex.toFloat() / resultsSize).toInt()
} else {
0
}

View File

@ -10,6 +10,8 @@ data class PaginationResult<T>(
override val size: Int
) : Pagination
fun <T> emptyPaginationResult() = PaginationResult<T>(0, 0, emptyList(), 0)
fun <T> List<T>.createPaginationResult(
pagination: Pagination,
commonObjectsNumber: Long
@ -23,6 +25,19 @@ fun <T> List<T>.createPaginationResult(
pagination.size
)
fun <T> List<T>.createPaginationResult(
firstIndex: Int,
commonObjectsNumber: Long
) = PaginationResult(
calculatePage(firstIndex, size),
calculatePagesNumber(
commonObjectsNumber,
size
),
this,
size
)
fun <T> Pair<Long, List<T>>.createPaginationResult(
pagination: Pagination
) = second.createPaginationResult(pagination, first)

View File

@ -0,0 +1,35 @@
package dev.inmo.micro_utils.pagination.utils
import dev.inmo.micro_utils.pagination.*
fun <T> Iterable<T>.paginate(with: Pagination): PaginationResult<T> {
var i = 0
val result = mutableListOf<T>()
val lowerIndex = with.firstIndex
val greatestIndex = with.lastIndex
for (item in this) {
when {
i < lowerIndex || i > greatestIndex -> i++
else -> {
result.add(item)
i++
}
}
}
return result.createPaginationResult(with, i.toLong())
}
fun <T> List<T>.paginate(with: Pagination): PaginationResult<T> {
return subList(with.firstIndex, with.lastIndex + 1).createPaginationResult(
with,
size.toLong()
)
}
fun <T> Set<T>.paginate(with: Pagination): PaginationResult<T> {
return this.drop(with.firstIndex).take(with.size).createPaginationResult(
with,
size.toLong()
)
}

View File

@ -2,8 +2,9 @@ package dev.inmo.micro_utils.repos
import dev.inmo.micro_utils.pagination.Pagination
import dev.inmo.micro_utils.pagination.PaginationResult
import kotlinx.coroutines.flow.Flow
interface OneToManyReadKeyValueRepo<Key, Value> : Repo {
interface ReadOneToManyKeyValueRepo<Key, Value> : Repo {
suspend fun get(k: Key, pagination: Pagination, reversed: Boolean = false): PaginationResult<Value>
suspend fun keys(pagination: Pagination, reversed: Boolean = false): PaginationResult<Key>
suspend fun contains(k: Key): Boolean
@ -11,11 +12,19 @@ interface OneToManyReadKeyValueRepo<Key, Value> : Repo {
suspend fun count(k: Key): Long
suspend fun count(): Long
}
@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>
interface OneToManyWriteKeyValueRepo<Key, Value> : Repo {
suspend fun add(k: Key, v: Value)
suspend fun remove(k: Key, v: 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> : OneToManyReadKeyValueRepo<Key, Value>, OneToManyWriteKeyValueRepo<Key, Value>
interface OneToManyKeyValueRepo<Key, Value> : ReadOneToManyKeyValueRepo<Key, Value>, WriteOneToManyKeyValueRepo<Key, Value>

View File

@ -8,6 +8,7 @@ interface ReadStandardCRUDRepo<ObjectType, IdType> : Repo {
suspend fun getByPagination(pagination: Pagination): PaginationResult<ObjectType>
suspend fun getById(id: IdType): ObjectType?
suspend fun contains(id: IdType): Boolean
suspend fun count(): Long
}
typealias UpdatedValuePair<IdType, ValueType> = Pair<IdType, ValueType>

View File

@ -4,7 +4,7 @@ import dev.inmo.micro_utils.pagination.Pagination
import dev.inmo.micro_utils.pagination.PaginationResult
import kotlinx.coroutines.flow.Flow
interface StandardReadKeyValueRepo<Key, Value> : Repo {
interface ReadStandardKeyValueRepo<Key, Value> : Repo {
suspend fun get(k: Key): Value?
suspend fun values(pagination: Pagination, reversed: Boolean = false): PaginationResult<Value>
suspend fun keys(pagination: Pagination, reversed: Boolean = false): PaginationResult<Key>
@ -12,7 +12,7 @@ interface StandardReadKeyValueRepo<Key, Value> : Repo {
suspend fun count(): Long
}
interface StandardWriteKeyValueRepo<Key, Value> : Repo {
interface WriteStandardKeyValueRepo<Key, Value> : Repo {
val onNewValue: Flow<Pair<Key, Value>>
val onValueRemoved: Flow<Key>
@ -20,4 +20,4 @@ interface StandardWriteKeyValueRepo<Key, Value> : Repo {
suspend fun unset(k: Key)
}
interface StandardKeyValueRepo<Key, Value> : StandardReadKeyValueRepo<Key, Value>, StandardWriteKeyValueRepo<Key, Value>
interface StandardKeyValueRepo<Key, Value> : ReadStandardKeyValueRepo<Key, Value>, WriteStandardKeyValueRepo<Key, Value>

View File

@ -3,7 +3,7 @@ package dev.inmo.micro_utils.repos.pagination
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.*
suspend inline fun <Key, Value, REPO : StandardReadKeyValueRepo<Key, Value>> REPO.doForAll(
suspend inline fun <Key, Value, REPO : ReadStandardKeyValueRepo<Key, Value>> REPO.doForAll(
@Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE")
methodCaller: suspend REPO.(Pagination) -> PaginationResult<Key>,
block: (List<Pair<Key, Value>>) -> Unit
@ -15,11 +15,11 @@ suspend inline fun <Key, Value, REPO : StandardReadKeyValueRepo<Key, Value>> REP
}
}
suspend inline fun <Key, Value, REPO : StandardReadKeyValueRepo<Key, Value>> REPO.doForAll(
suspend inline fun <Key, Value, REPO : ReadStandardKeyValueRepo<Key, Value>> REPO.doForAll(
block: (List<Pair<Key, Value>>) -> Unit
) = doForAll({ keys(it, false) }, block)
suspend inline fun <Key, Value, REPO : StandardReadKeyValueRepo<Key, Value>> REPO.getAll(
suspend inline fun <Key, Value, REPO : ReadStandardKeyValueRepo<Key, Value>> REPO.getAll(
@Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE")
methodCaller: suspend REPO.(Pagination) -> PaginationResult<Key>
): List<Pair<Key, Value>> {

View File

@ -3,7 +3,7 @@ package dev.inmo.micro_utils.repos.pagination
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.*
suspend inline fun <Key, Value, REPO : OneToManyReadKeyValueRepo<Key, Value>> REPO.doForAll(
suspend inline fun <Key, Value, REPO : ReadOneToManyKeyValueRepo<Key, Value>> REPO.doForAll(
@Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE")
methodCaller: suspend REPO.(Pagination) -> PaginationResult<Key>,
block: (List<Pair<Key, List<Value>>>) -> Unit
@ -25,11 +25,11 @@ suspend inline fun <Key, Value, REPO : OneToManyReadKeyValueRepo<Key, Value>> RE
}
}
suspend inline fun <Key, Value, REPO : OneToManyReadKeyValueRepo<Key, Value>> REPO.doForAll(
suspend inline fun <Key, Value, REPO : ReadOneToManyKeyValueRepo<Key, Value>> REPO.doForAll(
block: (List<Pair<Key, List<Value>>>) -> Unit
) = doForAll({ keys(it, false) }, block)
suspend inline fun <Key, Value, REPO : OneToManyReadKeyValueRepo<Key, Value>> REPO.getAll(
suspend inline fun <Key, Value, REPO : ReadOneToManyKeyValueRepo<Key, Value>> REPO.getAll(
@Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE")
methodCaller: suspend REPO.(Pagination) -> PaginationResult<Key>
): List<Pair<Key, List<Value>>> {

View File

@ -33,4 +33,6 @@ abstract class AbstractExposedReadCRUDRepo<ObjectType, IdType>(
override suspend fun contains(id: IdType): Boolean = transaction(db = database) {
select { selectById(id) }.limit(1).any()
}
override suspend fun count(): Long = transaction(db = database) { selectAll().count() }
}

View File

@ -1,7 +1,7 @@
package dev.inmo.micro_utils.repos.exposed.keyvalue
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.StandardReadKeyValueRepo
import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
@ -9,7 +9,7 @@ abstract class AbstractExposedReadKeyValueRepo<Key, Value>(
protected val database: Database,
protected val keyColumn: Column<Key>,
protected val valueColumn: Column<Value>
) : StandardReadKeyValueRepo<Key, Value>, Table() {
) : ReadStandardKeyValueRepo<Key, Value>, Table() {
override val primaryKey: PrimaryKey = PrimaryKey(keyColumn, valueColumn)
override suspend fun get(k: Key): Value? = transaction(database) {

View File

@ -4,11 +4,11 @@ import dev.inmo.micro_utils.repos.OneToManyKeyValueRepo
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
abstract class AbstractOneToManyExposedKeyValueRepo<Key, Value>(
abstract class AbstractExposedOneToManyKeyValueRepo<Key, Value>(
keyColumnAllocator: ColumnAllocator<Key>,
valueColumnAllocator: ColumnAllocator<Value>,
database: Database
) : OneToManyKeyValueRepo<Key, Value>, AbstractOneToManyExposedReadKeyValueRepo<Key, Value>(
) : OneToManyKeyValueRepo<Key, Value>, AbstractExposedReadOneToManyKeyValueRepo<Key, Value>(
keyColumnAllocator,
valueColumnAllocator,
database
@ -30,3 +30,6 @@ abstract class AbstractOneToManyExposedKeyValueRepo<Key, Value>(
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

@ -1,17 +1,17 @@
package dev.inmo.micro_utils.repos.exposed.onetomany
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.OneToManyReadKeyValueRepo
import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
typealias ColumnAllocator<T> = Table.() -> Column<T>
abstract class AbstractOneToManyExposedReadKeyValueRepo<Key, Value>(
abstract class AbstractExposedReadOneToManyKeyValueRepo<Key, Value>(
keyColumnAllocator: ColumnAllocator<Key>,
valueColumnAllocator: ColumnAllocator<Value>,
protected val database: Database
) : OneToManyReadKeyValueRepo<Key, Value>, Table() {
) : ReadOneToManyKeyValueRepo<Key, Value>, Table() {
protected val keyColumn: Column<Key> = keyColumnAllocator()
protected val valueColumn: Column<Value> = valueColumnAllocator()
@ -45,3 +45,6 @@ abstract class AbstractOneToManyExposedReadKeyValueRepo<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

@ -0,0 +1,17 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
}
apply from: "$mppProjectWithSerializationPresetPath"
kotlin {
sourceSets {
commonMain {
dependencies {
api internalProject("micro_utils.repos.common")
api internalProject("micro_utils.coroutines")
}
}
}
}

View File

@ -0,0 +1,96 @@
package dev.inmo.micro_utils.repos
import dev.inmo.micro_utils.coroutines.BroadcastFlow
import dev.inmo.micro_utils.pagination.*
import kotlinx.coroutines.flow.Flow
class ReadMapCRUDRepo<ObjectType, IdType>(
private val map: Map<IdType, ObjectType> = emptyMap()
) : ReadStandardCRUDRepo<ObjectType, IdType> {
override suspend fun getByPagination(pagination: Pagination): PaginationResult<ObjectType> {
return map.keys.drop(pagination.firstIndex).take(pagination.size).mapNotNull {
map[it]
}.createPaginationResult(
pagination,
count()
)
}
override suspend fun getById(id: IdType): ObjectType? = map[id]
override suspend fun contains(id: IdType): Boolean = map.containsKey(id)
override suspend fun count(): Long = map.size.toLong()
}
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
protected abstract suspend fun updateObject(newValue: InputValueType, id: IdType, old: ObjectType): ObjectType
protected abstract suspend fun createObject(newValue: InputValueType): Pair<IdType, ObjectType>
override suspend fun create(values: List<InputValueType>): List<ObjectType> {
return values.map {
val (id, newObject) = createObject(it)
map[id] = newObject
newObject.also { _ ->
_newObjectsFlow.send(newObject)
}
}
}
override suspend fun update(id: IdType, value: InputValueType): ObjectType? {
val newValue = updateObject(value, id, map[id] ?: return null)
return newValue.also {
map[id] = it
_updatedObjectsFlow.send(it)
}
}
override suspend fun update(values: List<UpdatedValuePair<IdType, InputValueType>>): List<ObjectType> {
return values.mapNotNull { update(it.first, it.second) }
}
override suspend fun deleteById(ids: List<IdType>) {
ids.forEach {
map.remove(it) ?.also { _ -> _deletedObjectsIdsFlow.send(it) }
}
}
}
abstract class MapCRUDRepo<ObjectType, IdType, InputValueType>(
map: MutableMap<IdType, ObjectType>
) : StandardCRUDRepo<ObjectType, IdType, InputValueType>,
ReadStandardCRUDRepo<ObjectType, IdType> by ReadMapCRUDRepo(map),
WriteMapCRUDRepo<ObjectType, IdType, InputValueType>(map)
fun <ObjectType, IdType, InputValueType> MapCRUDRepo(
map: MutableMap<IdType, ObjectType>,
updateCallback: suspend (newValue: InputValueType, id: IdType, old: ObjectType) -> ObjectType,
createCallback: suspend (newValue: InputValueType) -> Pair<IdType, ObjectType>
) = object : MapCRUDRepo<ObjectType, IdType, InputValueType>(map) {
override suspend fun updateObject(
newValue: InputValueType,
id: IdType,
old: ObjectType
): ObjectType = updateCallback(newValue, id, old)
override suspend fun createObject(newValue: InputValueType): Pair<IdType, ObjectType> = createCallback(newValue)
}
fun <ObjectType, IdType, InputValueType> MutableMap<IdType, ObjectType>.asCrudRepo(
updateCallback: suspend (newValue: InputValueType, id: IdType, old: ObjectType) -> ObjectType,
createCallback: suspend (newValue: InputValueType) -> Pair<IdType, ObjectType>
) = MapCRUDRepo(this, updateCallback, createCallback)

View File

@ -0,0 +1,74 @@
package dev.inmo.micro_utils.repos
import dev.inmo.micro_utils.coroutines.BroadcastFlow
import dev.inmo.micro_utils.pagination.*
import kotlinx.coroutines.flow.Flow
class ReadMapKeyValueRepo<Key, Value>(
private val map: Map<Key, Value> = emptyMap()
) : ReadStandardKeyValueRepo<Key, Value> {
override suspend fun get(k: Key): Value? = map[k]
override suspend fun values(
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
}
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
}
return map.keys.drop(firstIndex).take(pagination.size).createPaginationResult(
firstIndex,
count()
)
}
override suspend fun contains(key: Key): Boolean = map.containsKey(key)
override suspend fun count(): Long = map.size.toLong()
}
class WriteMapKeyValueRepo<Key, Value>(
private val map: MutableMap<Key, Value> = mutableMapOf()
) : WriteStandardKeyValueRepo<Key, Value> {
private val _onNewValue: BroadcastFlow<Pair<Key, Value>> = BroadcastFlow()
override val onNewValue: Flow<Pair<Key, Value>>
get() = _onNewValue
private val _onValueRemoved: BroadcastFlow<Key> = BroadcastFlow()
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 unset(k: Key) {
map.remove(k) ?.also { _onValueRemoved.send(k) }
}
}
class MapKeyValueRepo<Key, Value>(
private val map: MutableMap<Key, Value> = mutableMapOf()
) : StandardKeyValueRepo<Key, Value>,
ReadStandardKeyValueRepo<Key, Value> by ReadMapKeyValueRepo(map),
WriteStandardKeyValueRepo<Key, Value> by WriteMapKeyValueRepo(map)
fun <K, V> MutableMap<K, V>.asKeyValueRepo(): StandardKeyValueRepo<K, V> = MapKeyValueRepo(this)

View File

@ -0,0 +1,82 @@
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
class MapReadOneToManyKeyValueRepo<Key, Value>(
private val map: Map<Key, List<Value>> = emptyMap()
) : ReadOneToManyKeyValueRepo<Key, Value> {
override suspend fun get(k: Key, pagination: Pagination, reversed: Boolean): PaginationResult<Value> {
val list = map[k] ?: return emptyPaginationResult()
return list.paginate(
if (reversed) {
val firstIndex = (map.size - pagination.lastIndex).let { if (it < 0) 0 else it }
SimplePagination(firstIndex, pagination.size)
} else {
pagination
}
)
}
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
}
return map.keys.drop(firstIndex).take(pagination.size).createPaginationResult(
firstIndex,
count()
)
}
override suspend fun contains(k: Key): Boolean = map.containsKey(k)
override suspend fun contains(k: Key, v: Value): Boolean = map[k] ?.contains(v) == true
override suspend fun count(k: Key): Long = map[k] ?.size ?.toLong() ?: 0L
override suspend fun count(): Long = map.size.toLong()
}
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
override suspend fun add(k: Key, v: Value) {
map.getOrPut(k) { mutableListOf() }.add(v)
_onNewValue.send(k to v)
}
override suspend fun remove(k: Key, v: Value) {
map[k] ?.remove(v) ?.also { _onValueRemoved.send(k to v) }
}
override suspend fun clear(k: Key) {
map.remove(k) ?.also { _onDataCleared.send(k) }
}
}
class MapOneToManyKeyValueRepo<Key, Value>(
private val map: MutableMap<Key, MutableList<Value>> = mutableMapOf()
) : OneToManyKeyValueRepo<Key, Value>,
ReadOneToManyKeyValueRepo<Key, Value> by MapReadOneToManyKeyValueRepo(map),
WriteOneToManyKeyValueRepo<Key, Value> by MapWriteOneToManyKeyValueRepo(map)
fun <K, V> MutableMap<K, List<V>>.asOneToManyKeyValueRepo(): OneToManyKeyValueRepo<K, V> = MapOneToManyKeyValueRepo(
map { (k, v) -> k to v.toMutableList() }.toMap().toMutableMap()
)

View File

@ -47,4 +47,12 @@ class KtorReadStandardCrudRepo<ObjectType, IdType> (
),
Boolean.serializer()
)
override suspend fun count(): Long = client.uniget(
buildStandardUrl(
baseUrl,
countRouting
),
Long.serializer()
)
}

View File

@ -6,19 +6,19 @@ 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.repos.StandardReadKeyValueRepo
import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo
import dev.inmo.micro_utils.repos.ktor.common.key_value.*
import io.ktor.client.*
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer
class KtorStandartReadKeyValueRepo<Key, Value> (
class KtorReadStandardKeyValueRepo<Key, Value> (
private var baseUrl: String,
private var client: HttpClient = HttpClient(),
private var keySerializer: KSerializer<Key>,
private var valueSerializer: KSerializer<Value>,
private var valueNullableSerializer: KSerializer<Value?>,
) : StandardReadKeyValueRepo<Key, Value> {
) : ReadStandardKeyValueRepo<Key, Value> {
override suspend fun get(k: Key): Value? = client.uniget(
buildStandardUrl(
baseUrl,
@ -71,3 +71,6 @@ class KtorStandartReadKeyValueRepo<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,8 +1,8 @@
package dev.inmo.micro_utils.repos.ktor.client.key_value
import dev.inmo.micro_utils.repos.StandardKeyValueRepo
import dev.inmo.micro_utils.repos.StandardReadKeyValueRepo
import dev.inmo.micro_utils.repos.StandardWriteKeyValueRepo
import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo
import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo
import io.ktor.client.*
import kotlinx.serialization.KSerializer
@ -14,14 +14,14 @@ class KtorStandartKeyValueRepo<K, V> (
valueSerializer: KSerializer<V>,
valueNullableSerializer: KSerializer<V?>
) : StandardKeyValueRepo<K, V>,
StandardReadKeyValueRepo<K, V> by KtorStandartReadKeyValueRepo(
ReadStandardKeyValueRepo<K, V> by KtorReadStandardKeyValueRepo(
"$baseUrl/$baseSubpart",
client,
keySerializer,
valueSerializer,
valueNullableSerializer
),
StandardWriteKeyValueRepo<K, V> by KtorStandartWriteKeyValueRepo(
WriteStandardKeyValueRepo<K, V> by KtorWriteStandardKeyValueRepo(
"$baseUrl/$baseSubpart",
client,
keySerializer,

View File

@ -4,7 +4,7 @@ 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.common.buildStandardUrl
import dev.inmo.micro_utils.repos.StandardWriteKeyValueRepo
import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo
import dev.inmo.micro_utils.repos.ktor.common.key_value.*
import io.ktor.client.*
import kotlinx.coroutines.flow.Flow
@ -12,12 +12,12 @@ import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.PairSerializer
import kotlinx.serialization.builtins.serializer
class KtorStandartWriteKeyValueRepo<K, V> (
class KtorWriteStandardKeyValueRepo<K, V> (
private var baseUrl: String,
private var client: HttpClient = HttpClient(),
private var keySerializer: KSerializer<K>,
private var valueSerializer: KSerializer<V>,
) : StandardWriteKeyValueRepo<K, V> {
) : WriteStandardKeyValueRepo<K, V> {
override val onNewValue: Flow<Pair<K, V>> = client.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, onNewValueRoute),
deserializer = PairSerializer(keySerializer, valueSerializer)
@ -46,3 +46,6 @@ class KtorStandartWriteKeyValueRepo<K, V> (
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,8 @@
package dev.inmo.micro_utils.repos.ktor.client.one_to_many
import dev.inmo.micro_utils.repos.OneToManyKeyValueRepo
import dev.inmo.micro_utils.repos.OneToManyReadKeyValueRepo
import dev.inmo.micro_utils.repos.OneToManyWriteKeyValueRepo
import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo
import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo
import io.ktor.client.HttpClient
import kotlinx.serialization.KSerializer
@ -13,13 +13,13 @@ class KtorOneToManyKeyValueRepo<Key, Value>(
keySerializer: KSerializer<Key>,
valueSerializer: KSerializer<Value>,
) : OneToManyKeyValueRepo<Key, Value>,
OneToManyReadKeyValueRepo<Key, Value> by KtorOneToManyReadKeyValueRepo<Key, Value> (
ReadOneToManyKeyValueRepo<Key, Value> by KtorReadOneToManyKeyValueRepo<Key, Value> (
"$baseUrl/$baseSubpart",
client,
keySerializer,
valueSerializer,
),
OneToManyWriteKeyValueRepo<Key, Value> by KtorOneToManyWriteKeyValueRepo<Key, Value> (
WriteOneToManyKeyValueRepo<Key, Value> by KtorWriteOneToManyKeyValueRepo<Key, Value> (
"$baseUrl/$baseSubpart",
client,
keySerializer,

View File

@ -6,18 +6,18 @@ 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.repos.OneToManyReadKeyValueRepo
import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo
import dev.inmo.micro_utils.repos.ktor.common.one_to_many.*
import io.ktor.client.HttpClient
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer
class KtorOneToManyReadKeyValueRepo<Key, Value> (
class KtorReadOneToManyKeyValueRepo<Key, Value> (
private val baseUrl: String,
private val client: HttpClient = HttpClient(),
private val keySerializer: KSerializer<Key>,
private val valueSerializer: KSerializer<Value>,
) : OneToManyReadKeyValueRepo<Key, Value> {
) : ReadOneToManyKeyValueRepo<Key, Value> {
private val paginationValueResultSerializer = PaginationResult.serializer(valueSerializer)
private val paginationKeyResultSerializer = PaginationResult.serializer(keySerializer)

View File

@ -1,22 +1,36 @@
package dev.inmo.micro_utils.repos.ktor.client.one_to_many
import dev.inmo.micro_utils.ktor.client.BodyPair
import dev.inmo.micro_utils.ktor.client.unipost
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.OneToManyWriteKeyValueRepo
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
class KtorOneToManyWriteKeyValueRepo<Key, Value> (
class KtorWriteOneToManyKeyValueRepo<Key, Value> (
private val baseUrl: String,
private val client: HttpClient = HttpClient(),
private val keySerializer: KSerializer<Key>,
private val valueSerializer: KSerializer<Value>,
) : OneToManyWriteKeyValueRepo<Key, Value> {
private val valueSerializer: KSerializer<Value>
) : WriteOneToManyKeyValueRepo<Key, Value> {
private val keyValueSerializer = PairSerializer(keySerializer, valueSerializer)
override val onNewValue: Flow<Pair<Key, Value>> = client.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, onNewValueRoute),
deserializer = keyValueSerializer
)
override val onValueRemoved: Flow<Pair<Key, Value>> = client.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, onValueRemovedRoute),
deserializer = keyValueSerializer
)
override val onDataCleared: Flow<Key> = client.createStandardWebsocketFlow(
buildStandardUrl(baseUrl, onDataClearedRoute),
deserializer = keySerializer
)
override suspend fun add(k: Key, v: Value) = client.unipost(
buildStandardUrl(

View File

@ -3,3 +3,4 @@ package dev.inmo.micro_utils.repos.ktor.common.crud
const val getByPaginationRouting = "getByPagination"
const val getByIdRouting = "getById"
const val containsRouting = "contains"
const val countRouting = "count"

View File

@ -7,6 +7,10 @@ const val containsByKeyValueRoute = "containsByKeyValue"
const val countByKeyRoute = "countByKey"
const val countRoute = "count"
const val onNewValueRoute = "onNewValue"
const val onValueRemovedRoute = "onValueRemoved"
const val onDataClearedRoute = "onDataCleared"
const val addRoute = "add"
const val removeRoute = "remove"
const val clearRoute = "clear"

View File

@ -2,12 +2,10 @@ package dev.inmo.micro_utils.repos.ktor.server.crud
import dev.inmo.micro_utils.ktor.server.decodeUrlQueryValueOrSendError
import dev.inmo.micro_utils.ktor.server.unianswer
import dev.inmo.micro_utils.repos.ktor.common.crud.containsRouting
import dev.inmo.micro_utils.repos.ktor.common.crud.getByIdRouting
import dev.inmo.micro_utils.repos.ktor.common.crud.getByPaginationRouting
import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.pagination.extractPagination
import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo
import dev.inmo.micro_utils.repos.ktor.common.crud.*
import io.ktor.application.call
import io.ktor.routing.Route
import io.ktor.routing.get
@ -54,4 +52,11 @@ fun <ObjectType, IdType> Route.configureReadStandardCrudRepoRoutes(
originalRepo.contains(id)
)
}
get(countRouting) {
call.unianswer(
Long.serializer(),
originalRepo.count()
)
}
}

View File

@ -4,7 +4,7 @@ import dev.inmo.micro_utils.ktor.server.decodeUrlQueryValueOrSendError
import dev.inmo.micro_utils.ktor.server.unianswer
import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.pagination.extractPagination
import dev.inmo.micro_utils.repos.StandardReadKeyValueRepo
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.*
@ -12,7 +12,7 @@ import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer
fun <K, V> Route.configureReadStandartKeyValueRepoRoutes (
originalRepo: StandardReadKeyValueRepo<K, V>,
originalRepo: ReadStandardKeyValueRepo<K, V>,
keySerializer: KSerializer<K>,
valueSerializer: KSerializer<V>,
valueNullableSerializer: KSerializer<V?>,

View File

@ -2,7 +2,7 @@ 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.repos.StandardWriteKeyValueRepo
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.*
@ -10,7 +10,7 @@ import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.PairSerializer
fun <K, V> Route.configureWriteStandartKeyValueRepoRoutes (
originalRepo: StandardWriteKeyValueRepo<K, V>,
originalRepo: WriteStandardKeyValueRepo<K, V>,
keySerializer: KSerializer<K>,
valueSerializer: KSerializer<V>,
) {

View File

@ -5,7 +5,7 @@ import dev.inmo.micro_utils.ktor.server.decodeUrlQueryValueOrSendError
import dev.inmo.micro_utils.ktor.server.unianswer
import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.pagination.extractPagination
import dev.inmo.micro_utils.repos.OneToManyReadKeyValueRepo
import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo
import dev.inmo.micro_utils.repos.ktor.common.one_to_many.*
import io.ktor.application.call
import io.ktor.routing.Route
@ -14,7 +14,7 @@ import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer
fun <Key, Value> Route.configureOneToManyReadKeyValueRepoRoutes(
originalRepo: OneToManyReadKeyValueRepo<Key, Value>,
originalRepo: ReadOneToManyKeyValueRepo<Key, Value>,
keySerializer: KSerializer<Key>,
valueSealizer: KSerializer<Value>,
) {

View File

@ -1,8 +1,7 @@
package dev.inmo.micro_utils.repos.ktor.server.one_to_many
import dev.inmo.micro_utils.ktor.server.unianswer
import dev.inmo.micro_utils.ktor.server.uniload
import dev.inmo.micro_utils.repos.OneToManyWriteKeyValueRepo
import dev.inmo.micro_utils.ktor.server.*
import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo
import dev.inmo.micro_utils.repos.ktor.common.one_to_many.*
import io.ktor.application.call
import io.ktor.routing.Route
@ -12,12 +11,28 @@ import kotlinx.serialization.builtins.PairSerializer
import kotlinx.serialization.builtins.serializer
fun <Key, Value> Route.configureOneToManyWriteKeyValueRepoRoutes(
originalRepo: OneToManyWriteKeyValueRepo<Key, Value>,
originalRepo: WriteOneToManyKeyValueRepo<Key, Value>,
keySerializer: KSerializer<Key>,
valueSealizer: KSerializer<Value>,
) {
val keyValueSerializer = PairSerializer(keySerializer, valueSealizer)
includeWebsocketHandling(
onNewValueRoute,
originalRepo.onNewValue,
keyValueSerializer
)
includeWebsocketHandling(
onValueRemovedRoute,
originalRepo.onValueRemoved,
keyValueSerializer
)
includeWebsocketHandling(
onDataClearedRoute,
originalRepo.onDataCleared,
keySerializer
)
post(addRoute) {
val obj = call.uniload(
keyValueSerializer

View File

@ -9,6 +9,7 @@ String[] includes = [
":mime_types",
":repos:common",
":repos:exposed",
":repos:inmemory",
":repos:ktor:client",
":repos:ktor:common",
":repos:ktor:server",