diff --git a/lucene/core/src/java/org/apache/lucene/index/FlushByRamOrCountsPolicy.java b/lucene/core/src/java/org/apache/lucene/index/FlushByRamOrCountsPolicy.java index 174474c3723..dcecb9ff444 100644 --- a/lucene/core/src/java/org/apache/lucene/index/FlushByRamOrCountsPolicy.java +++ b/lucene/core/src/java/org/apache/lucene/index/FlushByRamOrCountsPolicy.java @@ -57,7 +57,8 @@ class FlushByRamOrCountsPolicy extends FlushPolicy { @Override public void flushRamManager(IndexWriter writer) throws IOException { IndexWriterRAMManager ramManager = writer.getConfig().indexWriterRAMManager; - if (ramManager.getRamBufferSizeMB() != IndexWriterConfig.DISABLE_AUTO_FLUSH) { + if (ramManager.getRamBufferSizeMB() != IndexWriterConfig.DISABLE_AUTO_FLUSH + && ramManager.getWriterCount() > 1) { long totalBytes = ramManager.updateAndGetCurrentBytesUsed(writer.ramManagerId); if (totalBytes > ramManager.getRamBufferSizeMB() * 1024 * 1024) { ramManager.flushRoundRobin(); diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexWriterRAMManager.java b/lucene/core/src/java/org/apache/lucene/index/IndexWriterRAMManager.java index 09b9e4f3bfb..886ff7a8cac 100644 --- a/lucene/core/src/java/org/apache/lucene/index/IndexWriterRAMManager.java +++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriterRAMManager.java @@ -37,7 +37,7 @@ public class IndexWriterRAMManager { * @param ramBufferSizeMB the RAM buffer size to use between all registered {@link IndexWriter} * instances */ - IndexWriterRAMManager(double ramBufferSizeMB) { + public IndexWriterRAMManager(double ramBufferSizeMB) { if (ramBufferSizeMB != IndexWriterConfig.DISABLE_AUTO_FLUSH && ramBufferSizeMB <= 0.0) { throw new IllegalArgumentException("ramBufferSize should be > 0.0 MB when enabled"); } @@ -63,6 +63,11 @@ public class IndexWriterRAMManager { return idToWriter.flushRoundRobin(); } + /** Gets the number of writers registered with this ram manager */ + public int getWriterCount() { + return idToWriter.size(); + } + /** Registers a writer can returns the associated ID */ protected int registerWriter(IndexWriter writer) { int id = idGenerator.incrementAndGet(); @@ -170,6 +175,12 @@ public class IndexWriterRAMManager { } } + int size() { + synchronized (lock) { + return idToWriterNode.size(); + } + } + private static class IndexWriterNode { IndexWriter writer; int id;