Skip to content

Commit cfcf317

Browse files
committed
Merge branch 'master' of github.com:PolymerLabs/arcs into kt-gen-utils
2 parents 60582ed + 623daf7 commit cfcf317

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1193
-417
lines changed

java/arcs/android/storage/handle/AndroidHandleManager.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package arcs.android.storage.handle
33
import android.content.Context
44
import androidx.lifecycle.Lifecycle
55
import arcs.android.crdt.ParcelableCrdtType
6+
import arcs.core.common.Referencable
67
import arcs.core.crdt.CrdtEntity
78
import arcs.core.data.RawEntity
89
import arcs.core.storage.EntityActivationFactory
@@ -57,7 +58,7 @@ fun AndroidHandleManager(
5758
* Create an [ActivationFactory] that will create [ServiceStore] instances that can manage
5859
* singleton [RawEntities]
5960
*/
60-
override fun singletonFactory() = SingletonServiceStoreFactory<RawEntity>(
61+
override fun <T : Referencable> singletonFactory() = SingletonServiceStoreFactory<T>(
6162
context,
6263
lifecycle,
6364
ParcelableCrdtType.Singleton,
@@ -69,7 +70,7 @@ fun AndroidHandleManager(
6970
* Create a ActivationFactory that will create [ServiceStore] instances that can manage
7071
* sets of [RawEntities]
7172
*/
72-
override fun setFactory() = SetServiceStoreFactory<RawEntity>(
73+
override fun <T : Referencable> setFactory() = SetServiceStoreFactory<T>(
7374
context,
7475
lifecycle,
7576
ParcelableCrdtType.Set,

java/arcs/android/storage/handle/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ kt_android_library(
99
srcs = glob(["*.kt"]),
1010
deps = [
1111
"//java/arcs/android/crdt",
12+
"//java/arcs/core/common",
1213
"//java/arcs/core/crdt",
1314
"//java/arcs/core/data:rawentity",
1415
"//java/arcs/core/storage/handle",
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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
13+
14+
import arcs.core.type.Type
15+
16+
data class HandleConnectionSpec(
17+
val name: String,
18+
val direction: Direction,
19+
val type: Type
20+
) {
21+
enum class Direction {
22+
READS, WRITES, READS_WRITES
23+
}
24+
}

java/arcs/core/data/ParticleSpec.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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
13+
14+
data class ParticleSpec(
15+
val name: String,
16+
val connections: List<HandleConnectionSpec>,
17+
val location: String
18+
)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.HandleConnectionSpec
15+
import arcs.core.data.HandleConnectionSpec.Direction
16+
import arcs.core.data.ParticleSpec
17+
18+
typealias DirectionProto = HandleConnectionSpecProto.Direction
19+
20+
/** Converts [HandleConnectionSpecProto.Direction] to [HandleConnectionSpec.Direction]. */
21+
fun DirectionProto.decode() =
22+
when (this) {
23+
DirectionProto.READS -> Direction.READS
24+
DirectionProto.WRITES -> Direction.WRITES
25+
DirectionProto.READS_WRITES -> Direction.READS_WRITES
26+
DirectionProto.UNRECOGNIZED ->
27+
throw IllegalArgumentException("Invalid direction when decoding [HandleConnectionSpec]")
28+
}
29+
30+
/** Converts a [HandleConnnectionSpecProto] to the corresponding [HandleConnectionSpec] instance. */
31+
fun HandleConnectionSpecProto.decode() = HandleConnectionSpec(
32+
name = getName(),
33+
direction = getDirection().decode(),
34+
type = getType().decode()
35+
)
36+
37+
/** Converts a [ParticleSpecProto] to the corresponding [ParticleSpec] instance. */
38+
fun ParticleSpecProto.decode() = ParticleSpec(
39+
name = getName(),
40+
connections = getConnectionsList().map { it.decode() },
41+
location = getLocation()
42+
)

java/arcs/core/data/proto/SchemaProtoDecoder.kt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,10 @@ import arcs.core.data.Schema
1717
import arcs.core.data.SchemaFields
1818
import arcs.core.data.SchemaName
1919

20-
/**
21-
* Returns the names in the [SchemaProto] as List<SchemaName>.
22-
*/
20+
/** Returns the names in the [SchemaProto] as List<SchemaName>. */
2321
fun SchemaProto.decodeNames(): List<SchemaName> = getNamesList().map { SchemaName(it) }
2422

25-
/**
26-
* Returns the fields in the [SchemaProto] as a Kotlin [SchemaFields] instance.
27-
*/
23+
/** Returns the fields in the [SchemaProto] as a Kotlin [SchemaFields] instance. */
2824
fun SchemaProto.decodeFields(): SchemaFields {
2925
val singletons = mutableMapOf<FieldName, FieldType>()
3026
val collections = mutableMapOf<FieldName, FieldType>()
@@ -43,8 +39,6 @@ fun SchemaProto.decodeFields(): SchemaFields {
4339
return SchemaFields(singletons, collections)
4440
}
4541

46-
/**
47-
* Converts a [SchemaProto] proto instance into a Kotlin [Schema] instance.
48-
*/
42+
/** Converts a [SchemaProto] proto instance into a Kotlin [Schema] instance. */
4943
fun SchemaProto.decode() = Schema(names = decodeNames(), fields = decodeFields(), hash = "")
5044
// TODO: hash

java/arcs/core/data/proto/TypeProtoDecoders.kt

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ import arcs.core.data.FieldType
1616
import arcs.core.data.PrimitiveType
1717
import arcs.core.type.Type
1818

19-
/**
20-
* Converts a [PrimitiveTypeProto] protobuf instance into a Kotlin [PrimitiveType] instance.
21-
*/
19+
/** Converts a [PrimitiveTypeProto] protobuf instance into a Kotlin [PrimitiveType] instance. */
2220
fun PrimitiveTypeProto.decode(): PrimitiveType =
2321
when (this) {
2422
PrimitiveTypeProto.TEXT -> PrimitiveType.Text
@@ -28,9 +26,7 @@ fun PrimitiveTypeProto.decode(): PrimitiveType =
2826
throw IllegalArgumentException("Unknown PrimitiveTypeProto value.")
2927
}
3028

31-
/**
32-
* Converts a [PrimitiveTypeProto] protobuf instance into a Kotlin [FieldType] instance.
33-
*/
29+
/** Converts a [PrimitiveTypeProto] protobuf instance into a Kotlin [FieldType] instance. */
3430
fun PrimitiveTypeProto.decodeAsFieldType(): FieldType.Primitive = FieldType.Primitive(decode())
3531

3632
/**
@@ -50,14 +46,10 @@ fun TypeProto.decodeAsFieldType(): FieldType =
5046
"Cannot decode a ${getDataCase().name} type to a [FieldType].")
5147
}
5248

53-
/**
54-
* Converts a [EntityTypeProto] protobuf instance into a Kotlin [EntityType] instance.
55-
*/
49+
/** Converts a [EntityTypeProto] protobuf instance into a Kotlin [EntityType] instance. */
5650
fun EntityTypeProto.decode() = EntityType(getSchema().decode())
5751

58-
/**
59-
* Converts a [TypeProto] protobuf instance into a Kotlin [Type] instance.
60-
*/
52+
/** Converts a [TypeProto] protobuf instance into a Kotlin [Type] instance. */
6153
fun TypeProto.decode(): Type =
6254
// TODO: optional, RefinementExpression.
6355
when (getDataCase()) {
@@ -70,5 +62,5 @@ fun TypeProto.decode(): Type =
7062
throw IllegalArgumentException("Unknown data field in TypeProto.")
7163
else ->
7264
throw IllegalArgumentException(
73-
"Cannot decode a ${getDataCase().name} type to a [FieldType].")
65+
"Cannot decode a ${getDataCase().name} type to a [Type].")
7466
}

java/arcs/core/host/EntityHandleManager.kt

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import arcs.core.storage.StorageKey
1111
import arcs.core.storage.api.Entity
1212
import arcs.core.storage.api.EntitySpec
1313
import arcs.core.storage.api.Handle
14-
import arcs.core.storage.api.ReadWriteCollection
15-
import arcs.core.storage.api.ReadWriteSingleton
16-
import arcs.core.storage.api.ReadableCollection
17-
import arcs.core.storage.api.ReadableSingleton
18-
import arcs.core.storage.api.WritableCollection
19-
import arcs.core.storage.api.WritableSingleton
14+
import arcs.core.storage.api.ReadCollectionHandle
15+
import arcs.core.storage.api.ReadSingletonHandle
16+
import arcs.core.storage.api.ReadWriteCollectionHandle
17+
import arcs.core.storage.api.ReadWriteSingletonHandle
18+
import arcs.core.storage.api.WriteCollectionHandle
19+
import arcs.core.storage.api.WriteSingletonHandle
2020
import arcs.core.storage.handle.CollectionImpl
2121
import arcs.core.storage.handle.HandleManager
2222
import arcs.core.storage.handle.SetData
@@ -37,14 +37,16 @@ typealias CollectionSenderCallbackAdapter<E> =
3737

3838
/**
3939
* Wraps a [HandleManager] and creates [Entity] handles based on [HandleMode], such as
40-
* [ReadableSingleton] for [HandleMode.Read]. To obtain a [HandleHolder], use
40+
* [ReadSingletonHandle] for [HandleMode.Read]. To obtain a [HandleHolder], use
4141
* `arcs_kt_schema` on a manifest file to generate a `{ParticleName}Handles' class, and
4242
* invoke its default constructor, or obtain it from the [BaseParticle.handles] field.
43+
*
44+
* TODO(cromwellian): Add support for creating Singleton/Set handles of [Reference]s.
4345
*/
4446
class EntityHandleManager(val handleManager: HandleManager) {
4547
/**
46-
* Creates and returns a new [SingletonHandle]. Will also populate the appropriate field inside
47-
* the given [HandleHolder].
48+
* Creates and returns a new [SingletonHandle] for managing an [Entity]. Will also populate the
49+
* appropriate field inside the given [HandleHolder].
4850
*
4951
* @property handleHolder contains handle and entitySpec declarations
5052
* @property handleName name for the handle, must be present in [HandleHolder.entitySpecs]
@@ -65,15 +67,20 @@ class EntityHandleManager(val handleManager: HandleManager) {
6567
) = createSdkHandle(
6668
handleHolder,
6769
handleName,
68-
handleManager.singletonHandle(storageKey, schema, canRead = handleMode != HandleMode.Write),
70+
handleManager.rawEntitySingletonHandle(
71+
storageKey,
72+
schema,
73+
name = handleName,
74+
canRead = handleMode != HandleMode.Write
75+
),
6976
handleMode,
7077
idGenerator,
7178
sender
7279
)
7380

7481
/**
75-
* Creates and returns a new [SetHandle]. Will also populate the appropriate field inside
76-
* the given [HandleHolder].
82+
* Creates and returns a new [SetHandle] for a set of [Entity]s. Will also populate the
83+
* appropriate field inside the given [HandleHolder].
7784
*
7885
* @property handleHolder contains handle and entitySpec declarations
7986
* @property handleName name for the handle, must be present in [HandleHolder.entitySpecs]
@@ -94,7 +101,12 @@ class EntityHandleManager(val handleManager: HandleManager) {
94101
) = createSdkHandle(
95102
handleHolder,
96103
handleName,
97-
handleManager.setHandle(storageKey, schema, canRead = handleMode != HandleMode.Write),
104+
handleManager.rawEntitySetHandle(
105+
storageKey,
106+
schema,
107+
name = handleName,
108+
canRead = handleMode != HandleMode.Write
109+
),
98110
handleMode,
99111
idGenerator,
100112
sender
@@ -216,12 +228,12 @@ internal open class HandleEventBase<T, H : Handle> {
216228
}
217229
}
218230

219-
internal open class ReadableSingletonHandleImpl<T : Entity>(
231+
internal open class ReadSingletonHandleImpl<T : Entity>(
220232
val entitySpec: EntitySpec<T>,
221233
val handleName: String,
222234
val storageHandle: SingletonHandle<RawEntity>,
223235
val sender: Sender
224-
) : HandleEventBase<T?, ReadableSingleton<T>>(), ReadableSingleton<T> {
236+
) : HandleEventBase<T?, ReadSingletonHandle<T>>(), ReadSingletonHandle<T> {
225237
init {
226238
storageHandle.callback = SingletonSenderCallbackAdapter(
227239
this::fetch,
@@ -242,11 +254,11 @@ internal open class ReadableSingletonHandleImpl<T : Entity>(
242254
}
243255
}
244256

245-
internal class WritableSingletonHandleImpl<T : Entity>(
257+
internal class WriteSingletonHandleImpl<T : Entity>(
246258
val handleName: String,
247259
val storageHandle: SingletonHandle<RawEntity>,
248260
val idGenerator: Id.Generator
249-
) : WritableSingleton<T> {
261+
) : WriteSingletonHandle<T> {
250262
override val name: String
251263
get() = handleName
252264

@@ -265,24 +277,24 @@ internal class ReadWriteSingletonHandleImpl<T : Entity>(
265277
storageHandle: SingletonHandle<RawEntity>,
266278
idGenerator: Id.Generator,
267279
sender: Sender,
268-
private val writableSingleton: WritableSingletonHandleImpl<T> = WritableSingletonHandleImpl(
280+
private val writableSingleton: WriteSingletonHandleImpl<T> = WriteSingletonHandleImpl(
269281
handleName,
270282
storageHandle,
271283
idGenerator
272284
)
273-
) : ReadWriteSingleton<T>,
274-
ReadableSingletonHandleImpl<T>(entitySpec, handleName, storageHandle, sender),
275-
WritableSingleton<T> by writableSingleton {
285+
) : ReadWriteSingletonHandle<T>,
286+
ReadSingletonHandleImpl<T>(entitySpec, handleName, storageHandle, sender),
287+
WriteSingletonHandle<T> by writableSingleton {
276288
override val name: String
277289
get() = writableSingleton.name
278290
}
279291

280-
internal open class ReadableCollectionHandleImpl<T : Entity>(
292+
internal open class ReadCollectionHandleImpl<T : Entity>(
281293
val entitySpec: EntitySpec<T>,
282294
val handleName: String,
283295
val storageHandle: SetHandle<RawEntity>,
284296
val sender: Sender
285-
) : HandleEventBase<Set<T>, ReadableCollection<T>>(), ReadableCollection<T> {
297+
) : HandleEventBase<Set<T>, ReadCollectionHandle<T>>(), ReadCollectionHandle<T> {
286298
init {
287299
storageHandle.callback = CollectionSenderCallbackAdapter(
288300
this::fetchAll,
@@ -307,11 +319,11 @@ internal open class ReadableCollectionHandleImpl<T : Entity>(
307319
}.toSet()
308320
}
309321

310-
internal class WritableCollectionHandleImpl<T : Entity>(
322+
internal class WriteCollectionHandleImpl<T : Entity>(
311323
val handleName: String,
312324
val storageHandle: CollectionImpl<RawEntity>,
313325
val idGenerator: Id.Generator
314-
) : WritableCollection<T> {
326+
) : WriteCollectionHandle<T> {
315327
override val name: String
316328
get() = handleName
317329

@@ -336,14 +348,14 @@ internal class ReadWriteCollectionHandleImpl<T : Entity>(
336348
storageHandle: CollectionImpl<RawEntity>,
337349
idGenerator: Id.Generator,
338350
sender: Sender,
339-
private val writableCollection: WritableCollectionHandleImpl<T> = WritableCollectionHandleImpl(
351+
private val writableCollection: WriteCollectionHandleImpl<T> = WriteCollectionHandleImpl(
340352
handleName,
341353
storageHandle,
342354
idGenerator
343355
)
344-
) : ReadWriteCollection<T>,
345-
ReadableCollectionHandleImpl<T>(entitySpec, handleName, storageHandle, sender),
346-
WritableCollection<T> by writableCollection {
356+
) : ReadWriteCollectionHandle<T>,
357+
ReadCollectionHandleImpl<T>(entitySpec, handleName, storageHandle, sender),
358+
WriteCollectionHandle<T> by writableCollection {
347359
override val name: String
348360
get() = writableCollection.name
349361
}
@@ -396,13 +408,13 @@ private fun <T : Entity> createSingletonHandle(
396408
idGenerator,
397409
sender
398410
)
399-
HandleMode.Read -> ReadableSingletonHandleImpl<T>(
411+
HandleMode.Read -> ReadSingletonHandleImpl<T>(
400412
entitySpec,
401413
handleName,
402414
storageHandle,
403415
sender
404416
)
405-
HandleMode.Write -> WritableSingletonHandleImpl<T>(
417+
HandleMode.Write -> WriteSingletonHandleImpl<T>(
406418
handleName,
407419
storageHandle,
408420
idGenerator
@@ -426,13 +438,13 @@ private fun <T : Entity> createSetHandle(
426438
idGenerator,
427439
sender
428440
)
429-
HandleMode.Read -> ReadableCollectionHandleImpl<T>(
441+
HandleMode.Read -> ReadCollectionHandleImpl<T>(
430442
entitySpec,
431443
handleName,
432444
storageHandle,
433445
sender
434446
)
435-
HandleMode.Write -> WritableCollectionHandleImpl<T>(
447+
HandleMode.Write -> WriteCollectionHandleImpl<T>(
436448
handleName,
437449
storageHandle,
438450
idGenerator

0 commit comments

Comments
 (0)