1616import java .util .Map ;
1717
1818/**
19- * Writes and merges per-source SCIP shards produced by the compiler plugin.
20- *
21- * <p>Each source file produces a self-contained {@link Index} shard containing a single {@link
22- * Document}. When a shard already exists on disk (e.g. during annotation processing rounds), the
23- * new document is merged into the existing one, deduplicating occurrences, symbols and
24- * relationships.
19+ * Writes a per-source SCIP shard, merging it with any pre-existing shard at the same path (e.g. one
20+ * written by a prior annotation-processing round).
2521 */
2622public final class ScipShardWriter {
2723
2824 private ScipShardWriter () {}
2925
30- /**
31- * Writes the given {@code shard} to {@code output}, creating parent directories as needed. If
32- * {@code output} already exists, the existing shard is parsed and merged with the new one.
33- */
3426 public static void writeOrMerge (Path output , Index shard ) throws IOException {
3527 Files .createDirectories (output .getParent ());
3628 if (Files .exists (output )) {
@@ -43,10 +35,7 @@ public static void writeOrMerge(Path output, Index shard) throws IOException {
4335 Files .write (output , shard .toByteArray ());
4436 }
4537
46- /**
47- * Merges two SCIP shards by combining their document lists. Documents that share a {@code
48- * relative_path} have their occurrences, symbols and external symbols deduplicated.
49- */
38+ /** Merges two shards: documents sharing a {@code relative_path} are dedup-merged. */
5039 static Index merge (Index a , Index b ) {
5140 Index .Builder builder = Index .newBuilder ();
5241 if (b .hasMetadata ()) {
@@ -69,7 +58,7 @@ static Index merge(Index a, Index b) {
6958 }
7059 builder .addAllDocuments (byPath .values ());
7160
72- // External symbols: deduplicate by symbol string. Last writer wins to keep latest data.
61+ // External symbols: last writer wins to keep the most recent data.
7362 LinkedHashMap <String , SymbolInformation > externals = new LinkedHashMap <>();
7463 for (SymbolInformation s : a .getExternalSymbolsList ()) externals .put (s .getSymbol (), s );
7564 for (SymbolInformation s : b .getExternalSymbolsList ()) externals .put (s .getSymbol (), s );
@@ -79,18 +68,15 @@ static Index merge(Index a, Index b) {
7968 }
8069
8170 private static Document mergeDocuments (Document a , Document b ) {
71+ // b.toBuilder() carries forward the most recent language/relative_path/text/encoding.
8272 Document .Builder builder = b .toBuilder ().clearOccurrences ().clearSymbols ();
83- // Use the most recent metadata for language/relative_path/text/encoding which already
84- // come from b via toBuilder().
8573
86- // Deduplicate occurrences by (range, symbol, roles). Variants that differ only in
87- // enclosing_range get collapsed, preferring the one that carries the enclosing range.
8874 ScipOccurrences occurrences = new ScipOccurrences ();
8975 occurrences .addAll (a .getOccurrencesList ());
9076 occurrences .addAll (b .getOccurrencesList ());
9177 builder .addAllOccurrences (occurrences .values ());
9278
93- // Deduplicate symbols by symbol string; merge relationships and documentation.
79+ // Dedup symbols by symbol string; merge relationships and documentation.
9480 Map <String , SymbolInformation > bySymbol = new LinkedHashMap <>();
9581 for (SymbolInformation info : a .getSymbolsList ()) bySymbol .put (info .getSymbol (), info );
9682 for (SymbolInformation info : b .getSymbolsList ()) {
@@ -104,12 +90,12 @@ private static Document mergeDocuments(Document a, Document b) {
10490
10591 private static SymbolInformation mergeSymbol (SymbolInformation a , SymbolInformation b ) {
10692 SymbolInformation .Builder builder = b .toBuilder ();
107- // Merge relationships, deduplicating by structural equality with deterministic ordering .
93+ // Dedup relationships by structural equality; preserve insertion order .
10894 LinkedHashSet <Relationship > rels = new LinkedHashSet <>(a .getRelationshipsList ());
10995 rels .addAll (b .getRelationshipsList ());
11096 builder .clearRelationships ().addAllRelationships (rels );
11197
112- // Merge documentation, preserving order and avoiding duplicates .
98+ // Dedup documentation; preserve insertion order .
11399 List <String > docs = new ArrayList <>(a .getDocumentationList ());
114100 for (String d : b .getDocumentationList ()) {
115101 if (!docs .contains (d )) docs .add (d );
0 commit comments