Apache Solr open-source search software
LUCENE-475 - patch from Volodymyr Bychkoviak to reduce memory usage when loading an index into RAMDirectory.
Bernhard Messer6cabdf372671
diff --git a/CHANGES.txt b/CHANGES.txtindex 20c3ca70cf1..96cf00ef92b 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -304,7 +304,10 @@ Bug fixes17. A single hit using the expert level sorted search methodsresulted in the score not being normalized.(Yonik Seeley, LUCENE-462)-++18. Fixed inefficient memory usage when loading an index into RAMDirectory.+ (Volodymyr Bychkoviak via Bernhard, LUCENE-475)+Optimizations1. Disk usage (peak requirements during indexing and optimization)diff --git a/src/java/org/apache/lucene/store/RAMDirectory.java b/src/java/org/apache/lucene/store/RAMDirectory.javaindex 4d0728573bd..a18abe45d42 100644--- a/src/java/org/apache/lucene/store/RAMDirectory.java+++ b/src/java/org/apache/lucene/store/RAMDirectory.java@@ -53,6 +53,7 @@ public final class RAMDirectory extends Directory {private RAMDirectory(Directory dir, boolean closeDir) throws IOException {final String[] files = dir.list();+ byte[] buf = new byte[BufferedIndexOutput.BUFFER_SIZE];for (int i = 0; i < files.length; i++) {// make place on ram diskIndexOutput os = createOutput(files[i]);@@ -60,9 +61,14 @@ public final class RAMDirectory extends Directory {IndexInput is = dir.openInput(files[i]);// and copy to ram diskint len = (int) is.length();- byte[] buf = new byte[len];- is.readBytes(buf, 0, len);- os.writeBytes(buf, len);+ int readCount = 0;+ while (readCount < len) {+ int toRead = readCount + BufferedIndexOutput.BUFFER_SIZE > len ? len - readCount : BufferedIndexOutput.BUFFER_SIZE;+ is.readBytes(buf, 0, toRead);+ os.writeBytes(buf, toRead);+ readCount += toRead;+ }+// graceful cleanupis.close();os.close();