Skip to content

Commit 99000c2

Browse files
committed
Merge branch 'master' of github.com:PolymerLabs/arcs into r2p
Fixed lint errors
2 parents 140111f + 5c6310d commit 99000c2

33 files changed

+964
-246
lines changed

java/arcs/android/crdt/BUILD

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ kt_android_library(
2121
"//java/arcs/core/crdt",
2222
"//java/arcs/core/data:rawentity",
2323
"//java/arcs/core/data/util:data-util",
24+
"//java/arcs/core/storage:reference",
25+
"//java/arcs/core/storage:storage_key",
2426
"//third_party/java/jsr305_annotations",
2527
],
2628
)

java/arcs/android/crdt/ParcelableReferencable.kt

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ package arcs.android.crdt
1313

1414
import android.os.Parcel
1515
import android.os.Parcelable
16+
import arcs.android.crdt.ParcelableReferencable.Type
1617
import arcs.core.common.Referencable
1718
import arcs.core.crdt.CrdtEntity
1819
import arcs.core.data.RawEntity
1920
import arcs.core.data.util.ReferencablePrimitive
20-
import java.lang.IllegalArgumentException
21+
import arcs.core.storage.Reference
2122
import javax.annotation.OverridingMethodsMustInvokeSuper
2223

2324
/**
@@ -34,6 +35,7 @@ interface ParcelableReferencable : Parcelable {
3435
// TODO: Add other ParcelableReferencable subclasses.
3536
RawEntity(ParcelableRawEntity.CREATOR),
3637
CrdtEntityReferenceImpl(ParcelableCrdtEntity.ReferenceImpl),
38+
StorageReferenceImpl(ParcelableReference.CREATOR),
3739
Primitive(ParcelableReferencablePrimitive.CREATOR);
3840

3941
override fun writeToParcel(parcel: Parcel, flags: Int) {
@@ -56,6 +58,7 @@ interface ParcelableReferencable : Parcelable {
5658
// TODO: Add other ParcelableReferencable subclasses.
5759
is ParcelableRawEntity -> Type.RawEntity
5860
is ParcelableCrdtEntity.ReferenceImpl -> Type.CrdtEntityReferenceImpl
61+
is ParcelableReference -> Type.StorageReferenceImpl
5962
is ParcelableReferencablePrimitive -> Type.Primitive
6063
else -> throw IllegalArgumentException(
6164
"Unsupported Referencable type: ${this.javaClass}"
@@ -71,6 +74,7 @@ interface ParcelableReferencable : Parcelable {
7174
operator fun invoke(actual: Referencable): ParcelableReferencable = when (actual) {
7275
// TODO: Add other ParcelableReferencable subclasses.
7376
is RawEntity -> ParcelableRawEntity(actual)
77+
is Reference -> ParcelableReference(actual)
7478
is CrdtEntity.ReferenceImpl -> ParcelableCrdtEntity.ReferenceImpl(actual)
7579
is ReferencablePrimitive<*> -> ParcelableReferencablePrimitive(actual)
7680
else ->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2020 Google LLC.
3+
*
4+
* This code may only be used under the BSD style license found at
5+
* http://polymer.github.io/LICENSE.txt
6+
*
7+
* Code distributed by Google as part of this project is also subject to an additional IP rights
8+
* grant found at
9+
* http://polymer.github.io/PATENTS.txt
10+
*/
11+
12+
package arcs.android.crdt
13+
14+
import android.os.Parcel
15+
import android.os.Parcelable
16+
import arcs.android.util.writeProto
17+
import arcs.core.storage.Reference
18+
import arcs.core.storage.StorageKeyParser
19+
20+
/** Parcelable version of [Reference]. */
21+
data class ParcelableReference(override val actual: Reference) : ParcelableReferencable {
22+
override fun writeToParcel(parcel: Parcel, flags: Int) {
23+
super.writeToParcel(parcel, flags)
24+
parcel.writeString(actual.id)
25+
parcel.writeString(actual.storageKey.toString())
26+
actual.version?.let {
27+
parcel.writeProto(it.toProto())
28+
} ?: {
29+
parcel.writeTypedObject(null, flags)
30+
}()
31+
}
32+
33+
override fun describeContents(): Int = 0
34+
35+
/* Don't use this directly, instead use ParcelableReferencable. */
36+
internal companion object CREATOR : Parcelable.Creator<ParcelableReference> {
37+
override fun createFromParcel(parcel: Parcel): ParcelableReference {
38+
val id = requireNotNull(parcel.readString()) {
39+
"Required id not found in parcel for ParcelableReference"
40+
}
41+
val storageKeyString = requireNotNull(parcel.readString()) {
42+
"Required storageKey not found in parcel for ParcelableReference"
43+
}
44+
val versionMap = parcel.readVersionMap()?.takeIf { it.isNotEmpty() }
45+
46+
return ParcelableReference(
47+
Reference(id, StorageKeyParser.parse(storageKeyString), versionMap)
48+
)
49+
}
50+
51+
override fun newArray(size: Int): Array<ParcelableReference?> = arrayOfNulls(size)
52+
}
53+
}
54+
55+
/** Writes the [Reference] to the receiving [Parcel]. */
56+
fun Parcel.writeReference(reference: Reference, flags: Int) =
57+
writeTypedObject(ParcelableReference(reference), flags)

