← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 22169: move translated flag out of Schema, and instead lazy cache flag in SchemaService.isTranslated

 

------------------------------------------------------------
revno: 22169
committer: Morten Olav Hansen <morten@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2016-03-08 08:51:19 +0700
message:
  move translated flag out of Schema, and instead lazy cache flag in SchemaService.isTranslated
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Schema.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/SchemaService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryParser.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/DefaultSchemaService.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-api/src/main/java/org/hisp/dhis/schema/Schema.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Schema.java	2016-02-03 11:47:33 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Schema.java	2016-03-08 01:51:19 +0000
@@ -114,12 +114,6 @@
     private boolean shareable;
 
     /**
-     * Does this type have any translations.
-     * Please remember that this is only updated at server start, so if any translation was added later, this flag is not updated.
-     */
-    private boolean translated;
-
-    /**
      * Points to relative Web-API endpoint (if exposed).
      */
     private String relativeApiEndpoint;
@@ -299,18 +293,6 @@
 
     @JsonProperty
     @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
-    public boolean isTranslated()
-    {
-        return translated;
-    }
-
-    public void setTranslated( boolean translated )
-    {
-        this.translated = translated;
-    }
-
-    @JsonProperty
-    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public String getRelativeApiEndpoint()
     {
         return relativeApiEndpoint;

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/SchemaService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/SchemaService.java	2016-01-27 07:18:12 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/SchemaService.java	2016-03-08 01:51:19 +0000
@@ -93,4 +93,6 @@
      * @return List of all available metadata schemas
      */
     List<Schema> getMetadataSchemas();
+
+    boolean isTranslated( Class<?> klass );
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryParser.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryParser.java	2016-01-08 16:26:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryParser.java	2016-03-08 01:51:19 +0000
@@ -77,7 +77,7 @@
     private Restriction getRestriction( Schema schema, String path, String operator, Object arg ) throws QueryParserException
     {
         // optimize if not translated
-        if ( !schema.isTranslated() )
+        if ( !schemaService.isTranslated( schema.getKlass() ) )
         {
             if ( path.startsWith( "displayName:" ) && schema.havePersistedProperty( "name" ) )
             {

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/DefaultSchemaService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/DefaultSchemaService.java	2016-03-07 18:04:27 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/DefaultSchemaService.java	2016-03-08 01:51:19 +0000
@@ -30,6 +30,8 @@
 
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
 import com.google.common.base.CaseFormat;
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import org.hibernate.SessionFactory;
@@ -47,11 +49,13 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
-public class DefaultSchemaService 
+public class DefaultSchemaService
     implements ApplicationListener<ContextRefreshedEvent>, SchemaService
 {
     private Map<Class<?>, Schema> classSchemaMap = new HashMap<>();
@@ -62,6 +66,12 @@
 
     private Map<Class<?>, Schema> dynamicClassSchemaMap = new HashMap<>();
 
+    private static Cache<Class<?>, Boolean> IS_TRANSLATED_CACHE = CacheBuilder.newBuilder()
+        .expireAfterAccess( 15, TimeUnit.MINUTES )
+        .initialCapacity( 100 )
+        .maximumSize( 200 )
+        .build();
+
     @Autowired
     private PropertyIntrospectorService propertyIntrospectorService;
 
@@ -84,7 +94,6 @@
             if ( sessionFactory.getClassMetadata( schema.getKlass() ) != null )
             {
                 schema.setPersisted( true );
-                schema.setTranslated( translationService.hasTranslations( schema.getKlass().getSimpleName() ) );
             }
 
             schema.setDisplayName( beautify( schema.getName() ) );
@@ -101,7 +110,7 @@
             updateSelf( schema );
         }
     }
-    
+
     @Override
     public Schema getSchema( Class<?> klass )
     {
@@ -220,6 +229,20 @@
         return schemas;
     }
 
+    @Override
+    public boolean isTranslated( final Class<?> klass )
+    {
+        try
+        {
+            return IS_TRANSLATED_CACHE.get( klass, () -> translationService.hasTranslations( klass.getSimpleName() ) );
+        }
+        catch ( ExecutionException ignored )
+        {
+        }
+
+        return true;
+    }
+
     private void updateSelf( Schema schema )
     {
         if ( schema.haveProperty( "__self__" ) )