12
12
import org .apache .logging .log4j .Logger ;
13
13
import org .apache .lucene .store .AlreadyClosedException ;
14
14
import org .apache .lucene .store .IndexInput ;
15
+ import org .opensearch .common .annotation .ExperimentalApi ;
15
16
16
17
import java .io .IOException ;
17
18
import java .nio .file .Path ;
18
19
import java .util .HashSet ;
19
20
import java .util .Set ;
20
21
22
+ /**
23
+ * Extension of {@link FileCachedIndexInput} for full files for handling clones and slices
24
+ * We maintain a clone map so that we can close them when the parent IndexInput is closed so that ref count is properly maintained in file cache
25
+ * Closing of clones explicitly is needed as Lucene does not guarantee that it will close the clones
26
+ * https://github.com/apache/lucene/blob/8340b01c3cc229f33584ce2178b07b8984daa6a9/lucene/core/src/java/org/apache/lucene/store/IndexInput.java#L32-L33
27
+ * @opensearch.experimental
28
+ */
29
+ @ ExperimentalApi
21
30
public class FullFileCachedIndexInput extends FileCachedIndexInput {
22
31
private static final Logger logger = LogManager .getLogger (FullFileCachedIndexInput .class );
23
32
private final Set <FullFileCachedIndexInput > clones ;
@@ -31,6 +40,10 @@ public FullFileCachedIndexInput(FileCache cache, Path filePath, IndexInput under
31
40
clones = new HashSet <>();
32
41
}
33
42
43
+ /**
44
+ * Clones the index input and returns the clone
45
+ * Increase the ref count whenever the index input is cloned and add it to the clone map as well
46
+ */
34
47
@ Override
35
48
public FullFileCachedIndexInput clone () {
36
49
FullFileCachedIndexInput clonedIndexInput = new FullFileCachedIndexInput (cache , filePath , luceneIndexInput .clone (), true );
@@ -39,6 +52,10 @@ public FullFileCachedIndexInput clone() {
39
52
return clonedIndexInput ;
40
53
}
41
54
55
+ /**
56
+ * Clones the index input and returns the slice
57
+ * Increase the ref count whenever the index input is sliced and add it to the clone map as well
58
+ */
42
59
@ Override
43
60
public IndexInput slice (String sliceDescription , long offset , long length ) throws IOException {
44
61
if (offset < 0 || length < 0 || offset + length > this .length ()) {
@@ -62,6 +79,10 @@ public IndexInput slice(String sliceDescription, long offset, long length) throw
62
79
return slicedIndexInput ;
63
80
}
64
81
82
+ /**
83
+ * Closes the index input and it's clones as well
84
+ * @throws IOException
85
+ */
65
86
@ Override
66
87
public void close () throws IOException {
67
88
if (!closed ) {
0 commit comments