[infinispan-commits] Infinispan SVN: r2210 - in trunk/lucene-directory/src: test/java/org/infinispan/lucene and 1 other directories.

infinispan-commits at lists.jboss.org infinispan-commits at lists.jboss.org
Thu Aug 12 11:30:57 EDT 2010


Author: sannegrinovero
Date: 2010-08-12 11:30:57 -0400 (Thu, 12 Aug 2010)
New Revision: 2210

Added:
   trunk/lucene-directory/src/test/java/org/infinispan/lucene/Key2StringMapperTest.java
Modified:
   trunk/lucene-directory/src/main/java/org/infinispan/lucene/ChunkCacheKey.java
   trunk/lucene-directory/src/main/java/org/infinispan/lucene/FileCacheKey.java
   trunk/lucene-directory/src/main/java/org/infinispan/lucene/FileListCacheKey.java
   trunk/lucene-directory/src/main/java/org/infinispan/lucene/FileReadLockKey.java
   trunk/lucene-directory/src/main/java/org/infinispan/lucene/InfinispanDirectory.java
   trunk/lucene-directory/src/main/java/org/infinispan/lucene/LuceneKey2StringMapper.java
   trunk/lucene-directory/src/test/java/org/infinispan/lucene/DatabaseStoredIndexTest.java
   trunk/lucene-directory/src/test/java/org/infinispan/lucene/profiling/CacheStoreStressTest.java
Log:
[ISPN-592] (Update the LuceneKey2StringMapper to implement TwoWayKey2StringMapper) - trunk

Modified: trunk/lucene-directory/src/main/java/org/infinispan/lucene/ChunkCacheKey.java
===================================================================
--- trunk/lucene-directory/src/main/java/org/infinispan/lucene/ChunkCacheKey.java	2010-08-12 15:29:46 UTC (rev 2209)
+++ trunk/lucene-directory/src/main/java/org/infinispan/lucene/ChunkCacheKey.java	2010-08-12 15:30:57 UTC (rev 2210)
@@ -41,6 +41,8 @@
    private final int hashCode;
 
    public ChunkCacheKey(String indexName, String fileName, int chunkId) {
+      if (fileName == null)
+         throw new IllegalArgumentException("filename must not be null");
       this.indexName = indexName;
       this.fileName = fileName;
       this.chunkId = chunkId;
@@ -81,11 +83,9 @@
 
    private int generatedHashCode() {
       final int prime = 31;
-      int result = 1;
-      result = prime * result + chunkId;
-      result = prime * result + ((fileName == null) ? 0 : fileName.hashCode());
-      result = prime * result + ((indexName == null) ? 0 : indexName.hashCode());
-      return result;
+      int result = prime + chunkId;
+      result = prime * result + fileName.hashCode();
+      return prime * result + indexName.hashCode();
    }
 
    @Override
@@ -99,22 +99,18 @@
       ChunkCacheKey other = (ChunkCacheKey) obj;
       if (chunkId != other.chunkId)
          return false;
-      if (fileName == null) {
-         if (other.fileName != null)
-            return false;
-      } else if (!fileName.equals(other.fileName))
+      if (!fileName.equals(other.fileName))
          return false;
-      if (indexName == null) {
-         if (other.indexName != null)
-            return false;
-      } else if (!indexName.equals(other.indexName))
-         return false;
-      return true;
+      return indexName.equals(other.indexName);
    }
 
+   /**
+    * Changing the encoding could break backwards compatibility
+    * @see LuceneKey2StringMapper#getKeyMapping(String)
+    */
    @Override
    public String toString() {
-      return "ChunkCacheKey{chunkId=" + chunkId + ", fileName=" + fileName + ", indexName=" + indexName + "} ";
+      return fileName + "|" + chunkId + "|" + indexName;
    }
    
 }

