2222import org .apache .lucene .util .hnsw .IntToIntFunction ;
2323import org .apache .lucene .util .packed .PackedInts ;
2424import org .apache .lucene .util .packed .PackedLongValues ;
25+ import org .elasticsearch .core .SuppressForbidden ;
2526import org .elasticsearch .index .codec .vectors .cluster .HierarchicalKMeans ;
2627import org .elasticsearch .index .codec .vectors .cluster .KMeansResult ;
2728import org .elasticsearch .logging .LogManager ;
@@ -154,6 +155,7 @@ CentroidOffsetAndLength buildAndWritePostingsLists(
154155 }
155156
156157 @ Override
158+ @ SuppressForbidden (reason = "require usage of Lucene's IOUtils#deleteFilesIgnoringExceptions(...)" )
157159 CentroidOffsetAndLength buildAndWritePostingsLists (
158160 FieldInfo fieldInfo ,
159161 CentroidSupplier centroidSupplier ,
@@ -166,10 +168,14 @@ CentroidOffsetAndLength buildAndWritePostingsLists(
166168 ) throws IOException {
167169 // first, quantize all the vectors into a temporary file
168170 String quantizedVectorsTempName = null ;
169- IndexOutput quantizedVectorsTemp = null ;
170171 boolean success = false ;
171- try {
172- quantizedVectorsTemp = mergeState .segmentInfo .dir .createTempOutput (mergeState .segmentInfo .name , "qvec_" , IOContext .DEFAULT );
172+ try (
173+ IndexOutput quantizedVectorsTemp = mergeState .segmentInfo .dir .createTempOutput (
174+ mergeState .segmentInfo .name ,
175+ "qvec_" ,
176+ IOContext .DEFAULT
177+ )
178+ ) {
173179 quantizedVectorsTempName = quantizedVectorsTemp .getName ();
174180 OptimizedScalarQuantizer quantizer = new OptimizedScalarQuantizer (fieldInfo .getVectorSimilarityFunction ());
175181 int [] quantized = new int [fieldInfo .getVectorDimension ()];
@@ -202,12 +208,10 @@ CentroidOffsetAndLength buildAndWritePostingsLists(
202208 writeQuantizedValue (quantizedVectorsTemp , binary , zeroResult );
203209 }
204210 }
205- // close the temporary file so we can read it later
206- quantizedVectorsTemp .close ();
207211 success = true ;
208212 } finally {
209- if (success == false && quantizedVectorsTemp != null ) {
210- mergeState . segmentInfo . dir . deleteFile ( quantizedVectorsTemp . getName () );
213+ if (success == false && quantizedVectorsTempName != null ) {
214+ org . apache . lucene . util . IOUtils . deleteFilesIgnoringExceptions ( mergeState . segmentInfo . dir , quantizedVectorsTempName );
211215 }
212216 }
213217 int [] centroidVectorCount = new int [centroidSupplier .size ()];
@@ -298,6 +302,8 @@ CentroidOffsetAndLength buildAndWritePostingsLists(
298302 printClusterQualityStatistics (assignmentsByCluster );
299303 }
300304 return new CentroidOffsetAndLength (offsets .build (), lengths .build ());
305+ } finally {
306+ org .apache .lucene .util .IOUtils .deleteFilesIgnoringExceptions (mergeState .segmentInfo .dir , quantizedVectorsTempName );
301307 }
302308 }
303309
0 commit comments