Skip to content

Commit 1fd7d21

Browse files
refactor(graph exploration): log number of vertices
loaded or stored from/to a file
1 parent ddf59fd commit 1fd7d21

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

src/main/java/org/commoncrawl/webgraph/explore/GraphExplorer.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.Arrays;
1313
import java.util.Comparator;
1414
import java.util.Map.Entry;
15+
import java.util.concurrent.atomic.AtomicLong;
1516
import java.util.function.Function;
1617
import java.util.stream.Collectors;
1718
import java.util.stream.IntStream;
@@ -186,40 +187,56 @@ public void sl(String vertexLabel) {
186187
/* Utilities */
187188

188189
public long[] loadVerticesFromFile(String fileName) {
190+
AtomicLong lines = new AtomicLong();
189191
try (Stream<String> in = Files.lines(Paths.get(fileName), StandardCharsets.UTF_8)) {
190-
return in.mapToLong(label -> g.vertexLabelToId(label)).filter(id -> id > -1).toArray();
192+
long[] res = in.mapToLong(
193+
label -> {
194+
lines.incrementAndGet();
195+
long id = g.vertexLabelToId(label);
196+
if (id == -1) {
197+
LOG.debug("Vertex `{}` not found in graph.", label);
198+
}
199+
return id;
200+
}).filter(id -> id > -1).toArray();
201+
LOG.info("Loaded {} vertices of {} lines in {}.", res.length, lines, fileName);
202+
return res;
191203
} catch (IOException e) {
192204
LOG.error("Failed to load vertices from file {}", fileName, e);
193205
}
194206
return new long[0];
195207
}
196208

197209
public void saveVerticesToFile(long[] vertexIDs, String fileName) {
198-
try (PrintStream out = new PrintStream(Files.newOutputStream(Paths.get(fileName)), false,
199-
StandardCharsets.UTF_8)) {
200-
Arrays.stream(vertexIDs).forEach(id -> out.println(g.vertexIdToLabel(id)));
201-
} catch (IOException e) {
202-
LOG.error("Failed to write vertices to file {}", fileName, e);
203-
}
210+
saveVerticesToFile(Arrays.stream(vertexIDs), fileName);
204211
}
205212

206213
public void saveVerticesToFile(int[] vertexIDs, String fileName) {
207214
saveVerticesToFile(Arrays.stream(vertexIDs), fileName);
208215
}
209216

210217
public void saveVerticesToFile(IntStream vertexIDs, String fileName) {
218+
AtomicLong count = new AtomicLong();
211219
try (PrintStream out = new PrintStream(Files.newOutputStream(Paths.get(fileName)), false,
212220
StandardCharsets.UTF_8)) {
213-
vertexIDs.forEach(id -> out.println(g.vertexIdToLabel(id)));
221+
vertexIDs.forEach(id -> {
222+
count.incrementAndGet();
223+
out.println(g.vertexIdToLabel(id));
224+
});
225+
LOG.info("Saved {} vertices to file {}.", count.get(), fileName);
214226
} catch (IOException e) {
215227
LOG.error("Failed to write vertices to file {}", fileName, e);
216228
}
217229
}
218230

219231
public void saveVerticesToFile(LongStream vertexIDs, String fileName) {
232+
AtomicLong count = new AtomicLong();
220233
try (PrintStream out = new PrintStream(Files.newOutputStream(Paths.get(fileName)), false,
221234
StandardCharsets.UTF_8)) {
222-
vertexIDs.forEach(id -> out.println(g.vertexIdToLabel(id)));
235+
vertexIDs.forEach(id -> {
236+
count.incrementAndGet();
237+
out.println(g.vertexIdToLabel(id));
238+
});
239+
LOG.info("Saved {} vertices to file {}.", count.get(), fileName);
223240
} catch (IOException e) {
224241
LOG.error("Failed to write vertices to file {}", fileName, e);
225242
}

0 commit comments

Comments
 (0)