Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change TreeTextNode to value class #190

Merged
merged 3 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("com.android.application") version libs.versions.agp apply false
id("com.android.library") version libs.versions.agp apply false
id("org.jetbrains.kotlin.android") version "1.9.23" apply false
id("org.jetbrains.kotlin.android") version "1.9.24" apply false
id("com.google.protobuf") version "0.9.4" apply false
id("org.jmailen.kotlinter") version "4.1.1" apply true
id("org.jetbrains.dokka") version "1.9.10" apply false
Expand Down
8 changes: 4 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
minSdk = "24"
compileSdk = "34"
targetSdk = "34"
agp = "8.4.0"
agp = "8.4.1"
connectKotlin = "0.6.1"
okhttp = "4.12.0"
coroutines = "1.8.1"
androidxActivity = "1.9.0"
androidxLifecycle = "2.8.0"
androidxLifecycle = "2.8.1"
androidxBenchmark = "1.2.4"
androidxComposeCompiler = "1.5.13"
androidxComposeCompiler = "1.5.14"

[libraries]
androidx-annotation = { module = "androidx.annotation:annotation", version = "1.8.0" }
Expand All @@ -25,7 +25,7 @@ kotlinx-collections-immutable = { group = "org.jetbrains.kotlinx", name = "kotli
apache-commons-collections = { group = "org.apache.commons", name = "commons-collections4", version = "4.4" }

androidx-core = { group = "androidx.core", name = "core-ktx", version = "1.13.1" }
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version = "1.6.1" }
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version = "1.7.0" }
androidx-activity = { group = "androidx.activity", name = "activity-ktx", version.ref = "androidxActivity" }
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "androidxActivity" }
androidx-constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version = "2.1.4" }
Expand Down
13 changes: 8 additions & 5 deletions yorkie/src/main/kotlin/dev/yorkie/document/crdt/TreeInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import dev.yorkie.util.IndexTreeNode.Companion.DEFAULT_TEXT_TYPE
* [TreeNode] represents the JSON representation of a node in the tree.
* It is used to serialize and deserialize the tree.
*/
internal sealed class TreeNode {
abstract val type: String
internal sealed interface TreeNode {
val type: String

fun toJsonTreeNode(): JsonTree.TreeNode {
return this as JsonTree.TreeNode
Expand All @@ -21,7 +21,7 @@ internal data class TreeElementNode(
override val type: String,
val childNodes: List<TreeNode> = emptyList(),
override val attributes: Map<String, String> = emptyMap(),
) : TreeNode(), JsonTree.ElementNode {
) : TreeNode, JsonTree.ElementNode {

@Suppress("UNCHECKED_CAST")
override val children: List<JsonTree.TreeNode> = childNodes as List<JsonTree.TreeNode>
Expand All @@ -48,8 +48,11 @@ internal data class TreeElementNode(
}
}

internal data class TreeTextNode(override val value: String = "") : TreeNode(), JsonTree.TextNode {
override val type: String = DEFAULT_TEXT_TYPE
@JvmInline
internal value class TreeTextNode(override val value: String = "") : TreeNode, JsonTree.TextNode {

override val type: String
get() = DEFAULT_TEXT_TYPE

override fun toString(): String {
return """{"type":"$type","value":"$value"}"""
Expand Down
11 changes: 8 additions & 3 deletions yorkie/src/main/kotlin/dev/yorkie/document/json/JsonTree.kt
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,14 @@ public class JsonTree internal constructor(

val ticket = context.lastTimeTicket
val crdtNodes = if (contents.firstOrNull()?.type == DEFAULT_TEXT_TYPE) {
val compVal = contents
.filterIsInstance<TextNode>()
.joinToString("") { it.value }
val textNodes = contents.filterIsInstance<TextNode>()
val compVal = if (textNodes.size == 1) {
textNodes.single().value
} else {
textNodes.joinTo(StringBuilder(textNodes.sumOf { it.value.length }), "") {
it.value
}.toString()
}
Comment on lines +173 to +180
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default joinToString for large text can result in multiple resizings of the list,
so I changed to joinTo with initial size that matched the text length.

listOf(CrdtTreeText(CrdtTreeNodeID(context.issueTimeTicket(), 0), compVal))
} else {
contents.map { createCrdtTreeNode(context, it) }
Expand Down
Loading