← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16491: Calendar. Impl class PeriodCache which provides a cache for createPeriod( date, calendar). Using ...

 

------------------------------------------------------------
revno: 16491
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2014-08-24 23:04:36 +0200
message:
  Calendar. Impl class PeriodCache which provides a cache for createPeriod( date, calendar). Using cache to improve performance in data export and period resource tables.
added:
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/cache/
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/cache/PeriodCache.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodType.java
  dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/resourcetable/DefaultResourceTableService.java
  dhis-2/dhis-services/dhis-service-administration/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/SpringDataValueSetStore.java
  dhis-2/dhis-support/dhis-support-system/pom.xml
  dhis-2/dhis-support/dhis-support-system/src/main/resources/META-INF/dhis/beans.xml


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodType.java	2014-08-24 17:53:31 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodType.java	2014-08-24 21:04:36 +0000
@@ -38,7 +38,6 @@
 import org.hisp.dhis.calendar.PeriodTypeParser;
 import org.hisp.dhis.calendar.impl.Iso8601Calendar;
 import org.hisp.dhis.common.DxfNamespaces;
-import org.hisp.dhis.common.Timer;
 
 import java.io.Serializable;
 import java.util.ArrayList;

=== modified file 'dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/resourcetable/DefaultResourceTableService.java'
--- dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/resourcetable/DefaultResourceTableService.java	2014-08-15 07:40:20 +0000
+++ dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/resourcetable/DefaultResourceTableService.java	2014-08-24 21:04:36 +0000
@@ -28,6 +28,20 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import static org.hisp.dhis.resourcetable.ResourceTableStore.TABLE_NAME_CATEGORY_OPTION_COMBO_NAME;
+import static org.hisp.dhis.resourcetable.ResourceTableStore.TABLE_NAME_DATA_ELEMENT_STRUCTURE;
+import static org.hisp.dhis.resourcetable.ResourceTableStore.TABLE_NAME_DATE_PERIOD_STRUCTURE;
+import static org.hisp.dhis.resourcetable.ResourceTableStore.TABLE_NAME_ORGANISATION_UNIT_STRUCTURE;
+import static org.hisp.dhis.resourcetable.ResourceTableStore.TABLE_NAME_PERIOD_STRUCTURE;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.calendar.Calendar;
@@ -58,18 +72,8 @@
 import org.hisp.dhis.resourcetable.statement.CreateCategoryTableStatement;
 import org.hisp.dhis.sqlview.SqlView;
 import org.hisp.dhis.sqlview.SqlViewService;
+import org.hisp.dhis.system.cache.PeriodCache;
 import org.springframework.transaction.annotation.Transactional;
-import org.springframework.util.Assert;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import static org.hisp.dhis.resourcetable.ResourceTableStore.*;
 
 /**
  * @author Lars Helge Overland
@@ -138,7 +142,14 @@
     {
         this.sqlViewService = sqlViewService;
     }
+    
+    private PeriodCache periodCache;
 
+    public void setPeriodCache( PeriodCache periodCache )
+    {
+        this.periodCache = periodCache;
+    }
+    
     // -------------------------------------------------------------------------
     // OrganisationUnitStructure
     // -------------------------------------------------------------------------
@@ -437,11 +448,11 @@
         List<Object[]> batchArgs = new ArrayList<>();
 
         Date startDate = new Cal( 1975, 1, 1, true ).time(); //TODO
-        Date endDate = new Cal( 2030, 1, 1, true ).time();
+        Date endDate = new Cal( 2025, 1, 1, true ).time();
 
         List<Period> days = new DailyPeriodType().generatePeriods( startDate, endDate );
 
-        Calendar cal = PeriodType.getCalendar();
+        Calendar calendar = PeriodType.getCalendar();
 
         for ( Period day : days )
         {
@@ -451,11 +462,7 @@
 
             for ( PeriodType periodType : periodTypes )
             {
-                Period period = periodType.createPeriod( day.getStartDate(), cal );
-
-                Assert.notNull( period );
-
-                values.add( period.getIsoDate() );
+                values.add( periodCache.getIsoPeriod( periodType, day.getStartDate(), calendar ) );
             }
 
             batchArgs.add( values.toArray() );
@@ -481,6 +488,8 @@
         // Populate table
         // ---------------------------------------------------------------------
 
+        Calendar calendar = PeriodType.getCalendar();
+
         List<Object[]> batchArgs = new ArrayList<>();
 
         for ( Period period : periods )
@@ -498,7 +507,7 @@
             {
                 if ( rowType.getFrequencyOrder() <= periodType.getFrequencyOrder() )
                 {
-                    values.add( periodType.createPeriod( startDate ).getIsoDate() );
+                    values.add( periodCache.getIsoPeriod( periodType, startDate, calendar ) );
                 }
                 else
                 {

=== modified file 'dhis-2/dhis-services/dhis-service-administration/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-administration/src/main/resources/META-INF/dhis/beans.xml	2014-04-16 15:03:22 +0000
+++ dhis-2/dhis-services/dhis-service-administration/src/main/resources/META-INF/dhis/beans.xml	2014-08-24 21:04:36 +0000
@@ -18,6 +18,7 @@
     <property name="indicatorService" ref="org.hisp.dhis.indicator.IndicatorService" />
     <property name="periodService" ref="org.hisp.dhis.period.PeriodService" />
     <property name="sqlViewService" ref="org.hisp.dhis.sqlview.SqlViewService" />
+    <property name="periodCache" ref="periodCache" />
   </bean>
   
   <!-- Data integrity -->

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/SpringDataValueSetStore.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/SpringDataValueSetStore.java	2014-08-24 17:53:31 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/SpringDataValueSetStore.java	2014-08-24 21:04:36 +0000
@@ -29,7 +29,7 @@
  */
 
 import static org.hisp.dhis.common.IdentifiableObjectUtils.getIdentifiers;
