update onetomany naming and changelog

This commit is contained in:
InsanusMokrassar 2020-10-14 22:02:10 +06:00
parent de97872d41
commit 22f5a59dfa
12 changed files with 85 additions and 40 deletions

View File

@ -20,12 +20,14 @@ Inited :)
* `Set` * `Set`
* `Repos` * `Repos`
* `Common` * `Common`
* Interfaces related to `OneToManyKeyValueRepo` 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: * Extensions `doForAll` and `getAll` were added for all current types of repos:
* `ReadStandardCRUDRepo` * `ReadStandardCRUDRepo`
* `StandardReadKeyValueRepo` * `ReadStandardKeyValueRepo`
* `OneToManyReadKeyValueRepo` * `ReadOneToManyKeyValueRepo`
* `StandardReadKeyValueRepo` methods `values` and `keys` now have default value for `reversed` parameter `false` * `ReadStandardKeyValueRepo` methods `values` and `keys` now have default value for `reversed` parameter `false`
* New `Flow`'s in `OneToManyWriteKeyValueRepo`: * New `Flow`'s in `WriteOneToManyKeyValueRepo`:
* `onNewValue` * `onNewValue`
* `onValueRemoved` * `onValueRemoved`
* `onDataCleared` * `onDataCleared`

View File

@ -4,7 +4,7 @@ import dev.inmo.micro_utils.pagination.Pagination
import dev.inmo.micro_utils.pagination.PaginationResult import dev.inmo.micro_utils.pagination.PaginationResult
import kotlinx.coroutines.flow.Flow 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 get(k: Key, pagination: Pagination, reversed: Boolean = false): PaginationResult<Value>
suspend fun keys(pagination: Pagination, reversed: Boolean = false): PaginationResult<Key> suspend fun keys(pagination: Pagination, reversed: Boolean = false): PaginationResult<Key>
suspend fun contains(k: Key): Boolean suspend fun contains(k: Key): Boolean
@ -12,8 +12,10 @@ interface OneToManyReadKeyValueRepo<Key, Value> : Repo {
suspend fun count(k: Key): Long suspend fun count(k: Key): Long
suspend fun count(): Long suspend fun count(): Long
} }
@Deprecated("Renamed", ReplaceWith("ReadOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo"))
typealias OneToManyReadKeyValueRepo<Key, Value> = ReadOneToManyKeyValueRepo<Key, Value>
interface OneToManyWriteKeyValueRepo<Key, Value> : Repo { interface WriteOneToManyKeyValueRepo<Key, Value> : Repo {
val onNewValue: Flow<Pair<Key, Value>> val onNewValue: Flow<Pair<Key, Value>>
val onValueRemoved: Flow<Pair<Key, Value>> val onValueRemoved: Flow<Pair<Key, Value>>
val onDataCleared: Flow<Key> val onDataCleared: Flow<Key>
@ -22,5 +24,7 @@ interface OneToManyWriteKeyValueRepo<Key, Value> : Repo {
suspend fun remove(k: Key, v: Value) suspend fun remove(k: Key, v: Value)
suspend fun clear(k: Key) 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

@ -3,7 +3,7 @@ package dev.inmo.micro_utils.repos.pagination
import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.* 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") @Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE")
methodCaller: suspend REPO.(Pagination) -> PaginationResult<Key>, methodCaller: suspend REPO.(Pagination) -> PaginationResult<Key>,
block: (List<Pair<Key, List<Value>>>) -> Unit 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 block: (List<Pair<Key, List<Value>>>) -> Unit
) = doForAll({ keys(it, false) }, block) ) = 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") @Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE")
methodCaller: suspend REPO.(Pagination) -> PaginationResult<Key> methodCaller: suspend REPO.(Pagination) -> PaginationResult<Key>
): List<Pair<Key, List<Value>>> { ): List<Pair<Key, List<Value>>> {

View File

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

@ -5,9 +5,9 @@ import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.pagination.utils.paginate import dev.inmo.micro_utils.pagination.utils.paginate
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
class ReadOneToManyKeyValueRepo<Key, Value>( class MapReadOneToManyKeyValueRepo<Key, Value>(
private val map: Map<Key, List<Value>> = emptyMap() private val map: Map<Key, List<Value>> = emptyMap()
) : OneToManyReadKeyValueRepo<Key, Value> { ) : ReadOneToManyKeyValueRepo<Key, Value> {
override suspend fun get(k: Key, pagination: Pagination, reversed: Boolean): PaginationResult<Value> { override suspend fun get(k: Key, pagination: Pagination, reversed: Boolean): PaginationResult<Value> {
val list = map[k] ?: return emptyPaginationResult() val list = map[k] ?: return emptyPaginationResult()
@ -44,9 +44,9 @@ class ReadOneToManyKeyValueRepo<Key, Value>(
override suspend fun count(): Long = map.size.toLong() override suspend fun count(): Long = map.size.toLong()
} }
class WriteOneToManyKeyValueRepo<Key, Value>( class MapWriteOneToManyKeyValueRepo<Key, Value>(
private val map: MutableMap<Key, MutableList<Value>> = mutableMapOf() private val map: MutableMap<Key, MutableList<Value>> = mutableMapOf()
) : OneToManyWriteKeyValueRepo<Key, Value> { ) : WriteOneToManyKeyValueRepo<Key, Value> {
private val _onNewValue: BroadcastFlow<Pair<Key, Value>> = BroadcastFlow() private val _onNewValue: BroadcastFlow<Pair<Key, Value>> = BroadcastFlow()
override val onNewValue: Flow<Pair<Key, Value>> override val onNewValue: Flow<Pair<Key, Value>>
get() = _onNewValue get() = _onNewValue
@ -74,8 +74,8 @@ class WriteOneToManyKeyValueRepo<Key, Value>(
class MapOneToManyKeyValueRepo<Key, Value>( class MapOneToManyKeyValueRepo<Key, Value>(
private val map: MutableMap<Key, MutableList<Value>> = mutableMapOf() private val map: MutableMap<Key, MutableList<Value>> = mutableMapOf()
) : OneToManyKeyValueRepo<Key, Value>, ) : OneToManyKeyValueRepo<Key, Value>,
OneToManyReadKeyValueRepo<Key, Value> by ReadOneToManyKeyValueRepo(map), ReadOneToManyKeyValueRepo<Key, Value> by MapReadOneToManyKeyValueRepo(map),
OneToManyWriteKeyValueRepo<Key, Value> by WriteOneToManyKeyValueRepo(map) WriteOneToManyKeyValueRepo<Key, Value> by MapWriteOneToManyKeyValueRepo(map)
fun <K, V> MutableMap<K, List<V>>.asOneToManyKeyValueRepo(): OneToManyKeyValueRepo<K, V> = MapOneToManyKeyValueRepo( fun <K, V> MutableMap<K, List<V>>.asOneToManyKeyValueRepo(): OneToManyKeyValueRepo<K, V> = MapOneToManyKeyValueRepo(
map { (k, v) -> k to v.toMutableList() }.toMap().toMutableMap() map { (k, v) -> k to v.toMutableList() }.toMap().toMutableMap()

View File

@ -1,8 +1,8 @@
package dev.inmo.micro_utils.repos.ktor.client.one_to_many package dev.inmo.micro_utils.repos.ktor.client.one_to_many
import dev.inmo.micro_utils.repos.OneToManyKeyValueRepo import dev.inmo.micro_utils.repos.OneToManyKeyValueRepo
import dev.inmo.micro_utils.repos.OneToManyReadKeyValueRepo import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo
import dev.inmo.micro_utils.repos.OneToManyWriteKeyValueRepo import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
@ -13,13 +13,13 @@ class KtorOneToManyKeyValueRepo<Key, Value>(
keySerializer: KSerializer<Key>, keySerializer: KSerializer<Key>,
valueSerializer: KSerializer<Value>, valueSerializer: KSerializer<Value>,
) : OneToManyKeyValueRepo<Key, Value>, ) : OneToManyKeyValueRepo<Key, Value>,
OneToManyReadKeyValueRepo<Key, Value> by KtorOneToManyReadKeyValueRepo<Key, Value> ( ReadOneToManyKeyValueRepo<Key, Value> by KtorReadOneToManyKeyValueRepo<Key, Value> (
"$baseUrl/$baseSubpart", "$baseUrl/$baseSubpart",
client, client,
keySerializer, keySerializer,
valueSerializer, valueSerializer,
), ),
OneToManyWriteKeyValueRepo<Key, Value> by KtorOneToManyWriteKeyValueRepo<Key, Value> ( WriteOneToManyKeyValueRepo<Key, Value> by KtorWriteOneToManyKeyValueRepo<Key, Value> (
"$baseUrl/$baseSubpart", "$baseUrl/$baseSubpart",
client, client,
keySerializer, 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.Pagination
import dev.inmo.micro_utils.pagination.PaginationResult import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.pagination.asUrlQueryParts 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 dev.inmo.micro_utils.repos.ktor.common.one_to_many.*
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer import kotlinx.serialization.builtins.serializer
class KtorOneToManyReadKeyValueRepo<Key, Value> ( class KtorReadOneToManyKeyValueRepo<Key, Value> (
private val baseUrl: String, private val baseUrl: String,
private val client: HttpClient = HttpClient(), private val client: HttpClient = HttpClient(),
private val keySerializer: KSerializer<Key>, private val keySerializer: KSerializer<Key>,
private val valueSerializer: KSerializer<Value>, private val valueSerializer: KSerializer<Value>,
) : OneToManyReadKeyValueRepo<Key, Value> { ) : ReadOneToManyKeyValueRepo<Key, Value> {
private val paginationValueResultSerializer = PaginationResult.serializer(valueSerializer) private val paginationValueResultSerializer = PaginationResult.serializer(valueSerializer)
private val paginationKeyResultSerializer = PaginationResult.serializer(keySerializer) private val paginationKeyResultSerializer = PaginationResult.serializer(keySerializer)

View File

@ -1,22 +1,36 @@
package dev.inmo.micro_utils.repos.ktor.client.one_to_many package dev.inmo.micro_utils.repos.ktor.client.one_to_many
import dev.inmo.micro_utils.ktor.client.BodyPair import dev.inmo.micro_utils.coroutines.broadcastStateFlow
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.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 dev.inmo.micro_utils.repos.ktor.common.one_to_many.*
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.PairSerializer import kotlinx.serialization.builtins.PairSerializer
import kotlinx.serialization.builtins.serializer import kotlinx.serialization.builtins.serializer
class KtorOneToManyWriteKeyValueRepo<Key, Value> ( class KtorWriteOneToManyKeyValueRepo<Key, Value> (
private val baseUrl: String, private val baseUrl: String,
private val client: HttpClient = HttpClient(), private val client: HttpClient = HttpClient(),
private val keySerializer: KSerializer<Key>, private val keySerializer: KSerializer<Key>,
private val valueSerializer: KSerializer<Value>, private val valueSerializer: KSerializer<Value>
) : OneToManyWriteKeyValueRepo<Key, Value> { ) : WriteOneToManyKeyValueRepo<Key, Value> {
private val keyValueSerializer = PairSerializer(keySerializer, valueSerializer) 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( override suspend fun add(k: Key, v: Value) = client.unipost(
buildStandardUrl( buildStandardUrl(

View File

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

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

View File

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