You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by sh...@apache.org on 2010/12/01 15:29:47 UTC

svn commit: r1041019 - in /lucene/dev/branches/branch_3x/lucene: backwards/src/test/org/apache/lucene/store/MockRAMDirectory.java src/java/org/apache/lucene/store/RAMDirectory.java

Author: shaie
Date: Wed Dec  1 14:29:47 2010
New Revision: 1041019

URL: http://svn.apache.org/viewvc?rev=1041019&view=rev
Log:
LUCENE-2779: changes include fix to backwards MockRAMDir

Modified:
    lucene/dev/branches/branch_3x/lucene/backwards/src/test/org/apache/lucene/store/MockRAMDirectory.java
    lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/store/RAMDirectory.java

Modified: lucene/dev/branches/branch_3x/lucene/backwards/src/test/org/apache/lucene/store/MockRAMDirectory.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/backwards/src/test/org/apache/lucene/store/MockRAMDirectory.java?rev=1041019&r1=1041018&r2=1041019&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/backwards/src/test/org/apache/lucene/store/MockRAMDirectory.java (original)
+++ lucene/dev/branches/branch_3x/lucene/backwards/src/test/org/apache/lucene/store/MockRAMDirectory.java Wed Dec  1 14:29:47 2010
@@ -47,6 +47,12 @@ public class MockRAMDirectory extends RA
   private Set<String> unSyncedFiles;
   private Set<String> createdFiles;
   volatile boolean crashed;
+  
+  // NOTE: BACKWARDS HACK !
+  // fileMap in RAMDir changed from HashMap to Map, and while this change is
+  // allowed (as it wasn't public API), it breaks the backwards tests. We
+  // declare a private member here, that is inited by reflection from super's.
+  private Map<String,RAMFile> fileMap;
 
   // NOTE: we cannot initialize the Map here due to the
   // order in which our constructor actually does this
@@ -54,7 +60,17 @@ public class MockRAMDirectory extends RA
   // like super is called, then our members are initialized:
   Map<String,Integer> openFiles;
 
