[jboss-cvs] JBossCache/src/org/jboss/cache/loader/jdbm ...

Manik Surtani manik at jboss.org
Tue Jun 19 12:57:43 EDT 2007


  User: msurtani
  Date: 07/06/19 12:57:43

  Modified:    src/org/jboss/cache/loader/jdbm  JdbmCacheLoader.java
  Log:
  Thread safety
  
  Revision  Changes    Path
  1.27      +137 -85   JBossCache/src/org/jboss/cache/loader/jdbm/JdbmCacheLoader.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: JdbmCacheLoader.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/loader/jdbm/JdbmCacheLoader.java,v
  retrieving revision 1.26
  retrieving revision 1.27
  diff -u -b -r1.26 -r1.27
  --- JdbmCacheLoader.java	19 Jun 2007 10:45:43 -0000	1.26
  +++ JdbmCacheLoader.java	19 Jun 2007 16:57:43 -0000	1.27
  @@ -14,6 +14,7 @@
   import org.jboss.cache.Modification;
   import org.jboss.cache.config.CacheLoaderConfig.IndividualCacheLoaderConfig;
   import org.jboss.cache.loader.AbstractCacheLoader;
  +import org.jboss.cache.lock.StripedLock;
   
   import java.io.File;
   import java.io.IOException;
  @@ -56,7 +57,7 @@
    * plans to fix this.
    *
    * @author Elias Ross
  - * @version $Id: JdbmCacheLoader.java,v 1.26 2007/06/19 10:45:43 msurtani Exp $
  + * @version $Id: JdbmCacheLoader.java,v 1.27 2007/06/19 16:57:43 msurtani Exp $
    */
   @ThreadSafe
   public class JdbmCacheLoader extends AbstractCacheLoader
  @@ -72,6 +73,7 @@
      private RecordManager recman;
      private BTree tree;
      private Map<Object, List<Modification>> transactions = new ConcurrentHashMap<Object, List<Modification>>();
  +   private StripedLock lock = new StripedLock();
   
      /*
       * Service implementation -- lifecycle methods.
  @@ -94,7 +96,9 @@
      public void start()
            throws Exception
      {
  -
  +      lock.acquireLock(Fqn.ROOT, true);
  +      try
  +      {
         log.trace("Starting JdbmCacheLoader instance.");
         checkNotOpen();
         checkNonNull(cache, "CacheSPI object is required");
  @@ -143,6 +147,11 @@
            throw e;
         }
      }
  +      finally
  +      {
  +         lock.releaseLock(Fqn.ROOT);
  +      }
  +   }
   
      /**
       * Opens all databases and initializes database related information.
  @@ -268,11 +277,15 @@
         {
            log.trace("getChildrenNames " + name);
         }
  -
  -      synchronized (tree)
  +      lock.acquireLock(name, false);
  +      try
         {
            return getChildrenNames0(name);
         }
  +      finally
  +      {
  +         lock.releaseLock(name);
  +      }
      }
   
      private Set<String> getChildrenNames0(Fqn name) throws IOException
  @@ -332,7 +345,9 @@
   
         checkOpen();
         checkNonNull(name, "name");
  -
  +      lock.acquireLock(name, false);
  +      try
  +      {
         if (tree.find(name) == null)
         {
            if (log.isTraceEnabled())
  @@ -346,8 +361,6 @@
         Tuple t = new Tuple();
         Map map = new HashMap();
   
  -      synchronized (tree)
  -      {
            TupleBrowser browser = tree.browse(keys);
            while (browser.getNext(t))
            {
  @@ -360,7 +373,6 @@
               Object v = t.getValue();
               map.put(nullUnmask(k), nullUnmask(v));
            }
  -      }
   
         if (log.isTraceEnabled())
         {
  @@ -369,14 +381,27 @@
   
         return map;
      }
  +      finally
  +      {
  +         lock.releaseLock(name);
  +      }
  +   }
   
      /**
       * Returns whether the given node exists.
       */
      public boolean exists(Fqn name) throws IOException
      {
  +      lock.acquireLock(name, false);
  +      try
  +      {
         return tree.find(name) != null;
      }
  +      finally
  +      {
  +         lock.releaseLock(name);
  +      }
  +   }
   
      private void commit() throws Exception
      {
  @@ -390,6 +415,7 @@
       */
      public Object put(Fqn name, Object key, Object value) throws Exception
      {
  +      lock.acquireLock(name, true);
         try
         {
            return put0(name, key, value);
  @@ -397,6 +423,7 @@
         finally
         {
            commit();
  +         lock.releaseLock(name);
         }
      }
   
  @@ -421,8 +448,16 @@
       */
      public void put(Fqn name, Map values) throws Exception
      {
  +      lock.acquireLock(name, true);
  +      try
  +      {
         put0(name, values);
  +      }
  +      finally
  +      {
         commit();
  +         lock.releaseLock(name);
  +      }
      }
   
      private void put0(Fqn name, Map<?, ?> values) throws Exception
  @@ -560,8 +595,16 @@
      public void remove(Fqn name)
            throws Exception
      {
  +      lock.acquireLock(name, true);
  +      try
  +      {
         erase0(name);
  +      }
  +      finally
  +      {
         commit();
  +         lock.releaseLock(name);
  +      }
      }
   
      /**
  @@ -572,7 +615,7 @@
      public Object remove(Fqn name, Object key)
            throws Exception
      {
  -
  +      lock.acquireLock(name, true);
         try
         {
            return eraseKey0(name, key);
  @@ -580,6 +623,7 @@
         finally
         {
            commit();
  +         lock.releaseLock(name);
         }
      }
   
  @@ -589,8 +633,16 @@
      public void removeData(Fqn name)
            throws Exception
      {
  +      lock.acquireLock(name, true);
  +      try
  +      {
         erase0(name, false);
      }
  +      finally
  +      {
  +         lock.releaseLock(name);
  +      }
  +   }
   
      /**
       * Applies and commits the given modifications in one transaction.
  
  
  



More information about the jboss-cvs-commits mailing list