3030import org .apache .lucene .store .FlushInfo ;
3131import org .apache .lucene .store .IOContext ;
3232import org .apache .lucene .store .MergeInfo ;
33+ import org .elasticsearch .common .util .set .Sets ;
3334
3435import java .io .IOException ;
3536import java .util .Set ;
36- import java .util .stream .Collectors ;
37- import java .util .stream .Stream ;
3837
3938/**
4039 * Copied from Lucene99FlatVectorsFormat in Lucene 10.1
@@ -69,13 +68,16 @@ public FlatVectorsWriter fieldsWriter(SegmentWriteState state) throws IOExceptio
6968
7069 @ Override
7170 public FlatVectorsReader fieldsReader (SegmentReadState state ) throws IOException {
72- SegmentReadState directIOState = new SegmentReadState (
73- state .directory ,
74- state .segmentInfo ,
75- state .fieldInfos ,
76- new DirectIOContext (state .context ),
77- state .segmentSuffix
78- );
71+ // only override the context for the random-access use case
72+ SegmentReadState directIOState = state .context .context () == IOContext .Context .DEFAULT
73+ ? new SegmentReadState (
74+ state .directory ,
75+ state .segmentInfo ,
76+ state .fieldInfos ,
77+ new DirectIOContext (state .context .hints ()),
78+ state .segmentSuffix
79+ )
80+ : state ;
7981 // Use mmap for merges and direct I/O for searches.
8082 // TODO: Open the mmap file with sequential access instead of random (current behavior).
8183 return new MergeReaderWrapper (
@@ -91,27 +93,26 @@ public String toString() {
9193
9294 static class DirectIOContext implements IOContext {
9395
94- final IOContext delegate ;
9596 final Set <FileOpenHint > hints ;
9697
97- DirectIOContext (IOContext delegate ) {
98- this . delegate = delegate ;
99- hints = Stream . concat ( delegate . hints (). stream (), Stream .of (DirectIOHint .INSTANCE )). collect ( Collectors . toSet ( ));
98+ DirectIOContext (Set < FileOpenHint > hints ) {
99+ // always add DirectIOHint to the hints given
100+ this . hints = Sets . union ( hints , Set .of (DirectIOHint .INSTANCE ));
100101 }
101102
102103 @ Override
103104 public Context context () {
104- return delegate . context () ;
105+ return Context . DEFAULT ;
105106 }
106107
107108 @ Override
108109 public MergeInfo mergeInfo () {
109- return delegate . mergeInfo () ;
110+ return null ;
110111 }
111112
112113 @ Override
113114 public FlushInfo flushInfo () {
114- return delegate . flushInfo () ;
115+ return null ;
115116 }
116117
117118 @ Override
@@ -121,7 +122,7 @@ public Set<FileOpenHint> hints() {
121122
122123 @ Override
123124 public IOContext withHints (FileOpenHint ... hints ) {
124- return delegate . withHints (hints ); // TODO: keep the directIO or drop it ?
125+ return new DirectIOContext ( Set . of (hints ));
125126 }
126127 }
127128}
0 commit comments