updates in versions

This commit is contained in:
InsanusMokrassar 2020-04-10 12:35:33 +06:00
parent 4e5e427c31
commit 42eda90a6b
7 changed files with 35 additions and 50 deletions

View File

@ -1,9 +1,9 @@
kotlin.code.style=official kotlin.code.style=official
kotlin_version=1.3.61 kotlin_version=1.3.71
kotlin_coroutines_version=1.3.3 kotlin_coroutines_version=1.3.5
kotlin_serialisation_runtime_version=0.14.0 kotlin_serialisation_runtime_version=0.20.0
klockVersion=1.8.7 klockVersion=1.10.3
uuidVersion=0.0.7 uuidVersion=0.1.0
gradle_bintray_plugin_version=1.8.4 gradle_bintray_plugin_version=1.8.4

View File

@ -1,3 +0,0 @@
project_public_name=PostsSystemCore
kotlin.incremental.multiplatform=true

View File

@ -1,7 +1,5 @@
exposed_version=0.20.3 exposed_version=0.20.3
test_sqlite_version=3.28.0 test_sqlite_version=3.30.1
test_junit_version=5.5.2 test_junit_version=5.5.2
core_version=0.2.0 core_version=0.2.0
kotlin.incremental.multiplatform=true

View File

@ -0,0 +1,3 @@
package com.insanusmokrassar.postssystem.core.exposed
internal const val ChannelDefaultSize = 64

View File

