3838import java .util .function .IntFunction ;
3939import java .util .stream .Collectors ;
4040
41+ /**
42+ * Abstract base class for writing graph indexes to disk.
43+ * @param <T> the type of the output writer
44+ */
4145public abstract class AbstractGraphIndexWriter <T extends IndexWriter > implements GraphIndexWriter {
42- public static final int FOOTER_MAGIC = 0x4a564244 ; // "EOF magic"
43- public static final int FOOTER_OFFSET_SIZE = Long .BYTES ; // The size of the offset in the footer
44- public static final int FOOTER_MAGIC_SIZE = Integer .BYTES ; // The size of the magic number in the footer
45- public static final int FOOTER_SIZE = FOOTER_MAGIC_SIZE + FOOTER_OFFSET_SIZE ; // The total size of the footer
46+ /** A magic number to indicate the file footer */
47+ public static final int FOOTER_MAGIC = 0x4a564244 ;
48+ /** The size of the offset in the footer. */
49+ public static final int FOOTER_OFFSET_SIZE = Long .BYTES ;
50+ /** The size of the magic number in the footer. */
51+ public static final int FOOTER_MAGIC_SIZE = Integer .BYTES ;
52+ /** The total size of the footer. */
53+ public static final int FOOTER_SIZE = FOOTER_MAGIC_SIZE + FOOTER_OFFSET_SIZE ;
4654 final int version ;
4755 final ImmutableGraphIndex graph ;
4856 final OrdinalMapper ordinalMapper ;
@@ -99,12 +107,17 @@ public abstract class AbstractGraphIndexWriter<T extends IndexWriter> implements
99107 }
100108
101109 /**
110+ * Gets the maximum ordinal written so far.
102111 * @return the maximum ordinal written so far, or -1 if no ordinals have been written yet
103112 */
104113 public int getMaxOrdinal () {
105114 return maxOrdinalWritten ;
106115 }
107116
117+ /**
118+ * Gets the set of features.
119+ * @return the feature set
120+ */
108121 public Set <FeatureId > getFeatureSet () {
109122 return featureMap .keySet ();
110123 }
@@ -123,6 +136,8 @@ boolean isSeparated(Feature feature) {
123136 }
124137
125138 /**
139+ * Computes sequential renumbering for graph ordinals.
140+ * @param graph the graph index to renumber
126141 * @return a Map of old to new graph ordinals where the new ordinals are sequential starting at 0,
127142 * while preserving the original relative ordering in `graph`. That is, for all node ids i and j,
128143 * if i < j in `graph` then map[i] < map[j] in the returned map. "Holes" left by
@@ -174,11 +189,11 @@ void writeFooter(ImmutableGraphIndex.View view, long headerOffset) throws IOExce
174189 /**
175190 * Writes the index header, including the graph size, so that OnDiskGraphIndex can open it.
176191 * The output IS flushed.
177- * <p>
178- * Public so that you can write the index size (and thus usefully open an OnDiskGraphIndex against the index)
179- * to read Features from it before writing the edges.
192+ * @param view the graph index view
193+ * @param startOffset the start offset
194+ * @throws IOException if an I/O error occurs
180195 */
181- public synchronized void writeHeader (ImmutableGraphIndex .View view , long startOffset ) throws IOException {
196+ protected synchronized void writeHeader (ImmutableGraphIndex .View view , long startOffset ) throws IOException {
182197 // graph-level properties
183198 var layerInfo = CommonHeader .LayerInfo .fromGraph (graph , ordinalMapper );
184199 var commonHeader = new CommonHeader (version ,
@@ -302,6 +317,8 @@ void writeSeparatedFeatures(Map<FeatureId, IntFunction<Feature.State>> featureSt
302317 * <p>
303318 * K - the type of the writer to build
304319 * T - the type of the output stream
320+ * @param <K> the type of the writer to build
321+ * @param <T> the type of the output stream
305322 */
306323 public abstract static class Builder <K extends AbstractGraphIndexWriter <T >, T extends IndexWriter > {
307324 final ImmutableGraphIndex graphIndex ;
@@ -310,13 +327,23 @@ public abstract static class Builder<K extends AbstractGraphIndexWriter<T>, T ex
310327 OrdinalMapper ordinalMapper ;
311328 int version ;
312329
330+ /**
331+ * Constructs a Builder.
332+ * @param graphIndex the graph index
333+ * @param out the output writer
334+ */
313335 public Builder (ImmutableGraphIndex graphIndex , T out ) {
314336 this .graphIndex = graphIndex ;
315337 this .out = out ;
316338 this .features = new EnumMap <>(FeatureId .class );
317339 this .version = OnDiskGraphIndex .CURRENT_VERSION ;
318340 }
319341
342+ /**
343+ * Sets the version.
344+ * @param version the version
345+ * @return this builder
346+ */
320347 public Builder <K , T > withVersion (int version ) {
321348 if (version > OnDiskGraphIndex .CURRENT_VERSION ) {
322349 throw new IllegalArgumentException ("Unsupported version: " + version );
@@ -326,16 +353,31 @@ public Builder<K, T> withVersion(int version) {
326353 return this ;
327354 }
328355
356+ /**
357+ * Adds a feature.
358+ * @param feature the feature
359+ * @return this builder
360+ */
329361 public Builder <K , T > with (Feature feature ) {
330362 features .put (feature .id (), feature );
331363 return this ;
332364 }
333365
366+ /**
367+ * Sets the ordinal mapper.
368+ * @param ordinalMapper the ordinal mapper
369+ * @return this builder
370+ */
334371 public Builder <K , T > withMapper (OrdinalMapper ordinalMapper ) {
335372 this .ordinalMapper = ordinalMapper ;
336373 return this ;
337374 }
338375
376+ /**
377+ * Builds the writer.
378+ * @return the writer
379+ * @throws IOException if an I/O error occurs
380+ */
339381 public K build () throws IOException {
340382 if (version < 3 && (!features .containsKey (FeatureId .INLINE_VECTORS ) || features .size () > 1 )) {
341383 throw new IllegalArgumentException ("Only INLINE_VECTORS is supported until version 3" );
@@ -360,12 +402,28 @@ public K build() throws IOException {
360402 return reallyBuild (dimension );
361403 }
362404
405+ /**
406+ * Actually builds the writer with the given dimension.
407+ * @param dimension the dimension
408+ * @return the writer
409+ * @throws IOException if an I/O error occurs
410+ */
363411 protected abstract K reallyBuild (int dimension ) throws IOException ;
364412
413+ /**
414+ * Sets the ordinal mapping.
415+ * @param oldToNewOrdinals the old to new ordinals map
416+ * @return this builder
417+ */
365418 public Builder <K , T > withMap (Map <Integer , Integer > oldToNewOrdinals ) {
366419 return withMapper (new OrdinalMapper .MapMapper (oldToNewOrdinals ));
367420 }
368421
422+ /**
423+ * Gets a feature by ID.
424+ * @param featureId the feature ID
425+ * @return the feature
426+ */
369427 public Feature getFeature (FeatureId featureId ) {
370428 return features .get (featureId );
371429 }
0 commit comments