new ReadOneToManyKeyValueRepo#keys

This commit is contained in:
InsanusMokrassar 2020-11-14 16:44:28 +06:00
parent 498cd12f94
commit 3ba630684a
8 changed files with 98 additions and 1 deletions

View File

@ -2,6 +2,10 @@
## 0.4.0 ## 0.4.0
* `Repos`:
* `ReadOneToManyKeyValueRepo` got `keys` method with value parameter
* All implementations inside of this library has been updated
## 0.3.3 ## 0.3.3
* `Coroutines`: * `Coroutines`:

View File

@ -6,6 +6,7 @@ import kotlinx.coroutines.flow.Flow
interface ReadOneToManyKeyValueRepo<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 keys(v: Value, pagination: Pagination, reversed: Boolean = false): PaginationResult<Key>
suspend fun contains(k: Key): Boolean suspend fun contains(k: Key): Boolean
suspend fun contains(k: Key, v: Value): Boolean suspend fun contains(k: Key, v: Value): Boolean
suspend fun count(k: Key): Long suspend fun count(k: Key): Long

View File

@ -42,6 +42,23 @@ open class MapperReadOneToManyKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
) )
} }
override suspend fun keys(
v: FromValue,
pagination: Pagination,
reversed: Boolean
): PaginationResult<FromKey> = to.keys(
v.toOutValue(),
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): Boolean = to.contains(k.toOutKey())
override suspend fun contains(k: FromKey, v: FromValue): Boolean = to.contains(k.toOutKey(), v.toOutValue()) override suspend fun contains(k: FromKey, v: FromValue): Boolean = to.contains(k.toOutKey(), v.toOutValue())

View File

@ -169,6 +169,33 @@ class OneToManyAndroidRepo<Key, Value>(
) )
} }
override suspend fun keys(
v: Value,
pagination: Pagination,
reversed: Boolean
): PaginationResult<Key> = count().let { count ->
val resultPagination = pagination.let { if (reversed) pagination.reverse(count) else pagination }
helper.readableTransaction {
select(
tableName,
selection = "$valueColumnName=?",
selectionArgs = arrayOf(v.asValue()),
limit = resultPagination.limitClause()
).use { c ->
mutableListOf<Key>().also {
if (c.moveToFirst()) {
do {
it.add(c.getString(idColumnName).asKey())
} while (c.moveToNext())
}
}
}
}.createPaginationResult(
pagination,
count
)
}
override suspend fun remove(toRemove: Map<Key, List<Value>>) { override suspend fun remove(toRemove: Map<Key, List<Value>>) {
helper.writableTransaction { helper.writableTransaction {
toRemove.flatMap { (k, vs) -> toRemove.flatMap { (k, vs) ->

View File

@ -39,6 +39,21 @@ open class ExposedReadOneToManyKeyValueRepo<Key, Value>(
count() count()
) )
override suspend fun keys(
v: Value,
pagination: Pagination,
reversed: Boolean
): PaginationResult<Key> = transaction(database) {
select { valueColumn.eq(v) }.let {
it.count() to it.paginate(pagination, keyColumn, reversed).map { it[keyColumn] }
}
}.let { (count, list) ->
list.createPaginationResult(
pagination,
count
)
}
override suspend fun contains(k: Key): Boolean = transaction(database) { override suspend fun contains(k: Key): Boolean = transaction(database) {
select { keyColumn.eq(k) }.limit(1).any() select { keyColumn.eq(k) }.limit(1).any()
} }

View File

@ -32,6 +32,18 @@ class MapReadOneToManyKeyValueRepo<Key, Value>(
} }
} }
override suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean): PaginationResult<Key> {
val keys = map.keys.filter { map[it] ?.contains(v) == true }
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
}
}
}
override suspend fun contains(k: Key): Boolean = map.containsKey(k) 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 contains(k: Key, v: Value): Boolean = map[k] ?.contains(v) == true

View File

@ -42,6 +42,18 @@ class KtorReadOneToManyKeyValueRepo<Key, Value> (
paginationKeyResultSerializer paginationKeyResultSerializer
) )
override suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean): PaginationResult<Key> = client.uniget(
buildStandardUrl(
baseUrl,
keysRoute,
mapOf(
valueParameterName to valueSerializer.encodeUrlQueryValue(v),
reversedParameterName to Boolean.serializer().encodeUrlQueryValue(reversed)
) + pagination.asUrlQueryParts
),
paginationKeyResultSerializer
)
override suspend fun contains(k: Key): Boolean = client.uniget( override suspend fun contains(k: Key): Boolean = client.uniget(
buildStandardUrl( buildStandardUrl(
baseUrl, baseUrl,

View File

@ -42,8 +42,17 @@ fun <Key, Value> Route.configureOneToManyReadKeyValueRepoRoutes(
reversedParameterName, reversedParameterName,
Boolean.serializer() Boolean.serializer()
) ?: false ) ?: false
val value: Value? = call.decodeUrlQueryValue(
valueParameterName,
valueSealizer
)
call.unianswer( value ?.also {
call.unianswer(
paginationKeyResult,
originalRepo.keys(value, pagination, reversed)
)
} ?: call.unianswer(
paginationKeyResult, paginationKeyResult,
originalRepo.keys(pagination, reversed) originalRepo.keys(pagination, reversed)
) )