Modified: trunk/lucene-directory/src/main/java/org/infinispan/lucene/FileCacheKey.java
===================================================================
--- trunk/lucene-directory/src/main/java/org/infinispan/lucene/FileCacheKey.java	2010-08-12 15:29:46 UTC (rev 2209)
+++ trunk/lucene-directory/src/main/java/org/infinispan/lucene/FileCacheKey.java	2010-08-12 15:30:57 UTC (rev 2210)
@@ -40,6 +40,8 @@
    private final int hashCode;
 
    public FileCacheKey(String indexName, String fileName) {
+      if (fileName == null)
+         throw new IllegalArgumentException("filename must not be null");
       this.indexName = indexName;
       this.fileName = fileName;
       this.hashCode = generatedHashCode();
@@ -70,10 +72,8 @@
    
    private int generatedHashCode() {
       final int prime = 31;
-      int result = 1;
-      result = prime * result + ((fileName == null) ? 0 : fileName.hashCode());
-      result = prime * result + ((indexName == null) ? 0 : indexName.hashCode());
-      return result;
+      int result = prime + fileName.hashCode();
+      return prime * result + indexName.hashCode();
    }
 
    @Override
@@ -85,22 +85,18 @@
       if (FileCacheKey.class != obj.getClass())
          return false;
       FileCacheKey other = (FileCacheKey) obj;
-      if (fileName == null) {
-         if (other.fileName != null)
-            return false;
-      } else if (!fileName.equals(other.fileName))
+      if (!fileName.equals(other.fileName))
          return false;
-      if (indexName == null) {
-         if (other.indexName != null)
-            return false;
-      } else if (!indexName.equals(other.indexName))
-         return false;
-      return true;
+      return indexName.equals(other.indexName);
    }
    
+   /**
+    * Changing the encoding could break backwards compatibility
+    * @see LuceneKey2StringMapper#getKeyMapping(String)
+    */
    @Override
    public String toString() {
-      return "FileCacheKey{fileName='" + fileName + "', indexName='" + indexName + '}';
+      return fileName + "|M|"+ indexName;
    }
 
 }

Modified: trunk/lucene-directory/src/main/java/org/infinispan/lucene/FileListCacheKey.java
===================================================================
--- trunk/lucene-directory/src/main/java/org/infinispan/lucene/FileListCacheKey.java	2010-08-12 15:29:46 UTC (rev 2209)
+++ trunk/lucene-directory/src/main/java/org/infinispan/lucene/FileListCacheKey.java	2010-08-12 15:30:57 UTC (rev 2210)
@@ -58,10 +58,7 @@
    }
 
    private int generatedHashCode() {
-      final int prime = 31;
-      int result = 1;
-      result = prime * result + ((indexName == null) ? 0 : indexName.hashCode());
-      return result;
+      return 31 + indexName.hashCode();
    }
 
    @Override
@@ -73,17 +70,16 @@
       if (FileListCacheKey.class != obj.getClass())
          return false;
       FileListCacheKey other = (FileListCacheKey) obj;
-      if (indexName == null) {
-         if (other.indexName != null)
-            return false;
-      } else if (!indexName.equals(other.indexName))
-         return false;
-      return true;
+      return indexName.equals(other.indexName);
    }
    
+   /**
+    * Changing the encoding could break backwards compatibility
+    * @see LuceneKey2StringMapper#getKeyMapping(String)
+    */
    @Override
    public String toString() {
-      return "FileListCacheKey{indexName='" + indexName + "'}";
+      return "*|" + indexName;
    }
    
 }

Modified: trunk/lucene-directory/src/main/java/org/infinispan/lucene/FileReadLockKey.java
===================================================================
--- trunk/lucene-directory/src/main/java/org/infinispan/lucene/FileReadLockKey.java	2010-08-12 15:29:46 UTC (rev 2209)
+++ trunk/lucene-directory/src/main/java/org/infinispan/lucene/FileReadLockKey.java	2010-08-12 15:30:57 UTC (rev 2210)
@@ -77,8 +77,7 @@
    private int generateHashCode() {
       final int prime = 31;
       int result = prime + fileName.hashCode();
-      result = prime * result + indexName.hashCode();
-      return result;
+      return prime * result + indexName.hashCode();
    }
 
    @Override
@@ -92,14 +91,12 @@
       FileReadLockKey other = (FileReadLockKey) obj;
       if (!fileName.equals(other.fileName))
          return false;
-      if (!indexName.equals(other.indexName))
-         return false;
-      return true;
+      return indexName.equals(other.indexName);
    }
 
    @Override
    public String toString() {
-      return "FileReadLockKey{fileName=" + fileName + ", indexName=" + indexName + "} ";
+      return fileName + "|RL|"+ indexName;
    }
 
 }