+  @SuppressWarnings("unchecked")
   private synchronized void init() {
+    if (fileMap == null) {
+      try {
+        // NOTE: BACKWARDS HACK !
+        fileMap = (Map<String,RAMFile>) RAMDirectory.class.getDeclaredField("fileMap").get(this);
+      } catch (Exception e) {
+        throw new RuntimeException("Invalid backwards hack", e);
+      }
+    }
+
     if (openFiles == null)
       openFiles = new HashMap<String,Integer>();
     if (createdFiles == null)
@@ -214,9 +230,11 @@ public class MockRAMDirectory extends RA
       throw new IOException("file " + name + " already exists");
     else {
       if (existing!=null) {
-        //BACKWARDS BREAK in RamDirectory: sizeInBytes -= existing.sizeInBytes;
+        // BACKWARDS HACK in RamDirectory: sizeInBytes used to be a long and not
+        // AtomicLong - however since it wasn't public API, the change was
+        // allowed, but this breaks this class.
         try {
-          ((AtomicLong) getClass().getSuperclass().getDeclaredField("sizeInBytes").get(this)).getAndAdd(-existing.sizeInBytes);
+          ((AtomicLong) RAMDirectory.class.getDeclaredField("sizeInBytes").get(this)).getAndAdd(-existing.sizeInBytes);
         } catch (Exception e) {
           throw new RuntimeException("Backwards-hack failed.", e);
         }

Modified: lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/store/RAMDirectory.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/store/RAMDirectory.java?rev=1041019&r1=1041018&r2=1041019&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/store/RAMDirectory.java (original)
+++ lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/store/RAMDirectory.java Wed Dec  1 14:29:47 2010
@@ -20,8 +20,11 @@ package org.apache.lucene.store;
 import java.io.IOException;
 import java.io.FileNotFoundException;
 import java.io.Serializable;
-import java.util.HashMap;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.atomic.AtomicLong;
 
 import org.apache.lucene.index.IndexFileNameFilter;
@@ -36,7 +39,7 @@ public class RAMDirectory extends Direct
 
   private static final long serialVersionUID = 1l;
 
-  protected HashMap<String,RAMFile> fileMap = new HashMap<String,RAMFile>();
+  protected final Map<String,RAMFile> fileMap = new ConcurrentHashMap<String,RAMFile>();
   protected final AtomicLong sizeInBytes = new AtomicLong();
   
   // *****
@@ -83,25 +86,21 @@ public class RAMDirectory extends Direct
   }
 
   @Override
-  public synchronized final String[] listAll() {
+  public final String[] listAll() {
     ensureOpen();
+    // NOTE: fileMap.keySet().toArray(new String[0]) is broken in non Sun JDKs,
+    // and the code below is resilient to map changes during the array population.
     Set<String> fileNames = fileMap.keySet();
-    String[] result = new String[fileNames.size()];
-    int i = 0;
-    for(final String fileName: fileNames) 
-      result[i++] = fileName;
-    return result;
+    List<String> names = new ArrayList<String>(fileNames.size());
+    for (String name : fileNames) names.add(name);
+    return names.toArray(new String[names.size()]);
   }
 
   /** Returns true iff the named file exists in this directory. */
   @Override
   public final boolean fileExists(String name) {
     ensureOpen();
-    RAMFile file;
-    synchronized (this) {
-      file = fileMap.get(name);
-    }
-    return file != null;
+    return fileMap.containsKey(name);
   }
 
   /** Returns the time the named file was last modified.
@@ -110,12 +109,10 @@ public class RAMDirectory extends Direct
   @Override
   public final long fileModified(String name) throws IOException {
     ensureOpen();
-    RAMFile file;
-    synchronized (this) {
-      file = fileMap.get(name);
-    }
-    if (file==null)
+    RAMFile file = fileMap.get(name);
+    if (file == null) {
       throw new FileNotFoundException(name);
+    }
     return file.getLastModified();
   }
 
@@ -125,12 +122,10 @@ public class RAMDirectory extends Direct
   @Override
   public void touchFile(String name) throws IOException {
     ensureOpen();
-    RAMFile file;
-    synchronized (this) {
-      file = fileMap.get(name);
-    }
-    if (file==null)
+    RAMFile file = fileMap.get(name);
+    if (file == null) {
       throw new FileNotFoundException(name);
+    }
     
     long ts2, ts1 = System.currentTimeMillis();
     do {
@@ -151,19 +146,18 @@ public class RAMDirectory extends Direct
   @Override
   public final long fileLength(String name) throws IOException {
     ensureOpen();
-    RAMFile file;
-    synchronized (this) {
-      file = fileMap.get(name);
-    }
-    if (file==null)
+    RAMFile file = fileMap.get(name);
+    if (file == null) {
       throw new FileNotFoundException(name);
+    }
     return file.getLength();
   }
   
-  /** Return total size in bytes of all files in this
-   * directory.  This is currently quantized to
-   * RAMOutputStream.BUFFER_SIZE. */
-  public synchronized final long sizeInBytes() {
+  /**
+   * Return total size in bytes of all files in this directory. This is
+   * currently quantized to RAMOutputStream.BUFFER_SIZE.
+   */
+  public final long sizeInBytes() {
     ensureOpen();
     return sizeInBytes.get();
   }
@@ -172,14 +166,15 @@ public class RAMDirectory extends Direct
    * @throws IOException if the file does not exist
    */
   @Override
-  public synchronized void deleteFile(String name) throws IOException {
+  public void deleteFile(String name) throws IOException {
     ensureOpen();
     RAMFile file = fileMap.remove(name);
-    if (file!=null) {
-        file.directory = null;
-        sizeInBytes.addAndGet(-file.sizeInBytes);
-    } else
+    if (file != null) {
+      file.directory = null;
+      sizeInBytes.addAndGet(-file.sizeInBytes);
+    } else {
       throw new FileNotFoundException(name);
+    }
   }
 
   /** Creates a new, empty file in the directory with the given name. Returns a stream writing this file. */
@@ -187,14 +182,12 @@ public class RAMDirectory extends Direct
   public IndexOutput createOutput(String name) throws IOException {
     ensureOpen();
     RAMFile file = newRAMFile();
-    synchronized (this) {
-      RAMFile existing = fileMap.get(name);
-      if (existing!=null) {
-        sizeInBytes.addAndGet(-existing.sizeInBytes);
-        existing.directory = null;
-      }
-      fileMap.put(name, file);
+    RAMFile existing = fileMap.remove(name);
+    if (existing != null) {
+      sizeInBytes.addAndGet(-existing.sizeInBytes);
+      existing.directory = null;
     }
+    fileMap.put(name, file);
     return new RAMOutputStream(file);
   }
 
@@ -211,12 +204,10 @@ public class RAMDirectory extends Direct
   @Override
   public IndexInput openInput(String name) throws IOException {
     ensureOpen();
-    RAMFile file;
-    synchronized (this) {
-      file = fileMap.get(name);
-    }
-    if (file == null)
+    RAMFile file = fileMap.get(name);
+    if (file == null) {
       throw new FileNotFoundException(name);
+    }
     return new RAMInputStream(file);
   }
 
@@ -224,6 +215,6 @@ public class RAMDirectory extends Direct
   @Override
   public void close() {
     isOpen = false;
-    fileMap = null;
+    fileMap.clear();
   }
 }