-import static org.hisp.dhis.system.util.DateUtils.*;
+import static org.hisp.dhis.system.util.DateUtils.getLongDateString;
 import static org.hisp.dhis.system.util.TextUtils.getCommaDelimitedString;
 
 import java.io.OutputStream;
@@ -41,13 +41,13 @@
 
 import org.amplecode.staxwax.factory.XMLFactory;
 import org.hisp.dhis.calendar.Calendar;
-import org.hisp.dhis.common.Timer;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.dxf2.datavalue.DataValue;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.period.PeriodType;
+import org.hisp.dhis.system.cache.PeriodCache;
 import org.hisp.dhis.system.util.DateUtils;
 import org.hisp.dhis.system.util.StreamUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -66,7 +66,10 @@
 
     @Autowired
     private JdbcTemplate jdbcTemplate;
-
+    
+    @Autowired
+    private PeriodCache periodCache;
+    
     //--------------------------------------------------------------------------
     // DataValueSetStore implementation
     //--------------------------------------------------------------------------
@@ -141,13 +144,10 @@
         while ( rowSet.next() )
         {
             DataValue dataValue = dataValueSet.getDataValueInstance();
+            PeriodType pt = PeriodType.getPeriodTypeByName( rowSet.getString( "ptname" ) );
 
-            String periodType = rowSet.getString( "ptname" );
-            Date startDate = rowSet.getDate( "pestart" );            
-            Period isoPeriod = PeriodType.getPeriodTypeByName( periodType ).createPeriod( startDate, calendar );
-            
             dataValue.setDataElement( rowSet.getString( "deuid" ) );
-            dataValue.setPeriod( isoPeriod.getIsoDate() );
+            dataValue.setPeriod( periodCache.getIsoPeriod( pt, rowSet.getDate( "pestart" ), calendar ) );
             dataValue.setOrgUnit( rowSet.getString( "ouuid" ) );
             dataValue.setCategoryOptionCombo( rowSet.getString( "cocuid" ) );
             dataValue.setAttributeOptionCombo( rowSet.getString( "aocuid" ) );
@@ -162,7 +162,7 @@
 
         dataValueSet.close();
     }
-
+    
     //--------------------------------------------------------------------------
     // DataValueSetStore implementation
     //--------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-support/dhis-support-system/pom.xml'
--- dhis-2/dhis-support/dhis-support-system/pom.xml	2014-07-23 14:53:00 +0000
+++ dhis-2/dhis-support/dhis-support-system/pom.xml	2014-08-24 21:04:36 +0000
@@ -163,6 +163,10 @@
       <groupId>org.htmlparser</groupId>
       <artifactId>htmlparser</artifactId>
     </dependency>
+    <dependency>
+      <groupId>com.google.guava</groupId>
+      <artifactId>guava</artifactId>
+    </dependency>
   </dependencies>
   <properties>
     <rootDir>../../</rootDir>

=== added directory 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/cache'
=== added file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/cache/PeriodCache.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/cache/PeriodCache.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/cache/PeriodCache.java	2014-08-24 21:04:36 +0000
@@ -0,0 +1,78 @@
+package org.hisp.dhis.system.cache;
+
+/*
+ * Copyright (c) 2004-2014, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import java.util.Date;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+
+import javax.annotation.PostConstruct;
+
+import org.hisp.dhis.calendar.Calendar;
+import org.hisp.dhis.period.PeriodType;
+
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class PeriodCache
+{
+    private Cache<String, String> periodCache;
+    
+    /**
+     * TODO: In Spring 4 replace with org.springframework.cache.guava.GuavaCacheManager.
+     */
+    @PostConstruct
+    public void init()
+    {
+        periodCache = CacheBuilder.newBuilder().maximumSize( 30000 ).build();
+    }
+    
+    public String getIsoPeriod( final PeriodType periodType, final Date startDate, final Calendar calendar )
+    {
+        try
+        {
+            String key = calendar.name() + periodType.getName() + startDate.toString();
+            
+            return periodCache.get( key, new Callable<String>() 
+            {                
+                public String call()
+                {
+                    return periodType.createPeriod( startDate, calendar ).getIsoDate();
+                }
+            } );
+        }
+        catch ( ExecutionException ex )
+        {
+            throw new RuntimeException( ex );
+        }
+    }
+}

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-support/dhis-support-system/src/main/resources/META-INF/dhis/beans.xml	2014-07-07 10:06:36 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/resources/META-INF/dhis/beans.xml	2014-08-24 21:04:36 +0000
@@ -32,5 +32,7 @@
   <bean id="notifier" class="org.hisp.dhis.system.notification.InMemoryNotifier" />
 
   <bean id="debugger" class="org.hisp.dhis.system.debug.DebuggerImpl" />
+  
+  <bean id="periodCache" class="org.hisp.dhis.system.cache.PeriodCache" />
 
 </beans>