← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18459: performance improvements for event import, wip

 

------------------------------------------------------------
revno: 18459
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-03-03 10:40:50 +0700
message:
  performance improvements for event import, wip
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java
  dhis-2/dhis-services/dhis-service-dxf2/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-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java	2015-02-20 12:35:55 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java	2015-03-03 03:40:50 +0000
@@ -34,16 +34,17 @@
 import org.apache.commons.logging.LogFactory;
 import org.hibernate.SessionFactory;
 import org.hisp.dhis.common.CodeGenerator;
+import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.common.IdentifiableProperty;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementService;
+import org.hisp.dhis.dxf2.common.IdSchemes;
+import org.hisp.dhis.dxf2.common.ImportOptions;
 import org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance;
 import org.hisp.dhis.dxf2.importsummary.ImportConflict;
 import org.hisp.dhis.dxf2.importsummary.ImportStatus;
 import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
-import org.hisp.dhis.dxf2.common.ImportOptions;
-import org.hisp.dhis.dxf2.common.IdSchemes;
 import org.hisp.dhis.event.EventStatus;
 import org.hisp.dhis.i18n.I18nManager;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
@@ -72,6 +73,7 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.Assert;
+import org.springframework.util.StringUtils;
 
 import java.io.IOException;
 import java.util.ArrayList;
@@ -141,10 +143,21 @@
     @Autowired
     protected SessionFactory sessionFactory;
 
+    @Autowired
+    protected IdentifiableObjectManager manager;
+
     protected final int FLUSH_FREQUENCY = 20;
 
     private static final ObjectMapper objectMapper = new ObjectMapper();
 
+    private Map<String, OrganisationUnit> organisationUnitCache = new HashMap<>();
+
+    private Map<String, Program> programCache = new HashMap<>();
+
+    private Map<String, ProgramStage> programStageCache = new HashMap<>();
+
+    private Map<String, DataElement> dataElementCache = new HashMap<>();
+
     // -------------------------------------------------------------------------
     // CREATE
     // -------------------------------------------------------------------------
@@ -153,7 +166,6 @@
     public ImportSummaries addEvents( List<Event> events, ImportOptions importOptions )
     {
         ImportSummaries importSummaries = new ImportSummaries();
-
         int counter = 0;
 
         for ( Event event : events )
@@ -206,9 +218,10 @@
     @Override
     public ImportSummary addEvent( Event event, ImportOptions importOptions )
     {
-        Program program = programService.getProgram( event.getProgram() );
+        Program program = getProgram( event.getProgram() );
+        ProgramStage programStage = getProgramStage( event.getProgramStage() );
+
         ProgramInstance programInstance;
-        ProgramStage programStage = programStageService.getProgramStage( event.getProgramStage() );
         ProgramStageInstance programStageInstance = null;
 
         if ( importOptions == null )
@@ -359,8 +372,7 @@
             return new ImportSummary( ImportStatus.ERROR, "Program is not assigned to this organisation unit" );
         }
 
-        return saveEvent( program, programInstance, programStage, programStageInstance, organisationUnit, event,
-            importOptions );
+        return saveEvent( program, programInstance, programStage, programStageInstance, organisationUnit, event, importOptions );
     }
 
     // -------------------------------------------------------------------------
@@ -519,7 +531,7 @@
 
         for ( DataValue value : event.getDataValues() )
         {
-            DataElement dataElement = dataElementService.getDataElement( value.getDataElement() );
+            DataElement dataElement = getDataElement( value.getDataElement() );
 
             TrackedEntityDataValue dataValue = dataValueService.getTrackedEntityDataValue( programStageInstance, dataElement );
 
@@ -547,7 +559,6 @@
                 dataValueService.deleteTrackedEntityDataValue( value );
             }
         }
-
     }
 
     @Override
@@ -920,7 +931,7 @@
             }
             else
             {
-                dataElement = dataElementService.getDataElement( dataValue.getDataElement() );
+                dataElement = getDataElement( dataValue.getDataElement() );
             }
 
             if ( dataElement != null )
@@ -982,6 +993,16 @@
     {
         OrganisationUnit organisationUnit = null;
 
+        if ( StringUtils.isEmpty( value ) )
+        {
+            return null;
+        }
+
+        if ( organisationUnitCache.containsKey( value ) )
+        {
+            return organisationUnitCache.get( value );
+        }
+
         if ( IdentifiableProperty.UUID.equals( scheme ) )
         {
             organisationUnit = organisationUnitService.getOrganisationUnitByUuid( value );
@@ -1004,6 +1025,77 @@
             organisationUnit = organisationUnitService.getOrganisationUnit( value );
         }
 
+        if ( organisationUnit != null )
+        {
+            organisationUnitCache.put( value, organisationUnit );
+        }
+
         return organisationUnit;
     }
+
+    private Program getProgram( String programId )
+    {
+        if ( StringUtils.isEmpty( programId ) )
+        {
+            return null;
+        }
+
+        if ( !programCache.containsKey( programId ) )
+        {
+            Program program = manager.get( Program.class, programId );
+
+            if ( program == null )
+            {
+                return null;
+            }
+
+            programCache.put( programId, program );
+        }
+
+        return programCache.get( programId );
+    }
+
+    private ProgramStage getProgramStage( String programStageId )
+    {
+        if ( StringUtils.isEmpty( programStageId ) )
+        {
+            return null;
+        }
+
+        if ( !programStageCache.containsKey( programStageId ) )
+        {
+            ProgramStage programStage = manager.get( ProgramStage.class, programStageId );
+
+            if ( programStage == null )
+            {
+                return null;
+            }
+
+            programStageCache.put( programStageId, programStage );
+        }
+
+        return programStageCache.get( programStageId );
+    }
+
+    private DataElement getDataElement( String dataElementId )
+    {
+        if ( StringUtils.isEmpty( dataElementId ) )
+        {
+            return null;
+        }
+
+        if ( !dataElementCache.containsKey( dataElementId ) )
+        {
+            DataElement dataElement = manager.get( DataElement.class, dataElementId );
+
+            if ( dataElement == null )
+            {
+                return null;
+            }
+
+            dataElementCache.put( dataElementId, dataElement );
+        }
+
+        return dataElementCache.get( dataElementId );
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/resources/META-INF/dhis/beans.xml	2015-02-27 13:42:52 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/resources/META-INF/dhis/beans.xml	2015-03-03 03:40:50 +0000
@@ -33,7 +33,7 @@
 
   <bean id="org.hisp.dhis.dxf2.events.event.EventStore" class="org.hisp.dhis.dxf2.events.event.JdbcEventStore" />
 
-  <bean id="org.hisp.dhis.dxf2.events.event.EventService" class="org.hisp.dhis.dxf2.events.event.JacksonEventService" />
+  <bean id="org.hisp.dhis.dxf2.events.event.EventService" class="org.hisp.dhis.dxf2.events.event.JacksonEventService" scope="prototype" />
 
   <bean id="org.hisp.dhis.dxf2.events.report.EventRowService" class="org.hisp.dhis.dxf2.events.report.AbstractEventRowService" />