private void recurseTree()

in aws-datastore/src/main/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelTree.java [110:165]


    private void recurseTree(
            Map<ModelSchema, Set<String>> map,
            ModelSchema modelSchema,
            Collection<String> parentIds
    ) {
        for (ModelAssociation association : modelSchema.getAssociations().values()) {
            switch (association.getName()) {
                case "HasOne":
                case "HasMany":
                    String childModel = association.getAssociatedType(); // model name
                    ModelSchema childSchema = registry.getModelSchemaForModelClass(childModel);
                    SQLiteTable childTable = SQLiteTable.fromSchema(childSchema);
                    String childId;
                    String parentId;
                    try {
                        childId = childTable.getPrimaryKey().getName();
                        parentId = childSchema.getAssociations() // get a map of associations
                                .get(association.getAssociatedName()) // get @BelongsTo association linked to this field
                                .getTargetName(); // get the target field (parent) name
                    } catch (NullPointerException unexpectedAssociation) {
                        LOG.warn("Foreign key was not found due to unidirectional relationship without @BelongsTo. " +
                                "Failed to publish cascading mutations.",
                                unexpectedAssociation);
                        return;
                    }

                    // Collect every children one level deeper than current level
                    Set<String> childrenIds = new HashSet<>();
                    try (Cursor cursor = queryChildren(childTable.getName(), childId, parentId, parentIds)) {
                        if (cursor != null && cursor.moveToFirst()) {
                            int index = cursor.getColumnIndexOrThrow(childId);
                            do {
                                childrenIds.add(cursor.getString(index));
                            } while (cursor.moveToNext());
                        }
                    } catch (SQLiteException exception) {
                        // Don't cut the search short. Populate rest of the tree.
                        LOG.warn("Failed to query children of deleted model(s).", exception);
                    }

                    // Add queried result to the map
                    if (!childrenIds.isEmpty()) {
                        if (!map.containsKey(childSchema)) {
                            map.put(childSchema, childrenIds);
                        } else {
                            map.get(childSchema).addAll(childrenIds);
                        }
                        recurseTree(map, childSchema, childrenIds);
                    }
                    break;
                case "BelongsTo":
                default:
                    // Ignore other relationships
            }
        }
    }