Skip to content

Apache Lucene open-source search software

LUCENE-2778: port from 3x

Shai Ererab57c84611b0a
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 8e58bbb6a16..0b787a0fa5d 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -161,6 +161,9 @@ API Changes
* LUCENE-2566: QueryParser: Unary operators +,-,! will not be treated as
operators if they are followed by whitespace. (yonik)
+* LUCENE-2778: RAMDirectory now exposes newRAMFile() which allows to override
+ and return a different RAMFile implementation. (Shai Erera)
+
New features
* LUCENE-2604: Added RegexpQuery support to QueryParser. Regular expressions
diff --git a/lucene/src/java/org/apache/lucene/store/RAMDirectory.java b/lucene/src/java/org/apache/lucene/store/RAMDirectory.java
index a7f39aa6d95..fdb88a15d6b 100644
--- a/lucene/src/java/org/apache/lucene/store/RAMDirectory.java
+++ b/lucene/src/java/org/apache/lucene/store/RAMDirectory.java
@@ -35,8 +35,8 @@ public class RAMDirectory extends Directory implements Serializable {
private static final long serialVersionUID = 1l;
- HashMap<String,RAMFile> fileMap = new HashMap<String,RAMFile>();
- final AtomicLong sizeInBytes = new AtomicLong();
+ protected HashMap<String,RAMFile> fileMap = new HashMap<String,RAMFile>();
+ protected final AtomicLong sizeInBytes = new AtomicLong();
// *****
// Lock acquisition sequence: RAMDirectory, then RAMFile
@@ -169,9 +169,8 @@ public class RAMDirectory extends Directory implements Serializable {
@Override
public synchronized void deleteFile(String name) throws IOException {
ensureOpen();
- RAMFile file = fileMap.get(name);
+ RAMFile file = fileMap.remove(name);
if (file!=null) {
- fileMap.remove(name);
file.directory = null;
sizeInBytes.addAndGet(-file.sizeInBytes);
} else
@@ -182,7 +181,7 @@ public class RAMDirectory extends Directory implements Serializable {
@Override
public IndexOutput createOutput(String name) throws IOException {
ensureOpen();
- RAMFile file = new RAMFile(this);
+ RAMFile file = newRAMFile();
synchronized (this) {
RAMFile existing = fileMap.get(name);
if (existing!=null) {
@@ -194,6 +193,15 @@ public class RAMDirectory extends Directory implements Serializable {
return new RAMOutputStream(file);
}
+ /**
+ * Returns a new {@link RAMFile} for storing data. This method can be
+ * overridden to return different {@link RAMFile} impls, that e.g. override
+ * {@link RAMFile#newBuffer(int)}.
+ */
+ protected RAMFile newRAMFile() {
+ return new RAMFile(this);
+ }
+
/** Returns a stream reading an existing file. */
@Override
public IndexInput openInput(String name) throws IOException {