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

Manik Surtani msurtani at jboss.com
Fri Sep 22 12:27:55 EDT 2006


  User: msurtani
  Date: 06/09/22 12:27:55

  Modified:    src/org/jboss/cache/loader/jdbm  JdbmCacheLoader.java
  Log:
  - Modification types to Enums.
  - Abstracted put(List)
  
  Revision  Changes    Path
  1.14      +230 -155  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.13
  retrieving revision 1.14
  diff -u -b -r1.13 -r1.14
  --- JdbmCacheLoader.java	12 Sep 2006 20:16:56 -0000	1.13
  +++ JdbmCacheLoader.java	22 Sep 2006 16:27:55 -0000	1.14
  @@ -8,46 +8,47 @@
   import jdbm.helper.TupleBrowser;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  +import org.jboss.cache.CacheSPI;
   import org.jboss.cache.Fqn;
   import org.jboss.cache.Modification;
  -import org.jboss.cache.CacheSPI;
  -import org.jboss.cache.buddyreplication.BuddyManager;
   import org.jboss.cache.loader.AbstractCacheLoader;
  -import org.jboss.cache.loader.NodeData;
   import org.jboss.cache.marshall.RegionManager;
   import org.jboss.cache.optimistic.FqnComparator;
   
  -import java.io.ByteArrayInputStream;
  -import java.io.ByteArrayOutputStream;
   import java.io.File;
   import java.io.IOException;
  -import java.io.ObjectInputStream;
  -import java.io.ObjectOutputStream;
  -import java.util.*;
  +import java.util.Collections;
  +import java.util.HashMap;
  +import java.util.HashSet;
  +import java.util.Iterator;
  +import java.util.List;
  +import java.util.Map;
  +import java.util.Properties;
  +import java.util.Set;
   
   
   /**
    * A persistent <code>CacheLoader</code> based on the JDBM project.
    * See http://jdbm.sourceforge.net/ .
    * Does not support transaction isolation.
  - *
  + * <p/>
    * <p>The configuration string format is:</p>
    * <pre>environmentDirectoryName[#databaseName]</pre>
    * <p>where databaseName, if omitted, defaults to the ClusterName property
    * of the TreeCache.</p>
    * <p/>
    * Data is sorted out like:
  -<pre>
  -/ = N
  -/node1 = N
  -/node1/K/k1 = v1
  -/node1/K/k2 = v2
  -/node2 = N
  -/node2/node3 = N
  -/node2/node3/K/k1 = v1
  -/node2/node3/K/k2 = v2
  -/node2/node4 = N
  -</pre>
  + * <pre>
  + * / = N
  + * /node1 = N
  + * /node1/K/k1 = v1
  + * /node1/K/k2 = v2
  + * /node2 = N
  + * /node2/node3 = N
  + * /node2/node3/K/k1 = v1
  + * /node2/node3/K/k2 = v2
  + * /node2/node4 = N
  + * </pre>
    * N represents a node, K represents a key block. k and v represent key/value
    * pairs.
    * <p/>
  @@ -55,7 +56,7 @@
    * plans to fix this.
    *
    * @author Elias Ross
  - * @version $Id: JdbmCacheLoader.java,v 1.13 2006/09/12 20:16:56 vblagojevic Exp $
  + * @version $Id: JdbmCacheLoader.java,v 1.14 2006/09/22 16:27:55 msurtani Exp $
    */
   public class JdbmCacheLoader extends AbstractCacheLoader
   {
  @@ -77,11 +78,13 @@
       * Note that setConfig() and setCache() are called before create().
       */
   
  -   public void create() throws Exception {
  +   public void create() throws Exception
  +   {
         checkNotOpen();
      }
   
  -   public void destroy() {
  +   public void destroy()
  +   {
      }
   
      /**
  @@ -89,14 +92,16 @@
       * string.  The environment and databases are created if necessary.
       */
      public void start()
  -      throws Exception {
  +           throws Exception
  +   {
   
         log.trace("Starting JdbmCacheLoader instance.");
         checkNotOpen();
         checkNonNull(cache, "CacheSPI object is required");
   
  -      if (locationStr == null) {
  -         locationStr=System.getProperty("java.io.tmpdir");
  +      if (locationStr == null)
  +      {
  +         locationStr = System.getProperty("java.io.tmpdir");
         }
   
         // test location
  @@ -107,23 +112,31 @@
             if (!created) throw new IOException("Unable to create cache loader location " + location);
   
         }
  -      if (!location.isDirectory()) throw new IOException("Cache loader location [" + location + "] is not a directory!");
  -       
  +      if (!location.isDirectory())
  +      {
  +         throw new IOException("Cache loader location [" + location + "] is not a directory!");
  +      }
   
         /* Parse config string. */
         File homeDir;
         int offset = locationStr.indexOf('#');
  -      if (offset >= 0 && offset < locationStr.length() - 1) {
  +      if (offset >= 0 && offset < locationStr.length() - 1)
  +      {
            homeDir = new File(locationStr.substring(0, offset));
            cacheDbName = locationStr.substring(offset + 1);
  -      } else {
  +      }
  +      else
  +      {
            homeDir = new File(locationStr);
            cacheDbName = cache.getClusterName();
         }
   
  -      try {
  +      try
  +      {
            openDatabase(new File(homeDir, cacheDbName));
  -      } catch (Exception e) {
  +      }
  +      catch (Exception e)
  +      {
            destroy();
            throw e;
         }
  @@ -142,10 +155,13 @@
         recman = RecordManagerFactory.createRecordManager(f.toString(), props);
         long recid = recman.getNamedObject(NAME);
         log.debug(NAME + " located as " + recid);
  -      if (recid == 0) {
  +      if (recid == 0)
  +      {
         tree = BTree.createInstance(recman, new FqnComparator());
            recman.setNamedObject(NAME, tree.getRecid());
  -      } else {
  +      }
  +      else
  +      {
            tree = BTree.load(recman, recid);
         }
   
  @@ -156,11 +172,16 @@
       * Closes all databases, ignoring exceptions, and nulls references to all
       * database related information.
       */
  -   private void closeDatabases() {
  -      if (recman != null) {
  -         try {
  +   private void closeDatabases()
  +   {
  +      if (recman != null)
  +      {
  +         try
  +         {
               recman.close();
  -         } catch (Exception shouldNotOccur) {
  +         }
  +         catch (Exception shouldNotOccur)
  +         {
               log.warn("Caught unexpected exception", shouldNotOccur);
            }
         }
  @@ -171,7 +192,8 @@
      /**
       * Closes the databases and environment, and nulls references to them.
       */
  -   public void stop() {
  +   public void stop()
  +   {
         log.debug("stop");
         closeDatabases();
      }
  @@ -183,16 +205,18 @@
      /**
       * Sets the configuration string for this cache loader.
       */
  -   public void setConfig(Properties props) {
  +   public void setConfig(Properties props)
  +   {
         checkNotOpen();
  -      locationStr = props != null? props.getProperty("location") : null;
  +      locationStr = props != null ? props.getProperty("location") : null;
         if (log.isTraceEnabled()) log.trace("Configuring cache loader with location = " + locationStr);
      }
   
      /**
       * Sets the TreeCache owner of this cache loader.
       */
  -   public void setCache(CacheSPI c) {
  +   public void setCache(CacheSPI c)
  +   {
         super.setCache(c);
         checkNotOpen();
      }
  @@ -200,14 +224,16 @@
      /**
       * Returns a special FQN for keys of a node.
       */
  -   private Fqn keys(Fqn name) {
  +   private Fqn keys(Fqn name)
  +   {
         return new Fqn(name, KEYS);
      }
   
      /**
       * Returns a special FQN for key of a node.
       */
  -   private Fqn key(Fqn name, Object key) {
  +   private Fqn key(Fqn name, Object key)
  +   {
         return new Fqn(name, KEYS, nullMask(key));
      }
   
  @@ -223,23 +249,31 @@
      {
   
         if (log.isTraceEnabled())
  +      {
            log.trace("getChildrenNames " + name);
  +      }
   
  -      synchronized (tree) {
  +      synchronized (tree)
  +      {
            return getChildrenNames0(name);
         }
      }
   
  -   private Set getChildrenNames0(Fqn name) throws IOException {
  +   private Set getChildrenNames0(Fqn name) throws IOException
  +   {
         TupleBrowser browser = tree.browse(name);
         Tuple t = new Tuple();
   
  -      if (browser.getNext(t)) {
  -         if (!t.getValue().equals(NODE)) {
  +      if (browser.getNext(t))
  +      {
  +         if (!t.getValue().equals(NODE))
  +         {
               log.trace(" not a node");
               return null;
            }
  -      } else {
  +      }
  +      else
  +      {
            log.trace(" no nodes");
            return null;
         }
  @@ -248,17 +282,24 @@
   
         // Want only /a/b/c/X nodes
         int depth = name.size() + 1;
  -      while (browser.getNext(t)) {
  -         Fqn fqn = (Fqn)t.getKey();
  +      while (browser.getNext(t))
  +      {
  +         Fqn fqn = (Fqn) t.getKey();
            int size = fqn.size();
            if (size < depth)
  +         {
               break;
  +         }
            if (size == depth && t.getValue().equals(NODE))
  +         {
               set.add(fqn.getLast());
         }
  +      }
   
         if (set.isEmpty())
  +      {
            return null;
  +      }
   
         return Collections.unmodifiableSet(set);
      }
  @@ -270,14 +311,18 @@
       * environment.
       */
      public Map get(Fqn name)
  -      throws Exception {
  +           throws Exception
  +   {
   
         checkOpen();
         checkNonNull(name, "name");
   
  -      if (tree.find(name) == null) {
  +      if (tree.find(name) == null)
  +      {
            if (log.isTraceEnabled())
  +         {
               log.trace("get, no node: " + name);
  +         }
            return null;
         }
   
  @@ -285,12 +330,16 @@
         Tuple t = new Tuple();
         Map map = new HashMap();
   
  -      synchronized (tree) {
  +      synchronized (tree)
  +      {
            TupleBrowser browser = tree.browse(keys);
  -         while (browser.getNext(t)) {
  -            Fqn fqn = (Fqn)t.getKey();
  +         while (browser.getNext(t))
  +         {
  +            Fqn fqn = (Fqn) t.getKey();
               if (!fqn.isChildOf(keys))
  +            {
                  break;
  +            }
               Object k = fqn.getLast();
               Object v = t.getValue();
               map.put(nullUnmask(k), nullUnmask(v));
  @@ -298,7 +347,9 @@
         }
   
         if (log.isTraceEnabled())
  +      {
            log.trace("get " + name + " map=" + map);
  +      }
   
         return map;
      }
  @@ -306,11 +357,13 @@
      /**
       * Returns whether the given node exists.
       */
  -   public boolean exists(Fqn name) throws IOException {
  +   public boolean exists(Fqn name) throws IOException
  +   {
         return tree.find(name) != null;
      }
   
  -   private void commit() throws Exception {
  +   private void commit() throws Exception
  +   {
         recman.commit();
      }
   
  @@ -319,21 +372,28 @@
       * Intended to be used in a non-transactional environment, but will use
       * auto-commit in a transactional environment.
       */
  -   public Object put(Fqn name, Object key, Object value) throws Exception {
  -      try {
  +   public Object put(Fqn name, Object key, Object value) throws Exception
  +   {
  +      try
  +      {
            return put0(name, key, value);
  -      } finally {
  +      }
  +      finally
  +      {
            commit();
         }
      }
   
  -   private Object put0(Fqn name, Object key, Object value) throws Exception {
  +   private Object put0(Fqn name, Object key, Object value) throws Exception
  +   {
         checkNonNull(name, "name");
         makeNode(name);
         Fqn rec = key(name, key);
         Object oldValue = insert(rec, value);
         if (log.isTraceEnabled())
  +      {
            log.trace("put " + rec + " value=" + value + " old=" + oldValue);
  +      }
         return oldValue;
      }
   
  @@ -343,21 +403,27 @@
       * Intended to be used in a non-transactional environment, but will use
       * auto-commit in a transactional environment.
       */
  -   public void put(Fqn name, Map values) throws Exception {
  +   public void put(Fqn name, Map values) throws Exception
  +   {
         put0(name, values);
         commit();
      }
   
  -   private void put0(Fqn name, Map values) throws Exception {
  +   private void put0(Fqn name, Map values) throws Exception
  +   {
         if (log.isTraceEnabled())
  +      {
            log.trace("put " + name + " values=" + values);
  +      }
         makeNode(name);
  -      if (values == null) {
  +      if (values == null)
  +      {
            return;
         }
         Iterator i = values.entrySet().iterator();
  -      while (i.hasNext()) {
  -         Map.Entry me = (Map.Entry)i.next();
  +      while (i.hasNext())
  +      {
  +         Map.Entry me = (Map.Entry) i.next();
            Fqn rec = key(name, me.getKey());
            insert(rec, nullMask(me.getValue()));
         }
  @@ -366,21 +432,28 @@
      /**
       * Marks a FQN as a node.
       */
  -   private void makeNode(Fqn fqn) throws IOException {
  +   private void makeNode(Fqn fqn) throws IOException
  +   {
         if (exists(fqn))
  +      {
           return;
  +      }
         int size = fqn.size();
         // TODO should not modify so darn often
  -      for (int i = size; i >= 0; i--) {
  +      for (int i = size; i >= 0; i--)
  +      {
            Fqn child = fqn.getFqnChild(i);
            Object existing = tree.insert(child, NODE, false);
            if (existing != null)
  +         {
              break;
         }
      }
  +   }
   
  -   private Object insert(Fqn fqn, Object value) throws IOException {
  -      return nullUnmask( tree.insert(fqn, nullMask(value), true) );
  +   private Object insert(Fqn fqn, Object value) throws IOException
  +   {
  +      return nullUnmask(tree.insert(fqn, nullMask(value), true));
      }
   
      /**
  @@ -397,18 +470,27 @@
         throws IOException
      {
         if (log.isTraceEnabled())
  +      {
            log.trace("erase " + name + " self=" + self);
  -      synchronized (tree) {
  +      }
  +      synchronized (tree)
  +      {
            TupleBrowser browser = tree.browse(name);
            Tuple t = new Tuple();
  -         if (browser.getNext(t)) {
  +         if (browser.getNext(t))
  +         {
               if (self)
  +            {
                  tree.remove(t.getKey());
            }
  -         while (browser.getNext(t)) {
  -            Fqn fqn = (Fqn)t.getKey();
  +         }
  +         while (browser.getNext(t))
  +         {
  +            Fqn fqn = (Fqn) t.getKey();
               if (!fqn.isChildOf(name))
  +            {
                  break;
  +            }
               tree.remove(fqn);
            }
         }
  @@ -422,11 +504,16 @@
         throws IOException
      {
         if (log.isTraceEnabled())
  +      {
            log.trace("eraseKey " + name + " key " + key);
  +      }
         Fqn fqnKey = key(name, key);
  -      try {
  +      try
  +      {
            return tree.remove(fqnKey);
  -      } catch (IllegalArgumentException e) {
  +      }
  +      catch (IllegalArgumentException e)
  +      {
            // Seems to be harmless
            // log.warn("IllegalArgumentException for " + fqnKey);
            // dump();
  @@ -439,52 +526,17 @@
       * Intended to be used in a non-transactional environment, but will use
       * auto-commit in a transactional environment.
       */
  -   public void put(List modifications)
  -      throws Exception {
  +   public void put(List<Modification> modifications)
  +           throws Exception
  +   {
   
         checkOpen();
         checkNonNull(modifications, "modifications");
   
  -      apply(modifications);
  +      super.put(modifications);
         commit();
      }
   
  -   private void apply(List modifications)
  -      throws Exception
  -   {
  -      for (Iterator i = modifications.iterator(); i.hasNext();) {
  -         Modification mod = (Modification) i.next();
  -         Fqn name = mod.getFqn();
  -         Object oldVal;
  -         switch (mod.getType()) {
  -            case Modification.PUT_KEY_VALUE:
  -               oldVal = put0(name, mod.getKey(), mod.getValue());
  -               mod.setOldValue(oldVal);
  -               break;
  -            case Modification.PUT_DATA:
  -               put0(name, mod.getData());
  -               break;
  -            case Modification.PUT_DATA_ERASE:
  -               erase0(name);
  -               put0(name, mod.getData());
  -               break;
  -            case Modification.REMOVE_KEY_VALUE:
  -               oldVal = eraseKey0(name, mod.getKey());
  -               mod.setOldValue(oldVal);
  -               break;
  -            case Modification.REMOVE_NODE:
  -               erase0(name);
  -               break;
  -            case Modification.REMOVE_DATA:
  -               erase0(name, false);
  -               break;
  -            default:
  -               throw new IllegalArgumentException(
  -                     "Unknown Modification type: " + mod.getType());
  -         }
  -      }
  -   }
  -
      /**
       * Deletes the node for a given FQN and all its descendent nodes.
       * Intended to be used in a non-transactional environment, but will use
  @@ -503,11 +555,15 @@
       * auto-commit in a transactional environment.
       */
      public Object remove(Fqn name, Object key)
  -      throws Exception {
  +           throws Exception
  +   {
   
  -      try {
  +      try
  +      {
            return eraseKey0(name, key);
  -      } finally {
  +      }
  +      finally
  +      {
            commit();
         }
      }
  @@ -528,18 +584,25 @@
         throws Exception
      {
         if (onePhase)
  +      {
            put(modifications);
  +      }
         else
  +      {
            transactions.put(tx, modifications);
      }
  +   }
   
      /**
       * Commits a transaction.
       */
  -   public void commit(Object tx) throws Exception {
  -      List modifications = (List)transactions.remove(tx);
  +   public void commit(Object tx) throws Exception
  +   {
  +      List modifications = (List) transactions.remove(tx);
         if (modifications == null)
  +      {
            throw new IllegalStateException("transaction " + tx + " not found in transaction table");
  +      }
         put(modifications);
         commit();
      }
  @@ -547,15 +610,18 @@
      /**
       * Removes transaction in progress.
       */
  -   public void rollback(Object tx) {
  +   public void rollback(Object tx)
  +   {
         transactions.remove(tx);
      }
   
      /**
       * Throws an exception if the environment is not open.
       */
  -   private void checkOpen() {
  -      if (tree == null) {
  +   private void checkOpen()
  +   {
  +      if (tree == null)
  +      {
            throw new IllegalStateException(
                  "Operation not allowed before calling create()");
         }
  @@ -564,8 +630,10 @@
      /**
       * Throws an exception if the environment is not open.
       */
  -   private void checkNotOpen() {
  -      if (tree != null) {
  +   private void checkNotOpen()
  +   {
  +      if (tree != null)
  +      {
            throw new IllegalStateException(
                  "Operation not allowed after calling create()");
         }
  @@ -574,36 +642,43 @@
      /**
       * Throws an exception if the parameter is null.
       */
  -   private void checkNonNull(Object param, String paramName) {
  -      if (param == null) {
  +   private void checkNonNull(Object param, String paramName)
  +   {
  +      if (param == null)
  +      {
            throw new NullPointerException(
                  "Parameter must not be null: " + paramName);
         }
      }
   
  -   private Object nullMask(Object o) {
  +   private Object nullMask(Object o)
  +   {
        return (o == null) ? Null.NULL : o;
      }
   
  -   private Object nullUnmask(Object o) {
  +   private Object nullUnmask(Object o)
  +   {
        return (o == Null.NULL) ? null : o;
      }
   
      /**
       * Dumps the tree to debug.
       */
  -   public void dump() throws IOException {
  +   public void dump() throws IOException
  +   {
         dump(Fqn.ROOT);
      }
   
      /**
       * Dumps the tree past the key to debug.
       */
  -   public void dump(Object key) throws IOException {
  +   public void dump(Object key) throws IOException
  +   {
         TupleBrowser browser = tree.browse(key);
         Tuple t = new Tuple();
         log.debug("contents: " + key);
  -      while (browser.getNext(t)) {
  +      while (browser.getNext(t))
  +      {
            log.debug(t.getKey() + "\t" + t.getValue());
         }
         log.debug("");
  
  
  



More information about the jboss-cvs-commits mailing list