From 4cb32d6327b35f1369a20927f9dd10eb85d920f9 Mon Sep 17 00:00:00 2001 From: Kacper Urbaniec Date: Sun, 17 Dec 2023 18:36:02 +0100 Subject: [PATCH] Continued work on generating views --- .../codegen/BeePersistentViewCodegen.kt | 86 ++++++++++++++++--- 1 file changed, 76 insertions(+), 10 deletions(-) diff --git a/bee.persistent/src/blaze-processor/kotlin/com/beeproduced/bee/persistent/blaze/processor/codegen/BeePersistentViewCodegen.kt b/bee.persistent/src/blaze-processor/kotlin/com/beeproduced/bee/persistent/blaze/processor/codegen/BeePersistentViewCodegen.kt index f4e0d9e..e348b94 100644 --- a/bee.persistent/src/blaze-processor/kotlin/com/beeproduced/bee/persistent/blaze/processor/codegen/BeePersistentViewCodegen.kt +++ b/bee.persistent/src/blaze-processor/kotlin/com/beeproduced/bee/persistent/blaze/processor/codegen/BeePersistentViewCodegen.kt @@ -6,7 +6,6 @@ import com.beeproduced.bee.persistent.blaze.processor.info.EntityInfo import com.google.devtools.ksp.processing.CodeGenerator import com.google.devtools.ksp.processing.Dependencies import com.google.devtools.ksp.processing.KSPLogger -import com.google.devtools.ksp.symbol.KSType import com.squareup.kotlinpoet.FileSpec import com.squareup.kotlinpoet.FunSpec import com.squareup.kotlinpoet.TypeSpec @@ -107,20 +106,87 @@ class BeePersistentViewCodegen( private fun FileSpec.Builder.processEntity( entity: EntityInfo, viewCount: ViewCount ) { - val viewName = viewCount.viewName(entity, null) - - if (!debugInfo.containsKey(viewName)) { - debugInfo[viewName] = mutableSetOf() + // val viewName = viewCount.viewName(entity, null) + // + // if (!debugInfo.containsKey(viewName)) { + // debugInfo[viewName] = mutableSetOf() + // } + // + // val relationEntities = mutableListOf() + // for (relation in entity.relations) { + // val relationEntity = entitiesMap[relation.qualifiedName!!] ?: continue + // relationEntities.add(ProcessE(relationEntity, entity, viewName)) + // } + // processEntities(relationEntities, viewCount, entity) + + + logger.info("Generating!") + val result = proc(entity, config.depth) + result.forEach { (key, value) -> + logger.info(key) + value.relations.forEach { r -> + logger.info(" $r") + } } + } - val relationEntities = mutableListOf() - for (relation in entity.relations) { - val relationEntity = entitiesMap[relation.qualifiedName!!] ?: continue - relationEntities.add(ProcessE(relationEntity, entity, viewName)) + data class EntityViewInfo( + val name: String, + val entity: EntityInfo, + val relations: MutableMap = mutableMapOf() + ) { + val entityRelations get() = entity.relations + val qualifiedName get() = entity.qualifiedName!! + } + + fun viewName(entity: EntityInfo, root: EntityInfo, count: Int? = null): String { + return "${entity.simpleName}__View__${root.simpleName}__${count ?: "Core"}" + } + + private fun FileSpec.Builder.proc( + root: EntityInfo, depth: Int + ): Map { + // Key -> ViewName + val result = mutableMapOf() + // Key -> QualifiedName + val count = mutableMapOf() + + val extend = mutableListOf() + + // Visited -> QualifiedName + fun dfs(start: EntityInfo, startViewName: String, visited: MutableSet) { + val info = EntityViewInfo(startViewName, start) + for (relation in info.entityRelations) { + val relationEntity = entitiesMap[relation.qualifiedName!!] ?: continue + // Copy visited already here to multiple relations to same entity via same view + val nextVisited = visited.toMutableSet() + + if (!nextVisited.contains(relationEntity.qualifiedName)) { + nextVisited.add(relationEntity.qualifiedName!!) + val c = count.getOrDefault(relation.qualifiedName!!, 1) + count[relation.qualifiedName!!] = c + 1 + val viewName = viewName(relationEntity, root, c) + info.relations[relation.simpleName] = viewName + dfs(relationEntity, viewName, nextVisited) + } else if (depth > 0) { + val c = count.getOrDefault(relation.qualifiedName!!, 1) + val viewName = viewName(relationEntity, root, c) + count[relation.qualifiedName!!] = c + 1 + info.relations[relation.simpleName] = viewName + extend.add(EntityViewInfo(viewName, relationEntity)) + } + } + result[startViewName] = info } - processEntities(relationEntities, viewCount, entity) + + dfs(root, viewName(root, root), mutableSetOf(root.qualifiedName!!)) + logger.info("Extend") + logger.info(extend.map { it.name }.toString()) + return result } + + data class ProcessE( val entity: EntityInfo, val parent: EntityInfo,