|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.firebase.dataconnect.sqlite |
| 18 | + |
| 19 | +import com.google.firebase.dataconnect.DataConnectPath |
| 20 | +import com.google.firebase.dataconnect.toEntityPathProto |
| 21 | +import com.google.firebase.dataconnect.toPathString |
| 22 | +import com.google.firebase.dataconnect.util.ProtoPrune |
| 23 | +import com.google.firebase.dataconnect.util.ProtoPrune.withPrunedDescendants |
| 24 | +import com.google.firebase.dataconnect.util.ProtoUtil.toCompactString |
| 25 | +import com.google.firebase.dataconnect.withAddedListIndex |
| 26 | +import com.google.protobuf.Struct |
| 27 | +import google.firebase.dataconnect.proto.kotlinsdk.Entity as EntityProto |
| 28 | +import google.firebase.dataconnect.proto.kotlinsdk.EntityList as EntityListProto |
| 29 | +import google.firebase.dataconnect.proto.kotlinsdk.EntityOrEntityList as EntityOrEntityListProto |
| 30 | +import google.firebase.dataconnect.proto.kotlinsdk.QueryResult as QueryResultProto |
| 31 | + |
| 32 | +internal typealias GetEntityIdForPathFunction = (DataConnectPath) -> String? |
| 33 | + |
| 34 | +internal data class DehydratedQueryResult( |
| 35 | + val proto: QueryResultProto, |
| 36 | + val entityStructById: Map<String, Struct>, |
| 37 | +) |
| 38 | + |
| 39 | +internal fun dehydrateQueryResult( |
| 40 | + queryResult: Struct, |
| 41 | + getEntityIdForPath: GetEntityIdForPathFunction? = null |
| 42 | +): DehydratedQueryResult { |
| 43 | + val protoBuilder = QueryResultProto.newBuilder() |
| 44 | + val entityById = protoBuilder.initialize(queryResult, getEntityIdForPath) |
| 45 | + return DehydratedQueryResult(protoBuilder.build(), entityById) |
| 46 | +} |
| 47 | + |
| 48 | +private fun QueryResultProto.Builder.initialize( |
| 49 | + queryResult: Struct, |
| 50 | + getEntityIdForPath: GetEntityIdForPathFunction? |
| 51 | +): Map<String, Struct> { |
| 52 | + val pruneResult = |
| 53 | + if (getEntityIdForPath === null) { |
| 54 | + null |
| 55 | + } else { |
| 56 | + pruneEntities(queryResult, EntityIdMemoizer(getEntityIdForPath)) |
| 57 | + } |
| 58 | + |
| 59 | + if (pruneResult === null) { |
| 60 | + setStruct(queryResult) |
| 61 | + return emptyMap() |
| 62 | + } |
| 63 | + |
| 64 | + setStruct(pruneResult.prunedStruct) |
| 65 | + |
| 66 | + val entityStructById = mutableMapOf<String, Struct>() |
| 67 | + fun updateEntityStructByIdWithEntity(entity: EntityIdStructPair) { |
| 68 | + val (entityId, struct) = entity |
| 69 | + |
| 70 | + val newStruct = |
| 71 | + if (!entityStructById.containsKey(entityId)) { |
| 72 | + struct |
| 73 | + } else { |
| 74 | + val oldStruct = entityStructById[entityId]!! |
| 75 | + if (oldStruct.fieldsMap.keys == struct.fieldsMap.keys) { |
| 76 | + struct |
| 77 | + } else { |
| 78 | + oldStruct.toBuilder().putAllFields(struct.fieldsMap).build() |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + entityStructById[entityId] = newStruct |
| 83 | + } |
| 84 | + |
| 85 | + pruneResult.entityByPath.entries.forEach { (path, entity) -> |
| 86 | + addEntities(entity.toEntityOrEntityListProto(path)) |
| 87 | + updateEntityStructByIdWithEntity(entity) |
| 88 | + } |
| 89 | + pruneResult.entityListByPath.entries.forEach { (path, entityList) -> |
| 90 | + addEntities(entityList.toEntityOrEntityListProto(path)) |
| 91 | + entityList.forEach(::updateEntityStructByIdWithEntity) |
| 92 | + } |
| 93 | + |
| 94 | + return entityStructById.toMap() |
| 95 | +} |
| 96 | + |
| 97 | +private fun EntityIdStructPair.toEntityOrEntityListProto( |
| 98 | + path: DataConnectPath |
| 99 | +): EntityOrEntityListProto = |
| 100 | + EntityOrEntityListProto.newBuilder().run { |
| 101 | + setPath(path.toEntityPathProto()) |
| 102 | + setEntity(toEntityProto()) |
| 103 | + build() |
| 104 | + } |
| 105 | + |
| 106 | +private fun EntityIdStructPair.toEntityProto(): EntityProto = |
| 107 | + EntityProto.newBuilder().run { |
| 108 | + setEntityId(this@toEntityProto.entityId) |
| 109 | + addAllFields(this@toEntityProto.struct.fieldsMap.keys) |
| 110 | + build() |
| 111 | + } |
| 112 | + |
| 113 | +private fun List<EntityIdStructPair>.toEntityOrEntityListProto( |
| 114 | + path: DataConnectPath |
| 115 | +): EntityOrEntityListProto = |
| 116 | + EntityOrEntityListProto.newBuilder().run { |
| 117 | + setPath(path.toEntityPathProto()) |
| 118 | + setEntityList(toEntityListProto()) |
| 119 | + build() |
| 120 | + } |
| 121 | + |
| 122 | +private fun List<EntityIdStructPair>.toEntityListProto(): EntityListProto = |
| 123 | + EntityListProto.newBuilder().run { |
| 124 | + forEach { addEntities(it.toEntityProto()) } |
| 125 | + build() |
| 126 | + } |
| 127 | + |
| 128 | +internal data class EntityIdStructPair( |
| 129 | + val entityId: String, |
| 130 | + val struct: Struct, |
| 131 | +) { |
| 132 | + override fun toString() = |
| 133 | + "EntityIdStructPair(entityId=$entityId, struct=${struct.toCompactString()})" |
| 134 | +} |
| 135 | + |
| 136 | +private data class PruneEntitiesResult( |
| 137 | + val prunedStruct: Struct, |
| 138 | + val entityByPath: Map<DataConnectPath, EntityIdStructPair>, |
| 139 | + val entityListByPath: Map<DataConnectPath, List<EntityIdStructPair>>, |
| 140 | +) |
| 141 | + |
| 142 | +private fun pruneEntities( |
| 143 | + queryResult: Struct, |
| 144 | + entityIdByPath: EntityIdMemoizer, |
| 145 | +): PruneEntitiesResult? { |
| 146 | + val dehydratedQueryResult = |
| 147 | + queryResult.withPrunedDescendants { path, listSize -> |
| 148 | + if (listSize === null) { |
| 149 | + entityIdByPath.isEntity(path) |
| 150 | + } else { |
| 151 | + var entityCount = 0 |
| 152 | + var nonEntityCount = 0 |
| 153 | + repeat(listSize) { |
| 154 | + val listElementPath = path.withAddedListIndex(it) |
| 155 | + if (entityIdByPath.isEntity(listElementPath)) { |
| 156 | + entityCount++ |
| 157 | + } else { |
| 158 | + nonEntityCount++ |
| 159 | + } |
| 160 | + } |
| 161 | + entityCount > 0 && nonEntityCount == 0 |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + if (dehydratedQueryResult === null) { |
| 166 | + return null |
| 167 | + } |
| 168 | + |
| 169 | + val entityByPath: Map<DataConnectPath, EntityIdStructPair> |
| 170 | + val entityListByPath: Map<DataConnectPath, List<EntityIdStructPair>> |
| 171 | + |
| 172 | + run { |
| 173 | + val entityByPathBuilder = mutableMapOf<DataConnectPath, EntityIdStructPair>() |
| 174 | + val entityListByPathBuilder = mutableMapOf<DataConnectPath, List<EntityIdStructPair>>() |
| 175 | + |
| 176 | + dehydratedQueryResult.prunedValueByPath.entries.forEach { (path, prunedValue) -> |
| 177 | + when (prunedValue) { |
| 178 | + is ProtoPrune.PrunedStruct -> { |
| 179 | + val entityId = entityIdByPath.getInitializedEntityId(path) |
| 180 | + val entity = EntityIdStructPair(entityId, prunedValue.struct) |
| 181 | + entityByPathBuilder[path] = entity |
| 182 | + } |
| 183 | + is ProtoPrune.PrunedListValue -> { |
| 184 | + val entities = |
| 185 | + prunedValue.structs.mapIndexed { index, struct -> |
| 186 | + val entityPath = path.withAddedListIndex(index) |
| 187 | + val entityId = entityIdByPath.getInitializedEntityId(entityPath) |
| 188 | + EntityIdStructPair(entityId, struct) |
| 189 | + } |
| 190 | + entityListByPathBuilder[path] = entities |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + entityByPath = entityByPathBuilder.toMap() |
| 196 | + entityListByPath = entityListByPathBuilder.toMap() |
| 197 | + } |
| 198 | + |
| 199 | + return PruneEntitiesResult(dehydratedQueryResult.prunedStruct, entityByPath, entityListByPath) |
| 200 | +} |
| 201 | + |
| 202 | +private class EntityIdMemoizer(private val getEntityIdForPath: GetEntityIdForPathFunction) { |
| 203 | + |
| 204 | + sealed interface EntityId { |
| 205 | + |
| 206 | + val entityIdOrNull: String? |
| 207 | + |
| 208 | + object NotAnEntity : EntityId { |
| 209 | + override val entityIdOrNull: Nothing? = null |
| 210 | + } |
| 211 | + |
| 212 | + @JvmInline |
| 213 | + value class IsAnEntity(val entityId: String) : EntityId { |
| 214 | + override val entityIdOrNull: String |
| 215 | + get() = entityId |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + private val entityIdByPath = mutableMapOf<DataConnectPath, EntityId>() |
| 220 | + |
| 221 | + private fun ensureInitialized(path: DataConnectPath): EntityId = |
| 222 | + entityIdByPath.getOrPut(path) { |
| 223 | + getEntityIdForPath(path)?.let(EntityId::IsAnEntity) ?: EntityId.NotAnEntity |
| 224 | + } |
| 225 | + |
| 226 | + fun isEntity(path: DataConnectPath): Boolean = |
| 227 | + when (ensureInitialized(path)) { |
| 228 | + is EntityId.IsAnEntity -> true |
| 229 | + EntityId.NotAnEntity -> false |
| 230 | + } |
| 231 | + |
| 232 | + fun getInitializedEntityId(path: DataConnectPath): String { |
| 233 | + val memoizedValue = |
| 234 | + entityIdByPath[path] |
| 235 | + ?: throw IllegalArgumentException( |
| 236 | + "entity ID for path=${path.toPathString()} is not initialized [d8a8k6bkzh]" |
| 237 | + ) |
| 238 | + return when (memoizedValue) { |
| 239 | + is EntityId.IsAnEntity -> memoizedValue.entityId |
| 240 | + EntityId.NotAnEntity -> |
| 241 | + throw IllegalArgumentException("path=${path.toPathString()} is not an entity [jserv86jpk]") |
| 242 | + } |
| 243 | + } |
| 244 | +} |
0 commit comments