JBoss Cache SVN: r6019 - core/trunk/src/test/java/org/jboss/cache/lock.
by jbosscache-commits@lists.jboss.org
Author: manik.surtani(a)jboss.com
Date: 2008-06-25 09:16:27 -0400 (Wed, 25 Jun 2008)
New Revision: 6019
Added:
core/trunk/src/test/java/org/jboss/cache/lock/AbstractLockManagerRecordingTest.java
core/trunk/src/test/java/org/jboss/cache/lock/MVCCLockManagerNoTxTest.java
core/trunk/src/test/java/org/jboss/cache/lock/MVCCLockManagerRecordingTest.java
core/trunk/src/test/java/org/jboss/cache/lock/MVCCLockManagerTest.java
core/trunk/src/test/java/org/jboss/cache/lock/NodeBasedLockManagerRecordingTest.java
Log:
Lock manager tests
Added: core/trunk/src/test/java/org/jboss/cache/lock/AbstractLockManagerRecordingTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/lock/AbstractLockManagerRecordingTest.java (rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/lock/AbstractLockManagerRecordingTest.java 2008-06-25 13:16:27 UTC (rev 6019)
@@ -0,0 +1,72 @@
+package org.jboss.cache.lock;
+
+import org.jboss.cache.Fqn;
+import org.jboss.cache.InvocationContext;
+import org.jboss.cache.NodeSPI;
+import org.jboss.cache.UnversionedNode;
+import org.jboss.cache.invocation.InvocationContextContainer;
+import org.jboss.cache.invocation.NodeInvocationDelegate;
+import static org.jboss.cache.lock.LockType.WRITE;
+import org.jboss.cache.transaction.DummyTransaction;
+import org.jboss.cache.transaction.DummyTransactionManager;
+import org.jboss.cache.transaction.GlobalTransaction;
+import org.jboss.cache.transaction.TransactionEntry;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.Test;
+
+import javax.transaction.RollbackException;
+import javax.transaction.SystemException;
+
+@Test(groups = "unit")
+public abstract class AbstractLockManagerRecordingTest
+{
+ LockManager lm;
+ InvocationContextContainer icc;
+ protected boolean fqnBasedLocking = true;
+
+ @AfterMethod
+ public void tearDown()
+ {
+ lm = null;
+ }
+
+ public void testRecordingLocksNoTx() throws InterruptedException
+ {
+ Fqn fqn = Fqn.fromString("/a/b/c");
+ NodeSPI node = createNode(fqn);
+ InvocationContext ctx = icc.get();
+
+ // lock and record.
+ lm.lockAndRecord(node, WRITE, ctx);
+ assert ctx.getLocks().contains(fqnBasedLocking ? fqn : node.getLock());
+ assert ctx.getLocks().size() == 1;
+ assert lm.isLocked(node) : "Should be locked";
+ lm.unlock(ctx);
+ assert !lm.isLocked(node) : "Should not be locked";
+ }
+
+ public void testRecordingLocksWithTx() throws InterruptedException, SystemException, RollbackException
+ {
+ Fqn fqn = Fqn.fromString("/a/b/c");
+ NodeSPI node = createNode(fqn);
+ InvocationContext ctx = icc.get();
+ ctx.setGlobalTransaction(new GlobalTransaction());
+ ctx.setTransaction(new DummyTransaction(DummyTransactionManager.getInstance()));
+ ctx.setTransactionEntry(new TransactionEntry(ctx.getTransaction()));
+
+ // lock and record.
+ lm.lockAndRecord(node, WRITE, ctx);
+ assert ctx.getLocks().contains(fqnBasedLocking ? fqn : node.getLock());
+ assert ctx.getTransactionEntry().getLocks().size() == 1;
+ assert lm.isLocked(node) : "Should be locked";
+ lm.unlock(ctx);
+ assert !lm.isLocked(node) : "Should not be locked";
+ }
+
+ protected NodeSPI createNode(Fqn fqn)
+ {
+ UnversionedNode un = new UnversionedNode(fqn);
+ un.injectDependencies(null, null, new LockStrategyFactory());
+ return new NodeInvocationDelegate(un);
+ }
+}
Added: core/trunk/src/test/java/org/jboss/cache/lock/MVCCLockManagerNoTxTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/lock/MVCCLockManagerNoTxTest.java (rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/lock/MVCCLockManagerNoTxTest.java 2008-06-25 13:16:27 UTC (rev 6019)
@@ -0,0 +1,15 @@
+package org.jboss.cache.lock;
+
+import org.testng.annotations.Test;
+
+import javax.transaction.TransactionManager;
+
+@Test(groups = "unit")
+public class MVCCLockManagerNoTxTest extends MVCCLockManagerTest
+{
+ @Override
+ protected TransactionManager getTransactionManager()
+ {
+ return null; // force the use of JDK ReentrantLocks.
+ }
+}
Added: core/trunk/src/test/java/org/jboss/cache/lock/MVCCLockManagerRecordingTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/lock/MVCCLockManagerRecordingTest.java (rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/lock/MVCCLockManagerRecordingTest.java 2008-06-25 13:16:27 UTC (rev 6019)
@@ -0,0 +1,28 @@
+package org.jboss.cache.lock;
+
+import org.jboss.cache.invocation.InvocationContextContainer;
+import org.jboss.cache.transaction.DummyTransactionManager;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import javax.transaction.TransactionManager;
+
+/**
+ * @author Manik Surtani (<a href="mailto:manik@jboss.org">manik(a)jboss.org</a>)
+ * @since 3.0
+ */
+@Test(groups = "unit")
+public class MVCCLockManagerRecordingTest extends AbstractLockManagerRecordingTest
+{
+ @BeforeMethod
+ public void setUp()
+ {
+ icc = new InvocationContextContainer();
+ MVCCLockManager mvccLockManager = new MVCCLockManager();
+ TransactionManager tm = DummyTransactionManager.getInstance();
+ mvccLockManager.injectDataContainer(null, null, tm, icc);
+ mvccLockManager.startLockManager();
+ lm = mvccLockManager;
+ }
+
+}
Added: core/trunk/src/test/java/org/jboss/cache/lock/MVCCLockManagerTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/lock/MVCCLockManagerTest.java (rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/lock/MVCCLockManagerTest.java 2008-06-25 13:16:27 UTC (rev 6019)
@@ -0,0 +1,98 @@
+package org.jboss.cache.lock;
+
+import org.jboss.cache.Fqn;
+import org.jboss.cache.NodeSPI;
+import org.jboss.cache.UnversionedNode;
+import org.jboss.cache.invocation.InvocationContextContainer;
+import org.jboss.cache.invocation.NodeInvocationDelegate;
+import static org.jboss.cache.lock.LockType.READ;
+import static org.jboss.cache.lock.LockType.WRITE;
+import org.jboss.cache.transaction.DummyTransactionManager;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import javax.transaction.TransactionManager;
+
+@Test(groups = "unit")
+public class MVCCLockManagerTest
+{
+ MVCCLockManager lm;
+ InvocationContextContainer icc;
+
+ @BeforeMethod
+ public void setUp()
+ {
+ icc = new InvocationContextContainer();
+ lm = new MVCCLockManager();
+ TransactionManager tm = getTransactionManager();
+ lm.injectDataContainer(null, null, tm, icc);
+ lm.startLockManager();
+ }
+
+ protected TransactionManager getTransactionManager()
+ {
+ return DummyTransactionManager.getInstance(); // use this to ensure the lock manager uses ownable reentrant locks
+ }
+
+ @AfterMethod
+ public void tearDown()
+ {
+ lm = null;
+ }
+
+ public void testUsingReadLocks()
+ {
+ // using any READ locks should throw an exception
+ try
+ {
+ lm.isLocked(null, READ);
+ assert false : "Should fail";
+ }
+ catch (Exception e)
+ {
+ // good
+ }
+
+ try
+ {
+ lm.getReadOwners(Fqn.ROOT);
+ assert false : "Should fail";
+ }
+ catch (Exception e)
+ {
+ // good
+ }
+
+ try
+ {
+ lm.getReadOwners((NodeSPI) null);
+ assert false : "Should fail";
+ }
+ catch (Exception e)
+ {
+ // good
+ }
+ }
+
+ public void testLockReentrancy() throws InterruptedException
+ {
+ Fqn fqn = Fqn.fromString("/a/b/c");
+ NodeSPI node = new NodeInvocationDelegate(new UnversionedNode(fqn));
+
+ assert lm.lock(fqn, WRITE, null);
+ assert lm.isLocked(node);
+
+ assert lm.lock(node, WRITE, null);
+ assert lm.isLocked(node);
+
+ lm.unlock(node, null);
+
+ assert lm.isLocked(node) : "Should still be locked";
+ assert lm.ownsLock(node, Thread.currentThread());
+
+ lm.unlock(fqn, null);
+
+ assert !lm.isLocked(node) : "Should not be locked";
+ }
+}
Added: core/trunk/src/test/java/org/jboss/cache/lock/NodeBasedLockManagerRecordingTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/lock/NodeBasedLockManagerRecordingTest.java (rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/lock/NodeBasedLockManagerRecordingTest.java 2008-06-25 13:16:27 UTC (rev 6019)
@@ -0,0 +1,21 @@
+package org.jboss.cache.lock;
+
+import org.jboss.cache.invocation.InvocationContextContainer;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+/**
+ * @author Manik Surtani (<a href="mailto:manik@jboss.org">manik(a)jboss.org</a>)
+ * @since 3.0
+ */
+@Test(groups = "unit")
+public class NodeBasedLockManagerRecordingTest extends AbstractLockManagerRecordingTest
+{
+ @BeforeMethod
+ public void setUp()
+ {
+ icc = new InvocationContextContainer();
+ lm = new NodeBasedLockManager();
+ fqnBasedLocking = false;
+ }
+}
16 years, 7 months
JBoss Cache SVN: r6018 - searchable/trunk/src/main/java/org/jboss/cache/search.
by jbosscache-commits@lists.jboss.org
Author: navssurtani
Date: 2008-06-25 06:53:33 -0400 (Wed, 25 Jun 2008)
New Revision: 6018
Added:
searchable/trunk/src/main/java/org/jboss/cache/search/QueryResultIteratorImpl.java
Modified:
searchable/trunk/src/main/java/org/jboss/cache/search/CacheQuery.java
searchable/trunk/src/main/java/org/jboss/cache/search/CacheQueryImpl.java
searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCache.java
Log:
Created QueryResultIteratorImpl and fixed CacheQueryImpl
Modified: searchable/trunk/src/main/java/org/jboss/cache/search/CacheQuery.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/CacheQuery.java 2008-06-25 09:32:57 UTC (rev 6017)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/CacheQuery.java 2008-06-25 10:53:33 UTC (rev 6018)
@@ -14,7 +14,7 @@
{
List<Object> list();
- QueryResultIterator iterator();
+ QueryResultIterator iterate();
void setFirstResult(int index);
Modified: searchable/trunk/src/main/java/org/jboss/cache/search/CacheQueryImpl.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/CacheQueryImpl.java 2008-06-25 09:32:57 UTC (rev 6017)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/CacheQueryImpl.java 2008-06-25 10:53:33 UTC (rev 6018)
@@ -5,41 +5,43 @@
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.*;
-import org.apache.lucene.search.Query;
-import org.apache.lucene.search.Filter;
-import org.hibernate.*;
-import org.hibernate.annotations.common.util.ReflectHelper;
-import org.hibernate.impl.AbstractQueryImpl;
-import org.hibernate.impl.CriteriaImpl;
-import org.hibernate.type.Type;
-import org.hibernate.transform.ResultTransformer;
+import org.hibernate.Criteria;
+import org.hibernate.HibernateException;
+import org.hibernate.LockMode;
+import org.hibernate.ScrollableResults;
import org.hibernate.search.FullTextFilter;
import org.hibernate.search.FullTextQuery;
import org.hibernate.search.SearchException;
-import org.hibernate.search.engine.*;
+import org.hibernate.search.engine.DocumentBuilder;
+import org.hibernate.search.engine.DocumentExtractor;
+import org.hibernate.search.engine.FilterDef;
+import org.hibernate.search.engine.SearchFactoryImplementor;
import org.hibernate.search.filter.ChainedFilter;
import org.hibernate.search.filter.FilterKey;
import org.hibernate.search.impl.SearchFactoryImpl;
import org.hibernate.search.query.FullTextFilterImpl;
-import org.hibernate.search.query.IteratorImpl;
import org.hibernate.search.reader.ReaderProvider;
import static org.hibernate.search.reader.ReaderProviderHelper.getIndexReaders;
import org.hibernate.search.store.DirectoryProvider;
+import org.hibernate.transform.ResultTransformer;
import org.jboss.cache.Cache;
import java.io.IOException;
-import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
-import java.util.*;
-import java.math.BigDecimal;
-import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
/**
* @author Navin Surtani - navin(a)surtani.org
* <p/>
* Implementation class of the FullTextQuery interface in Hibernate Search.
*/
-public class CacheQueryImpl extends AbstractQueryImpl implements FullTextQuery
+public class CacheQueryImpl implements CacheQuery
{
private Cache cache;
private Class[] classes;
@@ -62,7 +64,6 @@
public CacheQueryImpl(Query luceneQuery, SearchFactoryImpl searchFactory, Cache cache)
{
- super(luceneQuery.toString(), null, null, null);
this.luceneQuery = luceneQuery;
this.cache = cache;
entityLoader = new CacheEntityLoader(cache);
@@ -82,31 +83,8 @@
}
- /**
- * Allows to let lucene sort the results.
- *
- * @param sort
- * @return
- */
- public FullTextQuery setSort(Sort sort)
- {
- this.sort = sort;
- return this;
- }
/**
- * Allows to use lucene filters.
- *
- * @param filter
- * @return
- */
- public FullTextQuery setFilter(Filter filter)
- {
- this.filter = filter;
- return this;
- }
-
- /**
* @return The result size of the query.
*/
public int getResultSize()
@@ -141,6 +119,11 @@
return this.resultSize;
}
+ public void setSort(Sort s)
+ {
+ sort = s;
+ }
+
/**
* This method is not supported in JBossCache Searchable and should not be called.
*/
@@ -212,16 +195,15 @@
* @param firstResult
* @return
*/
- public FullTextQuery setFirstResult(int firstResult)
+ public void setFirstResult(int firstResult)
{
if (firstResult < 0) {
throw new IllegalArgumentException("'first' pagination parameter less than 0");
}
this.firstResult = firstResult;
- return this;
}
- public Iterator iterate() throws HibernateException
+ public QueryResultIterator iterate() throws HibernateException
{
List list;
IndexSearcher searcher = buildSearcher(searchFactory);
@@ -251,15 +233,12 @@
}
finally {
- try {
+
closeSearcher(searcher, searchFactory.getReaderProvider());
- }
- catch (SearchException e) {
- log.warn("Unable to properly close searcher during lucene query: " + getQueryString(), e);
- }
+
}
- return new SearchResultIterator(list, entityLoader);
+ return new QueryResultIteratorImpl(list, entityLoader);
}
public ScrollableResults scroll() throws HibernateException
@@ -296,11 +275,8 @@
return null;
}
- public ScrollableResults scroll(ScrollMode scrollMode) throws HibernateException
- {
- return null; // TODO: Implement me!!!
- }
+
/**
* Returns a the results from the query as a List object.
*
@@ -345,12 +321,9 @@
}
finally {
- try {
closeSearcher(searcher, searchFactory.getReaderProvider());
- }
- catch (SearchException e) {
- log.warn("Unable to properly close searcher during lucene query: " + getQueryString(), e);
- }
+
+
}
}
@@ -421,22 +394,20 @@
throw new UnsupportedOperationException(" This method is not supported in JBossCache Searchable");
}
- public FullTextQuery setMaxResults(int maxResults)
+ public void setMaxResults(int maxResults)
{
if (maxResults < 0) {
throw new IllegalArgumentException("'max' pagination parameter less than 0");
}
this.maxResults = maxResults;
- return this;
}
- public FullTextQuery setFetchSize(int fetchSize)
+ public void setFetchSize(int fetchSize)
{
if (fetchSize <= 0) {
throw new IllegalArgumentException("'fetch size' parameter less than or equals to 0");
}
this.fetchSize = fetchSize;
- return this;
}
public org.hibernate.Query setLockMode(String alias, LockMode lockMode)
Added: searchable/trunk/src/main/java/org/jboss/cache/search/QueryResultIteratorImpl.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/QueryResultIteratorImpl.java (rev 0)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/QueryResultIteratorImpl.java 2008-06-25 10:53:33 UTC (rev 6018)
@@ -0,0 +1,116 @@
+package org.jboss.cache.search;
+
+import org.jboss.cache.Cache;
+
+import java.util.List;
+
+/**
+ * This is the implementation class for the interface QueryResultIterator. It is what is returned when the iterate()
+ * method is run on a CacheQuery instance.
+ *
+ * @author Navin Surtani - navin(a)surtani.org
+ */
+public class QueryResultIteratorImpl implements QueryResultIterator
+{
+ private Cache cache;
+ private int index = 0;
+ //private final int size;
+ private Object next;
+ private int nextObjectIndex = -1;
+ private List<Object> list;
+
+ public QueryResultIteratorImpl(List list, CacheEntityLoader entityLoader)
+ {
+ this.list = list;
+ }
+
+ public void jumpToResult(int index) throws IndexOutOfBoundsException
+ {
+ //TODO: Implement
+ }
+
+ public void first()
+ {
+ //TODO: Implement
+ }
+
+ public void last()
+ {
+ //TODO: Implement
+ }
+
+ public void afterFirst()
+ {
+ //TODO: Implement
+ }
+
+ public void beforeLast()
+ {
+ //TODO: Implement
+ }
+
+ public boolean isFirst()
+ {
+ return false; //TODO: Implement
+ }
+
+ public boolean isLast()
+ {
+ return false; //TODO: Implement
+ }
+
+ public boolean isAfterFirst()
+ {
+ return false; //TODO: Implement
+ }
+
+ public boolean isBeforeLast()
+ {
+ return false; //TODO: Implement
+ }
+
+ public boolean hasNext()
+ {
+ return false; //TODO: Implement
+ }
+
+ public Object next()
+ {
+ return null; //TODO: Implement
+ }
+
+ public boolean hasPrevious()
+ {
+ return false; //TODO: Implement
+ }
+
+ public Object previous()
+ {
+ return null; //TODO: Implement
+ }
+
+ public int nextIndex()
+ {
+ return 0; //TODO: Implement
+ }
+
+ public int previousIndex()
+ {
+ return 0; //TODO: Implement
+ }
+
+ public void remove()
+ {
+ //TODO: Implement
+ }
+
+ public void set(Object o)
+ {
+ //TODO: Implement
+ }
+
+ public void add(Object o)
+ {
+ //TODO: Implement
+ }
+}
Modified: searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCache.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCache.java 2008-06-25 09:32:57 UTC (rev 6017)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCache.java 2008-06-25 10:53:33 UTC (rev 6018)
@@ -1,7 +1,6 @@
package org.jboss.cache.search;
import org.apache.lucene.search.Query;
-import org.hibernate.search.FullTextQuery;
import org.jboss.cache.Cache;
/**
@@ -17,20 +16,20 @@
{
/**
- * Creates a FullTextQuery object from a luceneQuery.
+ * Creates a CacheQuery object from a luceneQuery.
*
* @param luceneQuery
* @return
*/
- public FullTextQuery createQuery(Query luceneQuery);
+ public CacheQuery createQuery(Query luceneQuery);
/**
- * Creates a FullTextQuery from a lucene query and a class array.
+ * Creates a CacheQuery from a lucene query and a class array.
*
* @param luceneQuery
* @param classes
* @return
*/
- public FullTextQuery createQuery(Query luceneQuery, Class... classes);
+ public CacheQuery createQuery(Query luceneQuery, Class... classes);
}
16 years, 7 months
JBoss Cache SVN: r6017 - core/trunk/src/main/java/org/jboss/cache/util/concurrent/locks.
by jbosscache-commits@lists.jboss.org
Author: manik.surtani(a)jboss.com
Date: 2008-06-25 05:32:57 -0400 (Wed, 25 Jun 2008)
New Revision: 6017
Modified:
core/trunk/src/main/java/org/jboss/cache/util/concurrent/locks/OwnableReentrantLock.java
Log:
Made currentRequestor() final so it can be inlined
Modified: core/trunk/src/main/java/org/jboss/cache/util/concurrent/locks/OwnableReentrantLock.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/util/concurrent/locks/OwnableReentrantLock.java 2008-06-24 17:29:04 UTC (rev 6016)
+++ core/trunk/src/main/java/org/jboss/cache/util/concurrent/locks/OwnableReentrantLock.java 2008-06-25 09:32:57 UTC (rev 6017)
@@ -48,7 +48,7 @@
/**
* @return a GlobalTransaction instance if the current call is participating in a transaction, or the current thread otherwise.
*/
- protected Object currentRequestor()
+ protected final Object currentRequestor()
{
GlobalTransaction gtx;
return (gtx = invocationContextContainer.get().getGlobalTransaction()) == null ? Thread.currentThread() : gtx;
16 years, 7 months
JBoss Cache SVN: r6016 - core/trunk/src/test/java/org/jboss/cache/util/concurrent/locks.
by jbosscache-commits@lists.jboss.org
Author: manik.surtani(a)jboss.com
Date: 2008-06-24 13:29:04 -0400 (Tue, 24 Jun 2008)
New Revision: 6016
Modified:
core/trunk/src/test/java/org/jboss/cache/util/concurrent/locks/OwnableReentrantLockTest.java
Log:
Added unit test for OwnableReentrantLock
Modified: core/trunk/src/test/java/org/jboss/cache/util/concurrent/locks/OwnableReentrantLockTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/util/concurrent/locks/OwnableReentrantLockTest.java 2008-06-24 16:59:43 UTC (rev 6015)
+++ core/trunk/src/test/java/org/jboss/cache/util/concurrent/locks/OwnableReentrantLockTest.java 2008-06-24 17:29:04 UTC (rev 6016)
@@ -4,6 +4,11 @@
import org.jboss.cache.transaction.GlobalTransaction;
import org.testng.annotations.Test;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -19,7 +24,7 @@
assert lock.getOwner().equals(Thread.currentThread());
assert lock.getHoldCount() == 1;
- lock.lock();
+ assert lock.tryLock();
assert lock.getOwner().equals(Thread.currentThread());
assert lock.getHoldCount() == 2;
@@ -98,7 +103,9 @@
InvocationContextContainer icc = new InvocationContextContainer();
final OwnableReentrantLock lock = new OwnableReentrantLock(icc);
final AtomicBoolean acquired = new AtomicBoolean(false);
+ final AtomicBoolean threwExceptionOnRelease = new AtomicBoolean(false);
+
lock.lock();
assert lock.getOwner().equals(Thread.currentThread());
assert lock.getHoldCount() == 1;
@@ -115,6 +122,16 @@
{
// do nothing
}
+
+ try
+ {
+ lock.unlock();
+ }
+ catch (IllegalMonitorStateException e)
+ {
+ // expected
+ threwExceptionOnRelease.set(true);
+ }
}
};
@@ -122,6 +139,7 @@
t.join();
assert !acquired.get() : "Second thread should not have acquired lock";
+ assert threwExceptionOnRelease.get() : "Second thread should have thrown an exception trying to release lock";
lock.unlock();
assert !lock.isLocked();
@@ -135,6 +153,7 @@
gtx.setId(10);
icc.get().setGlobalTransaction(gtx);
final AtomicBoolean acquired = new AtomicBoolean(false);
+ final AtomicBoolean threwExceptionOnRelease = new AtomicBoolean(false);
lock.lock();
assert lock.getOwner().equals(gtx);
@@ -152,6 +171,16 @@
{
// do nothing
}
+
+ try
+ {
+ lock.unlock();
+ }
+ catch (IllegalMonitorStateException e)
+ {
+ // expected
+ threwExceptionOnRelease.set(true);
+ }
}
};
@@ -159,6 +188,7 @@
t.join();
assert !acquired.get() : "Second thread should not have acquired lock";
+ assert threwExceptionOnRelease.get() : "Second thread should have thrown an exception trying to release lock";
lock.unlock();
assert !lock.isLocked();
@@ -169,6 +199,7 @@
final InvocationContextContainer icc = new InvocationContextContainer();
final OwnableReentrantLock lock = new OwnableReentrantLock(icc);
final AtomicBoolean acquired = new AtomicBoolean(false);
+ final AtomicBoolean threwExceptionOnRelease = new AtomicBoolean(false);
lock.lock();
assert lock.getOwner().equals(Thread.currentThread());
@@ -189,6 +220,16 @@
{
// do nothing
}
+
+ try
+ {
+ lock.unlock();
+ }
+ catch (IllegalMonitorStateException e)
+ {
+ // expected
+ threwExceptionOnRelease.set(true);
+ }
}
};
@@ -196,6 +237,7 @@
t.join();
assert !acquired.get() : "Second thread should not have acquired lock";
+ assert threwExceptionOnRelease.get() : "Second thread should have thrown an exception trying to release lock";
lock.unlock();
assert !lock.isLocked();
@@ -206,6 +248,7 @@
final InvocationContextContainer icc = new InvocationContextContainer();
final OwnableReentrantLock lock = new OwnableReentrantLock(icc);
final AtomicBoolean acquired = new AtomicBoolean(false);
+ final AtomicBoolean threwExceptionOnRelease = new AtomicBoolean(false);
GlobalTransaction gtx = new GlobalTransaction();
gtx.setId(10);
icc.get().setGlobalTransaction(gtx);
@@ -229,6 +272,16 @@
{
// do nothing
}
+
+ try
+ {
+ lock.unlock();
+ }
+ catch (IllegalMonitorStateException e)
+ {
+ // expected
+ threwExceptionOnRelease.set(true);
+ }
}
};
@@ -236,6 +289,7 @@
t.join();
assert !acquired.get() : "Second thread should not have acquired lock";
+ assert threwExceptionOnRelease.get() : "Second thread should have thrown an exception trying to release lock";
lock.unlock();
assert !lock.isLocked();
@@ -282,4 +336,35 @@
assert !lock.isLocked();
}
+ public void satisfyCodeCoverage() throws InterruptedException
+ {
+ final InvocationContextContainer icc = new InvocationContextContainer();
+ final OwnableReentrantLock lock = new OwnableReentrantLock(icc);
+ System.out.println(lock.toString());
+ lock.lockInterruptibly();
+ System.out.println(lock.toString());
+
+ assert lock.newCondition() != null;
+ assert lock.isHeldExclusively();
+ lock.unlock();
+ assert lock.isHeldExclusively();
+ }
+
+ public void testSerialization() throws IOException, ClassNotFoundException
+ {
+ final InvocationContextContainer icc = new InvocationContextContainer();
+ final OwnableReentrantLock lock = new OwnableReentrantLock(icc);
+ lock.lock();
+ assert lock.isLocked();
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ ObjectOutputStream oos = new ObjectOutputStream(baos);
+ oos.writeObject(lock);
+ oos.close();
+ baos.close();
+ ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
+ OwnableReentrantLock l2 = (OwnableReentrantLock) ois.readObject();
+
+ assert !l2.isLocked();
+ assert l2.getOwner() == null;
+ }
}
16 years, 7 months
JBoss Cache SVN: r6015 - in core/trunk/src/test/java/org/jboss/cache/util: concurrent and 1 other directories.
by jbosscache-commits@lists.jboss.org
Author: manik.surtani(a)jboss.com
Date: 2008-06-24 12:59:43 -0400 (Tue, 24 Jun 2008)
New Revision: 6015
Added:
core/trunk/src/test/java/org/jboss/cache/util/concurrent/
core/trunk/src/test/java/org/jboss/cache/util/concurrent/locks/
core/trunk/src/test/java/org/jboss/cache/util/concurrent/locks/OwnableReentrantLockTest.java
Log:
Added unit test for OwnableReentrantLock
Added: core/trunk/src/test/java/org/jboss/cache/util/concurrent/locks/OwnableReentrantLockTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/util/concurrent/locks/OwnableReentrantLockTest.java (rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/util/concurrent/locks/OwnableReentrantLockTest.java 2008-06-24 16:59:43 UTC (rev 6015)
@@ -0,0 +1,285 @@
+package org.jboss.cache.util.concurrent.locks;
+
+import org.jboss.cache.invocation.InvocationContextContainer;
+import org.jboss.cache.transaction.GlobalTransaction;
+import org.testng.annotations.Test;
+
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+@Test(groups = "unit")
+public class OwnableReentrantLockTest
+{
+ public void testReentrancyThread()
+ {
+ InvocationContextContainer icc = new InvocationContextContainer();
+ OwnableReentrantLock lock = new OwnableReentrantLock(icc);
+
+ lock.lock(); // locked by current thread
+ assert lock.getOwner().equals(Thread.currentThread());
+ assert lock.getHoldCount() == 1;
+
+ lock.lock();
+ assert lock.getOwner().equals(Thread.currentThread());
+ assert lock.getHoldCount() == 2;
+
+ lock.unlock();
+ assert lock.getOwner().equals(Thread.currentThread());
+ assert lock.getHoldCount() == 1;
+
+ lock.unlock();
+ assert lock.getOwner() == null;
+ assert lock.getHoldCount() == 0;
+ }
+
+ public void testReentrancyGtx()
+ {
+ InvocationContextContainer icc = new InvocationContextContainer();
+ OwnableReentrantLock lock = new OwnableReentrantLock(icc);
+
+ // create and set a gtx
+ GlobalTransaction gtx = new GlobalTransaction();
+ gtx.setId(10);
+ icc.get().setGlobalTransaction(gtx);
+
+ lock.lock(); // locked by current thread
+ assert lock.getOwner().equals(gtx);
+ assert lock.getHoldCount() == 1;
+
+ lock.lock();
+ assert lock.getOwner().equals(gtx);
+ assert lock.getHoldCount() == 2;
+
+ lock.unlock();
+ assert lock.getOwner().equals(gtx);
+ assert lock.getHoldCount() == 1;
+
+ lock.unlock();
+ assert lock.getOwner() == null;
+ assert lock.getHoldCount() == 0;
+ }
+
+ public void testReentrancyNotSameGtx()
+ {
+ InvocationContextContainer icc = new InvocationContextContainer();
+ OwnableReentrantLock lock = new OwnableReentrantLock(icc);
+
+ // create and set a gtx
+ GlobalTransaction gtx = new GlobalTransaction();
+ gtx.setId(10);
+ icc.get().setGlobalTransaction(gtx);
+
+ GlobalTransaction gtx2 = new GlobalTransaction();
+ gtx2.setId(10);
+
+ assert gtx != gtx2;
+
+ lock.lock(); // locked by current thread
+ assert lock.getOwner().equals(gtx);
+ assert lock.getHoldCount() == 1;
+
+ icc.get().setGlobalTransaction(gtx2);
+ lock.lock();
+ assert lock.getOwner().equals(gtx2);
+ assert lock.getHoldCount() == 2;
+
+ lock.unlock();
+ assert lock.getOwner().equals(gtx2);
+ assert lock.getHoldCount() == 1;
+
+ icc.get().setGlobalTransaction(gtx);
+ lock.unlock();
+ assert lock.getOwner() == null;
+ assert lock.getHoldCount() == 0;
+ }
+
+ public void testThreadLockedByThread() throws InterruptedException
+ {
+ InvocationContextContainer icc = new InvocationContextContainer();
+ final OwnableReentrantLock lock = new OwnableReentrantLock(icc);
+ final AtomicBoolean acquired = new AtomicBoolean(false);
+
+ lock.lock();
+ assert lock.getOwner().equals(Thread.currentThread());
+ assert lock.getHoldCount() == 1;
+
+ Thread t = new Thread()
+ {
+ public void run()
+ {
+ try
+ {
+ acquired.set(lock.tryLock(10, TimeUnit.MILLISECONDS));
+ }
+ catch (InterruptedException e)
+ {
+ // do nothing
+ }
+ }
+ };
+
+ t.start();
+ t.join();
+
+ assert !acquired.get() : "Second thread should not have acquired lock";
+
+ lock.unlock();
+ assert !lock.isLocked();
+ }
+
+ public void testThreadLockedByGtx() throws InterruptedException
+ {
+ InvocationContextContainer icc = new InvocationContextContainer();
+ final OwnableReentrantLock lock = new OwnableReentrantLock(icc);
+ GlobalTransaction gtx = new GlobalTransaction();
+ gtx.setId(10);
+ icc.get().setGlobalTransaction(gtx);
+ final AtomicBoolean acquired = new AtomicBoolean(false);
+
+ lock.lock();
+ assert lock.getOwner().equals(gtx);
+ assert lock.getHoldCount() == 1;
+
+ Thread t = new Thread()
+ {
+ public void run()
+ {
+ try
+ {
+ acquired.set(lock.tryLock(10, TimeUnit.MILLISECONDS));
+ }
+ catch (InterruptedException e)
+ {
+ // do nothing
+ }
+ }
+ };
+
+ t.start();
+ t.join();
+
+ assert !acquired.get() : "Second thread should not have acquired lock";
+
+ lock.unlock();
+ assert !lock.isLocked();
+ }
+
+ public void testGtxLockedByThread() throws InterruptedException
+ {
+ final InvocationContextContainer icc = new InvocationContextContainer();
+ final OwnableReentrantLock lock = new OwnableReentrantLock(icc);
+ final AtomicBoolean acquired = new AtomicBoolean(false);
+
+ lock.lock();
+ assert lock.getOwner().equals(Thread.currentThread());
+ assert lock.getHoldCount() == 1;
+
+ Thread t = new Thread()
+ {
+ public void run()
+ {
+ try
+ {
+ GlobalTransaction gtx = new GlobalTransaction();
+ gtx.setId(10);
+ icc.get().setGlobalTransaction(gtx);
+ acquired.set(lock.tryLock(10, TimeUnit.MILLISECONDS));
+ }
+ catch (InterruptedException e)
+ {
+ // do nothing
+ }
+ }
+ };
+
+ t.start();
+ t.join();
+
+ assert !acquired.get() : "Second thread should not have acquired lock";
+
+ lock.unlock();
+ assert !lock.isLocked();
+ }
+
+ public void testGtxLockedByGtxFail() throws InterruptedException
+ {
+ final InvocationContextContainer icc = new InvocationContextContainer();
+ final OwnableReentrantLock lock = new OwnableReentrantLock(icc);
+ final AtomicBoolean acquired = new AtomicBoolean(false);
+ GlobalTransaction gtx = new GlobalTransaction();
+ gtx.setId(10);
+ icc.get().setGlobalTransaction(gtx);
+
+ lock.lock();
+ assert lock.getOwner().equals(gtx);
+ assert lock.getHoldCount() == 1;
+
+ Thread t = new Thread()
+ {
+ public void run()
+ {
+ try
+ {
+ GlobalTransaction gtx = new GlobalTransaction();
+ gtx.setId(20);
+ icc.get().setGlobalTransaction(gtx);
+ acquired.set(lock.tryLock(10, TimeUnit.MILLISECONDS));
+ }
+ catch (InterruptedException e)
+ {
+ // do nothing
+ }
+ }
+ };
+
+ t.start();
+ t.join();
+
+ assert !acquired.get() : "Second thread should not have acquired lock";
+
+ lock.unlock();
+ assert !lock.isLocked();
+ }
+
+ public void testGtxLockedByGtxSuccess() throws InterruptedException
+ {
+ final InvocationContextContainer icc = new InvocationContextContainer();
+ final OwnableReentrantLock lock = new OwnableReentrantLock(icc);
+ final AtomicBoolean acquired = new AtomicBoolean(false);
+ GlobalTransaction gtx = new GlobalTransaction();
+ gtx.setId(10);
+ icc.get().setGlobalTransaction(gtx);
+
+ lock.lock();
+ assert lock.getOwner().equals(gtx);
+ assert lock.getHoldCount() == 1;
+
+ Thread t = new Thread()
+ {
+ public void run()
+ {
+ try
+ {
+ GlobalTransaction gtx = new GlobalTransaction();
+ gtx.setId(10);
+ icc.get().setGlobalTransaction(gtx);
+ acquired.set(lock.tryLock(10, TimeUnit.MILLISECONDS));
+ }
+ catch (InterruptedException e)
+ {
+ // do nothing
+ }
+ }
+ };
+
+ t.start();
+ t.join();
+
+ assert acquired.get() : "Second thread should have acquired lock";
+ assert lock.getHoldCount() == 2;
+ lock.unlock();
+ lock.unlock();
+ assert !lock.isLocked();
+ }
+
+}
16 years, 7 months
JBoss Cache SVN: r6014 - in core/trunk/src/main/java/org/jboss/cache/util/concurrent: locks and 1 other directory.
by jbosscache-commits@lists.jboss.org
Author: manik.surtani(a)jboss.com
Date: 2008-06-24 11:23:45 -0400 (Tue, 24 Jun 2008)
New Revision: 6014
Added:
core/trunk/src/main/java/org/jboss/cache/util/concurrent/locks/
core/trunk/src/main/java/org/jboss/cache/util/concurrent/locks/OwnableReentrantLock.java
Log:
Added OwnableReentrantLock
Added: core/trunk/src/main/java/org/jboss/cache/util/concurrent/locks/OwnableReentrantLock.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/util/concurrent/locks/OwnableReentrantLock.java (rev 0)
+++ core/trunk/src/main/java/org/jboss/cache/util/concurrent/locks/OwnableReentrantLock.java 2008-06-24 15:23:45 UTC (rev 6014)
@@ -0,0 +1,188 @@
+package org.jboss.cache.util.concurrent.locks;
+
+import net.jcip.annotations.ThreadSafe;
+import org.jboss.cache.invocation.InvocationContextContainer;
+import org.jboss.cache.transaction.GlobalTransaction;
+
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.AbstractQueuedSynchronizer;
+import java.util.concurrent.locks.Lock;
+
+/**
+ * A lock that supports reentrancy based on owner (and not on current thread). For this to work, the lock needs to be
+ * constructed with a reference to the {@link org.jboss.cache.invocation.InvocationContextContainer}, so it is able
+ * to determine whether the caller's "owner" reference is the current thread or a {@link org.jboss.cache.transaction.GlobalTransaction}
+ * instance.
+ * <p/>
+ * This makes this lock implementation very closely tied to JBoss Cache internals, but it provides for a very clean, efficient
+ * and moreover familiar interface to work with, since it implements {@link java.util.concurrent.locks.Lock}.
+ * <p/>
+ * For the sake of performance, this lock only supports nonfair queueing.
+ * <p/>
+ *
+ * @author Manik Surtani (<a href="mailto:manik@jboss.org">manik(a)jboss.org</a>)
+ * @since 3.0
+ */
+@ThreadSafe
+public class OwnableReentrantLock extends AbstractQueuedSynchronizer implements Lock
+{
+ /**
+ * Current owner
+ */
+ transient Object owner;
+ /**
+ * Invocation context to consult when testing the current requestor
+ */
+ transient InvocationContextContainer invocationContextContainer;
+
+ /**
+ * Creates a new lock instance.
+ *
+ * @param invocationContextContainer InvocationContextContainer instance to consult for the invocation context of the call.
+ */
+ public OwnableReentrantLock(InvocationContextContainer invocationContextContainer)
+ {
+ this.invocationContextContainer = invocationContextContainer;
+ }
+
+ /**
+ * @return a GlobalTransaction instance if the current call is participating in a transaction, or the current thread otherwise.
+ */
+ protected Object currentRequestor()
+ {
+ GlobalTransaction gtx;
+ return (gtx = invocationContextContainer.get().getGlobalTransaction()) == null ? Thread.currentThread() : gtx;
+ }
+
+ public void lock()
+ {
+ if (compareAndSetState(0, 1))
+ owner = currentRequestor();
+ else
+ acquire(1);
+ }
+
+ public void lockInterruptibly() throws InterruptedException
+ {
+ acquireInterruptibly(1);
+ }
+
+ public boolean tryLock()
+ {
+ return tryAcquire(1);
+ }
+
+ public boolean tryLock(long time, TimeUnit unit) throws InterruptedException
+ {
+ return tryAcquireNanos(1, unit.toNanos(time));
+ }
+
+ public void unlock()
+ {
+ release(1);
+ }
+
+ public final ConditionObject newCondition()
+ {
+ return new ConditionObject();
+ }
+
+ @Override
+ protected final boolean tryAcquire(int acquires)
+ {
+ final Object current = currentRequestor();
+ int c = getState();
+ if (c == 0)
+ {
+ if (compareAndSetState(0, acquires))
+ {
+ owner = current;
+ return true;
+ }
+ }
+ else if (current.equals(owner))
+ {
+ setState(c + acquires);
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ protected final boolean tryRelease(int releases)
+ {
+ int c = getState() - releases;
+ if (!currentRequestor().equals(owner))
+ throw new IllegalMonitorStateException();
+ boolean free = false;
+ if (c == 0)
+ {
+ free = true;
+ owner = null;
+ }
+ setState(c);
+ return free;
+ }
+
+ @Override
+ protected final boolean isHeldExclusively()
+ {
+ return getState() != 0 && currentRequestor().equals(owner);
+ }
+
+ /**
+ * @return the owner of the lock, or null if it is currently unlocked.
+ */
+ public final Object getOwner()
+ {
+ int c = getState();
+ Object o = owner;
+ return (c == 0) ? null : o;
+ }
+
+ /**
+ * @return the hold count of the current lock, or 0 if it is not locked.
+ */
+ public final int getHoldCount()
+ {
+ int c = getState();
+ Object o = owner;
+ return (currentRequestor().equals(o)) ? c : 0;
+ }
+
+ /**
+ * @return true if the lock is locked, false otherwise
+ */
+ public final boolean isLocked()
+ {
+ return getState() != 0;
+ }
+
+ /**
+ * Reconstitute this lock instance from a stream, resetting the lock to an unlocked state.
+ *
+ * @param s the stream
+ */
+ private void readObject(java.io.ObjectInputStream s)
+ throws java.io.IOException, ClassNotFoundException
+ {
+ s.defaultReadObject();
+ setState(0); // reset to unlocked state
+ }
+
+ /**
+ * Returns a string identifying this lock, as well as its lock
+ * state. The state, in brackets, includes either the String
+ * "Unlocked" or the String "Locked by"
+ * followed by the String representation of the lock owner.
+ *
+ * @return a string identifying this lock, as well as its lock state.
+ */
+ public String toString()
+ {
+ Object owner = getOwner();
+ return super.toString() + ((owner == null) ?
+ "[Unlocked]" :
+ "[Locked by " + owner + "]");
+ }
+}
16 years, 7 months
JBoss Cache SVN: r6013 - searchable/trunk/src/main/java/org/jboss/cache/search.
by jbosscache-commits@lists.jboss.org
Author: navssurtani
Date: 2008-06-24 11:08:05 -0400 (Tue, 24 Jun 2008)
New Revision: 6013
Modified:
searchable/trunk/src/main/java/org/jboss/cache/search/CacheQuery.java
Log:
CacheQueryImpl implements CacheQuery and not FTQ
Modified: searchable/trunk/src/main/java/org/jboss/cache/search/CacheQuery.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/CacheQuery.java 2008-06-24 11:34:04 UTC (rev 6012)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/CacheQuery.java 2008-06-24 15:08:05 UTC (rev 6013)
@@ -5,7 +5,8 @@
import java.util.List;
/**
- * // TODO: Javadoc this properly - probably copy the javadoc comments from FullTextQuery since the methods will do pretty much the same thing.
+ * A cache-query is what will be returned when the createQuery() method is run. This object can have methods such
+ * as list, setFirstResult,setMaxResults, setFetchSize, getResultSize and setSort.
*
* @author Manik Surtani (<a href="mailto:manik@jboss.org">manik(a)jboss.org</a>)
*/
16 years, 7 months
JBoss Cache SVN: r6012 - searchable/trunk/src/main/java/org/jboss/cache/search.
by jbosscache-commits@lists.jboss.org
Author: manik.surtani(a)jboss.com
Date: 2008-06-24 07:34:04 -0400 (Tue, 24 Jun 2008)
New Revision: 6012
Added:
searchable/trunk/src/main/java/org/jboss/cache/search/CacheQuery.java
searchable/trunk/src/main/java/org/jboss/cache/search/QueryResultIterator.java
Log:
Added new public interfaces
Added: searchable/trunk/src/main/java/org/jboss/cache/search/CacheQuery.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/CacheQuery.java (rev 0)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/CacheQuery.java 2008-06-24 11:34:04 UTC (rev 6012)
@@ -0,0 +1,27 @@
+package org.jboss.cache.search;
+
+import org.apache.lucene.search.Sort;
+
+import java.util.List;
+
+/**
+ * // TODO: Javadoc this properly - probably copy the javadoc comments from FullTextQuery since the methods will do pretty much the same thing.
+ *
+ * @author Manik Surtani (<a href="mailto:manik@jboss.org">manik(a)jboss.org</a>)
+ */
+public interface CacheQuery
+{
+ List<Object> list();
+
+ QueryResultIterator iterator();
+
+ void setFirstResult(int index);
+
+ void setMaxResults(int numResults);
+
+ void setFetchSize(int size);
+
+ int getResultSize();
+
+ void setSort(Sort s);
+}
Added: searchable/trunk/src/main/java/org/jboss/cache/search/QueryResultIterator.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/QueryResultIterator.java (rev 0)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/QueryResultIterator.java 2008-06-24 11:34:04 UTC (rev 6012)
@@ -0,0 +1,59 @@
+package org.jboss.cache.search;
+
+import java.util.ListIterator;
+
+/**
+ * Iterates over query results
+ *
+ * @author Manik Surtani (<a href="mailto:manik@jboss.org">manik(a)jboss.org</a>)
+ */
+public interface QueryResultIterator extends ListIterator
+{
+ /**
+ * Jumps to a specific index in the iterator.
+ *
+ * @param index index to jump to.
+ * @throws IndexOutOfBoundsException if the index is out of bounds
+ */
+ void jumpToResult(int index) throws IndexOutOfBoundsException;
+
+ /**
+ * Jumps to the first result
+ */
+ void first();
+
+ /**
+ * Jumps to the last result
+ */
+ void last();
+
+ /**
+ * Jumps to the one-after-the-first result
+ */
+ void afterFirst();
+
+ /**
+ * Jumps to the one-before-the-last result
+ */
+ void beforeLast();
+
+ /**
+ * @return true if the current result is the first
+ */
+ boolean isFirst();
+
+ /**
+ * @return true if the current result is the last
+ */
+ boolean isLast();
+
+ /**
+ * @return true if the current result is one after the first
+ */
+ boolean isAfterFirst();
+
+ /**
+ * @return true if the current result is one before the last
+ */
+ boolean isBeforeLast();
+}
16 years, 7 months
JBoss Cache SVN: r6011 - in searchable/trunk: src/main/java/org/jboss/cache/search and 1 other directories.
by jbosscache-commits@lists.jboss.org
Author: navssurtani
Date: 2008-06-24 06:51:36 -0400 (Tue, 24 Jun 2008)
New Revision: 6011
Added:
searchable/trunk/src/main/java/org/jboss/cache/search/CacheEntityId.java
searchable/trunk/src/main/java/org/jboss/cache/search/CacheEntityLoader.java
searchable/trunk/src/main/java/org/jboss/cache/search/SearchResultIterator.java
Removed:
searchable/trunk/src/main/java/org/jboss/cache/search/CacheQuery.java
Modified:
searchable/trunk/pom.xml
searchable/trunk/src/main/java/org/jboss/cache/search/CacheQueryImpl.java
searchable/trunk/src/main/java/org/jboss/cache/search/InvalidFqnException.java
searchable/trunk/src/main/java/org/jboss/cache/search/NodeModifiedTransactionContext.java
searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCacheImpl.java
searchable/trunk/src/main/java/org/jboss/cache/search/SearchableListener.java
searchable/trunk/src/test/java/org/jboss/cache/search/BlackBoxTest.java
Log:
Created more classes
Modified: searchable/trunk/pom.xml
===================================================================
--- searchable/trunk/pom.xml 2008-06-23 17:38:03 UTC (rev 6010)
+++ searchable/trunk/pom.xml 2008-06-24 10:51:36 UTC (rev 6011)
@@ -27,13 +27,13 @@
<dependency>
<groupId>org.jboss.cache</groupId>
<artifactId>jbosscache-core</artifactId>
- <version>2.2.0.CR1</version>
+ <version>2.2.0.CR4</version>
</dependency>
<dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-search</artifactId>
- <version>3.0.1.GA</version>
+ <groupId>org.hibernate.sandbox</groupId>
+ <artifactId>hibernate-search-gsoc</artifactId>
+ <version>3.1.0-SNAPSHOT</version>
</dependency>
<dependency>
@@ -41,6 +41,13 @@
<artifactId>commons-logging</artifactId>
<version>1.0.4</version>
</dependency>
+
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ <version>1.4.2</version>
+ </dependency>
+
</dependencies>
<build>
<plugins>
Added: searchable/trunk/src/main/java/org/jboss/cache/search/CacheEntityId.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/CacheEntityId.java (rev 0)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/CacheEntityId.java 2008-06-24 10:51:36 UTC (rev 6011)
@@ -0,0 +1,58 @@
+package org.jboss.cache.search;
+
+import org.jboss.cache.Fqn;
+
+/**
+ @author Navin Surtani - navin(a)surtani.org
+ */
+public class CacheEntityId
+{
+ Fqn fqn;
+ String key;
+ String documentId;
+
+ public CacheEntityId(String documentId)
+ {
+ this.documentId = documentId;
+ }
+
+ public CacheEntityId(Fqn fqn, String key)
+ {
+ this.fqn = fqn;
+ this.key = key;
+ }
+
+ public Fqn getFqn()
+ {
+ if (fqn != null) return fqn;
+ if (documentId != null)
+ {
+ fqn = Transformer.getFqn(documentId);
+ return fqn;
+ }
+ throw new IllegalArgumentException("At least fqn or documentId must be set to call this method");
+ }
+
+ public String getKey()
+ {
+ if (key != null) return key;
+ if (documentId != null)
+ {
+ key = Transformer.getKey(documentId);
+ return key;
+ }
+
+ throw new IllegalArgumentException("At least key or documentId must be set to call this method");
+ }
+
+ public String getDocumentId()
+ {
+ if (key != null || fqn != null)
+ {
+
+ throw new IllegalArgumentException("Either your key or fqn is null. Please check again.");
+ }
+
+ return Transformer.generateId(fqn, key);
+ }
+}
Added: searchable/trunk/src/main/java/org/jboss/cache/search/CacheEntityLoader.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/CacheEntityLoader.java (rev 0)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/CacheEntityLoader.java 2008-06-24 10:51:36 UTC (rev 6011)
@@ -0,0 +1,47 @@
+package org.jboss.cache.search;
+
+import org.jboss.cache.Cache;
+
+import java.util.List;
+import java.util.ArrayList;
+
+/**
+ * @author Navin Surtani - navin(a)surtani.org
+ */
+public class CacheEntityLoader
+{
+ Cache cache;
+
+ public CacheEntityLoader(Cache cache)
+ {
+ this.cache = cache;
+ }
+
+ /**
+ * Takes a list of entity ids and gets them from the cache.
+ * @param ids
+ * @return
+ */
+ public List<Object> load(List<CacheEntityId> ids)
+ {
+ List<Object> retVal = new ArrayList<Object>(ids.size());
+
+ for (CacheEntityId id: ids)
+ {
+ retVal.add( cache.get(id.getFqn(), id.getKey()) );
+ }
+ return retVal;
+ }
+
+ /**
+ * Takes a list of entity ids and gets them from the cache.
+ * @param id
+ * @return
+ */
+ public Object load(CacheEntityId id)
+ {
+ return cache.get(id.getFqn(), id.getKey());
+ }
+
+
+}
Deleted: searchable/trunk/src/main/java/org/jboss/cache/search/CacheQuery.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/CacheQuery.java 2008-06-23 17:38:03 UTC (rev 6010)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/CacheQuery.java 2008-06-24 10:51:36 UTC (rev 6011)
@@ -1,70 +0,0 @@
-package org.jboss.cache.search;
-
-import org.apache.lucene.search.Filter;
-import org.apache.lucene.search.Sort;
-import org.hibernate.search.FullTextFilter;
-
-/**
-@author Navin Surtani - navin(a)surtani.org
-
-
-*/
-// TODO: Can we remove this?
-public interface CacheQuery
-{
- /**
- * Allows to let lucene sort the results. This is useful when you have
- * additional sort requirements on top of the default lucene ranking.
- * Without lucene sorting you would have to retrieve the full result set and
- * order the hibernate objects.
- *
- * @param sort The lucene sort object.
- * @return this for method chaining
- */
- CacheQuery setSort(Sort sort);
-
- /**
- * Allows to use lucene filters.
- * A preferred way is to use the @FullTextFilterDef approach
- *
- * @param filter The lucene filter.
- * @return this for method chaining
- */
- CacheQuery setFilter(Filter filter);
-
- /**
- * Returns the number of hits for this search
- * <p/>
- * Caution:
- * The number of results might be slightly different from
- * <code>list().size()</code> because list() if the index is
- * not in sync with the database at the time of query.
- */
- int getResultSize();
-
- /**
- * Enable a given filter by its name. Returns a FullTextFilter object that allows filter parameter injection
- */
- FullTextFilter enableFullTextFilter(String name);
-
- /**
- * Disable a given filter by its name
- */
- void disableFullTextFilter(String name);
-
- /**
- * {link:Query#setFirstResult}
- */
- CacheQuery setFirstResult(int firstResult);
-
- /**
- * {link:Query#setMaxResults}
- */
- CacheQuery setMaxResults(int maxResults);
-
- /**
- * Defines scrollable result fetch size as well as the JDBC fetch size
- */
- CacheQuery setFetchSize(int i);
-
-}
Modified: searchable/trunk/src/main/java/org/jboss/cache/search/CacheQueryImpl.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/CacheQueryImpl.java 2008-06-23 17:38:03 UTC (rev 6010)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/CacheQueryImpl.java 2008-06-24 10:51:36 UTC (rev 6011)
@@ -8,18 +8,20 @@
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Filter;
import org.hibernate.*;
+import org.hibernate.annotations.common.util.ReflectHelper;
+import org.hibernate.impl.AbstractQueryImpl;
+import org.hibernate.impl.CriteriaImpl;
import org.hibernate.type.Type;
import org.hibernate.transform.ResultTransformer;
import org.hibernate.search.FullTextFilter;
import org.hibernate.search.FullTextQuery;
import org.hibernate.search.SearchException;
-import org.hibernate.search.engine.DocumentBuilder;
-import org.hibernate.search.engine.FilterDef;
-import org.hibernate.search.engine.SearchFactoryImplementor;
+import org.hibernate.search.engine.*;
import org.hibernate.search.filter.ChainedFilter;
import org.hibernate.search.filter.FilterKey;
import org.hibernate.search.impl.SearchFactoryImpl;
import org.hibernate.search.query.FullTextFilterImpl;
+import org.hibernate.search.query.IteratorImpl;
import org.hibernate.search.reader.ReaderProvider;
import static org.hibernate.search.reader.ReaderProviderHelper.getIndexReaders;
import org.hibernate.search.store.DirectoryProvider;
@@ -34,10 +36,10 @@
/**
* @author Navin Surtani - navin(a)surtani.org
- *
- * Implementation class of the FullTextQuery interface in Hibernate Search.
+ * <p/>
+ * Implementation class of the FullTextQuery interface in Hibernate Search.
*/
-public class CacheQueryImpl implements FullTextQuery
+public class CacheQueryImpl extends AbstractQueryImpl implements FullTextQuery
{
private Cache cache;
private Class[] classes;
@@ -53,27 +55,36 @@
private boolean needClassFilterClause;
private Query luceneQuery;
private int fetchSize;
+ private String[] indexProjection;
+ private ResultTransformer resultTransformer;
+ private Criteria criteria;
+ CacheEntityLoader entityLoader;
public CacheQueryImpl(Query luceneQuery, SearchFactoryImpl searchFactory, Cache cache)
{
+ super(luceneQuery.toString(), null, null, null);
this.luceneQuery = luceneQuery;
this.cache = cache;
+ entityLoader = new CacheEntityLoader(cache);
this.searchFactory = searchFactory;
}
public CacheQueryImpl(Query luceneQuery, SearchFactoryImpl searchFactory, Cache cache, Class... classes)
{
- this.luceneQuery = luceneQuery;
- this.cache = cache;
+ this(luceneQuery, searchFactory, cache);
this.classes = classes;
- this.searchFactory = searchFactory;
+ }
+ public FullTextQuery setResultTransformer(ResultTransformer transformer)
+ {
+ // TODO: Do we need to impl this? What does HS do?
+ return null;
}
+
/**
- *Allows to let lucene sort the results.
+ * Allows to let lucene sort the results.
*
- *
* @param sort
* @return
*/
@@ -96,41 +107,32 @@
}
/**
- *
* @return The result size of the query.
*/
public int getResultSize()
{
- if (resultSize == null)
- {
+ if (resultSize == null) {
//get result size without object initialization
IndexSearcher searcher = buildSearcher(searchFactory);
- if (searcher == null)
- {
+ if (searcher == null) {
resultSize = 0;
}
- else
- {
+ else {
Hits hits;
- try
- {
+ try {
hits = getHits(searcher);
resultSize = hits.length();
}
- catch (IOException e)
- {
+ catch (IOException e) {
throw new HibernateException("Unable to query Lucene index", e);
}
- finally
- {
+ finally {
//searcher cannot be null
- try
- {
+ try {
closeSearcher(searcher, searchFactory.getReaderProvider());
//searchFactoryImplementor.getReaderProvider().closeReader( searcher.getIndexReader() );
}
- catch (SearchException e)
- {
+ catch (SearchException e) {
log.warn("Unable to properly close searcher during lucene query: " + e);
}
}
@@ -140,19 +142,18 @@
}
/**
- * Defines the Database Query used to load the Lucene results
- *
- * @param criteria
- * @return
+ * This method is not supported in JBossCache Searchable and should not be called.
*/
+
public FullTextQuery setCriteriaQuery(Criteria criteria)
{
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
+ throw new UnsupportedOperationException("This method is not supported in JBossCache Searchable");
}
/**
* Defines the Lucene field names projected and returned in a query result Each field is converted back to it's object representation,
* an Object[] being returned for each "row" (similar to an HQL or a Criteria API projection).
+ *
* @param strings
* @return
*/
@@ -166,8 +167,7 @@
{
Set<IndexReader> indexReaders = getIndexReaders(searcher);
- for (IndexReader indexReader : indexReaders)
- {
+ for (IndexReader indexReader : indexReaders) {
readerProvider.closeReader(indexReader);
}
}
@@ -175,13 +175,12 @@
/**
* Enable a given filter by its name.
*
- * @param name
+ * @param name
* @return
*/
public FullTextFilter enableFullTextFilter(String name)
{
- if (filterDefinitions == null)
- {
+ if (filterDefinitions == null) {
filterDefinitions = new HashMap<String, FullTextFilterImpl>();
}
FullTextFilterImpl filterDefinition = filterDefinitions.get(name);
@@ -190,8 +189,7 @@
filterDefinition = new FullTextFilterImpl();
filterDefinition.setName(name);
FilterDef filterDef = searchFactory.getFilterDefinition(name);
- if (filterDef == null)
- {
+ if (filterDef == null) {
throw new SearchException("Unkown @FullTextFilter: " + name);
}
filterDefinitions.put(name, filterDefinition);
@@ -209,727 +207,248 @@
}
/**
- *
+ * Sets the the result of the given integer value to the first result.
+ *
* @param firstResult
* @return
*/
public FullTextQuery setFirstResult(int firstResult)
{
- if (firstResult < 0)
- {
+ if (firstResult < 0) {
throw new IllegalArgumentException("'first' pagination parameter less than 0");
}
this.firstResult = firstResult;
return this;
}
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setReadOnly(boolean b)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setCacheable(boolean b)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setCacheRegion(String s)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setTimeout(int i)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public String getQueryString()
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public Type[] getReturnTypes() throws HibernateException
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public String[] getReturnAliases() throws HibernateException
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public String[] getNamedParameters() throws HibernateException
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
public Iterator iterate() throws HibernateException
{
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
+ List list;
+ IndexSearcher searcher = buildSearcher(searchFactory);
+ if (searcher == null) {
+ throw new NullPointerException("IndexSearcher instance is null.");
+ }
- public ScrollableResults scroll() throws HibernateException
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
+ try {
+ Hits hits = getHits(searcher);
+ int first = first();
+ int max = max(first, hits);
+ int size = max - first + 1 < 0 ? 0 : max - first + 1;
+ List<CacheEntityId> ids = new ArrayList<CacheEntityId>(size);
- public ScrollableResults scroll(ScrollMode scrollMode) throws HibernateException
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
+ DocumentExtractor extractor = new DocumentExtractor(searchFactory, indexProjection);
+ for (int index = first; index <= max; index++) {
+ String documentId = (String) extractor.extract(hits, index).id;
+ CacheEntityId id = new CacheEntityId(documentId);
+ ids.add(id);
+ }
- public List list() throws HibernateException
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
+ list = entityLoader.load(ids);
+ }
+ catch (IOException e) {
+ throw new HibernateException("Unable to query Lucene index", e);
- public Object uniqueResult() throws HibernateException
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public int executeUpdate() throws HibernateException
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- public FullTextQuery setMaxResults(int maxResults)
- {
- if (maxResults < 0)
- {
- throw new IllegalArgumentException("'max' pagination parameter less than 0");
}
- this.maxResults = maxResults;
- return this;
- }
- public FullTextQuery setFetchSize(int fetchSize)
- {
- if (fetchSize <= 0)
- {
- throw new IllegalArgumentException("'fetch size' parameter less than or equals to 0");
+ finally {
+ try {
+ closeSearcher(searcher, searchFactory.getReaderProvider());
+ }
+ catch (SearchException e) {
+ log.warn("Unable to properly close searcher during lucene query: " + getQueryString(), e);
+ }
}
- this.fetchSize = fetchSize;
- return this;
- }
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setLockMode(String s, LockMode lockMode)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
+ return new SearchResultIterator(list, entityLoader);
}
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setComment(String s)
+ public ScrollableResults scroll() throws HibernateException
{
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
+ IndexSearcher searcher = buildSearcher(searchFactory);
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
+ Hits hits;
- public org.hibernate.Query setFlushMode(FlushMode flushMode)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
+ try {
+ hits = getHits(searcher);
+ int first = first();
+ int max = max(first, hits);
+ int size = max - first + 1 < 0 ? 0 : max - first + 1;
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
+ DocumentExtractor extractor = new DocumentExtractor(searchFactory, indexProjection);
- public org.hibernate.Query setCacheMode(CacheMode cacheMode)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
+ List<CacheEntityId> ids = new ArrayList<CacheEntityId>(size);
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
+ for (int index = first; index <= max; index++) {
- public org.hibernate.Query setParameter(int i, Object o, Type type)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
+ String documentId = (String) extractor.extract(hits, index).id;
+ CacheEntityId id = new CacheEntityId(documentId);
+ ids.add(id);
+ }
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
+ List<Object> list = entityLoader.load(ids);
+
+ }
- public org.hibernate.Query setParameter(String s, Object o, Type type)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
+ catch (IOException e) {
+ throw new HibernateException("Unable to query Lucene index", e);
+ }
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setParameter(int i, Object o) throws HibernateException
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
+ return null;
}
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setParameter(String s, Object o) throws HibernateException
+ public ScrollableResults scroll(ScrollMode scrollMode) throws HibernateException
{
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
+ return null; // TODO: Implement me!!!
}
/**
- * This method is not supported in JBossCache Searchable and hence should not be called.
+ * Returns a the results from the query as a List object.
*
+ * @return List of results.
+ * @throws HibernateException
*/
-
- public org.hibernate.Query setParameters(Object[] objects, Type[] types) throws HibernateException
+ public List<Object> list() throws HibernateException
{
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
+ IndexSearcher searcher = buildSearcher(searchFactory);
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
+ if (searcher == null) return new ArrayList(0);
- public org.hibernate.Query setParameterList(String s, Collection collection, Type type) throws HibernateException
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
+ Hits hits;
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
+ try {
+ hits = getHits(searcher);
+ int first = first();
+ int max = max(first, hits);
- public org.hibernate.Query setParameterList(String s, Collection collection) throws HibernateException
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
+ int size = max - first + 1 < 0 ? 0 : max - first + 1;
+ List<CacheEntityId> ids = new ArrayList<CacheEntityId>(size);
+ DocumentExtractor extractor = new DocumentExtractor(searchFactory, indexProjection);
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
+ for (int index = first; index <= max; index++) {
+ String documentId = (String) extractor.extract(hits, index).id;
+ CacheEntityId id = new CacheEntityId(documentId);
+ ids.add(id);
+ }
- public org.hibernate.Query setParameterList(String s, Object[] objects, Type type) throws HibernateException
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
+ List<Object> list = entityLoader.load(ids);
+ if (resultTransformer == null) {
+ return list;
+ }
+ else {
+ return resultTransformer.transformList(list);
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
+ }
- public org.hibernate.Query setParameterList(String s, Object[] objects) throws HibernateException
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
+ }
+ catch (IOException e) {
+ throw new HibernateException("Unable to query Lucene index", e);
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
+ }
+ finally {
+ try {
+ closeSearcher(searcher, searchFactory.getReaderProvider());
+ }
+ catch (SearchException e) {
+ log.warn("Unable to properly close searcher during lucene query: " + getQueryString(), e);
+ }
+ }
- public org.hibernate.Query setProperties(Object o) throws HibernateException
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
}
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
+// private Loader getLoader(Session session)
+// {
+// if (indexProjection != null) {
+// ProjectionLoader loader = new ProjectionLoader();
+// loader.init(session, searchFactory, resultTransformer, indexProjection);
+// return loader;
+// }
+// if (criteria != null) {
+// if (classes.length > 1) throw new SearchException("Cannot mix criteria and multiple entity types");
+// if (criteria instanceof CriteriaImpl) {
+// String targetEntity = ((CriteriaImpl) criteria).getEntityOrClassName();
+// if (classes.length == 1 && !classes[0].getName().equals(targetEntity)) {
+// throw new SearchException("Criteria query entity should match query entity");
+// }
+// else {
+// try {
+// Class entityType = ReflectHelper.classForName(targetEntity);
+// classes = new Class[]{entityType};
+// }
+// catch (ClassNotFoundException e) {
+// throw new SearchException("Unable to load entity class from criteria: " + targetEntity, e);
+// }
+// }
+// }
+// QueryLoader loader = new QueryLoader();
+// loader.init(session, searchFactory);
+// loader.setEntityType(classes[0]);
+// loader.setCriteria(criteria);
+// return loader;
+// }
+// else if (classes.length == 1) {
+// QueryLoader loader = new QueryLoader();
+// loader.init(session, searchFactory);
+// loader.setEntityType(classes[0]);
+// return loader;
+// }
+// else {
+// final ObjectLoader objectLoader = new ObjectLoader();
+// objectLoader.init(session, searchFactory);
+// return objectLoader;
+// }
+// }
- public org.hibernate.Query setProperties(Map map) throws HibernateException
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setString(int i, String s)
+ private int max(int first, Hits hits)
{
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
+ return maxResults == null ?
+ hits.length() - 1 :
+ maxResults + first < hits.length() ?
+ first + maxResults - 1 :
+ hits.length() - 1;
}
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setCharacter(int i, char c)
+ private int first()
{
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
+ return firstResult != null ?
+ firstResult :
+ 0;
}
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
- public org.hibernate.Query setBoolean(int i, boolean b)
+ public int executeUpdate() throws HibernateException
{
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
+ throw new UnsupportedOperationException(" This method is not supported in JBossCache Searchable");
}
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setByte(int i, byte b)
+ public FullTextQuery setMaxResults(int maxResults)
{
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
+ if (maxResults < 0) {
+ throw new IllegalArgumentException("'max' pagination parameter less than 0");
+ }
+ this.maxResults = maxResults;
+ return this;
}
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setShort(int i, short i1)
+ public FullTextQuery setFetchSize(int fetchSize)
{
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
+ if (fetchSize <= 0) {
+ throw new IllegalArgumentException("'fetch size' parameter less than or equals to 0");
+ }
+ this.fetchSize = fetchSize;
+ return this;
}
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setInteger(int i, int i1)
+ public org.hibernate.Query setLockMode(String alias, LockMode lockMode)
{
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
+ throw new UnsupportedOperationException(" This method is not supported in JBossCache Searchable");
}
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setLong(int i, long l)
+ protected Map getLockModes()
{
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
+ throw new UnsupportedOperationException(" This method is not supported in JBossCache Searchable");
}
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setFloat(int i, float v)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setDouble(int i, double v)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setBinary(int i, byte[] bytes)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setText(int i, String s)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setSerializable(int i, Serializable serializable)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setLocale(int i, Locale locale)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setBigDecimal(int i, BigDecimal bigDecimal)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setBigInteger(int i, BigInteger bigInteger)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setDate(int i, Date date)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setTime(int i, Date date)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setTimestamp(int i, Date date)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setCalendar(int i, Calendar calendar)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setCalendarDate(int i, Calendar calendar)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setString(String s, String s1)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setCharacter(String s, char c)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setBoolean(String s, boolean b)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setByte(String s, byte b)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
-
- public org.hibernate.Query setShort(String s, short i)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
- public org.hibernate.Query setInteger(String s, int i)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
- public org.hibernate.Query setLong(String s, long l)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- public org.hibernate.Query setFloat(String s, float v)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
- public org.hibernate.Query setDouble(String s, double v)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
- public org.hibernate.Query setBinary(String s, byte[] bytes)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
- public org.hibernate.Query setText(String s, String s1)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
- public org.hibernate.Query setSerializable(String s, Serializable serializable)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
- public org.hibernate.Query setLocale(String s, Locale locale)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
- public org.hibernate.Query setBigDecimal(String s, BigDecimal bigDecimal)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
- public org.hibernate.Query setBigInteger(String s, BigInteger bigInteger)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
- public org.hibernate.Query setDate(String s, Date date)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
- public org.hibernate.Query setTime(String s, Date date)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
- public org.hibernate.Query setTimestamp(String s, Date date)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
- public org.hibernate.Query setCalendar(String s, Calendar calendar)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
- public org.hibernate.Query setCalendarDate(String s, Calendar calendar)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
- public org.hibernate.Query setEntity(int i, Object o)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
- public org.hibernate.Query setEntity(String s, Object o)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
- /**
- * This method is not supported in JBossCache Searchable and hence should not be called.
- *
- */
- public FullTextQuery setResultTransformer(ResultTransformer resultTransformer)
- {
- throw new UnsupportedOperationException("Not supported in SearchableCache!");
- }
-
private IndexSearcher buildSearcher(SearchFactoryImplementor searchFactoryImplementor)
{
Map<Class, DocumentBuilder<Object>> builders = searchFactoryImplementor.getDocumentBuilders();
@@ -937,29 +456,24 @@
Similarity searcherSimilarity = null;
- if (classes == null || classes.length == 0)
- {
+ if (classes == null || classes.length == 0) {
//no class means all classes
- for (DocumentBuilder builder : builders.values())
- {
+ for (DocumentBuilder builder : builders.values()) {
searcherSimilarity = checkSimilarity(searcherSimilarity, builder);
final DirectoryProvider[] directoryProviders = builder.getDirectoryProviderSelectionStrategy().getDirectoryProvidersForAllShards();
populateDirectories(directories, directoryProviders, searchFactoryImplementor);
}
classesAndSubclasses = null;
}
- else
- {
+ else {
Set<Class> involvedClasses = new HashSet<Class>(classes.length);
Collections.addAll(involvedClasses, classes);
- for (Class clazz : classes)
- {
+ for (Class clazz : classes) {
DocumentBuilder builder = builders.get(clazz);
if (builder != null) involvedClasses.addAll(builder.getMappedSubclasses());
}
- for (Class clazz : involvedClasses)
- {
+ for (Class clazz : involvedClasses) {
DocumentBuilder builder = builders.get(clazz);
if (builder == null)
throw new HibernateException("Not a mapped entity (don't forget to add @Indexed): " + clazz);
@@ -973,19 +487,14 @@
//compute optimization needClassFilterClause
//if at least one DP contains one class that is not part of the targeted classesAndSubclasses we can't optimize
- if (classesAndSubclasses != null)
- {
- for (DirectoryProvider dp : directories)
- {
+ if (classesAndSubclasses != null) {
+ for (DirectoryProvider dp : directories) {
final Set<Class> classesInDirectoryProvider = searchFactoryImplementor.getClassesInDirectoryProvider(dp);
// if a DP contains only one class, we know for sure it's part of classesAndSubclasses
- if (classesInDirectoryProvider.size() > 1)
- {
+ if (classesInDirectoryProvider.size() > 1) {
//risk of needClassFilterClause
- for (Class clazz : classesInDirectoryProvider)
- {
- if (!classesAndSubclasses.contains(clazz))
- {
+ for (Class clazz : classesInDirectoryProvider) {
+ if (!classesAndSubclasses.contains(clazz)) {
this.needClassFilterClause = true;
break;
}
@@ -1004,12 +513,10 @@
private Similarity checkSimilarity(Similarity similarity, DocumentBuilder builder)
{
- if (similarity == null)
- {
+ if (similarity == null) {
similarity = builder.getSimilarity();
}
- else if (!similarity.getClass().equals(builder.getSimilarity().getClass()))
- {
+ else if (!similarity.getClass().equals(builder.getSimilarity().getClass())) {
throw new HibernateException("Cannot perform search on two entities with differing Similarity implementations (" + similarity.getClass().getName() + " & " + builder.getSimilarity().getClass().getName() + ")");
}
@@ -1019,10 +526,8 @@
private void populateDirectories(List<DirectoryProvider> directories, DirectoryProvider[] directoryProviders,
SearchFactoryImplementor searchFactoryImplementor)
{
- for (DirectoryProvider provider : directoryProviders)
- {
- if (!directories.contains(provider))
- {
+ for (DirectoryProvider provider : directoryProviders) {
+ if (!directories.contains(provider)) {
directories.add(provider);
}
}
@@ -1045,19 +550,16 @@
private org.apache.lucene.search.Query filterQueryByClasses(org.apache.lucene.search.Query luceneQuery)
{
- if (!needClassFilterClause)
- {
+ if (!needClassFilterClause) {
return luceneQuery;
}
- else
- {
+ else {
//A query filter is more practical than a manual class filtering post query (esp on scrollable resultsets)
//it also probably minimise the memory footprint
BooleanQuery classFilter = new BooleanQuery();
//annihilate the scoring impact of DocumentBuilder.CLASS_FIELDNAME
classFilter.setBoost(0);
- for (Class clazz : classesAndSubclasses)
- {
+ for (Class clazz : classesAndSubclasses) {
Term t = new Term(DocumentBuilder.CLASS_FIELDNAME, clazz.getName());
TermQuery termQuery = new TermQuery(t);
classFilter.add(termQuery, BooleanClause.Occur.SHOULD);
@@ -1071,35 +573,27 @@
private void buildFilters()
{
- if (filterDefinitions != null && filterDefinitions.size() > 0)
- {
+ if (filterDefinitions != null && filterDefinitions.size() > 0) {
ChainedFilter chainedFilter = new ChainedFilter();
- for (FullTextFilterImpl filterDefinition : filterDefinitions.values())
- {
+ for (FullTextFilterImpl filterDefinition : filterDefinitions.values()) {
FilterDef def = searchFactory.getFilterDefinition(filterDefinition.getName());
Class implClass = def.getImpl();
Object instance;
- try
- {
+ try {
instance = implClass.newInstance();
}
- catch (Exception e)
- {
+ catch (Exception e) {
throw new SearchException("Unable to create @FullTextFilterDef: " + def.getImpl(), e);
}
- for (Map.Entry<String, Object> entry : filterDefinition.getParameters().entrySet())
- {
+ for (Map.Entry<String, Object> entry : filterDefinition.getParameters().entrySet()) {
def.invoke(entry.getKey(), instance, entry.getValue());
}
- if (def.isCache() && def.getKeyMethod() == null && filterDefinition.getParameters().size() > 0)
- {
+ if (def.isCache() && def.getKeyMethod() == null && filterDefinition.getParameters().size() > 0) {
throw new SearchException("Filter with parameters and no @Key method: " + filterDefinition.getName());
}
FilterKey key = null;
- if (def.isCache())
- {
- if (def.getKeyMethod() == null)
- {
+ if (def.isCache()) {
+ if (def.getKeyMethod() == null) {
key = new FilterKey()
{
public int hashCode()
@@ -1115,68 +609,54 @@
}
};
}
- else
- {
- try
- {
+ else {
+ try {
key = (FilterKey) def.getKeyMethod().invoke(instance);
}
- catch (IllegalAccessException e)
- {
+ catch (IllegalAccessException e) {
throw new SearchException("Unable to access @Key method: "
- + def.getImpl().getName() + "." + def.getKeyMethod().getName());
+ + def.getImpl().getName() + "." + def.getKeyMethod().getName());
}
- catch (InvocationTargetException e)
- {
+ catch (InvocationTargetException e) {
throw new SearchException("Unable to access @Key method: "
- + def.getImpl().getName() + "." + def.getKeyMethod().getName());
+ + def.getImpl().getName() + "." + def.getKeyMethod().getName());
}
- catch (ClassCastException e)
- {
+ catch (ClassCastException e) {
throw new SearchException("@Key method does not return FilterKey: "
- + def.getImpl().getName() + "." + def.getKeyMethod().getName());
+ + def.getImpl().getName() + "." + def.getKeyMethod().getName());
}
}
key.setImpl(def.getImpl());
}
Filter filter = def.isCache() ?
- searchFactory.getFilterCachingStrategy().getCachedFilter(key) :
- null;
- if (filter == null)
- {
- if (def.getFactoryMethod() != null)
- {
- try
- {
+ searchFactory.getFilterCachingStrategy().getCachedFilter(key) :
+ null;
+ if (filter == null) {
+ if (def.getFactoryMethod() != null) {
+ try {
filter = (Filter) def.getFactoryMethod().invoke(instance);
}
- catch (IllegalAccessException e)
- {
+ catch (IllegalAccessException e) {
throw new SearchException("Unable to access @Factory method: "
- + def.getImpl().getName() + "." + def.getFactoryMethod().getName());
+ + def.getImpl().getName() + "." + def.getFactoryMethod().getName());
}
- catch (InvocationTargetException e)
- {
+ catch (InvocationTargetException e) {
throw new SearchException("Unable to access @Factory method: "
- + def.getImpl().getName() + "." + def.getFactoryMethod().getName());
+ + def.getImpl().getName() + "." + def.getFactoryMethod().getName());
}
- catch (ClassCastException e)
- {
+ catch (ClassCastException e) {
throw new SearchException("@Key method does not return a org.apache.lucene.search.Filter class: "
- + def.getImpl().getName() + "." + def.getFactoryMethod().getName());
+ + def.getImpl().getName() + "." + def.getFactoryMethod().getName());
}
}
- else
- {
- try
- {
+ else {
+ try {
filter = (Filter) instance;
}
- catch (ClassCastException e)
- {
+ catch (ClassCastException e) {
throw new SearchException("@Key method does not return a org.apache.lucene.search.Filter class: "
- + def.getImpl().getName() + "." + def.getFactoryMethod().getName());
+ + def.getImpl().getName() + "." + def.getFactoryMethod().getName());
}
}
if (def.isCache())
Modified: searchable/trunk/src/main/java/org/jboss/cache/search/InvalidFqnException.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/InvalidFqnException.java 2008-06-23 17:38:03 UTC (rev 6010)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/InvalidFqnException.java 2008-06-24 10:51:36 UTC (rev 6011)
@@ -3,9 +3,11 @@
import org.jboss.cache.CacheException;
/**
- * @author Navin Surtani - navin(a)surtani.org
*
* Thrown when an invalid Fqn is passed into the generateId method in Transformer
+ * <p />
+ *
+ * @author Navin Surtani - navin(a)surtani.org
*/
public class InvalidFqnException extends CacheException
{
Modified: searchable/trunk/src/main/java/org/jboss/cache/search/NodeModifiedTransactionContext.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/NodeModifiedTransactionContext.java 2008-06-23 17:38:03 UTC (rev 6010)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/NodeModifiedTransactionContext.java 2008-06-24 10:51:36 UTC (rev 6011)
@@ -7,12 +7,12 @@
import javax.transaction.Transaction;
/**
+ * This class implements the TransactionContext interface in the org.hibernate.search.transaction package. It
+ * retrieves transaction context information from the NodeModifiedEvent that gets passed in.
+ * <p />
+ * It is used by the SearchableListener to pass in transaction information to a Hibernate Search Work object.
+ * <p />
* @author Navin Surtani - navin(a)surtani.org
-
- This class implements the TransactionContext interface in org.hibernate.search.transaction.
-
- It is used by the SearchableListener so that a TransactionContext instance can be created.
-
*/
public class NodeModifiedTransactionContext implements TransactionContext
{
@@ -27,7 +27,7 @@
/**
* Returns a boolean value whether or not a transaction is in progress (JTA transaction and in this case *not*
* an org.hibernate transaction).
- * @return
+ * @return true if a transaction is in progress, false otherwise.
*/
public boolean isTxInProgress()
{
@@ -35,8 +35,9 @@
}
/**
- * Returns a JTA transaction as an object.
- * @return
+ * Returns a JTA transaction.
+ * @return a JTA transaction if one is available, or a null otherwise.
+ * @see org.jboss.cache.notifications.event.NodeModifiedEvent#getTransaction()
*/
public Object getTransactionIdentifier()
Added: searchable/trunk/src/main/java/org/jboss/cache/search/SearchResultIterator.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/SearchResultIterator.java (rev 0)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/SearchResultIterator.java 2008-06-24 10:51:36 UTC (rev 6011)
@@ -0,0 +1,61 @@
+package org.jboss.cache.search;
+
+import org.hibernate.search.engine.EntityInfo;
+import org.hibernate.search.engine.Loader;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+
+/**
+ * @author Navin Surtani - navin(a)surtani.org
+ */
+public class SearchResultIterator implements Iterator
+{
+ protected final List<CacheEntityId> entityIds;
+ protected int index = 0;
+ protected final int size;
+ protected Object next;
+ protected int nextObjectIndex = -1;
+ protected final CacheEntityLoader loader;
+
+ public SearchResultIterator(List<CacheEntityId> entityIds, CacheEntityLoader loader) {
+ this.entityIds = entityIds;
+ this.size = entityIds.size();
+ this.loader = loader;
+ }
+
+ //side effect is to set up next
+ public boolean hasNext() {
+ if ( nextObjectIndex == index ) return next != null;
+ next = null;
+ nextObjectIndex = -1;
+ do {
+ if ( index >= size ) {
+ nextObjectIndex = index;
+ next = null;
+ return false;
+ }
+ next = loader.load( entityIds.get( index ) );
+ if ( next == null ) {
+ index++;
+ }
+ else {
+ nextObjectIndex = index;
+ }
+ }
+ while ( next == null );
+ return true;
+ }
+
+ public Object next() {
+ //hasNext() has side effect
+ if ( !hasNext() ) throw new NoSuchElementException( "Out of boundaries" );
+ index++;
+ return next;
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException( "Cannot remove from a lucene query iterator" );
+ }
+}
Modified: searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCacheImpl.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCacheImpl.java 2008-06-23 17:38:03 UTC (rev 6010)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCacheImpl.java 2008-06-24 10:51:36 UTC (rev 6011)
@@ -19,9 +19,9 @@
import java.util.Set;
/**
-@author Navin Surtani - navin(a)surtani.org
-
- Implementation class for the SearchableCache interface.
+ * @author Navin Surtani - navin(a)surtani.org
+ * <p/>
+ * Implementation class for the SearchableCache interface.
*/
public class SearchableCacheImpl implements SearchableCache
{
@@ -29,8 +29,7 @@
private Cache cache;
private SearchFactoryImpl searchFactory;
- //TODO: Does the impl class have to be javadocced?
-
+ //TODO: javadoc!!
public SearchableCacheImpl(Cache cache, SearchFactoryImpl searchFactory)
{
this.cache = cache;
@@ -243,19 +242,4 @@
{
return cache.put(fqn, key, value);
}
-
- public Set getCacheListeners(Fqn region)
- {
- return cache.getCacheListeners(region);
- }
-
- public void removeCacheListener(Fqn region, Object listener)
- {
- cache.removeCacheListener(region, listener);
- }
-
- public void addCacheListener(Fqn region, Object listener)
- {
- cache.addCacheListener(region, listener);
- }
}
Modified: searchable/trunk/src/main/java/org/jboss/cache/search/SearchableListener.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/SearchableListener.java 2008-06-23 17:38:03 UTC (rev 6010)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/SearchableListener.java 2008-06-24 10:51:36 UTC (rev 6011)
@@ -61,11 +61,10 @@
for (Object key : dataMap.keySet())
{
- String keyString = (String) key;
- String docId = Transformer.generateId(event.getFqn(), keyString);
+ CacheEntityId cacheEntityId = new CacheEntityId(event.getFqn(), (String) key);
- searchFactory.getWorker().performWork(new Work(dataMap.get(key), docId, WorkType.DELETE), ctx);
- searchFactory.getWorker().performWork(new Work(dataMap.get(key), docId, WorkType.ADD), ctx);
+ 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);
}
}
@@ -84,10 +83,9 @@
for (Object key : dataMap.keySet())
{
- String keyString = (String) key;
- String docId = Transformer.generateId(event.getFqn(), keyString);
+ CacheEntityId cacheEntityId = new CacheEntityId(event.getFqn(), (String) key);
- searchFactory.getWorker().performWork(new Work(dataMap.get(key), docId, WorkType.DELETE), ctx);
+ searchFactory.getWorker().performWork(new Work(dataMap.get(key), cacheEntityId.getDocumentId(), WorkType.DELETE), ctx);
}
Modified: searchable/trunk/src/test/java/org/jboss/cache/search/BlackBoxTest.java
===================================================================
--- searchable/trunk/src/test/java/org/jboss/cache/search/BlackBoxTest.java 2008-06-23 17:38:03 UTC (rev 6010)
+++ searchable/trunk/src/test/java/org/jboss/cache/search/BlackBoxTest.java 2008-06-24 10:51:36 UTC (rev 6011)
@@ -5,22 +5,36 @@
import org.jboss.cache.Fqn;
import org.jboss.cache.search.test.Person;
import org.testng.annotations.Test;
+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 org.hibernate.search.FullTextQuery;
+import java.util.List;
+
/**
* @author Navin Surtani - navin(a)surtani.org
*/
@Test
public class BlackBoxTest
{
- public void doTest()
- {
+ public void doTest() throws ParseException {
Cache cache = new DefaultCacheFactory().createCache();
SearchableCache sc = new SearchableCacheFactory().createSearchableCache(cache, Person.class);
Person p1 = new Person();
p1.setName("Navin Surtani");
p1.setBlurb("Likes playing WoW");
sc.put(Fqn.fromString("/a/b/c"), "Navin", p1);
+ QueryParser qp = new QueryParser("field", new StandardAnalyzer());
+ Query luceneQuery = qp.parse("playing");
+ FullTextQuery query = sc.createQuery(luceneQuery);
+ List found = query.list();
+
+ assert found.size() == 1;
+ assert found.get(0).equals(p1);
+
// try and search for navin
// TODO: Create a dummy lucene query
//sc.createQuery(new Query);
16 years, 7 months
JBoss Cache SVN: r6010 - core/branches/2.2.X/src/test/java/org/jboss/cache/notifications.
by jbosscache-commits@lists.jboss.org
Author: manik.surtani(a)jboss.com
Date: 2008-06-23 13:38:03 -0400 (Mon, 23 Jun 2008)
New Revision: 6010
Modified:
core/branches/2.2.X/src/test/java/org/jboss/cache/notifications/CacheListenerPassivationTest.java
Log:
Removed misleading javadoc
Modified: core/branches/2.2.X/src/test/java/org/jboss/cache/notifications/CacheListenerPassivationTest.java
===================================================================
--- core/branches/2.2.X/src/test/java/org/jboss/cache/notifications/CacheListenerPassivationTest.java 2008-06-23 17:32:56 UTC (rev 6009)
+++ core/branches/2.2.X/src/test/java/org/jboss/cache/notifications/CacheListenerPassivationTest.java 2008-06-23 17:38:03 UTC (rev 6010)
@@ -27,12 +27,6 @@
import java.util.List;
import java.util.Map;
-/**
- * // TODO: MANIK: Document this
- *
- * @author Manik Surtani (<a href="mailto:manik@jboss.org">manik(a)jboss.org</a>)
- * @since 3.0
- */
@Test(groups = "functional")
public class CacheListenerPassivationTest
{
16 years, 7 months