Skip to content

Commit a3d622c

Browse files
committed
Split ScipShardWriter into a pure ScipShards utility
ScipShardWriter was a static-only utility named like a stateful writer. Move the pure merge logic into ScipShards and inline the six-line file I/O at the only call site (SemanticdbTaskListener.onFinishedAnalyze).
1 parent 95ff4f6 commit a3d622c

2 files changed

Lines changed: 14 additions & 28 deletions

File tree

semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/ScipShardWriter.java renamed to semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/ScipShards.java

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,24 @@
22

33
import org.scip_code.scip.Document;
44
import org.scip_code.scip.Index;
5-
import org.scip_code.scip.Occurrence;
65
import org.scip_code.scip.Relationship;
76
import org.scip_code.scip.SymbolInformation;
87

9-
import java.io.IOException;
10-
import java.io.InputStream;
11-
import java.nio.file.Files;
12-
import java.nio.file.Path;
138
import java.util.ArrayList;
149
import java.util.LinkedHashMap;
1510
import java.util.List;
1611

1712
/**
18-
* Writes and merges per-source SCIP shards produced by the compiler plugin.
13+
* Pure merge helpers for SCIP shards produced by the compiler plugin.
1914
*
2015
* <p>Each source file produces a self-contained {@link Index} shard containing a single {@link
21-
* Document}. When a shard already exists on disk (e.g. during annotation processing rounds), the
22-
* new document is merged into the existing one, deduplicating occurrences, symbols and
23-
* relationships.
16+
* Document}. When the compiler runs multiple analyze rounds for the same source (e.g. during
17+
* annotation processing), the freshly built shard is merged with the one already on disk by
18+
* combining their document lists and deduplicating occurrences, symbols and relationships.
2419
*/
25-
public final class ScipShardWriter {
20+
public final class ScipShards {
2621

27-
private ScipShardWriter() {}
28-
29-
/**
30-
* Writes the given {@code shard} to {@code output}, creating parent directories as needed. If
31-
* {@code output} already exists, the existing shard is parsed and merged with the new one.
32-
*/
33-
public static void writeOrMerge(Path output, Index shard) throws IOException {
34-
Files.createDirectories(output.getParent());
35-
if (Files.exists(output)) {
36-
Index existing;
37-
try (InputStream is = Files.newInputStream(output)) {
38-
existing = Index.parseFrom(is);
39-
}
40-
shard = merge(existing, shard);
41-
}
42-
Files.write(output, shard.toByteArray());
43-
}
22+
private ScipShards() {}
4423

4524
/**
4625
* Merges two SCIP shards by combining their document lists. Documents that share a {@code

semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTaskListener.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import javax.tools.JavaFileObject;
1212
import java.io.ByteArrayOutputStream;
1313
import java.io.IOException;
14+
import java.io.InputStream;
1415
import java.io.PrintWriter;
1516
import java.net.URI;
1617
import java.nio.file.Files;
@@ -114,7 +115,13 @@ private void onFinishedAnalyze(TaskEvent e) {
114115
Index shard =
115116
new ScipVisitor(globals, e.getCompilationUnit(), options, types, trees, elements)
116117
.buildShard(e.getCompilationUnit());
117-
ScipShardWriter.writeOrMerge(shardPath, shard);
118+
Files.createDirectories(shardPath.getParent());
119+
if (Files.exists(shardPath)) {
120+
try (InputStream is = Files.newInputStream(shardPath)) {
121+
shard = ScipShards.merge(Index.parseFrom(is), shard);
122+
}
123+
}
124+
Files.write(shardPath, shard.toByteArray());
118125
} catch (IOException ex) {
119126
this.reportException(ex, e);
120127
}

0 commit comments

Comments
 (0)