mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-18 14:47:15 +00:00
small refactorings
This commit is contained in:
parent
1e72a4117a
commit
29b3e6b701
@ -1,5 +0,0 @@
|
|||||||
package dev.inmo.micro_utils.repos
|
|
||||||
|
|
||||||
import com.benasher44.uuid.uuid4
|
|
||||||
|
|
||||||
fun generateId() = uuid4().toString()
|
|
@ -12,12 +12,10 @@ interface OneToManyReadKeyValueRepo<Key, Value> : Repo {
|
|||||||
suspend fun count(): Long
|
suspend fun count(): Long
|
||||||
}
|
}
|
||||||
|
|
||||||
interface OneToManyWriteKeyValueRepo<Key, Value> :
|
interface OneToManyWriteKeyValueRepo<Key, Value> : Repo {
|
||||||
Repo {
|
|
||||||
suspend fun add(k: Key, v: Value)
|
suspend fun add(k: Key, v: Value)
|
||||||
suspend fun remove(k: Key, v: Value)
|
suspend fun remove(k: Key, v: Value)
|
||||||
suspend fun clear(k: Key)
|
suspend fun clear(k: Key)
|
||||||
}
|
}
|
||||||
|
|
||||||
interface OneToManyKeyValueRepo<Key, Value> : OneToManyReadKeyValueRepo<Key, Value>,
|
interface OneToManyKeyValueRepo<Key, Value> : OneToManyReadKeyValueRepo<Key, Value>, OneToManyWriteKeyValueRepo<Key, Value>
|
||||||
OneToManyWriteKeyValueRepo<Key, Value>
|
|
@ -20,5 +20,4 @@ interface StandardWriteKeyValueRepo<Key, Value> : Repo {
|
|||||||
suspend fun unset(k: Key)
|
suspend fun unset(k: Key)
|
||||||
}
|
}
|
||||||
|
|
||||||
interface StandardKeyValueRepo<Key, Value> : StandardReadKeyValueRepo<Key, Value>,
|
interface StandardKeyValueRepo<Key, Value> : StandardReadKeyValueRepo<Key, Value>, StandardWriteKeyValueRepo<Key, Value>
|
||||||
StandardWriteKeyValueRepo<Key, Value>
|
|
@ -24,7 +24,7 @@ abstract class AbstractExposedKeyValueRepo<Key, Value>(
|
|||||||
override val onValueRemoved: Flow<Key> = onValueRemovedChannel.asFlow()
|
override val onValueRemoved: Flow<Key> = onValueRemovedChannel.asFlow()
|
||||||
|
|
||||||
override suspend fun set(k: Key, v: Value) {
|
override suspend fun set(k: Key, v: Value) {
|
||||||
transaction(db = database) {
|
transaction(database) {
|
||||||
if (select { keyColumn.eq(k) }.limit(1).any()) {
|
if (select { keyColumn.eq(k) }.limit(1).any()) {
|
||||||
update({ keyColumn.eq(k) }) {
|
update({ keyColumn.eq(k) }) {
|
||||||
it[valueColumn] = v
|
it[valueColumn] = v
|
||||||
@ -40,7 +40,7 @@ abstract class AbstractExposedKeyValueRepo<Key, Value>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun unset(k: Key) {
|
override suspend fun unset(k: Key) {
|
||||||
transaction(db = database) {
|
transaction(database) {
|
||||||
deleteWhere { keyColumn.eq(k) }
|
deleteWhere { keyColumn.eq(k) }
|
||||||
}
|
}
|
||||||
onValueRemovedChannel.send(k)
|
onValueRemovedChannel.send(k)
|
||||||
|
@ -15,23 +15,23 @@ abstract class AbstractExposedReadKeyValueRepo<Key, Value>(
|
|||||||
) : StandardReadKeyValueRepo<Key, Value>, Table() {
|
) : StandardReadKeyValueRepo<Key, Value>, Table() {
|
||||||
override val primaryKey: PrimaryKey = PrimaryKey(keyColumn, valueColumn)
|
override val primaryKey: PrimaryKey = PrimaryKey(keyColumn, valueColumn)
|
||||||
|
|
||||||
override suspend fun get(k: Key): Value? = transaction(db = database) {
|
override suspend fun get(k: Key): Value? = transaction(database) {
|
||||||
select { keyColumn.eq(k) }.limit(1).firstOrNull() ?.getOrNull(valueColumn)
|
select { keyColumn.eq(k) }.limit(1).firstOrNull() ?.getOrNull(valueColumn)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun contains(key: Key): Boolean = transaction(db = database) {
|
override suspend fun contains(key: Key): Boolean = transaction(database) {
|
||||||
select { keyColumn.eq(key) }.limit(1).any()
|
select { keyColumn.eq(key) }.limit(1).any()
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun count(): Long = transaction(db = database) { selectAll().count() }
|
override suspend fun count(): Long = transaction(database) { selectAll().count() }
|
||||||
|
|
||||||
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> = transaction(db = database) {
|
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> = transaction(database) {
|
||||||
selectAll().paginate(pagination, keyColumn to if (reversed) SortOrder.DESC else SortOrder.ASC).map {
|
selectAll().paginate(pagination, keyColumn to if (reversed) SortOrder.DESC else SortOrder.ASC).map {
|
||||||
it[keyColumn]
|
it[keyColumn]
|
||||||
}
|
}
|
||||||
}.createPaginationResult(pagination, count())
|
}.createPaginationResult(pagination, count())
|
||||||
|
|
||||||
override suspend fun values(pagination: Pagination, reversed: Boolean): PaginationResult<Value> = transaction(db = database) {
|
override suspend fun values(pagination: Pagination, reversed: Boolean): PaginationResult<Value> = transaction(database) {
|
||||||
selectAll().paginate(pagination, keyColumn to if (reversed) SortOrder.DESC else SortOrder.ASC).map {
|
selectAll().paginate(pagination, keyColumn to if (reversed) SortOrder.DESC else SortOrder.ASC).map {
|
||||||
it[valueColumn]
|
it[valueColumn]
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ abstract class AbstractOneToManyExposedKeyValueRepo<Key, Value>(
|
|||||||
database
|
database
|
||||||
) {
|
) {
|
||||||
override suspend fun add(k: Key, v: Value) {
|
override suspend fun add(k: Key, v: Value) {
|
||||||
transaction(db = database) {
|
transaction(database) {
|
||||||
insert {
|
insert {
|
||||||
it[keyColumn] = k
|
it[keyColumn] = k
|
||||||
it[valueColumn] = v
|
it[valueColumn] = v
|
||||||
@ -23,10 +23,10 @@ abstract class AbstractOneToManyExposedKeyValueRepo<Key, Value>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun remove(k: Key, v: Value) {
|
override suspend fun remove(k: Key, v: Value) {
|
||||||
transaction(db = database) { deleteWhere { keyColumn.eq(k).and(valueColumn.eq(v)) } }
|
transaction(database) { deleteWhere { keyColumn.eq(k).and(valueColumn.eq(v)) } }
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun clear(k: Key) {
|
override suspend fun clear(k: Key) {
|
||||||
transaction(db = database) { deleteWhere { keyColumn.eq(k) } }
|
transaction(database) { deleteWhere { keyColumn.eq(k) } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,33 +18,33 @@ abstract class AbstractOneToManyExposedReadKeyValueRepo<Key, Value>(
|
|||||||
protected val keyColumn: Column<Key> = keyColumnAllocator()
|
protected val keyColumn: Column<Key> = keyColumnAllocator()
|
||||||
protected val valueColumn: Column<Value> = valueColumnAllocator()
|
protected val valueColumn: Column<Value> = valueColumnAllocator()
|
||||||
|
|
||||||
override suspend fun count(k: Key): Long = transaction(db = database) { select { keyColumn.eq(k) }.count() }
|
override suspend fun count(k: Key): Long = transaction(database) { select { keyColumn.eq(k) }.count() }
|
||||||
|
|
||||||
override suspend fun count(): Long = transaction(db = database) { selectAll().count() }
|
override suspend fun count(): Long = transaction(database) { selectAll().count() }
|
||||||
|
|
||||||
override suspend fun get(
|
override suspend fun get(
|
||||||
k: Key,
|
k: Key,
|
||||||
pagination: Pagination,
|
pagination: Pagination,
|
||||||
reversed: Boolean
|
reversed: Boolean
|
||||||
): PaginationResult<Value> = transaction(db = database) {
|
): PaginationResult<Value> = transaction(database) {
|
||||||
select { keyColumn.eq(k) }.paginate(pagination, keyColumn, reversed).map { it[valueColumn] }
|
select { keyColumn.eq(k) }.paginate(pagination, keyColumn, reversed).map { it[valueColumn] }
|
||||||
}.createPaginationResult(
|
}.createPaginationResult(
|
||||||
pagination,
|
pagination,
|
||||||
count(k)
|
count(k)
|
||||||
)
|
)
|
||||||
|
|
||||||
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> = transaction(db = database) {
|
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> = transaction(database) {
|
||||||
selectAll().paginate(pagination, keyColumn, reversed).map { it[keyColumn] }
|
selectAll().paginate(pagination, keyColumn, reversed).map { it[keyColumn] }
|
||||||
}.createPaginationResult(
|
}.createPaginationResult(
|
||||||
pagination,
|
pagination,
|
||||||
count()
|
count()
|
||||||
)
|
)
|
||||||
|
|
||||||
override suspend fun contains(k: Key): Boolean = transaction(db = database) {
|
override suspend fun contains(k: Key): Boolean = transaction(database) {
|
||||||
select { keyColumn.eq(k) }.limit(1).any()
|
select { keyColumn.eq(k) }.limit(1).any()
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun contains(k: Key, v: Value): Boolean = transaction(db = database) {
|
override suspend fun contains(k: Key, v: Value): Boolean = transaction(database) {
|
||||||
select { keyColumn.eq(k).and(valueColumn.eq(v)) }.limit(1).any()
|
select { keyColumn.eq(k).and(valueColumn.eq(v)) }.limit(1).any()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user