← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 9258: Analytics table, write to temp table, then swap

 

------------------------------------------------------------
revno: 9258
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2012-12-10 21:41:29 +0100
message:
  Analytics table, write to temp table, then swap
modified:
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/AnalyticsTableManager.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/DefaultAnalyticsTableService.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcAnalyticsTableManager.java


--
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-analytics/src/main/java/org/hisp/dhis/analytics/AnalyticsTableManager.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/AnalyticsTableManager.java	2012-12-03 21:04:39 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/AnalyticsTableManager.java	2012-12-10 20:41:29 +0000
@@ -32,14 +32,14 @@
 
 public interface AnalyticsTableManager
 {
-    void dropTable();
-    
     void createTable();
+
+    Future<?> createIndexesAsync( List<String> columns );
+    
+    void swapTable();
     
     void populateTable();    
 
-    Future<?> createIndexesAsync( List<String> columns );
-    
     /**
      * Returns a list of string arrays in where the first index holds the database
      * column name, the second index holds the database column type and the third

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/DefaultAnalyticsTableService.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/DefaultAnalyticsTableService.java	2012-12-10 20:15:39 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/DefaultAnalyticsTableService.java	2012-12-10 20:41:29 +0000
@@ -63,9 +63,6 @@
     {
         Clock clock = new Clock().startClock().logTime( "Starting update..." );
         
-        tableManager.dropTable();
-        clock.logTime( "Dropped analytics table" );
-        
         tableManager.createTable();
         clock.logTime( "Created analytics table" );
         
@@ -75,6 +72,9 @@
         createIndexes();
         clock.logTime( "Created all indexes, update done" );
         
+        tableManager.swapTable();
+        clock.logTime( "Swapped analytics table" );
+        
         return null;
     }
 

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcAnalyticsTableManager.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcAnalyticsTableManager.java	2012-12-10 20:15:39 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcAnalyticsTableManager.java	2012-12-10 20:41:29 +0000
@@ -67,6 +67,7 @@
     public static final String PREFIX_ORGUNITLEVEL = "idlevel";
     public static final String PREFIX_INDEX = "index_";
     public static final String TABLE_NAME = "analytics";
+    public static final String TABLE_NAME_TEMP = "analytics_temp";
     
     @Autowired
     private OrganisationUnitService organisationUnitService;
@@ -81,41 +82,36 @@
     // Implementation
     // -------------------------------------------------------------------------
   
-    //TODO all data types
-    //TODO create temp table then swap
     //TODO shard on data quarter based on start date
     //TODO average aggregation operator data, pre-aggregate in time dimension, not in org unit dimension
-    
-    public void dropTable()
-    {
-        final String sql = "drop table " + TABLE_NAME;
         
-        executeSilently( sql );
-    }
-    
     public void createTable()
     {
-        String sql = "create table " + TABLE_NAME + " (";
+        final String sqlDrop = "drop table " + TABLE_NAME_TEMP;
+        
+        executeSilently( sqlDrop );
+        
+        String sqlCreate = "create table " + TABLE_NAME_TEMP + " (";
         
         for ( String[] col : getDimensionColumns() )
         {
-            sql += col[0] + " " + col[1] + ",";
+            sqlCreate += col[0] + " " + col[1] + ",";
         }
         
-        sql += "value double precision)";
-        
-        log.info( "Create SQL: " + sql );
-        
-        executeSilently( sql );
+        sqlCreate += "value double precision)";
+        
+        log.info( "Create SQL: " + sqlCreate );
+        
+        executeSilently( sqlCreate );
     }
-        
+    
     @Async
     public Future<?> createIndexesAsync( List<String> columns )
     {
         for ( String column : columns )
         {        
             final String sql = "create index " + PREFIX_INDEX +
-                column + " on " + TABLE_NAME + " (" + column + ")";
+                column + " on " + TABLE_NAME_TEMP + " (" + column + ")";
                 
             executeSilently( sql );
             
@@ -126,6 +122,17 @@
         
         return null;
     }
+
+    public void swapTable()
+    {
+        final String sqlDrop = "drop table " + TABLE_NAME;
+        
+        executeSilently( sqlDrop );
+        
+        final String sqlAlter = "alter table " + TABLE_NAME_TEMP + " rename to " + TABLE_NAME;
+        
+        jdbcTemplate.execute( sqlAlter );
+    }
     
     public void populateTable()
     {
@@ -136,7 +143,7 @@
     
     private void populateTable( String valueExpression, String valueType )
     {
-        String insert = "insert into analytics (";
+        String insert = "insert into " + TABLE_NAME_TEMP + " (";
         
         for ( String[] col : getDimensionColumns() )
         {