2121import org .apache .lucene .index .DirectoryReader ;
2222import org .apache .lucene .index .IndexCommit ;
2323import org .apache .lucene .index .IndexWriter ;
24+ import org .apache .lucene .index .SegmentCommitInfo ;
2425import org .apache .lucene .index .SegmentInfos ;
2526import org .apache .lucene .index .SoftDeletesDirectoryReaderWrapper ;
2627import org .apache .lucene .search .IndexSearcher ;
@@ -95,15 +96,15 @@ public ReadOnlyEngine(EngineConfig config, SeqNoStats seqNoStats, TranslogStats
9596 this .lastCommittedSegmentInfos = Lucene .readSegmentInfos (directory );
9697 this .translogStats = translogStats == null ? new TranslogStats (0 , 0 , 0 , 0 , 0 ) : translogStats ;
9798 this .seqNoStats = seqNoStats == null ? buildSeqNoStats (lastCommittedSegmentInfos ) : seqNoStats ;
98- reader = ElasticsearchDirectoryReader .wrap (DirectoryReader . open (directory ), config .getShardId ());
99+ reader = ElasticsearchDirectoryReader .wrap (open (directory ), config .getShardId ());
99100 if (config .getIndexSettings ().isSoftDeleteEnabled ()) {
100101 reader = new SoftDeletesDirectoryReaderWrapper (reader , Lucene .SOFT_DELETES_FIELD );
101102 }
102103 reader = readerWrapperFunction .apply (reader );
103104 this .indexCommit = reader .getIndexCommit ();
104105 this .searcherManager = new SearcherManager (reader ,
105106 new RamAccountingSearcherFactory (engineConfig .getCircuitBreakerService ()));
106- this .docsStats = docsStats (reader );
107+ this .docsStats = docsStats (lastCommittedSegmentInfos );
107108 this .indexWriterLock = indexWriterLock ;
108109 success = true ;
109110 } finally {
@@ -116,6 +117,28 @@ public ReadOnlyEngine(EngineConfig config, SeqNoStats seqNoStats, TranslogStats
116117 }
117118 }
118119
120+ protected DirectoryReader open (final Directory directory ) throws IOException {
121+ return DirectoryReader .open (directory );
122+ }
123+
124+ private DocsStats docsStats (final SegmentInfos lastCommittedSegmentInfos ) {
125+ long numDocs = 0 ;
126+ long numDeletedDocs = 0 ;
127+ long sizeInBytes = 0 ;
128+ if (lastCommittedSegmentInfos != null ) {
129+ for (SegmentCommitInfo segmentCommitInfo : lastCommittedSegmentInfos ) {
130+ numDocs += segmentCommitInfo .info .maxDoc () - segmentCommitInfo .getDelCount () - segmentCommitInfo .getSoftDelCount ();
131+ numDeletedDocs += segmentCommitInfo .getDelCount () + segmentCommitInfo .getSoftDelCount ();
132+ try {
133+ sizeInBytes += segmentCommitInfo .sizeInBytes ();
134+ } catch (IOException e ) {
135+ throw new UncheckedIOException ("Failed to get size for [" + segmentCommitInfo .info .name + "]" , e );
136+ }
137+ }
138+ }
139+ return new DocsStats (numDocs , numDeletedDocs , sizeInBytes );
140+ }
141+
119142 @ Override
120143 protected void closeNoLock (String reason , CountDownLatch closedLatch ) {
121144 if (isClosed .compareAndSet (false , true )) {
0 commit comments