@ -5,7 +5,6 @@ import com.insanusmokrassar.postssystem.core.content.api.ContentAPI
import com.insanusmokrassar.postssystem.core.utils.generateContentId import com.insanusmokrassar.postssystem.core.utils.generateContentId
import com.insanusmokrassar.postssystem.core.utils.pagination.* import com.insanusmokrassar.postssystem.core.utils.pagination.*
import kotlinx.coroutines.channels.BroadcastChannel import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow import kotlinx.coroutines.flow.asFlow
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
@ -18,21 +17,19 @@ private class ContentAPIDatabaseTable(
internal val idColumn = text("_id") internal val idColumn = text("_id")
internal val dataColumn = text("data") internal val dataColumn = text("data")
private inline fun <T> transaction(noinline body: Transaction.() -> T): T = database.transaction(body)
init { init {
transaction { transaction(database) {
SchemaUtils.createMissingTablesAndColumns(this@ContentAPIDatabaseTable) SchemaUtils.createMissingTablesAndColumns(this@ContentAPIDatabaseTable)
} }
} }
private val contentCreatedBroadcastChannel = BroadcastChannel<RegisteredContent>(Channel.BUFFERED) private val contentCreatedBroadcastChannel = BroadcastChannel<RegisteredContent>(ChannelDefaultSize)
private val contentDeletedBroadcastChannel = BroadcastChannel<RegisteredContent>(Channel.BUFFERED) private val contentDeletedBroadcastChannel = BroadcastChannel<RegisteredContent>(ChannelDefaultSize)
override val contentCreatedFlow: Flow<RegisteredContent> = contentCreatedBroadcastChannel.asFlow() override val contentCreatedFlow: Flow<RegisteredContent> = contentCreatedBroadcastChannel.asFlow()
override val contentDeletedFlow: Flow<RegisteredContent> = contentDeletedBroadcastChannel.asFlow() override val contentDeletedFlow: Flow<RegisteredContent> = contentDeletedBroadcastChannel.asFlow()
override suspend fun createContent(content: Content): RegisteredContent? { override suspend fun createContent(content: Content): RegisteredContent? {
return transaction { return transaction(database) {
insert { insert {
it[idColumn] = generateContentId() it[idColumn] = generateContentId()
it[dataColumn] = Json.plain.stringify(Content.serializer(), content) it[dataColumn] = Json.plain.stringify(Content.serializer(), content)
@ -48,7 +45,7 @@ private class ContentAPIDatabaseTable(
} }
override suspend fun deleteContent(id: ContentId): Boolean { override suspend fun deleteContent(id: ContentId): Boolean {
val content = getContentById(id) ?: return false val content = getContentById(id) ?: return false
return transaction { return transaction(database) {
deleteWhere { deleteWhere {
idColumn.eq(id) idColumn.eq(id)
} > 0 } > 0
@ -65,17 +62,17 @@ private class ContentAPIDatabaseTable(
) )
override suspend fun getContentsIds(): Set<ContentId> { override suspend fun getContentsIds(): Set<ContentId> {
return transaction { return transaction(database) {
selectAll().map { it[idColumn] } selectAll().map { it[idColumn] }
}.toSet() }.toSet()
} }
override suspend fun getContentById(id: ContentId): RegisteredContent? { override suspend fun getContentById(id: ContentId): RegisteredContent? {
return transaction { return transaction(database) {
select { idColumn.eq(id) }.firstOrNull() ?.asRegisteredContent() select { idColumn.eq(id) }.firstOrNull() ?.asRegisteredContent()
} }
} }
override suspend fun getContentByPagination(pagination: Pagination): PaginationResult<out RegisteredContent> { override suspend fun getContentByPagination(pagination: Pagination): PaginationResult<out RegisteredContent> {
return transaction { return transaction(database) {
selectAll().count() to selectAll().limit(n = pagination.size, offset = pagination.firstIndex).map { it.asRegisteredContent() } selectAll().count() to selectAll().limit(n = pagination.size, offset = pagination.firstIndex).map { it.asRegisteredContent() }
}.let { (count, results) -> }.let { (count, results) ->
pagination.createResult(count, results) pagination.createResult(count, results)

View File

@ -7,10 +7,10 @@ import com.insanusmokrassar.postssystem.core.utils.generatePostId
import com.insanusmokrassar.postssystem.core.utils.pagination.* import com.insanusmokrassar.postssystem.core.utils.pagination.*
import com.soywiz.klock.* import com.soywiz.klock.*
import kotlinx.coroutines.channels.BroadcastChannel import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow import kotlinx.coroutines.flow.asFlow
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
private class PostsAPIContentRelations( private class PostsAPIContentRelations(
private val database: Database private val database: Database
@ -18,28 +18,26 @@ private class PostsAPIContentRelations(
private val postIdColumn = text("postId") private val postIdColumn = text("postId")
private val contentIdColumn = text("contentId") private val contentIdColumn = text("contentId")
private inline fun <T> transaction(noinline body: Transaction.() -> T): T = database.transaction(body)
init { init {
transaction { transaction(database) {
SchemaUtils.createMissingTablesAndColumns(this@PostsAPIContentRelations) SchemaUtils.createMissingTablesAndColumns(this@PostsAPIContentRelations)
} }
} }
fun getPostContents(postId: PostId): List<ContentId> { fun getPostContents(postId: PostId): List<ContentId> {
return transaction { return transaction(database) {
select { postIdColumn.eq(postId) }.map { it[contentIdColumn] } select { postIdColumn.eq(postId) }.map { it[contentIdColumn] }
} }
} }
fun getContentPosts(contentId: ContentId): List<PostId> { fun getContentPosts(contentId: ContentId): List<PostId> {
return transaction { return transaction(database) {
select { contentIdColumn.eq(contentId) }.map { it[postIdColumn] } select { contentIdColumn.eq(contentId) }.map { it[postIdColumn] }
} }
} }
fun linkPostAndContents(postId: PostId, vararg contentIds: ContentId) { fun linkPostAndContents(postId: PostId, vararg contentIds: ContentId) {
transaction { transaction(database) {
val leftToPut = contentIds.toSet() - getPostContents(postId) val leftToPut = contentIds.toSet() - getPostContents(postId)
leftToPut.forEach { contentId -> leftToPut.forEach { contentId ->
insert { insert {
@ -50,7 +48,7 @@ private class PostsAPIContentRelations(
} }
} }
fun unlinkPostAndContents(postId: PostId, vararg contentIds: ContentId): Boolean { fun unlinkPostAndContents(postId: PostId, vararg contentIds: ContentId): Boolean {
return transaction { return transaction(database) {
deleteWhere { deleteWhere {
postIdColumn.eq(postId).and(contentIdColumn.inList(contentIds.toList())) postIdColumn.eq(postId).and(contentIdColumn.inList(contentIds.toList()))
} > 0 } > 0
@ -71,19 +69,17 @@ private class PostsAPIDatabaseTable(
) )
private val postCreatedBroadcastChannel = BroadcastChannel<RegisteredPost>(Channel.BUFFERED) private val postCreatedBroadcastChannel = BroadcastChannel<RegisteredPost>(ChannelDefaultSize)
override val postCreatedFlow: Flow<RegisteredPost> = postCreatedBroadcastChannel.asFlow() override val postCreatedFlow: Flow<RegisteredPost> = postCreatedBroadcastChannel.asFlow()
private val postDeletedBroadcastChannel = BroadcastChannel<RegisteredPost>(Channel.BUFFERED) private val postDeletedBroadcastChannel = BroadcastChannel<RegisteredPost>(ChannelDefaultSize)
override val postDeletedFlow: Flow<RegisteredPost> = postDeletedBroadcastChannel.asFlow() override val postDeletedFlow: Flow<RegisteredPost> = postDeletedBroadcastChannel.asFlow()
private val postUpdatedBroadcastChannel = BroadcastChannel<RegisteredPost>(Channel.BUFFERED) private val postUpdatedBroadcastChannel = BroadcastChannel<RegisteredPost>(ChannelDefaultSize)
override val postUpdatedFlow: Flow<RegisteredPost> = postUpdatedBroadcastChannel.asFlow() override val postUpdatedFlow: Flow<RegisteredPost> = postUpdatedBroadcastChannel.asFlow()
private inline fun <T> transaction(noinline body: Transaction.() -> T): T = database.transaction(body)
init { init {
transaction { transaction(database) {
SchemaUtils.createMissingTablesAndColumns(this@PostsAPIDatabaseTable) SchemaUtils.createMissingTablesAndColumns(this@PostsAPIDatabaseTable)
} }
} }
@ -98,7 +94,7 @@ private class PostsAPIDatabaseTable(
override suspend fun createPost(post: Post): RegisteredPost? { override suspend fun createPost(post: Post): RegisteredPost? {
val id = generatePostId() val id = generatePostId()
return transaction { return transaction(database) {
insert { insert {
it[idColumn] = id it[idColumn] = id
} }
@ -111,7 +107,7 @@ private class PostsAPIDatabaseTable(
override suspend fun deletePost(id: PostId): Boolean { override suspend fun deletePost(id: PostId): Boolean {
val post = getPostById(id) ?: return false val post = getPostById(id) ?: return false
return (transaction { return (transaction(database) {
deleteWhere { idColumn.eq(id) } deleteWhere { idColumn.eq(id) }
} > 0).also { } > 0).also {
if (it) { if (it) {
@ -122,7 +118,7 @@ private class PostsAPIDatabaseTable(
} }
override suspend fun updatePostContent(postId: PostId, post: Post): Boolean { override suspend fun updatePostContent(postId: PostId, post: Post): Boolean {
return transaction { return transaction(database) {
val alreadyLinked = contentsTable.getPostContents(postId) val alreadyLinked = contentsTable.getPostContents(postId)
val toRemove = alreadyLinked - post.content val toRemove = alreadyLinked - post.content
val toInsert = post.content - alreadyLinked val toInsert = post.content - alreadyLinked
@ -138,32 +134,32 @@ private class PostsAPIDatabaseTable(
} }
} }
override suspend fun getPostsIds(): Set<PostId> { override suspend fun getPostsIds(): Set<PostId> {
return transaction { return transaction(database) {
selectAll().map { it[idColumn] }.toSet() selectAll().map { it[idColumn] }.toSet()
} }
} }
override suspend fun getPostById(id: PostId): RegisteredPost? { override suspend fun getPostById(id: PostId): RegisteredPost? {
return transaction { return transaction(database) {
select { idColumn.eq(id) }.firstOrNull() ?.toRegisteredPost() select { idColumn.eq(id) }.firstOrNull() ?.toRegisteredPost()
} }
} }
override suspend fun getPostsByContent(id: ContentId): List<RegisteredPost> { override suspend fun getPostsByContent(id: ContentId): List<RegisteredPost> {
return transaction { return transaction(database) {
val postsIds = contentsTable.getContentPosts(id) val postsIds = contentsTable.getContentPosts(id)
select { idColumn.inList(postsIds) }.map { it.toRegisteredPost() } select { idColumn.inList(postsIds) }.map { it.toRegisteredPost() }
} }
} }
override suspend fun getPostsByCreatingDates(from: DateTime, to: DateTime): List<RegisteredPost> { override suspend fun getPostsByCreatingDates(from: DateTime, to: DateTime): List<RegisteredPost> {
return transaction { return transaction(database) {
select { creationDateColumn.between(from, to) }.map { it.toRegisteredPost() } select { creationDateColumn.between(from, to) }.map { it.toRegisteredPost() }
} }
} }
override suspend fun getPostsByPagination(pagination: Pagination): PaginationResult<RegisteredPost> { override suspend fun getPostsByPagination(pagination: Pagination): PaginationResult<RegisteredPost> {
return transaction { return transaction(database) {
val posts = selectAll().limit(pagination.size, pagination.firstIndex).orderBy(creationDateColumn).map { val posts = selectAll().limit(pagination.size, pagination.firstIndex).orderBy(creationDateColumn).map {
it.toRegisteredPost() it.toRegisteredPost()
} }

View File

@ -1,6 +0,0 @@
package com.insanusmokrassar.postssystem.core.exposed
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.Transaction
inline fun <T> Database.transaction(noinline body: Transaction.() -> T): T = org.jetbrains.exposed.sql.transactions.transaction(this, body)