lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mikemcc...@apache.org
Subject svn commit: r598693 - in /lucene/java/trunk/src/java/org/apache/lucene/store: RAMFile.java RAMInputStream.java RAMOutputStream.java
Date Tue, 27 Nov 2007 17:01:23 GMT
Author: mikemccand
Date: Tue Nov 27 09:01:21 2007
New Revision: 598693

URL: http://svn.apache.org/viewvc?rev=598693&view=rev
Log:
LUCENE-1067: make access to RAMFile's buffers thread safe

Modified:
    lucene/java/trunk/src/java/org/apache/lucene/store/RAMFile.java
    lucene/java/trunk/src/java/org/apache/lucene/store/RAMInputStream.java
    lucene/java/trunk/src/java/org/apache/lucene/store/RAMOutputStream.java

Modified: lucene/java/trunk/src/java/org/apache/lucene/store/RAMFile.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/store/RAMFile.java?rev=598693&r1=598692&r2=598693&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/store/RAMFile.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/store/RAMFile.java Tue Nov 27 09:01:21 2007
@@ -24,8 +24,7 @@
 
   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 @@
     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
@@ -70,6 +68,14 @@
     else
       buffers.add(buffer);
     return buffer;
+  }
+
+  final synchronized byte[] getBuffer(int index) {
+    return (byte[]) buffers.get(index);
+  }
+
+  final synchronized int numBuffers() {
+    return buffers.size();
   }
 
   /**

Modified: lucene/java/trunk/src/java/org/apache/lucene/store/RAMInputStream.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/store/RAMInputStream.java?rev=598693&r1=598692&r2=598693&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/store/RAMInputStream.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/store/RAMInputStream.java Tue Nov 27 09:01:21
2007
@@ -84,11 +84,11 @@
   }
 
   private final void switchCurrentBuffer() throws IOException {
-    if (currentBufferIndex >= file.buffers.size()) {
+    if (currentBufferIndex >= file.numBuffers()) {
       // end of file reached, no more buffers left
       throw 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;

Modified: lucene/java/trunk/src/java/org/apache/lucene/store/RAMOutputStream.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/store/RAMOutputStream.java?rev=598693&r1=598692&r2=598693&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/store/RAMOutputStream.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/store/RAMOutputStream.java Tue Nov 27 09:01:21
2007
@@ -63,7 +63,7 @@
       if (nextPos > end) {                        // at the last buffer
         length = (int)(end - pos);
       }
-      out.writeBytes((byte[])file.buffers.get(buffer++), length);
+      out.writeBytes((byte[])file.getBuffer(buffer++), length);
       pos = nextPos;
     }
   }
@@ -124,10 +124,10 @@
   }
 
   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;



Mime
View raw message