[infinispan-commits] Infinispan SVN: r1103 - trunk/lucene-directory/src/main/java/org/infinispan/lucene.

infinispan-commits at lists.jboss.org infinispan-commits at lists.jboss.org
Wed Nov 4 17:30:20 EST 2009


Author: sannegrinovero
Date: 2009-11-04 17:30:20 -0500 (Wed, 04 Nov 2009)
New Revision: 1103

Modified:
   trunk/lucene-directory/src/main/java/org/infinispan/lucene/InfinispanDirectory.java
   trunk/lucene-directory/src/main/java/org/infinispan/lucene/InfinispanIndexIO.java
Log:
[ISPN-255] (Replace synchronized collections with proper concurrent collections in Lucene Directory)

Modified: trunk/lucene-directory/src/main/java/org/infinispan/lucene/InfinispanDirectory.java
===================================================================
--- trunk/lucene-directory/src/main/java/org/infinispan/lucene/InfinispanDirectory.java	2009-11-04 21:12:54 UTC (rev 1102)
+++ trunk/lucene-directory/src/main/java/org/infinispan/lucene/InfinispanDirectory.java	2009-11-04 22:30:20 UTC (rev 1103)
@@ -23,9 +23,8 @@
 
 import java.io.FileNotFoundException;
 import java.io.IOException;
-import java.util.Collections;
-import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.lucene.store.AlreadyClosedException;
 import org.apache.lucene.store.Directory;
@@ -57,24 +56,22 @@
    // access type will be changed in the next Lucene version
    volatile boolean isOpen = true;
 
-   private Cache<CacheKey, Object> cache;
+   private final Cache<CacheKey, Object> cache;
    // indexName is required when one common cache is used
-   private String indexName;
+   private final String indexName;
    // chunk size used in this directory, static filed not used as we want to have different chunk
    // size per dir
-   private int chunkSize;
+   private final int chunkSize;
 
+   private final FileListCacheKey fileListCacheKey;
+
    public InfinispanDirectory(Cache<CacheKey, Object> cache, String indexName, LockFactory lf, int chunkSize) {
       this.cache = cache;
       this.indexName = indexName;
       this.setLockFactory(lf);
       this.chunkSize = chunkSize;
-
-      // fixme change it to ConcurrentHashMap when [JBMAR-68] bug is fixed
-      // Infinispan does not provide efficient API (existing .keySet() is not recommended for
-      // production use) to retrieve list of available
-      // objects in cache. One entry in cache, store list of lucene file names.
-      cache.put(new FileListCacheKey(indexName), Collections.synchronizedMap(new HashMap<String, String>()));
+      this.fileListCacheKey = new FileListCacheKey(indexName);
+      cache.put(fileListCacheKey, new ConcurrentHashMap<String, String>());
       // register listener which add/remove file names to/from above list
       cache.addListener(new InfinispanCacheEntryListener(indexName));
    }
@@ -99,9 +96,9 @@
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
-   public synchronized String[] list() throws IOException {
+   public String[] list() throws IOException {
       checkIsOpen();
-      Map<String, String> filesList = (Map<String, String>) cache.get(new FileListCacheKey(indexName));
+      Map<String, String> filesList = (Map<String, String>) cache.get(fileListCacheKey);
       return (String[]) filesList.values().toArray(new String[] {});
    }
 
@@ -187,7 +184,7 @@
    /**
     * {@inheritDoc}
     */
-   public synchronized long fileLength(String name) throws IOException {
+   public long fileLength(String name) throws IOException {
       checkIsOpen();
       final FileMetadata file = getFile(indexName, name);
       if (file == null) {
@@ -199,12 +196,12 @@
    /**
     * {@inheritDoc}
     */
-   public synchronized IndexOutput createOutput(String name) throws IOException {
+   public IndexOutput createOutput(String name) throws IOException {
       final FileCacheKey key = new FileCacheKey(indexName, name);
-      if (!fileExists(name)) {
-         cache.put(key, new FileMetadata());
-      }
-      return new InfinispanIndexIO.InfinispanIndexOutput(cache, key, chunkSize);
+      FileMetadata newFileMetadata = new FileMetadata();
+      FileMetadata previous = (FileMetadata) cache.putIfAbsent(key, newFileMetadata);
+      FileMetadata usedMetadata = previous==null ? newFileMetadata : previous;
+      return new InfinispanIndexIO.InfinispanIndexOutput(cache, key, chunkSize, usedMetadata);
    }
 
    /**
@@ -222,7 +219,6 @@
       isOpen = false;
       if (cache != null) {
          cache.stop();
-         cache = null;
       }
    }
 

Modified: trunk/lucene-directory/src/main/java/org/infinispan/lucene/InfinispanIndexIO.java
===================================================================
--- trunk/lucene-directory/src/main/java/org/infinispan/lucene/InfinispanIndexIO.java	2009-11-04 21:12:54 UTC (rev 1102)
+++ trunk/lucene-directory/src/main/java/org/infinispan/lucene/InfinispanIndexIO.java	2009-11-04 22:30:20 UTC (rev 1103)
@@ -202,27 +202,21 @@
 
       private final int bufferSize;
 
-      private Cache<CacheKey, Object> cache;
-      private FileMetadata file;
-      private FileCacheKey fileKey;
+      private final Cache<CacheKey, Object> cache;
+      private final FileMetadata file;
+      private final FileCacheKey fileKey;
 
       private byte[] buffer;
       private int bufferPosition = 0;
       private int filePosition = 0;
       private int chunkNumber;
 
-      public InfinispanIndexOutput(Cache<CacheKey, Object> cache, FileCacheKey fileKey) throws IOException {
-         this(cache, fileKey, InfinispanIndexIO.DEFAULT_BUFFER_SIZE);
-      }
-
-      public InfinispanIndexOutput(Cache<CacheKey, Object> cache, FileCacheKey fileKey, int bufferSize) throws IOException {
+      public InfinispanIndexOutput(Cache<CacheKey, Object> cache, FileCacheKey fileKey, int bufferSize, FileMetadata fileMetadata) throws IOException {
          this.cache = cache;
          this.fileKey = fileKey;
          this.bufferSize = bufferSize;
-
-         buffer = new byte[this.bufferSize];
-
-         this.file = (FileMetadata) cache.get(fileKey);
+         this.buffer = new byte[this.bufferSize];
+         this.file = fileMetadata;
          if (log.isDebugEnabled()) {
             log.debug("Opened new IndexOutput for file:{0} in index: {1}", fileKey.getFileName(), fileKey.getIndexName());
          }



More information about the infinispan-commits mailing list