small refactorings

This commit is contained in:
InsanusMokrassar 2020-09-26 22:02:12 +06:00
parent 1e72a4117a
commit 29b3e6b701
7 changed files with 19 additions and 27 deletions

View File

@ -1,5 +0,0 @@
package dev.inmo.micro_utils.repos
import com.benasher44.uuid.uuid4
fun generateId() = uuid4().toString()

View File

@ -12,12 +12,10 @@ interface OneToManyReadKeyValueRepo<Key, Value> : Repo {
suspend fun count(): Long
}
interface OneToManyWriteKeyValueRepo<Key, Value> :
Repo {
interface OneToManyWriteKeyValueRepo<Key, Value> : Repo {
suspend fun add(k: Key, v: Value)
suspend fun remove(k: Key, v: Value)
suspend fun clear(k: Key)
}
interface OneToManyKeyValueRepo<Key, Value> : OneToManyReadKeyValueRepo<Key, Value>,
OneToManyWriteKeyValueRepo<Key, Value>
interface OneToManyKeyValueRepo<Key, Value> : OneToManyReadKeyValueRepo<Key, Value>, OneToManyWriteKeyValueRepo<Key, Value>

View File

@ -20,5 +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> : StandardReadKeyValueRepo<Key, Value>, StandardWriteKeyValueRepo<Key, Value>

View File

@ -24,7 +24,7 @@ abstract class AbstractExposedKeyValueRepo<Key, Value>(
override val onValueRemoved: Flow<Key> = onValueRemovedChannel.asFlow()
override suspend fun set(k: Key, v: Value) {
transaction(db = database) {
transaction(database) {
if (select { keyColumn.eq(k) }.limit(1).any()) {
update({ keyColumn.eq(k) }) {
it[valueColumn] = v
@ -40,7 +40,7 @@ abstract class AbstractExposedKeyValueRepo<Key, Value>(
}
override suspend fun unset(k: Key) {
transaction(db = database) {
transaction(database) {
deleteWhere { keyColumn.eq(k) }
}
onValueRemovedChannel.send(k)

View File

@ -15,23 +15,23 @@ abstract class AbstractExposedReadKeyValueRepo<Key, Value>(
) : StandardReadKeyValueRepo<Key, Value>, Table() {
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)
}
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()
}
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 {
it[keyColumn]
}
}.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 {
it[valueColumn]
}

View File

@ -14,7 +14,7 @@ abstract class AbstractOneToManyExposedKeyValueRepo<Key, Value>(
database
) {
override suspend fun add(k: Key, v: Value) {
transaction(db = database) {
transaction(database) {
insert {
it[keyColumn] = k
it[valueColumn] = v
@ -23,10 +23,10 @@ abstract class AbstractOneToManyExposedKeyValueRepo<Key, 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) {
transaction(db = database) { deleteWhere { keyColumn.eq(k) } }
transaction(database) { deleteWhere { keyColumn.eq(k) } }
}
}

View File

@ -18,33 +18,33 @@ abstract class AbstractOneToManyExposedReadKeyValueRepo<Key, Value>(
protected val keyColumn: Column<Key> = keyColumnAllocator()
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(
k: Key,
pagination: Pagination,
reversed: Boolean
): PaginationResult<Value> = transaction(db = database) {
): PaginationResult<Value> = transaction(database) {
select { keyColumn.eq(k) }.paginate(pagination, keyColumn, reversed).map { it[valueColumn] }
}.createPaginationResult(
pagination,
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] }
}.createPaginationResult(
pagination,
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()
}
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()
}
}