[exo-jcr-commits] exo-jcr SVN: r1315 - jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/dataflow/persistent/jbosscache.

do-not-reply at jboss.org do-not-reply at jboss.org
Thu Jan 7 11:38:57 EST 2010


Author: skabashnyuk
Date: 2010-01-07 11:38:57 -0500 (Thu, 07 Jan 2010)
New Revision: 1315

Added:
   jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/dataflow/persistent/jbosscache/BufferedJbossCache.java
Log:
EXOJCR-371: initial implementation of Buffered cache

Added: jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/dataflow/persistent/jbosscache/BufferedJbossCache.java
===================================================================
--- jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/dataflow/persistent/jbosscache/BufferedJbossCache.java	                        (rev 0)
+++ jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/dataflow/persistent/jbosscache/BufferedJbossCache.java	2010-01-07 16:38:57 UTC (rev 1315)
@@ -0,0 +1,716 @@
+package org.exoplatform.services.jcr.impl.dataflow.persistent.jbosscache;
+
+import org.exoplatform.services.log.ExoLogger;
+import org.exoplatform.services.log.Log;
+import org.jboss.cache.Cache;
+import org.jboss.cache.CacheException;
+import org.jboss.cache.CacheSPI;
+import org.jboss.cache.CacheStatus;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.InvocationContext;
+import org.jboss.cache.Node;
+import org.jboss.cache.NodeNotExistsException;
+import org.jboss.cache.Region;
+import org.jboss.cache.config.Configuration;
+import org.jboss.cache.interceptors.base.CommandInterceptor;
+import org.jgroups.Address;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.transaction.TransactionManager;
+
+public class BufferedJbossCache implements Cache<Serializable, Object>
+{
+   private final Log log = ExoLogger.getLogger(BufferedJbossCache.class);
+
+   /**
+    * Parent cache.
+    */
+   private final Cache<Serializable, Object> parentCache;
+
+   private ThreadLocal<List<ChangesContainer>> changesList = new ThreadLocal<List<ChangesContainer>>();
+
+   public BufferedJbossCache(Cache<Serializable, Object> parentCache)
+   {
+      super();
+      this.parentCache = parentCache;
+   }
+
+   /**
+    * Start buffering process.
+    */
+   public void beginTransaction()
+   {
+      changesList.set(new ArrayList<ChangesContainer>());
+   }
+
+   /**
+    * Sort changes and commit data to the cache.
+    */
+   public void commitTransaction()
+   {
+
+      List<ChangesContainer> changesContainer = changesList.get();
+      if (changesContainer == null)
+      {
+         throw new IllegalStateException("changesContainer == null");
+      }
+      //log.info("Before=" + changesContainer.toString());
+      Collections.sort(changesContainer);
+      //log.info("After=" + changesContainer.toString());
+      for (ChangesContainer cacheChange : changesContainer)
+      {
+         cacheChange.apply();
+      }
+      changesList.set(null);
+   }
+
+   /**
+    * Forget about changes
+    */
+   public void rollbackTransaction()
+   {
+      changesList.set(null);
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#addCacheListener(java.lang.Object)
+    */
+   public void addCacheListener(Object listener)
+   {
+
+      parentCache.addCacheListener(listener);
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#addInterceptor(org.jboss.cache.interceptors.base.CommandInterceptor, java.lang.Class)
+    */
+   public void addInterceptor(CommandInterceptor i, Class<? extends CommandInterceptor> afterInterceptor)
+   {
+      parentCache.addInterceptor(i, afterInterceptor);
+
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#addInterceptor(org.jboss.cache.interceptors.base.CommandInterceptor, int)
+    */
+   public void addInterceptor(CommandInterceptor i, int position)
+   {
+      parentCache.addInterceptor(i, position);
+
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#clearData(org.jboss.cache.Fqn)
+    */
+   public void clearData(Fqn fqn)
+   {
+      parentCache.clearData(fqn);
+
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#clearData(java.lang.String)
+    */
+   public void clearData(String fqn)
+   {
+      parentCache.clearData(fqn);
+
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#create()
+    */
+   public void create() throws CacheException
+   {
+      parentCache.create();
+
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#destroy()
+    */
+   public void destroy()
+   {
+      parentCache.destroy();
+
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#endBatch(boolean)
+    */
+   public void endBatch(boolean successful)
+   {
+
+      parentCache.endBatch(successful);
+
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#evict(org.jboss.cache.Fqn, boolean)
+    */
+   public void evict(Fqn fqn, boolean recursive)
+   {
+      parentCache.evict(fqn, recursive);
+
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#evict(org.jboss.cache.Fqn)
+    */
+   public void evict(Fqn fqn)
+   {
+      parentCache.evict(fqn);
+
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#get(org.jboss.cache.Fqn, java.lang.Object)
+    */
+   public Object get(Fqn fqn, Serializable key)
+   {
+
+      return parentCache.get(fqn, key);
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#get(java.lang.String, java.lang.Object)
+    */
+   public Object get(String fqn, Serializable key)
+   {
+
+      return parentCache.get(fqn, key);
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#getCacheListeners()
+    */
+   public Set<Object> getCacheListeners()
+   {
+
+      return parentCache.getCacheListeners();
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#getCacheStatus()
+    */
+   public CacheStatus getCacheStatus()
+   {
+
+      return parentCache.getCacheStatus();
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#getChildrenNames(org.jboss.cache.Fqn)
+    */
+   public Set<Object> getChildrenNames(Fqn fqn)
+   {
+
+      return parentCache.getChildrenNames(fqn);
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#getChildrenNames(java.lang.String)
+    */
+   public Set<String> getChildrenNames(String fqn)
+   {
+
+      return parentCache.getChildrenNames(fqn);
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#getConfiguration()
+    */
+   public Configuration getConfiguration()
+   {
+
+      return parentCache.getConfiguration();
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#getData(org.jboss.cache.Fqn)
+    */
+   public Map<Serializable, Object> getData(Fqn fqn)
+   {
+
+      return parentCache.getData(fqn);
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#getInvocationContext()
+    */
+   public InvocationContext getInvocationContext()
+   {
+
+      return parentCache.getInvocationContext();
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#getKeys(org.jboss.cache.Fqn)
+    */
+   public Set<Serializable> getKeys(Fqn fqn)
+   {
+
+      return parentCache.getKeys(fqn);
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#getKeys(java.lang.String)
+    */
+   public Set<Serializable> getKeys(String fqn)
+   {
+
+      return parentCache.getKeys(fqn);
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#getLocalAddress()
+    */
+   public Address getLocalAddress()
+   {
+
+      return parentCache.getLocalAddress();
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#getMembers()
+    */
+   public List<Address> getMembers()
+   {
+
+      return parentCache.getMembers();
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#getNode(org.jboss.cache.Fqn)
+    */
+   public Node<Serializable, Object> getNode(Fqn fqn)
+   {
+
+      return parentCache.getNode(fqn);
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#getNode(java.lang.String)
+    */
+   public Node<Serializable, Object> getNode(String fqn)
+   {
+
+      return parentCache.getNode(fqn);
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#getRegion(org.jboss.cache.Fqn, boolean)
+    */
+   public Region getRegion(Fqn fqn, boolean createIfAbsent)
+   {
+
+      return parentCache.getRegion(fqn, createIfAbsent);
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#getRoot()
+    */
+   public Node<Serializable, Object> getRoot()
+   {
+
+      return parentCache.getRoot();
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#getVersion()
+    */
+   public String getVersion()
+   {
+
+      return parentCache.getVersion();
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#isLeaf(org.jboss.cache.Fqn)
+    */
+   public boolean isLeaf(Fqn fqn)
+   {
+
+      return parentCache.isLeaf(fqn);
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#isLeaf(java.lang.String)
+    */
+   public boolean isLeaf(String fqn)
+   {
+
+      return parentCache.isLeaf(fqn);
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#move(org.jboss.cache.Fqn, org.jboss.cache.Fqn)
+    */
+   public void move(Fqn nodeToMove, Fqn newParent) throws NodeNotExistsException
+   {
+      parentCache.move(nodeToMove, newParent);
+
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#move(java.lang.String, java.lang.String)
+    */
+   public void move(String nodeToMove, String newParent) throws NodeNotExistsException
+   {
+
+      parentCache.move(nodeToMove, newParent);
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#put(org.jboss.cache.Fqn, java.util.Map)
+    */
+   public void put(Fqn fqn, Map<? extends Serializable, ? extends Object> data)
+   {
+      //parentCache.put(fqn, data);
+      List<ChangesContainer> changesContainer = changesList.get();
+      if (changesContainer == null)
+      {
+         throw new IllegalStateException("changesContainer == null");
+      }
+      changesContainer.add(new PutObjectContainer(fqn, data, parentCache));
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#put(org.jboss.cache.Fqn, java.lang.Object, java.lang.Object)
+    */
+   public Object put(Fqn fqn, Serializable key, Object value)
+   {
+      List<ChangesContainer> changesContainer = changesList.get();
+      if (changesContainer == null)
+      {
+         throw new IllegalStateException("changesContainer == null");
+      }
+      changesContainer.add(new PutKeyValueContainer(fqn, key, value, parentCache));
+
+      return parentCache.get(fqn, key);
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#put(java.lang.String, java.util.Map)
+    */
+   public void put(String fqn, Map<? extends Serializable, ? extends Object> data)
+   {
+      throw new IllegalStateException(
+         "Unexpected method call use  put(Fqn fqn, Map<? extends Serializable, ? extends Object> data)");
+      //parentCache.put(fqn, data);
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#put(java.lang.String, java.lang.Object, java.lang.Object)
+    */
+   public Object put(String fqn, Serializable key, Object value)
+   {
+
+      throw new IllegalStateException("Unexpected method call use  put(Fqn fqn, Serializable key, Object value)");
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#putForExternalRead(org.jboss.cache.Fqn, java.lang.Object, java.lang.Object)
+    */
+   public void putForExternalRead(Fqn fqn, Serializable key, Object value)
+   {
+
+      throw new IllegalStateException("Unexpected method call ");
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#remove(org.jboss.cache.Fqn, java.lang.Object)
+    */
+   public Object remove(Fqn fqn, Serializable key)
+   {
+      List<ChangesContainer> changesContainer = changesList.get();
+      if (changesContainer == null)
+      {
+         throw new IllegalStateException("changesContainer == null");
+      }
+      changesContainer.add(new RemoveKeyContainer(fqn, key, parentCache));
+      return parentCache.get(fqn, key);
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#remove(java.lang.String, java.lang.Object)
+    */
+   public Object remove(String fqn, Serializable key)
+   {
+
+      throw new IllegalStateException("Unexpected method call. Use  remove(Fqn fqn, Serializable key)");
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#removeCacheListener(java.lang.Object)
+    */
+   public void removeCacheListener(Object listener)
+   {
+
+      parentCache.removeCacheListener(listener);
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#removeInterceptor(java.lang.Class)
+    */
+   public void removeInterceptor(Class<? extends CommandInterceptor> interceptorType)
+   {
+
+      parentCache.removeInterceptor(interceptorType);
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#removeInterceptor(int)
+    */
+   public void removeInterceptor(int position)
+   {
+
+      parentCache.removeInterceptor(position);
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#removeNode(org.jboss.cache.Fqn)
+    */
+   public boolean removeNode(Fqn fqn)
+   {
+      List<ChangesContainer> changesContainer = changesList.get();
+      if (changesContainer == null)
+      {
+         throw new IllegalStateException("changesContainer == null");
+      }
+      changesContainer.add(new RemoveNodeContainer(fqn, parentCache));
+      return true;
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#removeNode(java.lang.String)
+    */
+   public boolean removeNode(String fqn)
+   {
+
+      throw new IllegalStateException("Unexpected method call. Use  remove removeNode(Fqn fqn)");
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#removeRegion(org.jboss.cache.Fqn)
+    */
+   public boolean removeRegion(Fqn fqn)
+   {
+
+      throw new IllegalStateException("Unexpected method call ");
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#setInvocationContext(org.jboss.cache.InvocationContext)
+    */
+   public void setInvocationContext(InvocationContext ctx)
+   {
+      parentCache.setInvocationContext(ctx);
+
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#start()
+    */
+   public void start() throws CacheException
+   {
+
+      parentCache.start();
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#startBatch()
+    */
+   public void startBatch()
+   {
+      parentCache.startBatch();
+
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.cache.Cache#stop()
+    */
+   public void stop()
+   {
+      parentCache.stop();
+
+   }
+
+   public TransactionManager getTransactionManager()
+   {
+      return ((CacheSPI<Serializable, Object>)parentCache).getTransactionManager();
+   }
+
+   interface ChangesType
+   {
+      int REMOVED = 0;
+
+      int ADDED = 1;
+
+   }
+
+   /**
+    * Container for changes
+    * @author sj
+    *
+    */
+   private abstract class ChangesContainer implements Comparable<ChangesContainer>
+   {
+      protected final Fqn fqn;
+
+      protected final int changesType;
+
+      protected final Cache<Serializable, Object> cache;
+
+      public ChangesContainer(Fqn fqn, int changesType, Cache<Serializable, Object> cache)
+      {
+         super();
+         this.fqn = fqn;
+         this.changesType = changesType;
+         this.cache = cache;
+      }
+
+      /**
+       * @return the fqn
+       */
+      public Fqn getFqn()
+      {
+         return fqn;
+      }
+
+      /**
+       * @return the changesType
+       */
+      public int getChangesType()
+      {
+         return changesType;
+      }
+
+      /* (non-Javadoc)
+       * @see java.lang.Object#toString()
+       */
+      @Override
+      public String toString()
+      {
+
+         return fqn + " type=" + changesType;
+      }
+
+      public int compareTo(ChangesContainer o)
+      {
+//         if (changesType != o.getChangesType())
+//         {
+//            //removed first
+//            return changesType == ChangesType.REMOVED ? -1 : 1;
+//         }
+
+         return fqn.compareTo(o.getFqn());
+      }
+
+      public abstract void apply();
+   }
+
+   /**
+    * Put object container;
+    * @author sj
+    *
+    */
+   private class PutObjectContainer extends ChangesContainer
+   {
+      private final Map<? extends Serializable, ? extends Object> data;
+
+      public PutObjectContainer(Fqn fqn, Map<? extends Serializable, ? extends Object> data,
+         Cache<Serializable, Object> cache)
+      {
+         super(fqn, ChangesType.ADDED, cache);
+
+         this.data = data;
+      }
+
+      @Override
+      public void apply()
+      {
+         cache.put(fqn, data);
+      }
+   }
+
+   /**
+    * Put  container.
+    * @author sj
+    *
+    */
+   private class PutKeyValueContainer extends ChangesContainer
+   {
+      private final Serializable key;
+
+      private final Object value;
+
+      public PutKeyValueContainer(Fqn fqn, Serializable key, Object value, Cache<Serializable, Object> cache)
+      {
+         super(fqn, ChangesType.ADDED, cache);
+         // TODO Auto-generated constructor stub
+         this.key = key;
+         this.value = value;
+      }
+
+      @Override
+      public void apply()
+      {
+         cache.put(fqn, key, value);
+      }
+   }
+
+   /**
+    * Remove container.
+    * @author sj
+    *
+    */
+   private class RemoveKeyContainer extends ChangesContainer
+   {
+      private final Serializable key;
+
+      public RemoveKeyContainer(Fqn fqn, Serializable key, Cache<Serializable, Object> cache)
+      {
+         super(fqn, ChangesType.REMOVED, cache);
+         this.key = key;
+      }
+
+      @Override
+      public void apply()
+      {
+         cache.remove(fqn, key);
+
+      }
+
+   }
+
+   /**
+    * Remove container.
+    * @author sj
+    *
+    */
+   private class RemoveNodeContainer extends ChangesContainer
+   {
+
+      public RemoveNodeContainer(Fqn fqn, Cache<Serializable, Object> cache)
+      {
+         super(fqn, ChangesType.REMOVED, cache);
+
+      }
+
+      @Override
+      public void apply()
+      {
+         cache.removeNode(fqn);
+
+      }
+
+   }
+
+}


Property changes on: jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/dataflow/persistent/jbosscache/BufferedJbossCache.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain



More information about the exo-jcr-commits mailing list