[Bedework-commit] calendarapi r815 - in trunk: calCore/src/org/bedework/calcore/hibernate calFacade/src/org/bedework/calfacade calsvc/src/org/bedework/calsvc

svnadmin at bedework.org svnadmin at bedework.org
Mon Feb 23 14:42:38 EST 2009


Author: douglm
Date: 2009-02-23 14:42:38 -0500 (Mon, 23 Feb 2009)
New Revision: 815

Removed:
   trunk/calFacade/src/org/bedework/calfacade/BwRWStats.java
Modified:
   trunk/calCore/src/org/bedework/calcore/hibernate/CalintfHelperHib.java
   trunk/calCore/src/org/bedework/calcore/hibernate/CalintfImpl.java
   trunk/calCore/src/org/bedework/calcore/hibernate/CoreCalendars.java
   trunk/calFacade/src/org/bedework/calfacade/BwStats.java
   trunk/calsvc/src/org/bedework/calsvc/CalSvc.java
Log:
Add some extra stats in preparation for further efficiency improvements

Modified: trunk/calCore/src/org/bedework/calcore/hibernate/CalintfHelperHib.java
===================================================================
--- trunk/calCore/src/org/bedework/calcore/hibernate/CalintfHelperHib.java	2009-02-23 06:24:17 UTC (rev 814)
+++ trunk/calCore/src/org/bedework/calcore/hibernate/CalintfHelperHib.java	2009-02-23 19:42:38 UTC (rev 815)
@@ -29,6 +29,7 @@
 import org.bedework.calcorei.CalintfDefs;
 import org.bedework.calcorei.HibSession;
 import org.bedework.calfacade.BwCalendar;
+import org.bedework.calfacade.BwStats;
 import org.bedework.calfacade.exc.CalFacadeException;
 
 import edu.rpi.cmt.access.PrivilegeDefs;
@@ -50,6 +51,12 @@
      */
     public HibSession getSess() throws CalFacadeException;
 
