[infinispan-commits] Infinispan SVN: r2185 - in trunk/lucene-directory: src/main/java/org/infinispan/lucene and 1 other directories.
infinispan-commits at lists.jboss.org
infinispan-commits at lists.jboss.org
Mon Aug 9 17:16:39 EDT 2010
Author: sannegrinovero
Date: 2010-08-09 17:16:39 -0400 (Mon, 09 Aug 2010)
New Revision: 2185
Added:
trunk/lucene-directory/src/main/java/org/infinispan/lucene/LuceneKey2StringMapper.java
trunk/lucene-directory/src/test/java/org/infinispan/lucene/DatabaseStoredIndexTest.java
Modified:
trunk/lucene-directory/pom.xml
Log:
[ISPN-584] (Provide a Key2StringMapper for the Lucene Directory) trunk
Modified: trunk/lucene-directory/pom.xml
===================================================================
--- trunk/lucene-directory/pom.xml 2010-08-09 21:11:33 UTC (rev 2184)
+++ trunk/lucene-directory/pom.xml 2010-08-09 21:16:39 UTC (rev 2185)
@@ -28,14 +28,36 @@
</dependency>
<dependency>
+ <groupId>org.infinispan</groupId>
+ <artifactId>infinispan-cachestore-jdbc</artifactId>
+ <version>${project.version}</version>
+ <optional>true</optional>
+ </dependency>
+
+ <dependency>
<groupId>${project.groupId}</groupId>
<artifactId>infinispan-core</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
+
+ <dependency>
+ <groupId>org.infinispan</groupId>
+ <artifactId>infinispan-cachestore-jdbc</artifactId>
+ <version>${project.version}</version>
+ <type>test-jar</type>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>com.h2database</groupId>
+ <artifactId>h2</artifactId>
+ <version>${version.h2.driver}</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
-
+
<build>
<plugins>
<plugin>
Added: trunk/lucene-directory/src/main/java/org/infinispan/lucene/LuceneKey2StringMapper.java
===================================================================
--- trunk/lucene-directory/src/main/java/org/infinispan/lucene/LuceneKey2StringMapper.java (rev 0)
+++ trunk/lucene-directory/src/main/java/org/infinispan/lucene/LuceneKey2StringMapper.java 2010-08-09 21:16:39 UTC (rev 2185)
@@ -0,0 +1,50 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.infinispan.lucene;
+
+import org.infinispan.loaders.jdbc.stringbased.JdbcStringBasedCacheStoreConfig;
+import org.infinispan.loaders.jdbc.stringbased.Key2StringMapper;
+
+/**
+ * To configure a JdbcStringBasedCacheStoreConfig for the Lucene Directory, use this
+ * Key2StringMapper implementation.
+ *
+ * @see JdbcStringBasedCacheStoreConfig#setKey2StringMapperClass(String)
+ *
+ * @author Sanne Grinovero
+ * @since 4.1
+ */
+public class LuceneKey2StringMapper implements Key2StringMapper {
+
+ @Override
+ public boolean isSupportedType(Class keyType) {
+ return (keyType == ChunkCacheKey.class ||
+ keyType == FileCacheKey.class ||
+ keyType == FileListCacheKey.class);
+ }
+
+ @Override
+ public String getStringMapping(Object key) {
+ return key.toString();
+ }
+
+}
Added: trunk/lucene-directory/src/test/java/org/infinispan/lucene/DatabaseStoredIndexTest.java
===================================================================
--- trunk/lucene-directory/src/test/java/org/infinispan/lucene/DatabaseStoredIndexTest.java (rev 0)
+++ trunk/lucene-directory/src/test/java/org/infinispan/lucene/DatabaseStoredIndexTest.java 2010-08-09 21:16:39 UTC (rev 2185)
@@ -0,0 +1,140 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.infinispan.lucene;
+
+import java.io.IOException;
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Set;
+
+import static org.infinispan.lucene.CacheTestSupport.assertTextIsFoundInIds;
+import static org.infinispan.lucene.CacheTestSupport.removeByTerm;
+import static org.infinispan.lucene.CacheTestSupport.writeTextToIndex;
+
+import org.apache.lucene.store.Directory;
+import org.infinispan.config.CacheLoaderManagerConfig;
+import org.infinispan.config.Configuration;
+import org.infinispan.loaders.jdbc.TableManipulation;
+import org.infinispan.loaders.jdbc.connectionfactory.ConnectionFactoryConfig;
+import org.infinispan.loaders.jdbc.stringbased.JdbcStringBasedCacheStoreConfig;
+import org.infinispan.manager.EmbeddedCacheManager;
+import org.infinispan.test.SingleCacheManagerTest;
+import org.infinispan.test.fwk.TestCacheManagerFactory;
+import org.infinispan.test.fwk.UnitTestDatabaseManager;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+/**
+ * Test to verify that it's possible to use the index using a JdbcStringBasedCacheStore
+ *
+ * @see org.infinispan.loaders.jdbc.stringbased.JdbcStringBasedCacheStore
+ *
+ * @author Sanne Grinovero
+ * @since 4.1
+ */
+ at Test(groups = "functional", testName = "lucene.DatabaseStoredIndexTest")
+public class DatabaseStoredIndexTest extends SingleCacheManagerTest {
+
+ private final ConnectionFactoryConfig connectionFactoryConfig = UnitTestDatabaseManager.getUniqueConnectionFactoryConfig();
+
+ /** The INDEX_NAME */
+ private static final String INDEX_NAME = "testing index";
+
+ private final HashMap cacheCopy = new HashMap();
+
+ public DatabaseStoredIndexTest() {
+ cleanup = CleanupPhase.AFTER_METHOD;
+ }
+
+ @Override
+ protected EmbeddedCacheManager createCacheManager() throws Exception {
+ Configuration configuration = CacheTestSupport.createTestConfiguration();
+ enableTestJdbcStorage(configuration);
+ return TestCacheManagerFactory.createClusteredCacheManager(configuration);
+ }
+
+ private void enableTestJdbcStorage(Configuration configuration) {
+ TableManipulation tm = UnitTestDatabaseManager.buildDefaultTableManipulation();
+ JdbcStringBasedCacheStoreConfig jdbcStoreConfiguration = new JdbcStringBasedCacheStoreConfig(connectionFactoryConfig, tm);
+ jdbcStoreConfiguration.setKey2StringMapperClass(LuceneKey2StringMapper.class.getName());
+ CacheLoaderManagerConfig loaderManagerConfig = configuration.getCacheLoaderManagerConfig();
+ loaderManagerConfig.setPreload(false); // TODO change after ISPN-579
+ loaderManagerConfig.addCacheLoaderConfig(jdbcStoreConfiguration);
+ }
+
+ @Test
+ public void testIndexUsage() throws IOException, ClassNotFoundException, SQLException {
+ cache = cacheManager.getCache();
+ Directory dir = new InfinispanDirectory(cache, INDEX_NAME);
+ writeTextToIndex(dir, 0, "hello database");
+ assertTextIsFoundInIds(dir, "hello", 0);
+ writeTextToIndex(dir, 1, "you have to store my index segments");
+ writeTextToIndex(dir, 2, "so that I can shut down all nodes");
+ writeTextToIndex(dir, 3, "and restart later keeping the index around");
+ assertTextIsFoundInIds(dir, "index", 1, 3);
+ removeByTerm(dir, "and");
+ assertTextIsFoundInIds(dir, "index", 1);
+ dir.close();
+ cacheCopy.putAll(cache);
+ cache.stop();
+ cacheManager.stop();
+ }
+
+ @Test(dependsOnMethods="testIndexUsage")
+ public void indexWasStored() throws IOException {
+ cache = cacheManager.getCache();
+ assert cache.isEmpty();
+ Directory dir = new InfinispanDirectory(cache, INDEX_NAME);
+ assertTextIsFoundInIds(dir, "index", 1);
+ dir.close();
+ boolean failed = false;
+ Set<FileReadLockKey> keysToRemove = new HashSet<FileReadLockKey>();
+ for (Object key : cacheCopy.keySet()) {
+ if (key instanceof FileReadLockKey) {
+ // ignore until ISPN-581 defines a behaviour
+ keysToRemove.add((FileReadLockKey) key);
+ }
+ else {
+ Object expected = cacheCopy.get(key);
+ Object actual = cache.get(key);
+ if (expected==null && actual==null)
+ continue;
+ if (expected instanceof byte[]){
+ expected = Arrays.toString((byte[]) expected);
+ actual = Arrays.toString((byte[]) actual);
+ }
+ if (!expected.equals(actual)) {
+ System.out.println("Failure on key["+key.toString()+"] expected value:\n"+expected+"\nactual value:\n"+actual);
+ failed = true;
+ }
+ }
+ }
+ Assert.assertFalse(failed);
+ for (FileReadLockKey key : keysToRemove){
+ Assert.assertNotNull(cacheCopy.remove(key));
+ }
+ Assert.assertEquals(cacheCopy.keySet().size(), cache.keySet().size(), "have a different number of keys");
+ }
+
+}
More information about the infinispan-commits
mailing list