drccrd commented on code in PR #3965:
URL: 
https://github.com/apache/incubator-kie-tools/pull/3965#discussion_r3843613223


##########
packages/drools-lsp/drools-completion/src/main/java/org/drools/completion/JavaSourceTypeIndex.java:
##########
@@ -0,0 +1,357 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.drools.completion;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.logging.Logger;
+import java.util.stream.Stream;
+
+/**
+ * Indexes {@code .java} source under a workspace's source roots via {@link
+ * JavaSourceTypeParser}, so completion/hover/lint can resolve types and
+ * members before a compile exists. An instance is an immutable snapshot;
+ * callers rebuild via {@link #build} on workspace changes rather than
+ * mutating one in place — the same model {@code ClassIndex} uses.
+ */
+public final class JavaSourceTypeIndex implements JavaMemberSource {
+
+    private static final Logger logger = 
Logger.getLogger(JavaSourceTypeIndex.class.getName());
+
+    private static final JavaSourceTypeIndex EMPTY =
+            new JavaSourceTypeIndex(Set.of(), Map.of(), Map.of(), Map.of());
+
+    private static final class CachedEntry {
+        final long modMillis;
+        final List<JavaSourceType> types;
+
+        CachedEntry(long modMillis, List<JavaSourceType> types) {
+            this.modMillis = modMillis;
+            this.types = types;
+        }
+    }
+
+    /** Per-file cache keyed by normalized absolute path, valid by mtime. 
Shared across builds. */
+    private static final Map<Path, CachedEntry> FILE_CACHE = new 
ConcurrentHashMap<>();
+
+    /**
+     * Drops all cached parse results. Entries are already mtime-validated, so
+     * this is about bounding memory in a long-running server rather than
+     * correctness; call it when the workspace source roots are rebuilt or on
+     * shutdown.
+     */
+    public static void clearCache() {
+        FILE_CACHE.clear();
+    }
+
+    private final Set<Path> roots;
+    private final Map<String, List<String>> classNames;
+    private final Map<String, JavaSourceType> typesByFqcn;
+    private final Map<String, Path> fileByFqcn;
+
+    private JavaSourceTypeIndex(Set<Path> roots, Map<String, List<String>> 
classNames,
+                                 Map<String, JavaSourceType> typesByFqcn, 
Map<String, Path> fileByFqcn) {
+        this.roots = roots;
+        this.classNames = classNames;
+        this.typesByFqcn = typesByFqcn;
+        this.fileByFqcn = fileByFqcn;
+    }
+
+    /** An index over no source roots; resolves nothing. */
+    public static JavaSourceTypeIndex empty() {
+        return EMPTY;
+    }
+
+    /**
+     * Walks each of {@code sourceRoots} for {@code .java} files, parses each
+     * (via the mtime cache) into its top-level types, and keeps those whose
+     * package passes {@code packageFilters} — a type's package is its FQCN
+     * minus the last segment; it passes when {@code packageFilters} is empty,
+     * when a filter equals the package exactly, or when a filter ends with
+     * {@code *} and the package starts with the prefix before it. A
+     * default-package type passes only when {@code packageFilters} is empty.
+     * On a duplicate FQCN across roots, the first one seen wins (walk order);
+     * later ones are logged at FINE and dropped.
+     */
+    public static JavaSourceTypeIndex build(Set<Path> sourceRoots, 
List<String> packageFilters) {
+        List<String> filters = packageFilters == null ? List.of() : 
packageFilters;
+        Set<Path> usedRoots = new LinkedHashSet<>();
+        Map<String, JavaSourceType> typesByFqcn = new LinkedHashMap<>();
+        Map<String, Path> fileByFqcn = new LinkedHashMap<>();
+
+        if (sourceRoots != null) {
+            for (Path root : sourceRoots) {
+                if (root == null || !Files.isDirectory(root)) {
+                    continue;
+                }
+                usedRoots.add(root);
+                indexRoot(root, filters, typesByFqcn, fileByFqcn);
+            }
+        }
+
+        // The roots are the one fact needed to explain everything else this
+        // index reports — including duplicate-FQCN drops, which are expected
+        // when two roots legitimately see the same file and alarming 
otherwise.
+        if (!usedRoots.isEmpty()) {
+            logger.info("Indexed " + typesByFqcn.size() + " Java source 
type(s) from "

Review Comment:
   Changed to log only when there is an actual change in 
7f58212ef4c42b2573ea00db2dd3638c5e23a8f8



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to