Modified: trunk/lucene-directory/src/main/java/org/infinispan/lucene/InfinispanDirectory.java
===================================================================
--- trunk/lucene-directory/src/main/java/org/infinispan/lucene/InfinispanDirectory.java	2010-08-12 15:29:46 UTC (rev 2209)
+++ trunk/lucene-directory/src/main/java/org/infinispan/lucene/InfinispanDirectory.java	2010-08-12 15:30:57 UTC (rev 2210)
@@ -70,6 +70,14 @@
    private final FileListCacheKey fileListCacheKey;
 
    public InfinispanDirectory(Cache cache, String indexName, LockFactory lf, int chunkSize) {
+      if (cache == null)
+         throw new IllegalArgumentException("Cache must not be null");
+      if (indexName == null)
+         throw new IllegalArgumentException("index name must not be null");
+      if (lf == null)
+         throw new IllegalArgumentException("LockFactory must not be null");
+      if (chunkSize<=0)
+         throw new IllegalArgumentException("chunkSize must be a non-null positive integer");
       this.cache = cache.getAdvancedCache();
       this.indexName = indexName;
       this.setLockFactory(lf);

Modified: trunk/lucene-directory/src/main/java/org/infinispan/lucene/LuceneKey2StringMapper.java
===================================================================
--- trunk/lucene-directory/src/main/java/org/infinispan/lucene/LuceneKey2StringMapper.java	2010-08-12 15:29:46 UTC (rev 2209)
+++ trunk/lucene-directory/src/main/java/org/infinispan/lucene/LuceneKey2StringMapper.java	2010-08-12 15:30:57 UTC (rev 2210)
@@ -21,8 +21,10 @@
  */
 package org.infinispan.lucene;
 
+import java.util.regex.Pattern;
+
 import org.infinispan.loaders.jdbc.stringbased.JdbcStringBasedCacheStoreConfig;
-import org.infinispan.loaders.jdbc.stringbased.Key2StringMapper;
+import org.infinispan.loaders.jdbc.stringbased.TwoWayKey2StringMapper;
 
 /**
  * To configure a JdbcStringBasedCacheStoreConfig for the Lucene Directory, use this
@@ -33,13 +35,20 @@
  * @author Sanne Grinovero
  * @since 4.1
  */
-public class LuceneKey2StringMapper implements Key2StringMapper {
-   
+public class LuceneKey2StringMapper implements TwoWayKey2StringMapper {
+
+   /**
+    * The pipe character was chosen as it's illegal to have a pipe in a filename, so we only have to
+    * check for the indexnames.
+    */
+   static final Pattern singlePipePattern = Pattern.compile("\\|");
+
    @Override
    public boolean isSupportedType(Class keyType) {
-      return (keyType == ChunkCacheKey.class ||
-             keyType == FileCacheKey.class ||
-             keyType == FileListCacheKey.class);
+      return (keyType == ChunkCacheKey.class    ||
+              keyType == FileCacheKey.class     ||
+              keyType == FileListCacheKey.class ||
+              keyType == FileReadLockKey.class);
    }
 
    @Override
@@ -47,4 +56,46 @@
       return key.toString();
    }
 
+   /**
+    * This method has to perform the inverse transformation of the keys used in the Lucene
+    * Directory from String to object. So this implementation is strongly coupled to the 
+    * toString method of each key type.
+    * 
+    * @see ChunkCacheKey#toString()
+    * @see FileCacheKey#toString()
+    * @see FileListCacheKey#toString()
+    * @see FileReadLockKey#toString()
+    */
+   @Override
+   public Object getKeyMapping(String key) {
+      if (key == null) {
+         throw new IllegalArgumentException("Not supporting null keys");
+      }
+      // ChunkCacheKey: fileName + "|" + chunkId + "|" + indexName;
+      // FileCacheKey : fileName + "|M|"+ indexName;
+      // FileListCacheKey : "*|" + indexName;
+      // FileReadLockKey : fileName + "|RL|"+ indexName;
+      if (key.startsWith("*|")) {
+         return new FileListCacheKey(key.substring(2));
+      } else {
+         String[] split = singlePipePattern.split(key);
+         if (split.length != 3) {
+            throw new IllegalArgumentException("Unexpected format of key in String form: " + key);
+         } else {
+            if ("M".equals(split[1])) {
+               return new FileCacheKey(split[2], split[0]);
+            } else if ("RL".equals(split[1])) {
+               return new FileReadLockKey(split[2], split[0]);
+            } else {
+               try {
+                  int parsedInt = Integer.parseInt(split[1]);
+                  return new ChunkCacheKey(split[2], split[0], parsedInt);
+               } catch (NumberFormatException nfe) {
+                  throw new IllegalArgumentException("Unexpected format of key in String form: " + key, nfe);
+               }
+            }
+         }
+      }
+   }
+   
 }

Modified: trunk/lucene-directory/src/test/java/org/infinispan/lucene/DatabaseStoredIndexTest.java
===================================================================
--- trunk/lucene-directory/src/test/java/org/infinispan/lucene/DatabaseStoredIndexTest.java	2010-08-12 15:29:46 UTC (rev 2209)
+++ trunk/lucene-directory/src/test/java/org/infinispan/lucene/DatabaseStoredIndexTest.java	2010-08-12 15:30:57 UTC (rev 2210)
@@ -79,7 +79,7 @@
       JdbcStringBasedCacheStoreConfig jdbcStoreConfiguration = new JdbcStringBasedCacheStoreConfig(connectionFactoryConfig, tm);
       jdbcStoreConfiguration.setKey2StringMapperClass(LuceneKey2StringMapper.class.getName());
       CacheLoaderManagerConfig loaderManagerConfig = configuration.getCacheLoaderManagerConfig();
-      loaderManagerConfig.setPreload(false); // TODO change after ISPN-579
+      loaderManagerConfig.setPreload(false);
       loaderManagerConfig.addCacheLoaderConfig(jdbcStoreConfiguration);
    }
 

