Skip to content

Commit

Permalink
feat: implement in-row block positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
d1snin committed Nov 9, 2023
1 parent 29d2aad commit 2693b40
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public class BlockContext internal constructor(
modifyBlock(row = row())
}

public suspend fun setIndex(index: suspend () -> BlockIndex?) {
modifyBlock(index = index())
}

public suspend fun setSize(size: suspend () -> BlockSize) {
modifyBlock(size = size())
}
Expand All @@ -58,12 +62,13 @@ public class BlockContext internal constructor(

private suspend fun modifyBlock(
row: RowIndex = block.row,
index: BlockIndex? = block.index,
size: BlockSize = block.size,
entities: ContentEntities = block.entities,
metadata: Metadata = block.metadata,
) {
operationLock.withLock {
val modification = BlockModification(row, size, entities, metadata, block.spaceId)
val modification = BlockModification(row, index, size, entities, metadata, block.spaceId)

log.d {
"Modifying block with the following data: $modification"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class BlockModificationBuilder() : ContentEntitiesBuilder() {

public var row: RowIndex? = null

public var index: BlockIndex? = null

public var size: BlockSize? = null

public var spaceId: SpaceIdentifier? = null
Expand All @@ -33,6 +35,7 @@ public class BlockModificationBuilder() : ContentEntitiesBuilder() {

public constructor(modification: BlockModification) : this() {
row = modification.row
index = modification.index
size = modification.size
entities = modification.entities.toMutableList()
metadataBuilder.metadata = modification.metadata.toMutableMap()
Expand All @@ -50,6 +53,7 @@ public class BlockModificationBuilder() : ContentEntitiesBuilder() {
public fun buildBlockModification(): BlockModification =
BlockModification(
row ?: error("Block row is undefined"),
index,
size ?: error("Block size is undefined"),
buildContentEntities(),
metadataBuilder.buildMetadata(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ public typealias BlockId = String

public typealias Blocks = List<Block>

public typealias BlockIndex = Int

public sealed interface AbstractBlock {

public val row: RowIndex

public val index: BlockIndex?

public val size: BlockSize

public val entities: ContentEntities
Expand All @@ -40,6 +44,7 @@ public sealed interface AbstractBlock {
public data class Block(
val id: BlockId,
override val row: RowIndex,
override val index: BlockIndex?,
override val size: BlockSize,
override val entities: ContentEntities,
override val metadata: Metadata,
Expand All @@ -49,6 +54,7 @@ public data class Block(
@Serializable
public data class BlockModification(
override val row: RowIndex,
override val index: BlockIndex?,
override val size: BlockSize,
override val entities: ContentEntities,
override val metadata: Metadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@
package dev.d1s.beam.commons.validation

import dev.d1s.beam.commons.AbstractBlock
import dev.d1s.beam.commons.BlockIndex
import io.konform.validation.Validation
import io.konform.validation.ValidationBuilder
import io.konform.validation.jsonschema.maxItems
import io.konform.validation.jsonschema.minItems
import io.konform.validation.jsonschema.minimum

public val validateBlock: Validation<AbstractBlock> = Validation {
AbstractBlock::row {
requireValidaRowIndex()
requireValidRowIndex()
}

AbstractBlock::index ifPresent {
requireValidBlockIndex()
}

AbstractBlock::entities {
Expand All @@ -38,4 +45,8 @@ public val validateBlock: Validation<AbstractBlock> = Validation {
AbstractBlock::metadata {
run(validateMetadata)
}
}

private fun ValidationBuilder<BlockIndex>.requireValidBlockIndex() {
minimum(0) hint "block index must be greater or equal to 0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ import dev.d1s.beam.commons.RowIndex
import io.konform.validation.ValidationBuilder
import io.konform.validation.jsonschema.minimum

internal fun ValidationBuilder<RowIndex>.requireValidaRowIndex() {
internal fun ValidationBuilder<RowIndex>.requireValidRowIndex() {
minimum(0) hint "row index must be greater or equal to 0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class BlockDtoConverter : DtoConverter<BlockEntity, Block>, KoinComponent {
Block(
id.toString(),
row,
index,
size,
entities,
metadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class BlockModificationDtoConverter : DtoConverter<BlockEntity, BlockModificatio
override suspend fun convertToEntity(dto: BlockModification) =
BlockEntity {
row = dto.row
index = dto.index
size = dto.size
entities = dto.entities
metadata = dto.metadata
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package dev.d1s.beam.daemon.database

import dev.d1s.beam.commons.RowIndex
import dev.d1s.beam.daemon.entity.BlockEntities
import dev.d1s.beam.daemon.entity.BlockEntity
import dev.d1s.beam.daemon.entity.SpaceEntity
Expand All @@ -36,8 +35,6 @@ interface BlockRepository {

suspend fun countBlocksInSpace(space: SpaceEntity): Result<Int>

suspend fun findLatestBlockRowInSpace(space: SpaceEntity): Result<Int>

suspend fun findBlocksInSpace(space: SpaceEntity): Result<BlockEntities>

suspend fun updateBlock(block: BlockEntity): Result<BlockEntity>
Expand Down Expand Up @@ -71,18 +68,9 @@ class DefaultBlockRepository : BlockRepository, KoinComponent {
findBlocksInSpaceAsSequence(space).count()
}

override suspend fun findLatestBlockRowInSpace(space: SpaceEntity): Result<RowIndex> =
withIoCatching {
findBlocksInSpaceAsSequence(space).sortedByDescending {
it.row
}.first().row
}

override suspend fun findBlocksInSpace(space: SpaceEntity): Result<BlockEntities> =
withIoCatching {
findBlocksInSpaceAsSequence(space).sortedBy {
it.row
}.toList()
findBlocksInSpaceAsSequence(space).toList()
}

override suspend fun updateBlock(block: BlockEntity): Result<BlockEntity> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ object Blocks : UuidIdentifiedEntities<BlockEntity>(tableName = "block") {
it.row
}

val index = int("index").bindTo {
it.index
}

val size = enum<BlockSize>("size").bindTo {
it.size
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ import org.koin.core.component.inject
import org.ktorm.database.Database
import org.ktorm.dsl.and
import org.ktorm.dsl.eq
import org.ktorm.entity.add
import org.ktorm.entity.filter
import org.ktorm.entity.find
import org.ktorm.entity.toList
import org.ktorm.entity.*

interface RowRepository {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package dev.d1s.beam.daemon.entity

import dev.d1s.beam.commons.BlockIndex
import dev.d1s.beam.commons.BlockSize
import dev.d1s.beam.commons.Metadata
import dev.d1s.beam.commons.RowIndex
Expand All @@ -29,6 +30,8 @@ interface BlockEntity : UuidIdentified<BlockEntity> {

var row: RowIndex

var index: BlockIndex?

var size: BlockSize

var entities: ContentEntities
Expand All @@ -41,4 +44,4 @@ interface BlockEntity : UuidIdentified<BlockEntity> {
}

val BlockEntity.asString
get() = "BlockEntity{row = $row, size = $size, entities = $entities, metadata = $metadata}"
get() = "BlockEntity{row = $row, index = $index, size = $size, entities = $entities, metadata = $metadata}"
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,17 @@ class DefaultBlockService : BlockService, KoinComponent {

val blocks = blockRepository.findBlocksInSpace(space).getOrThrow()

val translatedBlocks = translateOptionally(blocks, languageCode)
val sortedBlocks = blocks
.sortedBy {
it.index ?: MAX_INDEX
}
.sortedBy {
it.row
}

translatedBlocks to blockDtoConverter.convertToDtoListIf(blocks) {
val translatedBlocks = translateOptionally(sortedBlocks, languageCode)

translatedBlocks to blockDtoConverter.convertToDtoListIf(translatedBlocks) {
requireDto
}
}
Expand Down Expand Up @@ -253,4 +261,9 @@ class DefaultBlockService : BlockService, KoinComponent {
}
eventChannel.send(event)
}

private companion object {

private const val MAX_INDEX = Int.MAX_VALUE
}
}
6 changes: 6 additions & 0 deletions config/db/changelog.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
}
}
},
{
"column": {
"name": "index",
"type": "integer"
}
},
{
"column": {
"name": "row",
Expand Down
1 change: 1 addition & 0 deletions http/daemon.http
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Content-Type: application/json

{
"row": 0,
"index": null,
"size": "MEDIUM",
"entities": [
{
Expand Down

0 comments on commit 2693b40

Please sign in to comment.