[infinispan-commits] Infinispan SVN: r1221 - in trunk/query/src: test/java/org/infinispan/query/blackbox and 1 other directory.
infinispan-commits at lists.jboss.org
infinispan-commits at lists.jboss.org
Wed Nov 25 11:09:07 EST 2009
Author: navssurtani
Date: 2009-11-25 11:09:06 -0500 (Wed, 25 Nov 2009)
New Revision: 1221
Modified:
trunk/query/src/main/java/org/infinispan/query/backend/QueryInterceptor.java
trunk/query/src/test/java/org/infinispan/query/blackbox/AbstractLocalQueryTest.java
trunk/query/src/test/java/org/infinispan/query/blackbox/ClusteredCacheTest.java
Log:
[ISPN-288] Bug fixed. Tests written and pass.
Modified: trunk/query/src/main/java/org/infinispan/query/backend/QueryInterceptor.java
===================================================================
--- trunk/query/src/main/java/org/infinispan/query/backend/QueryInterceptor.java 2009-11-25 15:02:30 UTC (rev 1220)
+++ trunk/query/src/main/java/org/infinispan/query/backend/QueryInterceptor.java 2009-11-25 16:09:06 UTC (rev 1221)
@@ -4,6 +4,7 @@
import org.hibernate.search.backend.Work;
import org.hibernate.search.backend.WorkType;
import org.hibernate.search.engine.SearchFactoryImplementor;
+import org.infinispan.commands.write.ClearCommand;
import org.infinispan.commands.write.PutKeyValueCommand;
import org.infinispan.commands.write.PutMapCommand;
import org.infinispan.commands.write.RemoveCommand;
@@ -15,7 +16,9 @@
import static org.infinispan.query.KeyTransformationHandler.keyToString;
import javax.transaction.TransactionManager;
+import java.io.Serializable;
import java.util.Map;
+import java.util.Set;
/**
* This interceptor will be created when the System Property "infinispan.query.indexLocalOnly" is "false"
@@ -53,7 +56,7 @@
// do the actual put first.
Object toReturn = invokeNextInterceptor(ctx, command);
- if (shouldModifyIndexes(ctx)) {
+ if (shouldModifyIndexes(ctx)) {
addToIndexes(extractValue(command.getValue()), extractValue(command.getKey()));
}
@@ -64,7 +67,7 @@
@Override
public Object visitRemoveCommand(InvocationContext ctx, RemoveCommand command) throws Throwable {
- if (log.isDebugEnabled()) log.debug("Entered the searchable core interceptor visitRemoveCommand()");
+ if (log.isDebugEnabled()) log.debug("Entered QueryInterceptor.visitRemoveCommand()");
// remove the object out of the cache first.
Object valueRemoved = invokeNextInterceptor(ctx, command);
@@ -83,7 +86,7 @@
@Override
public Object visitReplaceCommand(InvocationContext ctx, ReplaceCommand command) throws Throwable {
- if (log.isDebugEnabled()) log.debug("Entered the searchable core interceptor visitReplaceCommand()");
+ if (log.isDebugEnabled()) log.debug("Entered the QueryInterceptor.visitReplaceCommand()");
Object valueReplaced = invokeNextInterceptor(ctx, command);
if (valueReplaced != null && shouldModifyIndexes(ctx)) {
@@ -101,7 +104,7 @@
@Override
public Object visitPutMapCommand(InvocationContext ctx, PutMapCommand command) throws Throwable {
- if (log.isDebugEnabled()) log.debug("Entered searchable core interceptor visitPutMapCommand()");
+ if (log.isDebugEnabled()) log.debug("Entered the QueryInterceptor.visitPutMapCommand()");
Object mapPut = invokeNextInterceptor(ctx, command);
@@ -117,7 +120,30 @@
return mapPut;
}
+ @Override
+ public Object visitClearCommand(InvocationContext ctx, ClearCommand command) throws Throwable {
+ if (log.isDebugEnabled()) log.debug("Entered the QueryInterceptor.visitClearCommand()");
+
+ Object returnValue = invokeNextInterceptor(ctx, command);
+
+ if (shouldModifyIndexes(ctx)) {
+ if (log.isDebugEnabled()) log.debug("shouldModifyIndexes() is true and we can clear the indexes");
+
+ Set<Class<?>> classes = searchFactory.getDocumentBuildersIndexedEntities().keySet();
+ for (Class c : classes) {
+ Serializable id = null;
+
+ if (log.isDebugEnabled()) log.debug("Clearing indexes for class: - " + c);
+
+ searchFactory.getWorker().performWork(new Work<Object>(c, id, WorkType.PURGE_ALL),
+ new TransactionalEventTransactionContext(transactionManager));
+ }
+ }
+ return returnValue;
+ }
+
+
// Method that will be called when data needs to be added into Lucene.
protected void addToIndexes(Object value, Object key) {
if (log.isDebugEnabled()) log.debug("Adding to indexes for key [{0}] and value [{0}]", key, value);
Modified: trunk/query/src/test/java/org/infinispan/query/blackbox/AbstractLocalQueryTest.java
===================================================================
--- trunk/query/src/test/java/org/infinispan/query/blackbox/AbstractLocalQueryTest.java 2009-11-25 15:02:30 UTC (rev 1220)
+++ trunk/query/src/test/java/org/infinispan/query/blackbox/AbstractLocalQueryTest.java 2009-11-25 16:09:06 UTC (rev 1221)
@@ -8,6 +8,7 @@
import org.apache.lucene.search.PrefixFilter;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.TermQuery;
import org.infinispan.Cache;
import org.infinispan.query.CacheQuery;
import org.infinispan.query.QueryFactory;
@@ -217,4 +218,29 @@
assert cacheQuery.getResultSize() == 1;
}
+
+ public void testClear() throws ParseException{
+
+ // Create a term that will return me everyone called Navin.
+ Term navin = new Term("name", "navin");
+
+ // Create a term that I know will return me everything with name goat.
+ Term goat = new Term ("name", "goat");
+
+ Query[] queries = new Query[2];
+ queries[0] = new TermQuery(goat);
+ queries[1] = new TermQuery(navin);
+
+ luceneQuery = queries[0].combine(queries);
+ cacheQuery = new QueryFactory(cache, qh).getQuery(luceneQuery);
+
+ // We know that we've got all 3 hits.
+ assert cacheQuery.getResultSize() == 3;
+
+ cache.clear();
+
+ cacheQuery = new QueryFactory(cache, qh).getQuery(luceneQuery);
+
+ assert cacheQuery.getResultSize() == 0;
+ }
}
Modified: trunk/query/src/test/java/org/infinispan/query/blackbox/ClusteredCacheTest.java
===================================================================
--- trunk/query/src/test/java/org/infinispan/query/blackbox/ClusteredCacheTest.java 2009-11-25 15:02:30 UTC (rev 1220)
+++ trunk/query/src/test/java/org/infinispan/query/blackbox/ClusteredCacheTest.java 2009-11-25 16:09:06 UTC (rev 1221)
@@ -8,13 +8,14 @@
import org.infinispan.config.Configuration;
import org.infinispan.query.CacheQuery;
import org.infinispan.query.QueryFactory;
+import org.infinispan.query.backend.QueryHelper;
import org.infinispan.query.helper.TestQueryHelperFactory;
-import org.infinispan.query.backend.QueryHelper;
import org.infinispan.query.test.Person;
import org.infinispan.test.MultipleCacheManagersTest;
import org.infinispan.test.TestingUtil;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
+import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
@@ -57,7 +58,7 @@
}
@BeforeMethod
- public void setUp(){
+ public void setUp() {
// We will put objects into cache1 and then try and run the queries on cache2. This would mean that indexLocal
// must be set to false.
@@ -86,10 +87,15 @@
cache1.put(key1, person1);
cache1.put(key2, person2);
cache1.put(key3, person3);
+ }
-
+ @AfterMethod(alwaysRun = true)
+ public void tearDown() {
+ if (cache1 != null) cache1.stop();
+ if (cache2 != null) cache2.stop();
}
+
public void testSimple() throws ParseException {
cacheQuery = new QueryFactory(cache2, qh)
.getBasicQuery("blurb", "playing");
@@ -196,7 +202,40 @@
}
+ public void testClear() throws ParseException {
+ queryParser = new QueryParser("blurb", new StandardAnalyzer());
+ luceneQuery = queryParser.parse("eats");
+ cacheQuery = new QueryFactory(cache1, qh).getQuery(luceneQuery);
+ found = cacheQuery.list();
+
+
+ Query[] queries = new Query[2];
+ queries[0] = luceneQuery;
+
+ luceneQuery = queryParser.parse("playing");
+ queries[1] = luceneQuery;
+
+ luceneQuery = luceneQuery.combine(queries);
+
+ cacheQuery = new QueryFactory(cache1, qh).getQuery(luceneQuery);
+ assert cacheQuery.getResultSize() == 3;
+
+ // run the same query on cache 2
+ cacheQuery = new QueryFactory(cache2, qh).getQuery(luceneQuery);
+ assert cacheQuery.getResultSize() == 3;
+
+ cache1.clear();
+ cacheQuery = new QueryFactory(cache1, qh).getQuery(luceneQuery);
+ assert cacheQuery.getResultSize() == 0;
+
+ // run the same query on cache 2
+ cacheQuery = new QueryFactory(cache2, qh).getQuery(luceneQuery);
+ assert cacheQuery.getResultSize() == 0;
+
+ }
+
+
}
More information about the infinispan-commits
mailing list