[infinispan-commits] Infinispan SVN: r2096 - in branches/4.1.x/query/src: test/java/org/infinispan/query/blackbox and 1 other directory.
infinispan-commits at lists.jboss.org
infinispan-commits at lists.jboss.org
Fri Jul 23 07:45:52 EDT 2010
Author: navssurtani
Date: 2010-07-23 07:45:52 -0400 (Fri, 23 Jul 2010)
New Revision: 2096
Modified:
branches/4.1.x/query/src/main/java/org/infinispan/query/backend/QueryInterceptor.java
branches/4.1.x/query/src/test/java/org/infinispan/query/blackbox/AbstractLocalQueryTest.java
Log:
[ISPN-548] Backported to 4.1.x branch from trunk
Modified: branches/4.1.x/query/src/main/java/org/infinispan/query/backend/QueryInterceptor.java
===================================================================
--- branches/4.1.x/query/src/main/java/org/infinispan/query/backend/QueryInterceptor.java 2010-07-23 10:49:59 UTC (rev 2095)
+++ branches/4.1.x/query/src/main/java/org/infinispan/query/backend/QueryInterceptor.java 2010-07-23 11:45:52 UTC (rev 2096)
@@ -30,10 +30,12 @@
import org.infinispan.commands.write.PutMapCommand;
import org.infinispan.commands.write.RemoveCommand;
import org.infinispan.commands.write.ReplaceCommand;
+import org.infinispan.container.entries.CacheEntry;
import org.infinispan.context.InvocationContext;
import org.infinispan.factories.annotations.Inject;
import org.infinispan.interceptors.base.CommandInterceptor;
import org.infinispan.marshall.MarshalledValue;
+import org.infinispan.util.logging.Log;
import javax.transaction.TransactionManager;
import java.io.Serializable;
@@ -75,9 +77,26 @@
// do the actual put first.
Object toReturn = invokeNextInterceptor(ctx, command);
- if (shouldModifyIndexes(ctx))
- addToIndexes(extractValue(command.getValue()), extractValue(command.getKey()));
+ if (shouldModifyIndexes(ctx)) {
+ // First making a check to see if the key is already in the cache or not. If it isn't we can add the key no problem,
+ // otherwise we need to be updating the indexes as opposed to simply adding to the indexes.
+ Object key = command.getKey();
+ Object value = command.getValue();
+ if(log.isDebugEnabled()) log.debug("Key, value pairing is: - " + key + " + " + value);
+ CacheEntry entry = ctx.lookupEntry(key);
+ // New entry so we will add it to the indexes.
+ if(entry.isCreated()) {
+ if(log.isDebugEnabled()) log.debug("Entry is created");
+ addToIndexes(extractValue(value), extractValue(key));
+ }
+ else{
+ // This means that the entry is just modified so we need to update the indexes and not add to them.
+ if(log.isDebugEnabled()) log.debug("Entry is changed");
+ updateIndexes(extractValue(value), extractValue(key));
+ }
+
+ }
return toReturn;
}
@@ -126,6 +145,8 @@
@Override
public Object visitClearCommand(InvocationContext ctx, ClearCommand command) throws Throwable {
+
+ // This method is called when somebody calls a cache.clear() and we will need to wipe everything in the indexes.
Object returnValue = invokeNextInterceptor(ctx, command);
if (shouldModifyIndexes(ctx)) {
@@ -144,6 +165,7 @@
// Method that will be called when data needs to be added into Lucene.
+
protected void addToIndexes(Object value, Object key) {
if (trace) log.trace("Adding to indexes for key [{0}] and value [{0}]", key, value);
@@ -157,6 +179,7 @@
// Method that will be called when data needs to be removed from Lucene.
+
protected void removeFromIndexes(Object value, Object key) {
// The key here is the String representation of the key that is stored in the cache.
@@ -167,6 +190,16 @@
searchFactory.getWorker().performWork(new Work<Object>(value, keyToString(key), WorkType.DELETE), transactionContext);
}
+ protected void updateIndexes(Object value, Object key){
+ // The key here is the String representation of the key that is stored in the cache.
+ // The key is going to be the documentID for Lucene.
+ // The object parameter is the actual value that needs to be removed from lucene.
+ if (value == null) throw new NullPointerException("Cannot handle a null value!");
+ TransactionContext transactionContext = new TransactionalEventTransactionContext(transactionManager);
+ searchFactory.getWorker().performWork(new Work<Object>(value, keyToString(key), WorkType.UPDATE), transactionContext);
+
+ }
+
private Object extractValue(Object wrappedValue) {
if (wrappedValue instanceof MarshalledValue)
return ((MarshalledValue) wrappedValue).get();
Modified: branches/4.1.x/query/src/test/java/org/infinispan/query/blackbox/AbstractLocalQueryTest.java
===================================================================
--- branches/4.1.x/query/src/test/java/org/infinispan/query/blackbox/AbstractLocalQueryTest.java 2010-07-23 10:49:59 UTC (rev 2095)
+++ branches/4.1.x/query/src/test/java/org/infinispan/query/blackbox/AbstractLocalQueryTest.java 2010-07-23 11:45:52 UTC (rev 2096)
@@ -120,6 +120,7 @@
found = cacheQuery.list();
+ System.out.println("Found size is: - " + found.size());
assert found.size() == 1;
assert found.get(0).equals(person1);
}
@@ -169,14 +170,34 @@
cacheQuery = new QueryFactory(cache, qh).getQuery(luceneQuery);
found = cacheQuery.list();
- System.out.println("found is: - " + found);
assert found.size() == 1;
assert found.contains(person2);
assert !found.contains(person3) : "The search should not return person3";
+ }
+ public void testUpdated() throws ParseException{
+ queryParser = new QueryParser("name", new StandardAnalyzer());
+ luceneQuery = queryParser.parse("Goat");
+ cacheQuery = new QueryFactory(cache, qh).getQuery(luceneQuery);
+ found = cacheQuery.list();
+
+ assert found.size() == 2 : "Size of list should be 2";
+ assert found.contains(person2) : "The search should have person2";
+
+ cache.put(key2, person1);
+
+ luceneQuery = queryParser.parse("Goat");
+ cacheQuery = new QueryFactory(cache, qh).getQuery(luceneQuery);
+ found = cacheQuery.list();
+
+ assert found.size() == 1 : "Size of list should be 1";
+ assert !found.contains(person2) : "Person 2 should not be found now";
+ assert !found.contains(person1) : "Person 1 should not be found because it does not meet the search criteria";
+
}
+
public void testSetSort() throws ParseException {
person2.setAge(35);
person3.setAge(12);
More information about the infinispan-commits
mailing list