3939import org .apache .lucene .util .BytesRef ;
4040import org .apache .lucene .util .BytesRefBuilder ;
4141import org .apache .lucene .util .LongsRef ;
42+ import org .apache .lucene .util .RoaringDocIdSet ;
4243import org .apache .lucene .util .StringHelper ;
4344import org .apache .lucene .util .compress .LZ4 ;
4445import org .apache .lucene .util .packed .DirectMonotonicWriter ;
@@ -145,28 +146,10 @@ private long[] writeField(
145146 numValues += count ;
146147 }
147148 }
148-
149- if (numDocsWithValue == 0 ) { // meta[-2, 0]: No documents with values
150- meta .writeLong (-2 ); // docsWithFieldOffset
151- meta .writeLong (0L ); // docsWithFieldLength
152- meta .writeShort ((short ) -1 ); // jumpTableEntryCount
153- meta .writeByte ((byte ) -1 ); // denseRankPower
154- } else if (numDocsWithValue == maxDoc ) { // meta[-1, 0]: All documents have values
155- meta .writeLong (-1 ); // docsWithFieldOffset
156- meta .writeLong (0L ); // docsWithFieldLength
157- meta .writeShort ((short ) -1 ); // jumpTableEntryCount
158- meta .writeByte ((byte ) -1 ); // denseRankPower
159- } else { // meta[data.offset, data.length]: IndexedDISI structure for documents with values
160- long offset = data .getFilePointer ();
161- meta .writeLong (offset ); // docsWithFieldOffset
162- values = valuesProducer .getSortedNumeric (field );
163- final short jumpTableEntryCount = IndexedDISI .writeBitSet (values , data , IndexedDISI .DEFAULT_DENSE_RANK_POWER );
164- meta .writeLong (data .getFilePointer () - offset ); // docsWithFieldLength
165- meta .writeShort (jumpTableEntryCount );
166- meta .writeByte (IndexedDISI .DEFAULT_DENSE_RANK_POWER );
167- }
168149 meta .writeLong (numValues );
169150
151+ // TODO: write DISI to temp file and append it later to data part:
152+ var docIdSetBuilder = new RoaringDocIdSet .Builder (maxDoc );
170153 if (numValues > 0 ) {
171154 // Special case for maxOrd of 1, signal -1 that no blocks will be written
172155 meta .writeInt (maxOrd != 1 ? ES87TSDBDocValuesFormat .DIRECT_MONOTONIC_BLOCK_SHIFT : -1 );
@@ -193,6 +176,7 @@ private long[] writeField(
193176
194177 for (int doc = values .nextDoc (); doc != DocIdSetIterator .NO_MORE_DOCS ; doc = values .nextDoc ()) {
195178 numDocsWithValue ++;
179+ docIdSetBuilder .add (doc );
196180 final int count = values .docValueCount ();
197181 if (docCountConsumer != null ) {
198182 docCountConsumer .accept (count );
@@ -235,6 +219,35 @@ private long[] writeField(
235219 meta .writeLong (valuesDataOffset );
236220 meta .writeLong (valuesDataLength );
237221 }
222+ if (numDocsWithValue == 0 ) { // meta[-2, 0]: No documents with values
223+ meta .writeLong (-2 ); // docsWithFieldOffset
224+ meta .writeLong (0L ); // docsWithFieldLength
225+ meta .writeShort ((short ) -1 ); // jumpTableEntryCount
226+ meta .writeByte ((byte ) -1 ); // denseRankPower
227+ } else if (numDocsWithValue == maxDoc ) { // meta[-1, 0]: All documents have values
228+ meta .writeLong (-1 ); // docsWithFieldOffset
229+ meta .writeLong (0L ); // docsWithFieldLength
230+ meta .writeShort ((short ) -1 ); // jumpTableEntryCount
231+ meta .writeByte ((byte ) -1 ); // denseRankPower
232+ } else { // meta[data.offset, data.length]: IndexedDISI structure for documents with values
233+ long offset = data .getFilePointer ();
234+ meta .writeLong (offset ); // docsWithFieldOffset
235+ final short jumpTableEntryCount ;
236+ if (maxOrd != 1 ) {
237+ var bitSet = docIdSetBuilder .build ();
238+ var iterator = bitSet .iterator ();
239+ if (iterator == null ) {
240+ iterator = DocIdSetIterator .empty ();
241+ }
242+ jumpTableEntryCount = IndexedDISI .writeBitSet (iterator , data , IndexedDISI .DEFAULT_DENSE_RANK_POWER );
243+ } else {
244+ values = valuesProducer .getSortedNumeric (field );
245+ jumpTableEntryCount = IndexedDISI .writeBitSet (values , data , IndexedDISI .DEFAULT_DENSE_RANK_POWER );
246+ }
247+ meta .writeLong (data .getFilePointer () - offset ); // docsWithFieldLength
248+ meta .writeShort (jumpTableEntryCount );
249+ meta .writeByte (IndexedDISI .DEFAULT_DENSE_RANK_POWER );
250+ }
238251
239252 return new long [] { numDocsWithValue , numValues };
240253 }
0 commit comments