LUCENE-7345: RAMDirectory now enforces write-once as well

This commit is contained in:
Mike McCandless 2016-06-20 10:03:19 -04:00
parent 4c630b2216
commit c311eb1b7e
3 changed files with 12 additions and 22 deletions

View File

@ -55,6 +55,9 @@ Improvements
module into core and is now the default analyzer in module into core and is now the default analyzer in
IndexWriterConfig (Robert Muir, Mike McCandless) IndexWriterConfig (Robert Muir, Mike McCandless)
* LUCENE-7345: RAMDirectory now enforces write-once files as well
(Robert Muir, Mike McCandless)
Optimizations Optimizations
* LUCENE-7330: Speed up conjunction queries. (Adrien Grand) * LUCENE-7330: Speed up conjunction queries. (Adrien Grand)

View File

@ -19,6 +19,7 @@ package org.apache.lucene.store;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -177,12 +178,9 @@ public class RAMDirectory extends BaseDirectory implements Accountable {
public IndexOutput createOutput(String name, IOContext context) throws IOException { public IndexOutput createOutput(String name, IOContext context) throws IOException {
ensureOpen(); ensureOpen();
RAMFile file = newRAMFile(); RAMFile file = newRAMFile();
RAMFile existing = fileMap.remove(name); if (fileMap.putIfAbsent(name, file) != null) {
if (existing != null) { throw new FileAlreadyExistsException(name);
sizeInBytes.addAndGet(-existing.sizeInBytes);
existing.directory = null;
} }
fileMap.put(name, file);
return new RAMOutputStream(name, file, true); return new RAMOutputStream(name, file, true);
} }
@ -222,7 +220,12 @@ public class RAMDirectory extends BaseDirectory implements Accountable {
if (file == null) { if (file == null) {
throw new FileNotFoundException(source); throw new FileNotFoundException(source);
} }
fileMap.put(dest, file); if (fileMap.putIfAbsent(dest, file) != null) {
throw new FileAlreadyExistsException(dest);
}
if (!fileMap.remove(source, file)) {
throw new IllegalStateException("file was unexpectedly replaced: " + source);
}
fileMap.remove(source); fileMap.remove(source);
} }

View File

@ -636,22 +636,6 @@ public class MockDirectoryWrapper extends BaseDirectoryWrapper {
unSyncedFiles.add(name); unSyncedFiles.add(name);
createdFiles.add(name); createdFiles.add(name);
if (in instanceof RAMDirectory) {
RAMDirectory ramdir = (RAMDirectory) in;
RAMFile file = new RAMFile(ramdir);
RAMFile existing = ramdir.fileMap.get(name);
// Enforce write once:
if (existing!=null && !name.equals("segments.gen")) {
throw new IOException("file " + name + " already exists");
} else {
if (existing!=null) {
ramdir.sizeInBytes.getAndAdd(-existing.sizeInBytes);
existing.directory = null;
}
ramdir.fileMap.put(name, file);
}
}
//System.out.println(Thread.currentThread().getName() + ": MDW: create " + name); //System.out.println(Thread.currentThread().getName() + ": MDW: create " + name);
IndexOutput delegateOutput = in.createOutput(name, LuceneTestCase.newIOContext(randomState, context)); IndexOutput delegateOutput = in.createOutput(name, LuceneTestCase.newIOContext(randomState, context));
final IndexOutput io = new MockIndexOutputWrapper(this, delegateOutput, name); final IndexOutput io = new MockIndexOutputWrapper(this, delegateOutput, name);