Apache Lucene open-source search software
LUCENE-1067: make access to RAMFile's buffers thread safe
Michael McCandless6ebd30610326
diff --git a/src/java/org/apache/lucene/store/RAMFile.java b/src/java/org/apache/lucene/store/RAMFile.javaindex 4d2eb7ba115..99736af9440 100644--- a/src/java/org/apache/lucene/store/RAMFile.java+++ b/src/java/org/apache/lucene/store/RAMFile.java@@ -24,8 +24,7 @@ class RAMFile implements Serializable {private static final long serialVersionUID = 1l;- // Direct read-only access to state supported for streams since a writing stream implies no other concurrent streams- ArrayList buffers = new ArrayList();+ private ArrayList buffers = new ArrayList();long length;RAMDirectory directory;long sizeInBytes; // Only maintained if in a directory; updates synchronized on directory@@ -58,8 +57,7 @@ class RAMFile implements Serializable {this.lastModified = lastModified;}- // Only one writing stream with no concurrent reading streams, so no file synchronization required- final byte[] addBuffer(int size) {+ final synchronized byte[] addBuffer(int size) {byte[] buffer = newBuffer(size);if (directory!=null)synchronized (directory) { // Ensure addition of buffer and adjustment to directory size are atomic wrt directory@@ -72,6 +70,14 @@ class RAMFile implements Serializable {return buffer;}+ final synchronized byte[] getBuffer(int index) {+ return (byte[]) buffers.get(index);+ }++ final synchronized int numBuffers() {+ return buffers.size();+ }+/*** Expert: allocate a new buffer.* Subclasses can allocate differently.diff --git a/src/java/org/apache/lucene/store/RAMInputStream.java b/src/java/org/apache/lucene/store/RAMInputStream.javaindex 7e9a54ba165..c2a3a72ce3d 100644--- a/src/java/org/apache/lucene/store/RAMInputStream.java+++ b/src/java/org/apache/lucene/store/RAMInputStream.java@@ -84,11 +84,11 @@ class RAMInputStream extends IndexInput implements Cloneable {}private final void switchCurrentBuffer() throws IOException {- if (currentBufferIndex >= file.buffers.size()) {+ if (currentBufferIndex >= file.numBuffers()) {// end of file reached, no more buffers leftthrow new IOException("Read past EOF");} else {- currentBuffer = (byte[]) file.buffers.get(currentBufferIndex);+ currentBuffer = (byte[]) file.getBuffer(currentBufferIndex);bufferPosition = 0;bufferStart = (long) BUFFER_SIZE * (long) currentBufferIndex;long buflen = length - bufferStart;diff --git a/src/java/org/apache/lucene/store/RAMOutputStream.java b/src/java/org/apache/lucene/store/RAMOutputStream.javaindex 7dec81aefc0..b0861807ea2 100644--- a/src/java/org/apache/lucene/store/RAMOutputStream.java+++ b/src/java/org/apache/lucene/store/RAMOutputStream.java@@ -63,7 +63,7 @@ public class RAMOutputStream extends IndexOutput {if (nextPos > end) { // at the last bufferlength = (int)(end - pos);}- out.writeBytes((byte[])file.buffers.get(buffer++), length);+ out.writeBytes((byte[])file.getBuffer(buffer++), length);pos = nextPos;}}@@ -124,10 +124,10 @@ public class RAMOutputStream extends IndexOutput {}private final void switchCurrentBuffer() throws IOException {- if (currentBufferIndex == file.buffers.size()) {+ if (currentBufferIndex == file.numBuffers()) {currentBuffer = file.addBuffer(BUFFER_SIZE);} else {- currentBuffer = (byte[]) file.buffers.get(currentBufferIndex);+ currentBuffer = (byte[]) file.getBuffer(currentBufferIndex);}bufferPosition = 0;bufferStart = (long) BUFFER_SIZE * (long) currentBufferIndex;