+    /**
+     * @return BwStats
+     * @throws CalFacadeException
+     */
+    public BwStats getStats() throws CalFacadeException;
+
     /** Used to fetch a calendar from the cache - assumes any access
      *
      * @param path

Modified: trunk/calCore/src/org/bedework/calcore/hibernate/CalintfImpl.java
===================================================================
--- trunk/calCore/src/org/bedework/calcore/hibernate/CalintfImpl.java	2009-02-23 06:24:17 UTC (rev 814)
+++ trunk/calCore/src/org/bedework/calcore/hibernate/CalintfImpl.java	2009-02-23 19:42:38 UTC (rev 815)
@@ -73,7 +73,6 @@
 import org.bedework.calfacade.BwEventProxy;
 import org.bedework.calfacade.BwFreeBusyComponent;
 import org.bedework.calfacade.BwPrincipal;
-import org.bedework.calfacade.BwRWStats;
 import org.bedework.calfacade.BwStats;
 import org.bedework.calfacade.BwSystem;
 import org.bedework.calfacade.BwUser;
@@ -135,7 +134,7 @@
  * @author Mike Douglass   douglm at rpi.edu
  */
 public class CalintfImpl extends CalintfBase implements PrivilegeDefs {
-  private BwStats stats = new BwRWStats();
+  private static BwStats stats = new BwStats();
 
   private static CalintfInfo info = new CalintfInfo(
        true,     // handlesLocations
@@ -229,6 +228,10 @@
       return (HibSession)intf.getDbSession();
     }
 
+    public BwStats getStats() throws CalFacadeException {
+      return intf.getStats();
+    }
+
     public BwCalendar getCollection(String path) throws CalFacadeException {
       try {
         return intf.calendars.getCalendar(path, Ace.privAny, true);

Modified: trunk/calCore/src/org/bedework/calcore/hibernate/CoreCalendars.java
===================================================================
--- trunk/calCore/src/org/bedework/calcore/hibernate/CoreCalendars.java	2009-02-23 06:24:17 UTC (rev 814)
+++ trunk/calCore/src/org/bedework/calcore/hibernate/CoreCalendars.java	2009-02-23 19:42:38 UTC (rev 815)
@@ -31,8 +31,10 @@
 import org.bedework.calcorei.HibSession;
 import org.bedework.calfacade.BwCalendar;
 import org.bedework.calfacade.BwPrincipal;
+import org.bedework.calfacade.BwStats;
 import org.bedework.calfacade.BwSystem;
 import org.bedework.calfacade.BwUser;
+import org.bedework.calfacade.BwStats.CacheStats;
 import org.bedework.calfacade.base.BwLastMod;
 import org.bedework.calfacade.exc.CalFacadeException;
 import org.bedework.sysevents.NotificationException;
@@ -43,6 +45,7 @@
 import edu.rpi.cmt.access.AceWho;
 import edu.rpi.cmt.access.Acl.CurrentAccess;
 
+import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
@@ -62,8 +65,47 @@
          implements CollectionGetter, CoreCalendarsI {
   private String userCalendarRootPath;
 
-  private Map<String, BwCalendar> colCache = new HashMap<String, BwCalendar>();
+  /**
+   * @author douglm
+   *
+   */
+  private static class CollectionCache implements Serializable {
+    private Map<String, BwCalendar> cache = new HashMap<String, BwCalendar>();
 
+    BwStats stats;
+    CacheStats cs;
+
+    CollectionCache(BwStats stats) {
+      this.stats = stats;
+      cs = stats.getCollectionCacheStats();
+    }
+
+    void put(BwCalendar col) {
+      cache.put(col.getPath(), col);
+      cs.incCached();
+    }
+
+    BwCalendar get(String path) {
+      BwCalendar col = cache.get(path);
+
+      if (col == null) {
+        cs.incMisses();
+      } else {
+        cs.incHits();
+      }
+
+      return col;
+    }
+
+    void flush() {
+      cache.clear();
+
+      cs.incFlushes();
+    }
+  }
+
+  private CollectionCache colCache;
+
   /** Constructor
    *
    * @param chcb
@@ -81,20 +123,22 @@
     super.init(cb, access, currentMode, debug);
 
     userCalendarRootPath = "/" + getSyspars().getUserCalendarRoot();
+
+    colCache = new CollectionCache(chcb.getStats());
   }
 
   /* (non-Javadoc)
    * @see org.bedework.calcore.CalintfHelper#startTransaction()
    */
   public void startTransaction() throws CalFacadeException {
-    colCache.clear();  // Just in case
+    colCache.flush();  // Just in case
   }
 
   /* (non-Javadoc)
    * @see org.bedework.calcore.CalintfHelper#endTransaction()
    */
   public void endTransaction() throws CalFacadeException {
-    colCache.clear();
+    colCache.flush();
   }
 
   /* (non-Javadoc)
@@ -273,7 +317,7 @@
 
     if (col != null) {
       col = wrap(col);
-      colCache.put(path, col);
+      colCache.put(col);
     }
 
     return col;
@@ -286,10 +330,6 @@
 
     col = checkAccess(col, desiredAccess, alwaysReturnResult);
 
-    if (col != null) {
-      colCache.put(path, col);
-    }
-
     return col;
   }
 
@@ -373,7 +413,7 @@
 
   public void renameCalendar(BwCalendar val,
                              String newName) throws CalFacadeException {
-    colCache.clear();
+    colCache.flush();
 
     val = unwrap(val);
 
@@ -399,7 +439,7 @@
    */
   public void moveCalendar(BwCalendar val,
                            BwCalendar newParent) throws CalFacadeException {
-    colCache.clear();
+    colCache.flush();
 
     val = unwrap(val);
     newParent = unwrap(newParent);
@@ -438,7 +478,7 @@
 
     notify(SysEvent.SysCode.COLLECTION_UPDATED, val);
 
-    colCache.put(val.getPath(), val);
+    colCache.put(val);
   }
 
   /* (non-Javadoc)
@@ -461,7 +501,7 @@
     access.changeAccess(cal, aces, replaceAll);
     sess.saveOrUpdate(cal);
 
-    colCache.put(cal.getPath(), cal);
+    colCache.put(cal);
 
     notify(SysEvent.SysCode.COLLECTION_UPDATED, cal);
   }
@@ -478,13 +518,13 @@
     access.defaultAccess(cal, who);
     sess.saveOrUpdate(cal);
 
-    colCache.put(cal.getPath(), cal);
+    colCache.put(cal);
 
     notify(SysEvent.SysCode.COLLECTION_UPDATED, cal);
   }
 
   public boolean deleteCalendar(BwCalendar val) throws CalFacadeException {
-    colCache.clear();
+    colCache.flush();
 
     HibSession sess = getSess();
 
@@ -688,7 +728,7 @@
 
     notify(SysEvent.SysCode.COLLECTION_ADDED, val);
 
-    colCache.put(val.getPath(), val);
+    colCache.put(val);
 
     return checkAccess(val, privAny, true);
   }
@@ -728,7 +768,7 @@
       cal = checkAccess(cal, desiredAccess, nullForNoAccess);
       if (cal != null) {
         out.add(cal);
-        colCache.put(cal.getPath(), cal);
+        colCache.put(cal);
       }
     }
 

Deleted: trunk/calFacade/src/org/bedework/calfacade/BwRWStats.java
===================================================================
--- trunk/calFacade/src/org/bedework/calfacade/BwRWStats.java	2009-02-23 06:24:17 UTC (rev 814)
+++ trunk/calFacade/src/org/bedework/calfacade/BwRWStats.java	2009-02-23 19:42:38 UTC (rev 815)
@@ -1,117 +0,0 @@
-/*
- Copyright (c) 2000-2005 University of Washington.  All rights reserved.
-
- Redistribution and use of this distribution in source and binary forms,
- with or without modification, are permitted provided that:
-
-   The above copyright notice and this permission notice appear in
-   all copies and supporting documentation;
-
-   The name, identifiers, and trademarks of the University of Washington
-   are not used in advertising or publicity without the express prior
-   written permission of the University of Washington;
-
-   Recipients acknowledge that this distribution is made available as a
-   research courtesy, "as is", potentially with defects, without
-   any obligation on the part of the University of Washington to
-   provide support, services, or repair;
-
-   THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR
-   IMPLIED, WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION
-   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
-   PARTICULAR PURPOSE, AND IN NO EVENT SHALL THE UNIVERSITY OF
-   WASHINGTON BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
-   DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
-   PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT (INCLUDING
-   NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION WITH
-   THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-/* **********************************************************************
-    Copyright 2005 Rensselaer Polytechnic Institute. All worldwide rights reserved.
-
-    Redistribution and use of this distribution in source and binary forms,
-    with or without modification, are permitted provided that:
-       The above copyright notice and this permission notice appear in all
-        copies and supporting documentation;
-
-        The name, identifiers, and trademarks of Rensselaer Polytechnic
-        Institute are not used in advertising or publicity without the
-        express prior written permission of Rensselaer Polytechnic Institute;
-
-    DISCLAIMER: The software is distributed" AS IS" without any express or
-    implied warranty, including but not limited to, any implied warranties
-    of merchantability or fitness for a particular purpose or any warrant)'
-    of non-infringement of any current or pending patent rights. The authors
-    of the software make no representations about the suitability of this
-    software for any particular purpose. The entire risk as to the quality
-    and performance of the software is with the user. Should the software
-    prove defective, the user assumes the cost of all necessary servicing,
-    repair or correction. In particular, neither Rensselaer Polytechnic
-    Institute, nor the authors of the software are liable for any indirect,
-    special, consequential, or incidental damages related to the software,
-    to the maximum extent the law permits.
-*/
-
-package org.bedework.calfacade;
-
-
-/** Some statistics for the Bedework Calendar. These are not necessarily
- * absolutely correct. We don't lock, just increment and decrement but
- * they work well enough to get an idea of how we're performing.
- *
- * @author Mike Douglass       douglm at rpi.edu
- */
-public class BwRWStats extends BwStats {
-  /**
-   */
-  public void incTzFetches() {
-    tzFetches++;
-  }
-
-  /**
-   */
-  public void incSystemTzFetches() {
-    systemTzFetches++;
-  }
-
-  /**
-   */
-  public void incTzStores() {
-    tzStores++;
-  }
-
-  /**
-   * @param  val  double event fetch millis.
-   */
-  public void incEventFetchTime(double val) {
-    eventFetchTime += val;
-  }
-
-  /**
-   * @param val   long event fetches.
-   */
-  public void incEventFetches(long val) {
-    eventFetches += val;
-  }
-
-  /**
-   * @param val   number of utc values cached
-   */
-  public void setDatesCached(long val) {
-    datesCached = val;
-  }
-
-  /**
-   * @param val    date cache hits
-   */
-  public void setDateCacheHits(long val) {
-    dateCacheHits = val;
-  }
-
-  /**
-   * @param val   data cache misses.
-   */
-  public void setDateCacheMisses(long val) {
-    dateCacheMisses = val;
-  }
-}

Modified: trunk/calFacade/src/org/bedework/calfacade/BwStats.java
===================================================================
--- trunk/calFacade/src/org/bedework/calfacade/BwStats.java	2009-02-23 06:24:17 UTC (rev 814)
+++ trunk/calFacade/src/org/bedework/calfacade/BwStats.java	2009-02-23 19:42:38 UTC (rev 815)
@@ -57,7 +57,6 @@
 import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Iterator;
 
 /** Some statistics for the Bedework calendar. These are not necessarily
  * absolutely correct. We don't lock, just increment and decrement but
@@ -179,6 +178,137 @@
     }
   }
 
+  /**
+   */
+  public static class CacheStats {
+    protected String name;
+
+    protected long cached;
+    protected long hits;
+    protected long misses;
+    protected long flushes;
+    protected long refetches;
+
+    CacheStats(String name) {
+      this.name = name;
+    }
+
+    /**
+     * @return name for these stats
+     */
+    public String getName() {
+      return name;
+    }
+
+    /**
+     * @param val
+     */
+    public void setCached(long val) {
+      cached = val;
+    }
+
+    /**
+     * @return Number of values cached
+     */
+    public long getCached() {
+      return cached;
+    }
+
+    /**
+     */
+    public void incCached() {
+      cached++;
+    }
+
+    /**
+     * @param val
+     */
+    public void setHits(long val) {
+      hits = val;
+    }
+
+    /**
+     * @return cache hits
+     */
+    public long getHits() {
+      return hits;
+    }
+
+    /**
+     */
+    public void incHits() {
+      hits++;
+    }
+
+    /**
+     * @param val
+     */
+    public void setMisses(long val) {
+      misses = val;
+    }
+
+    /**
+     * @return cache misses.
+     */
+    public long getMisses() {
+      return misses;
+    }
+
+    /**
+     */
+    public void incMisses() {
+      misses++;
+    }
+
+    /**
+     * @param val
+     */
+    public void setFlushes(long val) {
+      flushes = val;
+    }
+
+    /** A flush may clear the cache or reset flags causing a possible a
+     * revalidation of data
+     *
+     * @return cache flushes
+     */
+    public long getFlushes() {
+      return flushes;
+    }
+
+    /** A flush may clear the cache or reset flags causing a possible a
+     * revalidation of data
+     */
+    public void incFlushes() {
+      flushes++;
+    }
+
+    /**
+     * @param val
+     */
+    public void setRefetches(long val) {
+      refetches = val;
+    }
+
+    /** Data has to be refetched because invalid. May be 0 if we simply empty the
+     * cache on flush.
+     *
+     * @return cache refetches.
+     */
+    public long getRefetches() {
+      return refetches;
+    }
+
+    /**
+     */
+    public void incRefetches() {
+      refetches++;
+    }
+  }
+
+  /* Collection entity fetch data */
+  protected CacheStats collectionCacheStats = new CacheStats("Collections");
+
   protected int tzFetches;
 
   protected int systemTzFetches;
@@ -189,12 +319,17 @@
 
   protected long eventFetches;
 
-  /* Timezone cache stats */
-  protected long datesCached;
-  protected long dateCacheHits;
-  protected long dateCacheMisses;
+  /* Dates cache stats */
+  protected CacheStats dateCacheStats = new CacheStats("UTC Dates");
 
   /**
+   * @return Collection stats
+   */
+  public CacheStats getCollectionCacheStats() {
+    return collectionCacheStats;
+  }
+
+  /**
    * @return int   total num timezone fetches.
    */
   public int getTzFetches() {
@@ -202,6 +337,12 @@
   }
 
   /**
+   */
+  public void incTzFetches() {
+    tzFetches++;
+  }
+
+  /**
    * @return int   num system timezone fetches.
    */
   public int getSystemTzFetches() {
@@ -209,6 +350,12 @@
   }
 
   /**
+   */
+  public void incSystemTzFetches() {
+    systemTzFetches++;
+  }
+
+  /**
    * @return int   num timezone stores.
    */
   public int getTzStores() {
@@ -216,6 +363,12 @@
   }
 
   /**
+   */
+  public void incTzStores() {
+    tzStores++;
+  }
+
+  /**
    * @return double   event fetch millis.
    */
   public double getEventFetchTime() {
@@ -223,31 +376,31 @@
   }
 
   /**
-   * @return long   event fetches.
+   * @param  val  double event fetch millis.
    */
-  public long getEventFetches() {
-    return eventFetches;
+  public void incEventFetchTime(double val) {
+    eventFetchTime += val;
   }
 
   /**
-   * @return Number of utc values cached
+   * @return long   event fetches.
    */
-  public long getDatesCached() {
-    return datesCached;
+  public long getEventFetches() {
+    return eventFetches;
   }
 
   /**
-   * @return date cache hits
+   * @param val   long event fetches.
    */
-  public long getDateCacheHits() {
-    return dateCacheHits;
+  public void incEventFetches(long val) {
+    eventFetches += val;
   }
 
   /**
-   * @return data cache misses.
+   * @return date cache Stats
    */
-  public long getDateCacheMisses() {
-    return dateCacheMisses;
+  public CacheStats getDateCacheStats() {
+    return dateCacheStats;
   }
 
   /**
@@ -257,6 +410,9 @@
     ArrayList<StatsEntry> al = new ArrayList<StatsEntry>();
 
     al.add(new StatsEntry("Bedework statistics."));
+
+    cacheStatsToString(al, collectionCacheStats);
+
     al.add(new StatsEntry("tzFetches", getTzFetches()));
     al.add(new StatsEntry("systemTzFetches", getSystemTzFetches()));
     al.add(new StatsEntry("tzStores", getTzStores()));
@@ -264,25 +420,35 @@
     al.add(new StatsEntry("event fetch time", getEventFetchTime()));
     al.add(new StatsEntry("event fetches", getEventFetches()));
 
-    al.add(new StatsEntry("UTC dates cached", getDatesCached()));
-    al.add(new StatsEntry("UTC date cache hits", getDateCacheHits()));
-    al.add(new StatsEntry("UTC date cache misses", getDateCacheMisses()));
+    cacheStatsToString(al, dateCacheStats);
 
     return al;
   }
 
+  /**
+   * @param al
+   * @param cs
+   */
+  public void cacheStatsToString(ArrayList<StatsEntry> al,
+                                 CacheStats cs) {
+    String name = cs.getName() + " ";
+
+    al.add(new StatsEntry(name + "cached", cs.getCached()));
+    al.add(new StatsEntry(name + " hits", cs.getHits()));
+    al.add(new StatsEntry(name + " misses", cs.getMisses()));
+    al.add(new StatsEntry(name + " flushes", cs.getFlushes()));
+    al.add(new StatsEntry(name + " refetches", cs.getRefetches()));
+  }
+
   /** Turn the Collection of StatsEntry into a String for dumps.
    *
    * @param c  Collection of StatsEntry
    * @return String formatted result.
    */
-  public static String toString(Collection c) {
-    StringBuffer sb = new StringBuffer();
+  public static String toString(Collection<StatsEntry> c) {
+    StringBuilder sb = new StringBuilder();
 
-    Iterator it = c.iterator();
-
-    while (it.hasNext()) {
-      StatsEntry se = (StatsEntry)it.next();
+    for (StatsEntry se: c) {
       int k = se.getStatKind();
 
       if (k == StatsEntry.statKindHeader) {
@@ -308,7 +474,7 @@
 
   private static final int maxvalpad = 10;
 
-  private static void pad(StringBuffer sb, String val, int padlen) {
+  private static void pad(StringBuilder sb, String val, int padlen) {
     int len = padlen - val.length();
 
     if (len > 0) {
@@ -318,13 +484,13 @@
     sb.append(val);
   }
 
-  private static void header(StringBuffer sb, String h) {
+  private static void header(StringBuilder sb, String h) {
     sb.append("\n");
     pad(sb, h, padderLen);
     sb.append("\n");
   }
 
-  private static void format(StringBuffer sb, String name, String val) {
+  private static void format(StringBuilder sb, String name, String val) {
     pad(sb, name, padderLen);
     sb.append(": ");
     pad(sb, val, maxvalpad);

Modified: trunk/calsvc/src/org/bedework/calsvc/CalSvc.java
===================================================================
--- trunk/calsvc/src/org/bedework/calsvc/CalSvc.java	2009-02-23 06:24:17 UTC (rev 814)
+++ trunk/calsvc/src/org/bedework/calsvc/CalSvc.java	2009-02-23 19:42:38 UTC (rev 815)
@@ -55,6 +55,7 @@
 
 import org.bedework.calcorei.Calintf;
 import org.bedework.calcorei.CalintfFactory;
+import org.bedework.calfacade.BwStats.CacheStats;
 import org.bedework.calfacade.BwStats.StatsEntry;
 import org.bedework.calfacade.base.BwDbentity;
 import org.bedework.calfacade.base.BwShareableDbentity;
@@ -63,7 +64,6 @@
 import org.bedework.calfacade.BwCategory;
 import org.bedework.calfacade.BwLocation;
 import org.bedework.calfacade.BwContact;
-import org.bedework.calfacade.BwRWStats;
 import org.bedework.calfacade.BwStats;
 import org.bedework.calfacade.BwString;
 import org.bedework.calfacade.BwSystem;
@@ -399,15 +399,17 @@
   }
 
   public BwStats getStats() throws CalFacadeException {
-    BwRWStats rwstats = (BwRWStats)getCal().getStats();
+    BwStats stats = getCal().getStats();
 
     if (timezones != null) {
-      rwstats.setDateCacheHits(timezones.getDateCacheHits());
-      rwstats.setDateCacheMisses(timezones.getDateCacheMisses());
-      rwstats.setDatesCached(timezones.getDatesCached());
+      CacheStats cs = stats.getDateCacheStats();
+
+      cs.setHits(timezones.getDateCacheHits());
+      cs.setMisses(timezones.getDateCacheMisses());
+      cs.setCached(timezones.getDatesCached());
     }
 
-    return rwstats;
+    return stats;
   }
 
   public void setDbStatsEnabled(boolean enable) throws CalFacadeException {



More information about the Bedework-commit mailing list