mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-21 16:29:25 +00:00
Compare commits
22 Commits
Author | SHA1 | Date | |
---|---|---|---|
dbd2e963a1 | |||
6928ca5329 | |||
9ece160aa8 | |||
6115c1bcac | |||
e026e94cbf | |||
56cdd8d6af | |||
899e6760e1 | |||
864d0ffcc6 | |||
c6f417f8c8 | |||
9091fa5bd8 | |||
418af7874d | |||
4972cc9daa | |||
ff905e1491 | |||
64b0184a17 | |||
97cbe44fb5 | |||
452d8778c5 | |||
02ad1a748e | |||
c5a32e2b1b | |||
702300f761 | |||
efcaa08b70 | |||
7f7369b3a8 | |||
45a9a95888 |
19
CHANGELOG.md
19
CHANGELOG.md
@@ -1,5 +1,22 @@
|
||||
# Changelog
|
||||
|
||||
## 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`
|
||||
@@ -10,6 +27,7 @@
|
||||
* `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`
|
||||
@@ -20,6 +38,7 @@
|
||||
* Changed incoming types to `ColumnAllocator`
|
||||
* `open` instead of `abstract`
|
||||
* `OneToMany`:
|
||||
* `tableName` parameter
|
||||
* Class `AbstractExposedReadOneToManyKeyValueRepo`
|
||||
* Renamed to `ExposedReadOneToManyKeyValueRepo`
|
||||
* Changed incoming arguments order
|
||||
|
@@ -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
24
changelog_parser.sh
Executable 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
|
@@ -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()
|
||||
}
|
||||
|
@@ -19,4 +19,4 @@ github_release_plugin_version=2.2.12
|
||||
uuidVersion=0.2.2
|
||||
|
||||
group=dev.inmo
|
||||
version=0.2.0
|
||||
version=0.2.2
|
||||
|
@@ -43,6 +43,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]
|
||||
*/
|
||||
|
@@ -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)
|
||||
}
|
||||
|
@@ -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())
|
@@ -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))
|
||||
}
|
||||
}
|
@@ -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,6 +10,30 @@ 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>
|
||||
@@ -20,11 +43,51 @@ interface WriteOneToManyKeyValueRepo<Key, Value> : Repo {
|
||||
val onValueRemoved: Flow<Pair<Key, Value>>
|
||||
val onDataCleared: Flow<Key>
|
||||
|
||||
suspend fun add(toAdd: Map<Key, List<Value>>) = toAdd.forEach { (k, values) ->
|
||||
values.forEach { v ->
|
||||
add(k, v)
|
||||
}
|
||||
}
|
||||
@Deprecated("Will be extracted as extension for other add method")
|
||||
suspend fun add(k: Key, v: Value)
|
||||
|
||||
suspend fun remove(toRemove: Map<Key, List<Value>>) = toRemove.forEach { (k, values) ->
|
||||
values.forEach { v ->
|
||||
remove(k, v)
|
||||
}
|
||||
}
|
||||
@Deprecated("Will be extracted as extension for other remove method")
|
||||
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> : ReadOneToManyKeyValueRepo<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(
|
||||
k: Key,
|
||||
vararg v: Value
|
||||
) = add(mapOf(k to v.toList()))
|
||||
|
||||
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, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.remove(
|
||||
k: Key,
|
||||
vararg v: Value
|
||||
) = remove(mapOf(k to v.toList()))
|
||||
|
||||
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.remove(
|
||||
keysAndValues: List<Pair<Key, List<Value>>>
|
||||
) = remove(keysAndValues.toMap())
|
||||
|
||||
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.remove(
|
||||
vararg keysAndValues: Pair<Key, List<Value>>
|
||||
) = remove(keysAndValues.toMap())
|
||||
|
@@ -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")
|
||||
|
@@ -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")
|
||||
|
@@ -12,11 +12,13 @@ import org.jetbrains.exposed.sql.transactions.transaction
|
||||
open class ExposedKeyValueRepo<Key, Value>(
|
||||
database: Database,
|
||||
keyColumnAllocator: ColumnAllocator<Key>,
|
||||
valueColumnAllocator: ColumnAllocator<Value>
|
||||
valueColumnAllocator: ColumnAllocator<Value>,
|
||||
tableName: String? = null
|
||||
) : StandardKeyValueRepo<Key, Value>, ExposedReadKeyValueRepo<Key, Value>(
|
||||
database,
|
||||
keyColumnAllocator,
|
||||
valueColumnAllocator
|
||||
valueColumnAllocator,
|
||||
tableName
|
||||
) {
|
||||
private val onNewValueChannel = BroadcastChannel<Pair<Key, Value>>(Channel.BUFFERED)
|
||||
private val onValueRemovedChannel = BroadcastChannel<Key>(Channel.BUFFERED)
|
||||
@@ -24,8 +26,6 @@ open class ExposedKeyValueRepo<Key, Value>(
|
||||
override val onNewValue: Flow<Pair<Key, Value>> = onNewValueChannel.asFlow()
|
||||
override val onValueRemoved: Flow<Key> = onValueRemovedChannel.asFlow()
|
||||
|
||||
init { initTable() }
|
||||
|
||||
override suspend fun set(k: Key, v: Value) {
|
||||
transaction(database) {
|
||||
if (select { keyColumn.eq(k) }.limit(1).any()) {
|
||||
|
@@ -10,13 +10,12 @@ open class ExposedReadKeyValueRepo<Key, Value>(
|
||||
override val database: Database,
|
||||
keyColumnAllocator: ColumnAllocator<Key>,
|
||||
valueColumnAllocator: ColumnAllocator<Value>,
|
||||
) : ReadStandardKeyValueRepo<Key, Value>, ExposedRepo, Table() {
|
||||
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)
|
||||
|
||||
init { initTable() }
|
||||
|
||||
override suspend fun get(k: Key): Value? = transaction(database) {
|
||||
select { keyColumn.eq(k) }.limit(1).firstOrNull() ?.getOrNull(valueColumn)
|
||||
}
|
||||
|
@@ -11,11 +11,13 @@ import org.jetbrains.exposed.sql.transactions.transaction
|
||||
open class ExposedOneToManyKeyValueRepo<Key, Value>(
|
||||
database: Database,
|
||||
keyColumnAllocator: ColumnAllocator<Key>,
|
||||
valueColumnAllocator: ColumnAllocator<Value>
|
||||
valueColumnAllocator: ColumnAllocator<Value>,
|
||||
tableName: String? = null
|
||||
) : OneToManyKeyValueRepo<Key, Value>, ExposedReadOneToManyKeyValueRepo<Key, Value>(
|
||||
database,
|
||||
keyColumnAllocator,
|
||||
valueColumnAllocator
|
||||
valueColumnAllocator,
|
||||
tableName
|
||||
) {
|
||||
protected val _onNewValue: BroadcastFlow<Pair<Key, Value>> = BroadcastFlow()
|
||||
override val onNewValue: Flow<Pair<Key, Value>>
|
||||
@@ -27,8 +29,6 @@ open class ExposedOneToManyKeyValueRepo<Key, Value>(
|
||||
override val onDataCleared: Flow<Key>
|
||||
get() = _onDataCleared
|
||||
|
||||
init { initTable() }
|
||||
|
||||
override suspend fun add(k: Key, v: Value) {
|
||||
transaction(database) {
|
||||
insert {
|
||||
|
@@ -9,8 +9,9 @@ import org.jetbrains.exposed.sql.transactions.transaction
|
||||
open class ExposedReadOneToManyKeyValueRepo<Key, Value>(
|
||||
override val database: Database,
|
||||
keyColumnAllocator: ColumnAllocator<Key>,
|
||||
valueColumnAllocator: ColumnAllocator<Value>
|
||||
) : ReadOneToManyKeyValueRepo<Key, Value>, ExposedRepo, Table() {
|
||||
valueColumnAllocator: ColumnAllocator<Value>,
|
||||
tableName: String? = null
|
||||
) : ReadOneToManyKeyValueRepo<Key, Value>, ExposedRepo, Table(tableName ?: "") {
|
||||
protected val keyColumn: Column<Key> = keyColumnAllocator()
|
||||
protected val valueColumn: Column<Value> = valueColumnAllocator()
|
||||
|
||||
|
@@ -2,6 +2,8 @@ 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 dev.inmo.micro_utils.pagination.utils.reverse
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
class ReadMapKeyValueRepo<Key, Value>(
|
||||
@@ -13,31 +15,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)
|
||||
|
@@ -3,6 +3,7 @@ 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 dev.inmo.micro_utils.pagination.utils.reverse
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
class MapReadOneToManyKeyValueRepo<Key, Value>(
|
||||
@@ -13,8 +14,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 +22,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)
|
||||
|
Reference in New Issue
Block a user