Skip to content

Commit 8ebca76

Browse files
authored
Index stale operations to Lucene to have complete history (#29679)
Today, when processing out of order operations, we only add it into translog but skip adding into Lucene. Translog, therefore, has a complete history of sequence numbers while Lucene does not. Since we would like to have a complete history in Lucene, this change makes sure that stale operations will be added to Lucene as soft-deleted documents if required. Relates #29530
1 parent 112b5f1 commit 8ebca76

File tree

5 files changed

+293
-69
lines changed

5 files changed

+293
-69
lines changed

server/src/main/java/org/elasticsearch/common/lucene/Lucene.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.lucene.document.NumericDocValuesField;
3131
import org.apache.lucene.index.CorruptIndexException;
3232
import org.apache.lucene.index.DirectoryReader;
33+
import org.apache.lucene.index.FilterDirectoryReader;
3334
import org.apache.lucene.index.FilterLeafReader;
3435
import org.apache.lucene.index.IndexCommit;
3536
import org.apache.lucene.index.IndexFileNames;
@@ -833,6 +834,58 @@ public int length() {
833834
};
834835
}
835836

837+
/**
838+
* Wraps a directory reader to include all live docs.
839+
* The wrapped reader can be used to query all documents.
840+
*
841+
* @param in the input directory reader
842+
* @return the wrapped reader
843+
*/
844+
public static DirectoryReader wrapAllDocsLive(DirectoryReader in) throws IOException {
845+
return new DirectoryReaderWithAllLiveDocs(in);
846+
}
847+
848+
private static final class DirectoryReaderWithAllLiveDocs extends FilterDirectoryReader {
849+
static final class SubReaderWithAllLiveDocs extends FilterLeafReader {
850+
SubReaderWithAllLiveDocs(LeafReader in) {
851+
super(in);
852+
}
853+
@Override
854+
public Bits getLiveDocs() {
855+
return null;
856+
}
857+
@Override
858+
public int numDocs() {
859+
return maxDoc();
860+
}
861+
@Override
862+
public CacheHelper getCoreCacheHelper() {
863+
return in.getCoreCacheHelper();
864+
}
865+
@Override
866+
public CacheHelper getReaderCacheHelper() {
867+
return null; // Modifying liveDocs
868+
}
869+
}
870+
DirectoryReaderWithAllLiveDocs(DirectoryReader in) throws IOException {
871+
super(in, new FilterDirectoryReader.SubReaderWrapper() {
872+
@Override
873+
public LeafReader wrap(LeafReader leaf) {
874+
return new SubReaderWithAllLiveDocs(leaf);
875+
}
876+
});
877+
}
878+
@Override
879+
protected DirectoryReader doWrapDirectoryReader(DirectoryReader in) throws IOException {
880+
return wrapAllDocsLive(in);
881+
}
882+
883+
@Override
884+
public CacheHelper getReaderCacheHelper() {
885+
return null; // Modifying liveDocs
886+
}
887+
}
888+
836889
/**
837890
* Returns a numeric docvalues which can be used to soft-delete documents.
838891
*/

0 commit comments

Comments
 (0)