You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by lu...@apache.org on 2004/11/08 10:45:10 UTC
cvs commit: jakarta-slide/src/stores/org/apache/slide/index/lucene LucenePropertiesIndexer.java
luetzkendorf 2004/11/08 01:45:10
Modified: src/stores/org/apache/slide/index/lucene
LucenePropertiesIndexer.java
Log:
factored out the AbstractLuceneIndexer
Revision Changes Path
1.4 +59 -433 jakarta-slide/src/stores/org/apache/slide/index/lucene/LucenePropertiesIndexer.java
Index: LucenePropertiesIndexer.java
===================================================================
RCS file: /home/cvs/jakarta-slide/src/stores/org/apache/slide/index/lucene/LucenePropertiesIndexer.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- LucenePropertiesIndexer.java 28 Oct 2004 16:00:14 -0000 1.3
+++ LucenePropertiesIndexer.java 8 Nov 2004 09:45:10 -0000 1.4
@@ -20,112 +20,69 @@
package org.apache.slide.index.lucene;
-import java.io.IOException;
-import java.text.MessageFormat;
-import java.util.ArrayList;
-import java.util.Collection;
import java.util.Enumeration;
-import java.util.HashSet;
import java.util.Hashtable;
-import java.util.Iterator;
-import java.util.Set;
-import java.util.StringTokenizer;
-
-import javax.transaction.xa.XAException;
-import javax.transaction.xa.XAResource;
-import javax.transaction.xa.Xid;
-
-import org.apache.commons.transaction.util.LoggerFacade;
-import org.apache.commons.transaction.util.xa.AbstractTransactionalResource;
-import org.apache.commons.transaction.util.xa.AbstractXAResource;
-import org.apache.commons.transaction.util.xa.TransactionalResource;
-import org.apache.slide.common.AbstractServiceBase;
import org.apache.slide.common.NamespaceAccessToken;
-import org.apache.slide.common.ServiceAccessException;
-import org.apache.slide.common.ServiceConnectionFailedException;
-import org.apache.slide.common.ServiceDisconnectionFailedException;
import org.apache.slide.common.ServiceInitializationFailedException;
import org.apache.slide.common.ServiceParameterErrorException;
import org.apache.slide.common.ServiceParameterMissingException;
-import org.apache.slide.common.ServiceResetFailedException;
import org.apache.slide.common.Uri;
import org.apache.slide.content.NodeProperty;
import org.apache.slide.content.NodeRevisionContent;
import org.apache.slide.content.NodeRevisionDescriptor;
import org.apache.slide.content.NodeRevisionNumber;
import org.apache.slide.search.IndexException;
-import org.apache.slide.search.basic.IBasicExpressionFactory;
-import org.apache.slide.store.IndexStore;
-import org.apache.slide.util.logger.Logger;
-import org.apache.slide.util.logger.TxLogger;
+import org.apache.slide.util.conf.Configurable;
+import org.apache.slide.util.conf.Configuration;
+import org.apache.slide.util.conf.ConfigurationException;
/**
* IndexStore implementation for indexing properties based on Jakarta Lucene.
*/
-public class LucenePropertiesIndexer extends AbstractServiceBase implements
- IndexStore
+public class LucenePropertiesIndexer extends AbstractLuceneIndexer
+ implements Configurable
{
+ private Configuration indexedProperties = null;
- private static final String LOG_CHANNEL = LucenePropertiesIndexer.class
- .getName();
-
- private static final String INDEX_PATH = "indexpath";
-
- private static final String INCLUDES = "includes";
-
- private static final String USER_PROPERTIES_CONFIG = "user-properties-config";
-
- private static final String ASYNC_INDEX_PARAM = "asynchron";
-
- //------------------
-
- private Collection includes;
- private boolean started = false;
- private String userConfigName;
- private IndexConfiguration indexConfiguration = new IndexConfiguration();
-
- protected Index index = null;
-
- /**
- * XAResource implementation to delegate all XAResource requests to.
- */
- private XAResourceImpl xaResource = null;
-
- /**
- * Create Index, if not yet done.
- *
- * @param token
- * a NamespaceAccessToken
- *
- * @throws org.apache.slide.common.ServiceInitializationFailedException
- *
- */
public void initialize(NamespaceAccessToken token)
throws ServiceInitializationFailedException
{
- debug("initialize");
-
- TxLogger txLogger = new TxLogger(getLogger(), LOG_CHANNEL);
- this.xaResource = new XAResourceImpl(txLogger);
+ super.initialize(token);
try {
indexConfiguration.initDefaultConfiguration();
+ indexConfiguration.readPropertyConfiguration(this.indexedProperties);
- if (this.userConfigName != null) {
- info("loading user properties config: {0}", this.userConfigName);
- indexConfiguration.loadConfigurationFromResource(this.userConfigName);
- }
-
- this.index = new Index(indexConfiguration, getLogger());
+ this.index = new Index(indexConfiguration, getLogger(),
+ "properties " + this.scope);
}
catch (IndexException e) {
throw new ServiceInitializationFailedException(this, e);
}
}
+
+
+
+
+ public void setParameters(Hashtable parameters)
+ throws ServiceParameterErrorException,
+ ServiceParameterMissingException
+ {
+ super.setParameters(parameters);
+ }
+
+ public void configure(Configuration configuration)
+ throws ConfigurationException
+ {
+ if ("indexed-properties".equals(configuration.getAttribute("name"))) {
+ this.indexedProperties = configuration;
+ }
+ }
+
/**
- * Index an object content.
+ * Index an object properties.
*
* @param uri
* Uri
@@ -138,9 +95,10 @@
{
debug("createIndex {0} {1}", uri, revisionDescriptor.getRevisionNumber());
- TransactionalIndexResource indexResource = xaResource.getCurrentTxn();
- indexResource.addIndexJob(uri, revisionDescriptor.getRevisionNumber(),
- revisionDescriptor);
+ if (isIncluded(uri.toString())) {
+ TransactionalIndexResource indexResource = getCurrentTxn();
+ indexResource.addIndexJob(uri, revisionDescriptor);
+ }
}
@@ -163,27 +121,31 @@
{
debug("updateIndex {0} {1}", uri, revisionDescriptor.getRevisionNumber());
- boolean needsUpdate = false;
- for(Enumeration e = revisionDescriptor.enumerateUpdatedProperties();e.hasMoreElements();) {
- NodeProperty property = (NodeProperty)e.nextElement();
- if(index.getConfiguration().isIndexedProperty(property.getNamespace(), property.getName())) {
- needsUpdate = true;
- break;
+ if (isIncluded(uri.toString())) {
+
+ boolean needsUpdate = false;
+ // check whether we need an update, i.e. any of the updated or
+ // removed properties is to be indexed
+ for(Enumeration e = revisionDescriptor.enumerateUpdatedProperties();e.hasMoreElements();) {
+ NodeProperty property = (NodeProperty)e.nextElement();
+ if(index.getConfiguration().isIndexedProperty(property.getNamespace(), property.getName())) {
+ needsUpdate = true;
+ break;
+ }
+ }
+ for(Enumeration e = revisionDescriptor.enumerateRemovedProperties();!needsUpdate && e.hasMoreElements();) {
+ NodeProperty property = (NodeProperty)e.nextElement();
+ if(index.getConfiguration().isIndexedProperty(property.getNamespace(), property.getName())) {
+ needsUpdate = true;
+ break;
+ }
}
- }
- for(Enumeration e = revisionDescriptor.enumerateUpdatedProperties();!needsUpdate && e.hasMoreElements();) {
- NodeProperty property = (NodeProperty)e.nextElement();
- if(index.getConfiguration().isIndexedProperty(property.getNamespace(), property.getName())) {
- needsUpdate = true;
- break;
+
+ if (needsUpdate) {
+ TransactionalIndexResource indexResource = getCurrentTxn();
+ indexResource.addUpdateJob(uri, revisionDescriptor);
}
}
-
- if (needsUpdate) {
- TransactionalIndexResource indexResource = xaResource.getCurrentTxn();
- indexResource.addUpdateJob(uri,
- revisionDescriptor.getRevisionNumber(), revisionDescriptor);
- }
}
/**
@@ -198,349 +160,13 @@
{
debug("dropIndex {0} {1}", uri, number);
- TransactionalIndexResource indexResource = xaResource.getCurrentTxn();
- indexResource.addRemoveJob(uri, number);
- }
-
-
-
-
-
- /**
- * Method getFactory
- *
- * @return an IBasicExpressionFactory
- *
- */
- public IBasicExpressionFactory getBasicExpressionFactory()
- {
- return new LuceneExpressionFactory(this.index);
- }
-
- /**
- * Connects to the underlying data source (if any is needed).
- *
- * @exception ServiceConnectionFailedException
- * Connection failed
- */
- public void connect() throws ServiceConnectionFailedException
- {
- debug("connect");
- started = true;
- }
-
- /**
- * This function tells whether or not the service is connected.
- *
- * @return boolean true if we are connected
- * @exception ServiceAccessException
- * Service access error
- */
- public boolean isConnected() throws ServiceAccessException
- {
- return started;
- }
-
- /**
- * Parametrize the service. This index store expects a parameter
- * "indexpath" to contain the path to the directory to store the index.
- * Another optional parameter "includes" lists the paths of resources that
- * are to be indexed in a comma-separated format. Everything under an
- * included path is indexed. If not specified all resources will be
- * indexed.
- *
- * @param parameters
- * Hashtable containing the parameters' names and associated
- * values
- * @exception ServiceParameterErrorException
- * Incorrect service parameter
- * @exception ServiceParameterMissingException
- * Service parameter missing
- */
- public void setParameters(Hashtable parameters)
- throws ServiceParameterErrorException,
- ServiceParameterMissingException
- {
- String indexpath = (String) parameters.get(INDEX_PATH);
- if (indexpath == null || indexpath.length() == 0) {
- throw new ServiceParameterMissingException(this, INDEX_PATH);
+ if (isIncluded(uri.toString())) {
+ TransactionalIndexResource indexResource = getCurrentTxn();
+ indexResource.addRemoveJob(uri, number);
}
- this.indexConfiguration.setIndexPath(indexpath);
-
- this.indexConfiguration.setIndexAsynchron(
- "true".equals(parameters.get(ASYNC_INDEX_PARAM)));
-
- String includes = (String) parameters.get(INCLUDES);
- if (includes != null && includes.length() > 0) {
- StringTokenizer tokenizer = new StringTokenizer(includes, ",");
- this.includes = new ArrayList(tokenizer.countTokens());
- while (tokenizer.hasMoreTokens()) {
- this.includes.add(tokenizer.nextToken());
- }
- }
-
- userConfigName = (String)parameters.get(USER_PROPERTIES_CONFIG);
-
-
- }
-
- /**
- * Disconnects from the underlying data source.
- *
- * @exception ServiceDisconnectionFailedException
- * Disconnection failed
- */
- public void disconnect() throws ServiceDisconnectionFailedException
- {
- debug("disconnect");
- started = false;
- this.index.close();
- }
-
- /**
- * Deletes service underlying data source, if possible (and meaningful).
- *
- * @exception ServiceResetFailedException
- * Reset failed
- */
- public void reset() throws ServiceResetFailedException
- {
- debug("reset");
- }
-
- protected Enumeration readProperties(
- NodeRevisionDescriptor revisionDescriptor,
- NodeRevisionContent revisionContent) throws IOException
- {
- Enumeration em = revisionDescriptor.enumerateProperties();
- return em;
-
}
- protected boolean isIncluded(String uri)
- {
- if (includes == null)
- return true;
- Iterator iter = includes.iterator();
- while (iter.hasNext()) {
- if (uri.startsWith((String) iter.next())) {
- return true;
- }
- }
- return false;
- }
-
- // -------------------------------------------------------------------------
- // XAResource interface, all request are deletgated to this.xaResource
-
- public void commit(Xid xid, boolean onePhase) throws XAException
- {
- this.xaResource.commit(xid, onePhase);
- }
-
- public void end(Xid xid, int flags) throws XAException
- {
- this.xaResource.end(xid, flags);
- }
-
- public void forget(Xid xid) throws XAException
- {
- this.xaResource.forget(xid);
- }
-
- public int getTransactionTimeout() throws XAException
- {
- return this.xaResource.getTransactionTimeout();
- }
-
- public boolean isSameRM(XAResource xares) throws XAException
- {
- return this.xaResource != null && this.xaResource.isSameRM(xares); // ??
- }
-
- public int prepare(Xid xid) throws XAException
- {
- return this.xaResource.prepare(xid);
- }
-
- public Xid[] recover(int flag) throws XAException
- {
- return this.xaResource.recover(flag);
- }
-
- public void rollback(Xid xid) throws XAException
- {
- this.xaResource.rollback(xid);
- }
-
- public boolean setTransactionTimeout(int sec) throws XAException
- {
- return this.xaResource.setTransactionTimeout(sec);
- }
-
- public void start(Xid xid, int flags) throws XAException
- {
- this.xaResource.start(xid, flags);
- }
- private class XAResourceImpl extends AbstractXAResource
- {
-
- private LoggerFacade loggerFacade;
-
- XAResourceImpl(LoggerFacade loggerFacade)
- {
- this.loggerFacade = loggerFacade;
- }
-
- protected TransactionalResource createTransactionResource(Xid xid)
- throws Exception
- {
- return new TransactionalIndexResource(xid, index);
- }
-
- TransactionalIndexResource getCurrentTxn() {
- return (TransactionalIndexResource)getCurrentlyActiveTransactionalResource();
- }
-
- protected LoggerFacade getLoggerFacade()
- {
- return this.loggerFacade;
- }
-
- protected boolean includeBranchInXid()
- {
- return true;
- }
-
- public boolean isSameRM(XAResource xares) throws XAException
- {
- return xares == this;
- }
-
- public Xid[] recover(int flag) throws XAException
- {
- // TODO Auto-generated method stub
- return null;
- }
-
- public int getTransactionTimeout() throws XAException
- {
- return 0;
- }
-
- public boolean setTransactionTimeout(int seconds) throws XAException
- {
- return false;
- }
- }
-
- private static class TransactionalIndexResource extends
- AbstractTransactionalResource
- {
- private Index idx;
- private Set indexJobs = new HashSet();
- private Set removeJobs = new HashSet();
-
- TransactionalIndexResource(Xid xid, Index index)
- {
- super(xid);
- this.idx = index;
- }
-
- void addIndexJob(Uri uri, NodeRevisionNumber version, NodeRevisionDescriptor descriptor) {
- this.indexJobs.add(this.idx.createIndexJob(uri, version, descriptor));
- }
- void addRemoveJob(Uri uri, NodeRevisionNumber version) {
- Index.IndexJob job = idx.createDeleteJob(uri, version);
- this.indexJobs.remove(job);
- this.removeJobs.add(job);
- }
- void addUpdateJob(Uri uri, NodeRevisionNumber version, NodeRevisionDescriptor descriptor) {
- Index.IndexJob job = idx.createIndexJob(uri, version, descriptor);
- this.removeJobs.add(job);
- this.indexJobs.add(job);
- }
-
- public void begin() throws XAException
- {
- }
-
- public void commit() throws XAException
- {
- try {
- this.idx.scheduleIndexTransaction(this.removeJobs, this.indexJobs);
- } catch (IndexException e) {
- throw new XAException(e.toString());
- }
- }
-
- public int prepare() throws XAException
- {
- return 0;
- }
-
- public void resume() throws XAException
- {
- }
-
- public void rollback() throws XAException
- {
- }
-
- public void suspend() throws XAException
- {
- }
- }
-
- void info(String msg)
- {
- if (this.getLogger().isEnabled(Logger.INFO)) {
- this.getLogger().log(msg, LOG_CHANNEL, Logger.INFO);
- }
- }
-
- void info(String msg, Object o)
- {
- if (this.getLogger().isEnabled(Logger.INFO)) {
- Object[] args = { o};
- this.getLogger().log(MessageFormat.format(msg, args), LOG_CHANNEL,
- Logger.INFO);
- }
- }
-
- void debug(String msg)
- {
- if (this.getLogger().isEnabled(Logger.DEBUG)) {
- this.getLogger().log(msg, LOG_CHANNEL, Logger.DEBUG);
- }
- }
-
- void debug(String msg, Object o1, Object o2)
- {
- if (this.getLogger().isEnabled(Logger.DEBUG)) {
- Object[] args = { o1, o2};
- this.getLogger().log(MessageFormat.format(msg, args), LOG_CHANNEL,
- Logger.DEBUG);
- }
- }
-
- void error(String msg, Object o1)
- {
- if (this.getLogger().isEnabled(Logger.ERROR)) {
- Object[] args = { o1};
- this.getLogger().log(MessageFormat.format(msg, args), LOG_CHANNEL,
- Logger.ERROR);
- }
- }
- void error(String msg, Object o1, Object o2)
- {
- if (this.getLogger().isEnabled(Logger.ERROR)) {
- Object[] args = { o1, o2};
- this.getLogger().log(MessageFormat.format(msg, args), LOG_CHANNEL,
- Logger.ERROR);
- }
- }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: slide-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: slide-dev-help@jakarta.apache.org