Added: trunk/lucene-directory/src/test/java/org/infinispan/lucene/Key2StringMapperTest.java
===================================================================
--- trunk/lucene-directory/src/test/java/org/infinispan/lucene/Key2StringMapperTest.java	                        (rev 0)
+++ trunk/lucene-directory/src/test/java/org/infinispan/lucene/Key2StringMapperTest.java	2010-08-12 15:30:57 UTC (rev 2210)
@@ -0,0 +1,53 @@
+package org.infinispan.lucene;
+
+import java.util.Arrays;
+
+import junit.framework.Assert;
+
+import org.testng.annotations.Test;
+
+/**
+ * Tests basic functionality of LuceneKey2StringMapper
+ * @see LuceneKey2StringMapper
+ * 
+ * @author Sanne Grinovero
+ * @since 4.1
+ */
+ at Test(groups = "functional", testName = "lucene.Key2StringMapperTest")
+public class Key2StringMapperTest {
+   
+   final LuceneKey2StringMapper mapper = new LuceneKey2StringMapper();
+   
+   @Test
+   public void testRegex() {
+      String[] split = LuceneKey2StringMapper.singlePipePattern.split("hello|world");
+      Assert.assertTrue(Arrays.deepEquals(new String[] { "hello", "world" }, split));
+   }
+   
+   @Test
+   public void loadChunkCacheKey() {
+      Assert.assertEquals(new ChunkCacheKey("my addressbook", "sgments0.gen", 34), mapper.getKeyMapping("sgments0.gen|34|my addressbook"));
+   }
+   
+   @Test
+   public void loadFileCacheKey() {
+      Assert.assertEquals(new FileCacheKey("poems and songs, 3000AC-2000DC", "filename.extension"), mapper.getKeyMapping("filename.extension|M|poems and songs, 3000AC-2000DC"));
+   }
+   
+   @Test
+   public void loadFileListCacheKey() {
+      Assert.assertEquals(new FileListCacheKey(""), mapper.getKeyMapping("*|"));
+      Assert.assertEquals(new FileListCacheKey("the leaves of Amazonia"), mapper.getKeyMapping("*|the leaves of Amazonia"));
+   }
+   
+   @Test
+   public void loadReadLockKey() {
+      Assert.assertEquals(new FileReadLockKey("poems and songs, 3000AC-2000DC", "brushed steel lock"), mapper.getKeyMapping("brushed steel lock|RL|poems and songs, 3000AC-2000DC"));
+   }
+   
+   @Test(expectedExceptions=IllegalArgumentException.class)
+   public void failureForIllegalKeys(){
+      mapper.getKeyMapping("|*|the leaves of Amazonia");
+   }
+
+}

Modified: trunk/lucene-directory/src/test/java/org/infinispan/lucene/profiling/CacheStoreStressTest.java
===================================================================
--- trunk/lucene-directory/src/test/java/org/infinispan/lucene/profiling/CacheStoreStressTest.java	2010-08-12 15:29:46 UTC (rev 2209)
+++ trunk/lucene-directory/src/test/java/org/infinispan/lucene/profiling/CacheStoreStressTest.java	2010-08-12 15:30:57 UTC (rev 2210)
@@ -64,7 +64,7 @@
       JdbcStringBasedCacheStoreConfig jdbcStoreConfiguration = new JdbcStringBasedCacheStoreConfig(connectionFactoryConfig, tm);
       jdbcStoreConfiguration.setKey2StringMapperClass(LuceneKey2StringMapper.class.getName());
       CacheLoaderManagerConfig loaderManagerConfig = configuration.getCacheLoaderManagerConfig();
-      loaderManagerConfig.setPreload(false); // TODO change after ISPN-579
+      loaderManagerConfig.setPreload(true);
       loaderManagerConfig.addCacheLoaderConfig(jdbcStoreConfiguration);
    }
 



More information about the infinispan-commits mailing list