Author: nzamosenchuk
Date: 2009-12-07 09:27:08 -0500 (Mon, 07 Dec 2009)
New Revision: 934
Added:
jcr/branches/1.12.0-JBC/component/core/src/main/java/org/exoplatform/services/jcr/impl/core/query/jbosscache/IndexerSingleStoreCacheLoader.java
Modified:
jcr/branches/1.12.0-JBC/component/core/src/main/java/org/exoplatform/services/jcr/impl/core/query/jbosscache/IndexerCacheLoader.java
Log:
EXOJCR-291: Add new IndexerCacheLoader and IndexerSingleStoreCacheLoader
Modified:
jcr/branches/1.12.0-JBC/component/core/src/main/java/org/exoplatform/services/jcr/impl/core/query/jbosscache/IndexerCacheLoader.java
===================================================================
---
jcr/branches/1.12.0-JBC/component/core/src/main/java/org/exoplatform/services/jcr/impl/core/query/jbosscache/IndexerCacheLoader.java 2009-12-07
12:55:35 UTC (rev 933)
+++
jcr/branches/1.12.0-JBC/component/core/src/main/java/org/exoplatform/services/jcr/impl/core/query/jbosscache/IndexerCacheLoader.java 2009-12-07
14:27:08 UTC (rev 934)
@@ -16,217 +16,90 @@
*/
package org.exoplatform.services.jcr.impl.core.query.jbosscache;
-import org.exoplatform.services.jcr.datamodel.PropertyData;
+import org.exoplatform.services.jcr.config.RepositoryConfigurationException;
+import org.exoplatform.services.jcr.impl.core.query.SearchManager;
import
org.exoplatform.services.jcr.impl.storage.jbosscache.AbstractWriteOnlyCacheLoader;
-import org.exoplatform.services.jcr.impl.storage.jbosscache.JBossCacheStorage;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
-import org.jboss.cache.CacheException;
-import org.jboss.cache.Fqn;
import org.jboss.cache.Modification;
+import org.jboss.cache.Modification.ModificationType;
+import org.jboss.cache.factories.annotations.Inject;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
- * Created by The eXo Platform SAS.
- *
- * <br/>Date:
+ * @author <a href="mailto:nikolazius@gmail.com">Nikolay
Zamosenchuk</a>
+ * @version $Id: IndexerCacheLoader.java 34360 2009-07-22 23:58:59Z nzamosenchuk $
*
- * @author <a href="karpenko.sergiy(a)gmail.com">Karpenko Sergiy</a>
- * @version $Id: CacheIndexer.java 111 2008-11-11 11:11:11Z serg $
*/
-@Deprecated
public class IndexerCacheLoader extends AbstractWriteOnlyCacheLoader
{
+ private final Log log = ExoLogger.getLogger(this.getClass().getName());
- /**
- * Logger instance for this class
- */
- private static final Log log = ExoLogger.getLogger(IndexerCacheLoader.class);
+ public static String ADDED = "$add".intern();
- //IndexInterceptorHolder searchManagerHolder = null;
+ public static String REMOVED = "$remove".intern();
- public IndexerCacheLoader()
- {
- }
+ private SearchManager searchManager;
- // @Inject
- // public void injectDependencies(IndexInterceptorHolder holder)
- // {
- // searchManagerHolder = holder;
- // }
-
- private String parseUUID(Fqn<String> fqn)
+ @Inject
+ public void init(SearchManager searchManager) throws RepositoryConfigurationException
{
- if (fqn.size() > 1)
- {
-
- // get only uuid
- String uuid = (String)fqn.get(1);
- //remove slash
- return uuid;
- }
- else
- {
- return null;
- }
+ this.searchManager = searchManager;
}
- private boolean isNode(Fqn<String> fqn)
- {
- String items = (String)fqn.get(0);
- String s = items.toString();
- return s.equals(JBossCacheStorage.NODES);
- }
-
@Override
public void put(List<Modification> modifications) throws Exception
{
- // if (searchManagerHolder == null || searchManagerHolder.get() == null)
- // {
- // return;
- // }
-
- // nodes that need to be removed from the index.
+ if (log.isDebugEnabled())
+ {
+ log.info("Received list of modifications");
+ }
final Set<String> removedNodes = new HashSet<String>();
- // nodes that need to be added to the index.
final Set<String> addedNodes = new HashSet<String>();
-
- // node thst must be updated
- final Set<String> updateNodes = new HashSet<String>();
-
- //final Map<String, List<ItemState>> updatedNodes = new
HashMap<String, List<ItemState>>();
-
for (Modification m : modifications)
{
- // check do we need skip modification
- // $NODES ,$PROPS cache-node modification and child cache-node
(/$NODES/nodeUUID/[]childNodeName) are skipped
- // we process only modifications with Fqn like /[$NODES | $PROPS]/UUID
- if (m.getFqn().size() == 2)
+ if (m.getType() == ModificationType.PUT_KEY_VALUE && m.getFqn().size()
== 1)
{
- String uuid = parseUUID(m.getFqn());
-
- switch (m.getType())
+ if (m.getKey().equals(ADDED) && m.getValue() instanceof
Set<?>)
{
- case PUT_DATA :
- if (isNode(m.getFqn()))
- {
- // add node
- addedNodes.add(uuid);
- }
- else
- {
- // this is property - do nothing
- }
- break;
- case PUT_DATA_ERASE :
- // update node
- if (isNode(m.getFqn()))
- {
- removedNodes.add(uuid);
- addedNodes.add(uuid);
- }
- else
- {
- // this is property - do nothing
- }
- break;
- case PUT_KEY_VALUE :
-
- if (!isNode(m.getFqn()))
- {
- PropertyData property = (PropertyData)m.getValue();
- uuid = property.getParentIdentifier();
- }
-
- if (addedNodes.contains(uuid))
- {
- // do nothing - node by uuid will be indexed
- }
- else
- {
- // update document by this uuid
- updateNodes.add(uuid);
- }
- break;
- case REMOVE_DATA :
- // update node
- if (isNode(m.getFqn()))
- {
- removedNodes.add(uuid);
- }
- else
- {
- // this is a property - do nothing
- }
- break;
- case REMOVE_KEY_VALUE :
- // removed property - do update node
- if (isNode(m.getFqn()))
- {
- if (!removedNodes.contains(uuid))
- {
- updateNodes.add(uuid);
- }
- }
- else
- {
- // this is a property - do nothing
- }
-
- break;
- case REMOVE_NODE :
- // if node - remove it, otherwise ignore it
- if (isNode(m.getFqn()))
- {
- removedNodes.add(uuid);
- }
- else
- {
- // this is a property - do nothing
- }
-
- break;
- case MOVE :
- // involve moving all children too
- move(m.getFqn(), m.getFqn2());
- if (isNode(m.getFqn()))
- {
- //remove first
- removedNodes.add(parseUUID(m.getFqn()));
- //add second
- addedNodes.add(parseUUID(m.getFqn2()));
- }
- else
- {
- // must be never happen
- throw new UnsupportedOperationException("Moving of properties
is unsupported.");
- }
- break;
- default :
- throw new CacheException("Unknown modification " +
m.getType());
+ addedNodes.addAll((Set<String>)m.getValue());
}
+ else if (m.getKey().equals(REMOVED) && m.getValue() instanceof
Set<?>)
+ {
+ removedNodes.addAll((Set<String>)m.getValue());
+ }
}
+ else
+ {
+ if (log.isDebugEnabled())
+ {
+ log.info("Received " + m.getType() + " modification,
skipping...");
+ }
+ }
}
-
- for (String uuid : updateNodes)
+ new StubSearchManager().updateIndex(addedNodes, removedNodes);
+ if (searchManager!=null)
{
- removedNodes.add(uuid);
- addedNodes.add(uuid);
+ searchManager.updateIndex(removedNodes, addedNodes);
}
+ }
- try
+ class StubSearchManager
+ {
+ public void updateIndex(Set<String> added, Set<String> removed)
{
- //searchManagerHolder.get().updateIndex(removedNodes, addedNodes);
+ for (String uuid : added)
+ {
+ System.out.println("A\t" + uuid);
+ }
+ for (String uuid : removed)
+ {
+ System.out.println("D\t" + uuid);
+ }
}
- catch (Exception e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
}
}
Added:
jcr/branches/1.12.0-JBC/component/core/src/main/java/org/exoplatform/services/jcr/impl/core/query/jbosscache/IndexerSingleStoreCacheLoader.java
===================================================================
---
jcr/branches/1.12.0-JBC/component/core/src/main/java/org/exoplatform/services/jcr/impl/core/query/jbosscache/IndexerSingleStoreCacheLoader.java
(rev 0)
+++
jcr/branches/1.12.0-JBC/component/core/src/main/java/org/exoplatform/services/jcr/impl/core/query/jbosscache/IndexerSingleStoreCacheLoader.java 2009-12-07
14:27:08 UTC (rev 934)
@@ -0,0 +1,101 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.exoplatform.services.jcr.impl.core.query.jbosscache;
+
+import org.exoplatform.services.log.ExoLogger;
+import org.exoplatform.services.log.Log;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.NodeSPI;
+import org.jboss.cache.loader.SingletonStoreCacheLoader;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.concurrent.Callable;
+
+/**
+ * @author <a href="mailto:nikolazius@gmail.com">Nikolay
Zamosenchuk</a>
+ * @version $Id: IndexerSingleStoraCacheLoader.java 34360 2009-07-22 23:58:59Z
nzamosenchuk $
+ *
+ */
+public class IndexerSingleStoreCacheLoader extends SingletonStoreCacheLoader
+{
+
+ private final Log log = ExoLogger.getLogger(this.getClass().getName());
+
+ @Override
+ protected void activeStatusChanged(boolean newActiveState) throws PushStateException
+ {
+ // at first change indexer mode
+ modeChanged(newActiveState);
+ // and them push states if needed
+ super.activeStatusChanged(newActiveState);
+
+ }
+
+ /**
+ * Changes indexer mode RO->RW, RW->RO
+ *
+ * @param enableWrite
+ * true when this node of cluster is coordinator, means that indexer is RW
+ * false means that indexer is read only, and this node of cluster is no a
coordinator
+ */
+ protected void modeChanged(boolean enableWrite)
+ {
+ }
+
+ @Override
+ protected Callable<?> createPushStateTask()
+ {
+ return new Callable()
+ {
+ public Object call() throws Exception
+ {
+ final boolean debugEnabled = log.isDebugEnabled();
+
+ if (debugEnabled)
+ log.debug("start pushing in-memory state to cache cacheLoader
collection");
+
+ final Set<String> removedNodes = new HashSet<String>();
+ final Set<String> addedNodes = new HashSet<String>();
+
+ // merging all lists stored in memory
+ Collection<NodeSPI> children = cache.getRoot().getChildrenDirect();
+ for (NodeSPI aChildren : children)
+ {
+ Fqn fqn = aChildren.getFqn();
+ Object value = cache.get(fqn, IndexerCacheLoader.ADDED);
+ if (value != null && value instanceof Set<?>)
+ {
+ addedNodes.addAll((Set<String>)value);
+ };
+ value = cache.get(fqn, IndexerCacheLoader.REMOVED);
+ if (value != null && value instanceof Set<?>)
+ {
+ removedNodes.addAll((Set<String>)value);
+ };
+ }
+ //TODO: recover logic is here, lists are: removedNodes and addedNodes
+ if (debugEnabled)
+ log.debug("in-memory state passed to cache cacheLoader
successfully");
+ return null;
+ }
+ };
+ }
+}