Author: navssurtani
Date: 2009-03-19 20:38:33 -0400 (Thu, 19 Mar 2009)
New Revision: 7930
Added:
searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCoreRemover.java
searchable/trunk/src/test/java/org/jboss/cache/search/blackbox/NodeRemovedTest.java
Modified:
searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCacheFactory.java
searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCoreListener.java
searchable/trunk/src/test/java/org/jboss/cache/search/blackbox/IndexingInTransaction.java
Log:
More commit noise -
Modified:
searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCacheFactory.java
===================================================================
---
searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCacheFactory.java 2009-03-19
18:08:54 UTC (rev 7929)
+++
searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCacheFactory.java 2009-03-20
00:38:33 UTC (rev 7930)
@@ -100,6 +100,10 @@
SearchableCoreListener coreListener = new SearchableCoreListener(searchFactory);
c.addCacheListener(coreListener);
+ //Create secondary listener
+ SearchableCoreRemover remover = new SearchableCoreRemover(searchFactory);
+ c.addCacheListener(remover);
+
//Create the searchableCache and pass it on.
SearchableCache sc = new SearchableCacheImpl(c, searchFactory);
return sc;
Modified:
searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCoreListener.java
===================================================================
---
searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCoreListener.java 2009-03-19
18:08:54 UTC (rev 7929)
+++
searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCoreListener.java 2009-03-20
00:38:33 UTC (rev 7930)
@@ -31,6 +31,7 @@
import org.jboss.cache.notifications.annotation.NodeRemoved;
import org.jboss.cache.notifications.event.NodeModifiedEvent;
import org.jboss.cache.notifications.event.NodeRemovedEvent;
+import org.jboss.cache.notifications.event.NodeEvent;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -49,7 +50,6 @@
public SearchableCoreListener(SearchFactoryImplementor searchFactory)
{
-
this.searchFactory = searchFactory;
}
@@ -63,8 +63,7 @@
@NodeModified
public void updateLuceneIndexes(NodeModifiedEvent event) throws InvalidKeyException
{
-
- if (log.isTraceEnabled()) log.trace("You have entered the
SearchableListener");
+ if (log.isDebugEnabled()) log.debug("You have entered the
SearchableListener");
if (!event.isPre())
{
if (log.isTraceEnabled()) log.trace("event.isPre is false. Going to start
updating indexes");
@@ -87,14 +86,13 @@
protected void handleNodeRemoved(NodeRemovedEvent event) throws InvalidKeyException
{
- if (log.isWarnEnabled()) log.debug("Entered the SCL. @NodeRemoved annotation
found");
- if (!event.isPre())
+ log.debug("Entered the SCL. @NodeRemoved annotation found");
+ if (event.isPre())
{
// Directly going to handle the deletion of data.
Map dataMap = event.getData();
if (log.isTraceEnabled()) log.trace("Called event.getData() and saved to a
Map");
-
TransactionContext ctx = new NodeRemovedTransactionContext(event);
for (Object key : dataMap.keySet())
@@ -107,15 +105,13 @@
searchFactory.getWorker().performWork(new Work(dataMap.get(key),
cacheEntityId.getDocumentId(), WorkType.DELETE), ctx);
- searchFactory.getWorker().performWork(new Work(dataMap.get(key),
cacheEntityId.getDocumentId(), WorkType.ADD), ctx);
-
if (log.isTraceEnabled())
{
log.debug("Added your object into Lucene with Fqn " + fqnString
+ " and key " + keyString);
}
}
- }
+ }
}
/**
Added: searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCoreRemover.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCoreRemover.java
(rev 0)
+++
searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCoreRemover.java 2009-03-20
00:38:33 UTC (rev 7930)
@@ -0,0 +1,64 @@
+package org.jboss.cache.search;
+
+import org.jboss.cache.notifications.annotation.CacheListener;
+import org.jboss.cache.notifications.annotation.NodeModified;
+import org.jboss.cache.notifications.annotation.NodeRemoved;
+import org.jboss.cache.notifications.event.NodeEvent;
+import org.jboss.cache.notifications.event.NodeRemovedEvent;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.hibernate.search.backend.TransactionContext;
+import org.hibernate.search.backend.Work;
+import org.hibernate.search.backend.WorkType;
+import org.hibernate.search.engine.SearchFactoryImplementor;
+
+import java.util.Map;
+
+/**
+ * @author Navin Surtani (<a
href="mailto:nsurtani@redhat.com">nsurtani@redhat.com</a>)
+ */
+@CacheListener
+public class SearchableCoreRemover
+{
+ private SearchFactoryImplementor searchFactory;
+ private static final Log log = LogFactory.getLog(SearchableCoreListener.class);
+
+ public SearchableCoreRemover(SearchFactoryImplementor searchFactory)
+ {
+ this.searchFactory = searchFactory;
+ }
+
+
+ @NodeRemoved
+ public void logEvent(NodeRemovedEvent event) throws InvalidKeyException
+ {
+ log.debug("Entered - SearchableCoreRemover with @NodeRemoved");
+ if (event.isPre())
+ {
+ System.out.println("Event isPre()");
+
+ // Directly going to handle the deletion of data.
+ Map dataMap = event.getData();
+ if (log.isTraceEnabled()) log.trace("Called event.getData() and saved to a
Map");
+
+ TransactionContext ctx = new NodeRemovedTransactionContext(event);
+
+ for (Object key : dataMap.keySet())
+ {
+ CacheEntityId cacheEntityId = new CacheEntityId(event.getFqn(), (String)
key);
+ if (log.isTraceEnabled()) log.trace("Created new CacheEntityId");
+
+ String fqnString = cacheEntityId.getFqn().toString(); // Vars for logging
+ String keyString = (String) key;
+
+
+ searchFactory.getWorker().performWork(new Work(dataMap.get(key),
cacheEntityId.getDocumentId(), WorkType.DELETE), ctx);
+
+ if (log.isTraceEnabled())
+ {
+ log.debug("Added your object into Lucene with Fqn " + fqnString
+ " and key " + keyString);
+ }
+ }
+ }
+ }
+}
Modified:
searchable/trunk/src/test/java/org/jboss/cache/search/blackbox/IndexingInTransaction.java
===================================================================
---
searchable/trunk/src/test/java/org/jboss/cache/search/blackbox/IndexingInTransaction.java 2009-03-19
18:08:54 UTC (rev 7929)
+++
searchable/trunk/src/test/java/org/jboss/cache/search/blackbox/IndexingInTransaction.java 2009-03-20
00:38:33 UTC (rev 7930)
@@ -107,7 +107,9 @@
searchableCache.removeNode(Fqn.fromString("Name15"));
List l =
searchableCache.createQuery(Entity.searchByName("Name15")).list();
+ System.out.println(l.size());
+ System.out.println("Cache listeners are " +
coreCache.getCacheListeners());
// bug: collection is not empty - there is null as there are inconsistencies in the
indexes
assertTrue("there should be no record found", l.isEmpty());
Added:
searchable/trunk/src/test/java/org/jboss/cache/search/blackbox/NodeRemovedTest.java
===================================================================
--- searchable/trunk/src/test/java/org/jboss/cache/search/blackbox/NodeRemovedTest.java
(rev 0)
+++
searchable/trunk/src/test/java/org/jboss/cache/search/blackbox/NodeRemovedTest.java 2009-03-20
00:38:33 UTC (rev 7930)
@@ -0,0 +1,59 @@
+package org.jboss.cache.search.blackbox;
+
+import org.testng.annotations.Test;
+import org.testng.annotations.BeforeMethod;
+import org.jboss.cache.search.test.Person;
+import org.jboss.cache.search.SearchableCache;
+import org.jboss.cache.search.SearchableCacheFactory;
+import org.jboss.cache.search.SearchableCoreRemover;
+import org.jboss.cache.Cache;
+import org.jboss.cache.DefaultCacheFactory;
+import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.queryParser.ParseException;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.search.Query;
+
+import java.util.List;
+
+/**
+ * @author Navin Surtani (<a
href="mailto:nsurtani@redhat.com">nsurtani@redhat.com</a>)
+ */
+
+@Test
+public class NodeRemovedTest
+{
+ Person p1 = new Person();
+ SearchableCache searchableCache;
+
+ @BeforeMethod
+ public void setUp()
+ {
+ p1.setName("Cat");
+ p1.setBlurb("Smelly");
+ p1.setAge(12);
+
+ Cache cache = new DefaultCacheFactory().createCache(true);
+ searchableCache = new SearchableCacheFactory().createSearchableCache(cache,
Person.class);
+
+ }
+
+ public void testRemoveNode() throws ParseException
+ {
+ searchableCache.put("/a", "key", p1);
+
+ QueryParser parser = new QueryParser("name", new StandardAnalyzer());
+ Query luceneQuery = parser.parse("cat");
+ List found = searchableCache.createQuery(luceneQuery, Person.class).list();
+
+ assert found.size() == 1;
+
+ searchableCache.removeNode("/a");
+
+ parser = new QueryParser("name", new StandardAnalyzer());
+ luceneQuery = parser.parse("cat");
+ found = searchableCache.createQuery(luceneQuery, Person.class).list();
+
+ assert found.size() == 0;
+ }
+
+}