diff --git a/java/arcs/core/crdt/CrdtEntity.kt b/java/arcs/core/crdt/CrdtEntity.kt index 077637480bc..225a137e530 100644 --- a/java/arcs/core/crdt/CrdtEntity.kt +++ b/java/arcs/core/crdt/CrdtEntity.kt @@ -427,3 +427,9 @@ class CrdtEntity( ) : Operation(actor, versionMap) } } + +/** Converts the [RawEntity] into a [CrdtEntity.Data] model, at the given version. */ +fun RawEntity.toCrdtEntityData( + versionMap: VersionMap, + referenceBuilder: (Referencable) -> CrdtEntity.Reference = { CrdtEntity.ReferenceImpl(it.id) } +): CrdtEntity.Data = CrdtEntity.Data(versionMap.copy(), this, referenceBuilder) diff --git a/java/arcs/core/crdt/extension/BUILD b/java/arcs/core/crdt/extension/BUILD deleted file mode 100644 index 6283ec5da11..00000000000 --- a/java/arcs/core/crdt/extension/BUILD +++ /dev/null @@ -1,21 +0,0 @@ -load( - "//third_party/java/arcs/build_defs:build_defs.bzl", - "arcs_kt_library", -) - -licenses(["notice"]) - -package(default_visibility = ["//java/arcs:allowed-packages"]) - -arcs_kt_library( - name = "extension", - srcs = glob(["*.kt"]), - deps = [ - "//java/arcs/core/common", - "//java/arcs/core/crdt", - "//java/arcs/core/data", - "//java/arcs/core/data:rawentity", - "//java/arcs/core/data/util:data-util", - "//java/arcs/core/util", - ], -) diff --git a/java/arcs/core/crdt/extension/ConversionExtensions.kt b/java/arcs/core/crdt/extension/ConversionExtensions.kt deleted file mode 100644 index 34d4cf59f17..00000000000 --- a/java/arcs/core/crdt/extension/ConversionExtensions.kt +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2020 Google LLC. - * - * This code may only be used under the BSD style license found at - * http://polymer.github.io/LICENSE.txt - * - * Code distributed by Google as part of this project is also subject to an additional IP rights - * grant found at - * http://polymer.github.io/PATENTS.txt - */ - -package arcs.core.crdt.extension - -import arcs.core.common.Referencable -import arcs.core.crdt.CrdtEntity -import arcs.core.crdt.VersionMap -import arcs.core.data.RawEntity -import arcs.core.data.util.ReferencablePrimitive -import arcs.core.util.Base64 - -/** Converts the [RawEntity] into a [CrdtEntity.Data] model, at the given version. */ -fun RawEntity.toCrdtEntityData( - versionMap: VersionMap, - referenceBuilder: (Referencable) -> CrdtEntity.Reference = { CrdtEntity.ReferenceImpl(it.id) } -): CrdtEntity.Data = CrdtEntity.Data(versionMap.copy(), this, referenceBuilder) - -private fun Any?.toReferencable(): Referencable { - requireNotNull(this) { "Cannot create a referencable from a null value." } - return when { - ReferencablePrimitive.isSupportedPrimitive(this::class) -> { - if (this is ByteArray) { - ReferencablePrimitive( - ByteArray::class, - this, - valueRepr = Base64.encode(this) - ) - } else { - ReferencablePrimitive(this::class, this) - } - } - this is Referencable -> this - else -> throw IllegalArgumentException( - "Entity contains non-referencable non-primitive values." - ) - } -} diff --git a/java/arcs/core/storage/driver/BUILD b/java/arcs/core/storage/driver/BUILD index c7ab64bf522..59b788ac978 100644 --- a/java/arcs/core/storage/driver/BUILD +++ b/java/arcs/core/storage/driver/BUILD @@ -17,7 +17,6 @@ arcs_kt_library( ":ramdisk", "//java/arcs/core/common", "//java/arcs/core/crdt", - "//java/arcs/core/crdt/extension", "//java/arcs/core/data", "//java/arcs/core/data:rawentity", "//java/arcs/core/data/util:data-util", @@ -28,11 +27,9 @@ arcs_kt_library( "//java/arcs/core/storage/driver/volatiles", "//java/arcs/core/storage/driver/volatiles:volatile_impl", "//java/arcs/core/storage/keys", - "//java/arcs/core/storage/referencemode", "//java/arcs/core/type", "//java/arcs/core/util", "//third_party/java/androidx/annotation", - "//third_party/kotlin/kotlinx_atomicfu", "//third_party/kotlin/kotlinx_coroutines", ], ) @@ -56,7 +53,6 @@ arcs_kt_library( deps = [ "//java/arcs/core/common", "//java/arcs/core/crdt", - "//java/arcs/core/crdt/extension", "//java/arcs/core/data", "//java/arcs/core/data:rawentity", "//java/arcs/core/data/util:data-util", @@ -67,11 +63,9 @@ arcs_kt_library( "//java/arcs/core/storage/driver/volatiles", "//java/arcs/core/storage/driver/volatiles:volatile_impl", "//java/arcs/core/storage/keys", - "//java/arcs/core/storage/referencemode", "//java/arcs/core/type", "//java/arcs/core/util", "//third_party/java/androidx/annotation", - "//third_party/kotlin/kotlinx_atomicfu", "//third_party/kotlin/kotlinx_coroutines", ], ) diff --git a/java/arcs/core/storage/driver/Database.kt b/java/arcs/core/storage/driver/Database.kt index 5cd1c9d9db5..10c80f76754 100644 --- a/java/arcs/core/storage/driver/Database.kt +++ b/java/arcs/core/storage/driver/Database.kt @@ -17,7 +17,7 @@ import arcs.core.crdt.CrdtEntity import arcs.core.crdt.CrdtSet import arcs.core.crdt.CrdtSingleton import arcs.core.crdt.VersionMap -import arcs.core.crdt.extension.toCrdtEntityData +import arcs.core.crdt.toCrdtEntityData import arcs.core.data.RawEntity import arcs.core.data.Schema import arcs.core.data.util.ReferencableList diff --git a/javatests/arcs/core/crdt/CrdtEntityTest.kt b/javatests/arcs/core/crdt/CrdtEntityTest.kt index d5f28d870c6..33db8495024 100644 --- a/javatests/arcs/core/crdt/CrdtEntityTest.kt +++ b/javatests/arcs/core/crdt/CrdtEntityTest.kt @@ -18,6 +18,7 @@ import arcs.core.crdt.CrdtEntity.Operation.RemoveFromSet import arcs.core.crdt.CrdtEntity.Operation.SetSingleton import arcs.core.crdt.CrdtEntity.ReferenceImpl as Reference import arcs.core.data.RawEntity +import arcs.core.data.util.toReferencable import com.google.common.truth.Truth.assertThat import kotlin.test.assertFailsWith import org.junit.Ignore @@ -470,8 +471,46 @@ class CrdtEntityTest { val emptyEntity = CrdtEntity(VersionMap(), emptyRawEntity) val changes = emptyEntity.merge(entity.data) - println(changes) assertThat(changes.modelChange.isEmpty()).isFalse() assertThat(changes.otherChange.isEmpty()).isTrue() } + + @Test + fun toCrdtEntityData_empty() { + assertThat(RawEntity().toCrdtEntityData(VersionMap())).isEqualTo(CrdtEntity.Data()) + } + + @Test + fun toCrdtEntityData_populated() { + val entity = RawEntity( + id = "entityId", + singletons = mapOf( + "txt" to "foo".toReferencable(), + "ent" to RawEntity("id1", mapOf("val" to 3.toReferencable())) + ), + collections = mapOf( + "num" to setOf(3.toReferencable(), 7.toReferencable()) + ), + creationTimestamp = 30, + expirationTimestamp = 70 + ) + val versionMap = VersionMap("me" to 1) + val result = entity.toCrdtEntityData(versionMap) { CrdtEntity.ReferenceImpl("#" + it.id) } + + // Constructing the full expected CrdtEntity.Data is cumbersome; just check the fields manually. + assertThat(result.id).isEqualTo("entityId") + assertThat(result.versionMap).isEqualTo(versionMap) + assertThat(result.creationTimestamp).isEqualTo(30) + assertThat(result.expirationTimestamp).isEqualTo(70) + + val singletons = result.singletons + assertThat(singletons).hasSize(2) + assertThat(singletons["txt"]!!.consumerView!!.id).isEqualTo("#Primitive(foo)") + assertThat(singletons["ent"]!!.consumerView!!.id).isEqualTo("#id1") + + val collections = result.collections + assertThat(collections).hasSize(1) + assertThat(collections["num"]!!.consumerView.map { it.id }) + .containsExactly("#Primitive(3)", "#Primitive(7)") + } } diff --git a/javatests/arcs/core/storage/driver/BUILD b/javatests/arcs/core/storage/driver/BUILD index 78c527b65a3..9bbcc42a7ec 100644 --- a/javatests/arcs/core/storage/driver/BUILD +++ b/javatests/arcs/core/storage/driver/BUILD @@ -13,7 +13,6 @@ arcs_kt_jvm_test_suite( deps = [ "//java/arcs/core/common", "//java/arcs/core/crdt", - "//java/arcs/core/crdt/extension", "//java/arcs/core/data", "//java/arcs/core/data:schema_fields", "//java/arcs/core/data/util:data-util", @@ -26,7 +25,6 @@ arcs_kt_jvm_test_suite( "//java/arcs/core/storage/keys", "//java/arcs/core/testutil", "//java/arcs/core/type", - "//java/arcs/core/util", "//java/arcs/core/util/testutil", "//java/arcs/jvm/storage/database/testutil", "//third_party/java/junit:junit-android", diff --git a/javatests/arcs/core/storage/driver/DatabaseDriverTest.kt b/javatests/arcs/core/storage/driver/DatabaseDriverTest.kt index 51b1b8dde8a..1a86bab88c6 100644 --- a/javatests/arcs/core/storage/driver/DatabaseDriverTest.kt +++ b/javatests/arcs/core/storage/driver/DatabaseDriverTest.kt @@ -16,7 +16,7 @@ import arcs.core.crdt.CrdtEntity.Reference.Companion.buildReference import arcs.core.crdt.CrdtSet import arcs.core.crdt.CrdtSingleton import arcs.core.crdt.VersionMap -import arcs.core.crdt.extension.toCrdtEntityData +import arcs.core.crdt.toCrdtEntityData import arcs.core.data.FieldType import arcs.core.data.RawEntity import arcs.core.data.Schema