LUCENE-475 - patch from Volodymyr Bychkoviak to reduce memory usage

when loading an index into RAMDirectory.

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@351779 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Bernhard Messer 2005-12-02 17:37:50 +00:00
parent abb213261d
commit 6cabdf3726
2 changed files with 13 additions and 4 deletions

View File

@ -305,6 +305,9 @@ Bug fixes
resulted in the score not being normalized. resulted in the score not being normalized.
(Yonik Seeley, LUCENE-462) (Yonik Seeley, LUCENE-462)
18. Fixed inefficient memory usage when loading an index into RAMDirectory.
(Volodymyr Bychkoviak via Bernhard, LUCENE-475)
Optimizations Optimizations
1. Disk usage (peak requirements during indexing and optimization) 1. Disk usage (peak requirements during indexing and optimization)

View File

@ -53,6 +53,7 @@ public final class RAMDirectory extends Directory {
private RAMDirectory(Directory dir, boolean closeDir) throws IOException { private RAMDirectory(Directory dir, boolean closeDir) throws IOException {
final String[] files = dir.list(); final String[] files = dir.list();
byte[] buf = new byte[BufferedIndexOutput.BUFFER_SIZE];
for (int i = 0; i < files.length; i++) { for (int i = 0; i < files.length; i++) {
// make place on ram disk // make place on ram disk
IndexOutput os = createOutput(files[i]); IndexOutput os = createOutput(files[i]);
@ -60,9 +61,14 @@ public final class RAMDirectory extends Directory {
IndexInput is = dir.openInput(files[i]); IndexInput is = dir.openInput(files[i]);
// and copy to ram disk // and copy to ram disk
int len = (int) is.length(); int len = (int) is.length();
byte[] buf = new byte[len]; int readCount = 0;
is.readBytes(buf, 0, len); while (readCount < len) {
os.writeBytes(buf, 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 cleanup // graceful cleanup
is.close(); is.close();
os.close(); os.close();