java/arcs/core/crdt/extension/ConversionExtensions.kt

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ import arcs.core.data.util.ReferencablePrimitive
1919
import arcs.core.util.Base64
2020

2121
/** Converts the [RawEntity] into a [CrdtEntity.Data] model, at the given version. */
22-
fun RawEntity.toCrdtEntityData(versionMap: VersionMap): CrdtEntity.Data =
23-
CrdtEntity.Data(versionMap.copy(), this) { CrdtEntity.ReferenceImpl(it.id) }
22+
fun RawEntity.toCrdtEntityData(
23+
versionMap: VersionMap,
24+
referenceBuilder: (Referencable) -> CrdtEntity.Reference = { CrdtEntity.ReferenceImpl(it.id) }
25+
): CrdtEntity.Data = CrdtEntity.Data(versionMap.copy(), this, referenceBuilder)
2426

2527
private fun Any?.toReferencable(): Referencable {
2628
requireNotNull(this) { "Cannot create a referencable from a null value." }

java/arcs/core/data/Reference.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import kotlinx.coroutines.Dispatchers
2727
*
2828
* Developers can check the liveness of a [Reference] using either [isAlive] or [isDead].
2929
*/
30-
interface Reference<T : Referencable> {
30+
interface Reference<T : Referencable> : arcs.core.crdt.CrdtEntity.Reference {
3131
/**
3232
* Fetches the actual [Entity] value being referenced from storage.
3333
*

java/arcs/core/data/proto/BUILD

+36
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
load(
2+
"//third_party/java/arcs/build_defs:build_defs.bzl",
3+
"arcs_kt_library",
4+
)
15
load(
26
"//third_party/java/arcs/build_defs:native.oss.bzl",
7+
"android_proto_library",
38
"java_proto_library",
49
"proto_library",
510
)
@@ -8,11 +13,42 @@ licenses(["notice"])
813

914
package(default_visibility = ["//visibility:public"])
1015

16+
arcs_kt_library(
17+
name = "proto",
18+
srcs = glob(
19+
["*.kt"],
20+
),
21+
deps = [
22+
":recipe_java_proto_lite",
23+
"//java/arcs/core/data",
24+
],
25+
)
26+
27+
# This target pulls in the java protobuf library, as opposed to the *java_lite*
28+
# protobuf library. The regular protobuf library is needed for tests.
29+
arcs_kt_library(
30+
name = "proto_for_test",
31+
testonly = 1,
32+
srcs = glob(
33+
["*.kt"],
34+
),
35+
add_android_constraints = False,
36+
deps = [
37+
":recipe_java_proto",
38+
"//java/arcs/core/data",
39+
],
40+
)
41+
1142
proto_library(
1243
name = "recipe_proto",
1344
srcs = ["recipe.proto"],
1445
)
1546

47+
android_proto_library(
48+
name = "recipe_java_proto_lite",
49+
deps = [":recipe_proto"],
50+
)
51+
1652
java_proto_library(
1753
name = "recipe_java_proto",
1854
deps = [":recipe_proto"],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2020 Google LLC.
3+
*
4+
* This code may only be used under the BSD style license found at
5+
* http://polymer.github.io/LICENSE.txt
6+
*
7+
* Code distributed by Google as part of this project is also subject to an additional IP rights
8+
* grant found at
9+
* http://polymer.github.io/PATENTS.txt
10+
*/
11+
12+
package arcs.core.data.proto
13+
14+
import arcs.core.data.PrimitiveType
15+
16+
/**
17+
* Converts a [PrimitiveTypeProto] protobuf instance into a native kotlin [PrimitiveType] instance.
18+
*/
19+
fun PrimitiveTypeProto.decode(): PrimitiveType =
20+
when (this) {
21+
PrimitiveTypeProto.TEXT -> PrimitiveType.Text
22+
PrimitiveTypeProto.NUMBER -> PrimitiveType.Number
23+
PrimitiveTypeProto.BOOLEAN -> PrimitiveType.Boolean
24+
PrimitiveTypeProto.UNRECOGNIZED ->
25+
throw IllegalArgumentException("Unknown PrimitiveTypeProto value.")
26+
}

java/arcs/core/storage/DirectStore.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class DirectStore<Data : CrdtData, Op : CrdtOperation, T> /* internal */ constru
9292
return when (message) {
9393
is ProxyMessage.SyncRequest -> {
9494
callbacks.value[message.id]?.invoke(
95-
ProxyMessage.ModelUpdate(localModel.data, message.id)
95+
ProxyMessage.ModelUpdate(getLocalData(), message.id)
9696
)
9797
true
9898
}

java/arcs/core/storage/ReferenceModeStore.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,10 @@ class ReferenceModeStore private constructor(
566566
entity,
567567
VersionMap(crdtKey to maxVersion),
568568
fieldVersionProvider
569-
) { CrdtEntity.ReferenceImpl(it.id) }
569+
) {
570+
if (it is Reference) it
571+
else CrdtEntity.Reference.buildReference(it)
572+
}
570573
}
571574

572575
companion object {

java/arcs/core/storage/driver/Database.kt

+9-2
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,11 @@ class DatabaseDriver<Data : Any>(
271271
)?.also {
272272
dataAndVersion = when (it) {
273273
is DatabaseData.Entity ->
274-
it.rawEntity.toCrdtEntityData(it.versionMap)
274+
it.rawEntity.toCrdtEntityData(it.versionMap) { refable ->
275+
// Use the storage reference if it is one.
276+
if (refable is Reference) refable
277+
else CrdtEntity.Reference.buildReference(refable)
278+
}
275279
is DatabaseData.Singleton ->
276280
it.reference.toCrdtSingletonData(it.versionMap)
277281
is DatabaseData.Collection ->
@@ -361,7 +365,10 @@ class DatabaseDriver<Data : Any>(
361365
val actualData = when (data) {
362366
is DatabaseData.Singleton -> data.reference.toCrdtSingletonData(data.versionMap)
363367
is DatabaseData.Collection -> data.values.toCrdtSetData(data.versionMap)
364-
is DatabaseData.Entity -> data.rawEntity.toCrdtEntityData(data.versionMap)
368+
is DatabaseData.Entity -> data.rawEntity.toCrdtEntityData(data.versionMap) {
369+
if (it is Reference) it
370+
else CrdtEntity.Reference.buildReference(it)
371+
}
365372
} as Data
366373

367374
// Stash it locally.

java/arcs/core/storage/handle/RawEntityDereferencer.kt

+17-13
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class RawEntityDereferencer(
4343
override suspend fun dereference(
4444
reference: Reference,
4545
coroutineContext: CoroutineContext
46-
): RawEntity? = withContext(coroutineContext) {
46+
): RawEntity? {
4747
log.debug { "De-referencing $reference" }
4848

4949
val storageKey = reference.storageKey.childKeyWithComponent(reference.id)
@@ -70,20 +70,24 @@ class RawEntityDereferencer(
7070
true
7171
}
7272
)
73-
launch { store.onProxyMessage(ProxyMessage.SyncRequest(token)) }
7473

75-
// Only return the item if we've actually managed to pull it out of the database.
76-
deferred.await().takeIf { it matches schema }
74+
return withContext(coroutineContext) {
75+
launch { store.onProxyMessage(ProxyMessage.SyncRequest(token)) }
76+
77+
// Only return the item if we've actually managed to pull it out of the database.
78+
deferred.await().takeIf { it matches schema }?.copy(id = reference.id)
79+
}
7780
}
81+
}
7882

79-
private infix fun RawEntity.matches(schema: Schema): Boolean {
80-
// Only allow empty to match if the Schema is also empty.
81-
// TODO: Is this a correct assumption?
82-
if (singletons.isEmpty() && collections.isEmpty())
83-
return schema.fields.singletons.isEmpty() && schema.fields.collections.isEmpty()
83+
/* internal */
84+
infix fun RawEntity.matches(schema: Schema): Boolean {
85+
// Only allow empty to match if the Schema is also empty.
86+
// TODO: Is this a correct assumption?
87+
if (singletons.isEmpty() && collections.isEmpty())
88+
return schema.fields.singletons.isEmpty() && schema.fields.collections.isEmpty()
8489

85-
// Return true if any of the RawEntity's fields are part of the Schema.
86-
return (singletons.isEmpty() || singletons.keys.any { it in schema.fields.singletons }) &&
87-
(collections.isEmpty() || collections.keys.any { it in schema.fields.collections })
88-
}
90+
// Return true if any of the RawEntity's fields are part of the Schema.
91+
return (singletons.isEmpty() || singletons.keys.any { it in schema.fields.singletons }) &&
92+
(collections.isEmpty() || collections.keys.any { it in schema.fields.collections })
8993
}

java/arcs/sdk/android/storage/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ kt_android_library(
2121
"//third_party/java/androidx/annotation",
2222
"//third_party/java/androidx/lifecycle",
2323
"//third_party/kotlin/kotlinx_coroutines",
24+
"//third_party/kotlin/kotlinx_coroutines:kotlinx_coroutines_android",
2425
],
2526
)

java/arcs/sdk/android/storage/ServiceStore.kt

+3-7
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,7 @@ class ServiceStore<Data : CrdtData, Op : CrdtOperation, ConsumerData>(
103103
@Suppress("UNCHECKED_CAST")
104104
override suspend fun getLocalData(): Data {
105105
val service = checkNotNull(storageService)
106-
val channel = ParcelableProxyMessageChannel(
107-
coroutineContext
108-
)
106+
val channel = ParcelableProxyMessageChannel(coroutineContext)
109107
service.getLocalData(channel)
110108
val flow = channel.asFlow()
111109
val modelUpdate =
@@ -140,12 +138,10 @@ class ServiceStore<Data : CrdtData, Op : CrdtOperation, ConsumerData>(
140138
"Connection to StorageService is already alive."
141139
}
142140
val connection = connectionFactory(options, crdtType)
141+
// Need to initiate the connection on the main thread.
143142
val service = connection.connectAsync().await()
144143

145-
val messageChannel =
146-
ParcelableProxyMessageChannel(
147-
coroutineContext
148-
)
144+
val messageChannel = ParcelableProxyMessageChannel(coroutineContext)
149145
serviceCallbackToken = withContext(coroutineContext) {
150146
service.registerCallback(messageChannel)
151147
}

javatests/arcs/android/host/BUILD

+11-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ licenses(["notice"])
99

1010
package(default_visibility = ["//visibility:public"])
1111

12+
kt_android_library(
13+
name = "test_app",
14+
testonly = 1,
15+
srcs = ["TestActivity.kt"],
16+
manifest = ":AndroidManifest.xml",
17+
deps = [
18+
"//third_party/java/androidx/appcompat",
19+
],
20+
)
21+
1222
arcs_kt_android_test_suite(
1323
name = "host",
1424
srcs = glob(["*Test.kt"]),
@@ -17,6 +27,7 @@ arcs_kt_android_test_suite(
1727
deps = [
1828
":schemas",
1929
":services",
30+
":test_app",
2031
"//java/arcs/android/crdt",
2132
"//java/arcs/android/host",
2233
"//java/arcs/android/sdk/host",
@@ -42,7 +53,6 @@ arcs_kt_android_test_suite(
4253
"//java/arcs/sdk/android/storage",
4354
"//java/arcs/sdk/android/storage/service",
4455
"//java/arcs/sdk/android/storage/service/testutil",
45-
"//javatests/arcs/android/storage/handle:test_app",
4656
"//javatests/arcs/core/allocator:allocator-test-util",
4757
"//third_party/android/androidx_test/core",
4858
"//third_party/android/androidx_test/ext/junit",

0 commit comments

Comments
 (0)