deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tandrasc...@apache.org
Subject deltaspike git commit: DELTASPIKE-1168 [perf] optimize DelegateQueryBuilder#selectDelegate
Date Tue, 07 Jun 2016 20:02:50 GMT
Repository: deltaspike
Updated Branches:
  refs/heads/master f47b9d305 -> 6c5f4d21c


DELTASPIKE-1168 [perf] optimize DelegateQueryBuilder#selectDelegate

Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/6c5f4d21
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/6c5f4d21
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/6c5f4d21

Branch: refs/heads/master
Commit: 6c5f4d21ccb28d9f4d9c1b00087846180c0fdf94
Parents: f47b9d3
Author: Thomas Andraschko <tandraschko@apache.org>
Authored: Tue Jun 7 22:02:44 2016 +0200
Committer: Thomas Andraschko <tandraschko@apache.org>
Committed: Tue Jun 7 22:02:44 2016 +0200

----------------------------------------------------------------------
 .../data/impl/builder/DelegateQueryBuilder.java | 59 ++++++++++++--------
 1 file changed, 37 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6c5f4d21/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/DelegateQueryBuilder.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/DelegateQueryBuilder.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/DelegateQueryBuilder.java
index 87218b3..c17a1f6 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/DelegateQueryBuilder.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/DelegateQueryBuilder.java
@@ -21,6 +21,8 @@ package org.apache.deltaspike.data.impl.builder;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Set;
 import javax.enterprise.context.ApplicationScoped;
 
@@ -49,12 +51,15 @@ public class DelegateQueryBuilder extends QueryBuilder
     @Inject
     private BeanManager beanManager;
 
+    private final Map<Method, Bean<DelegateQueryHandler>> lookupCache
+        = new HashMap<Method, Bean<DelegateQueryHandler>>();
+    
     @Override
     public Object execute(CdiQueryInvocationContext context)
     {
         try
         {
-            DelegateQueryHandler delegate = selectDelegate(context);
+            DelegateQueryHandler delegate = lookup(context);
             if (delegate != null)
             {
                 Object result = invoke(delegate, context);
@@ -83,25 +88,42 @@ public class DelegateQueryBuilder extends QueryBuilder
         throw new QueryInvocationException("No DelegateQueryHandler found", context);
     }
 
-    private DelegateQueryHandler selectDelegate(CdiQueryInvocationContext context)
+    private DelegateQueryHandler lookup(CdiQueryInvocationContext context)
     {
-        Set<Bean<DelegateQueryHandler>> beans = BeanProvider
-                .getBeanDefinitions(DelegateQueryHandler.class, true, true);
-        for (Bean<DelegateQueryHandler> bean : beans)
+        Bean<DelegateQueryHandler> selectedBean = lookupCache.get(context.getMethod());
+        
+        if (selectedBean == null)
         {
-            if (ClassUtils.containsPossiblyGenericMethod(bean.getBeanClass(), context.getMethod()))
+            Set<Bean<DelegateQueryHandler>> beans = BeanProvider
+                    .getBeanDefinitions(DelegateQueryHandler.class, true, true);
+            for (Bean<DelegateQueryHandler> bean : beans)
             {
-                if (bean.getScope().equals(Dependent.class))
+                if (ClassUtils.containsPossiblyGenericMethod(bean.getBeanClass(), context.getMethod()))
                 {
-                    CreationalContext<DelegateQueryHandler> cc = beanManager.createCreationalContext(bean);
-                    DelegateQueryHandler instance = (DelegateQueryHandler) beanManager.getReference(
-                            bean, DelegateQueryHandler.class, cc);
-                    context.addDestroyable(new BeanDestroyable<DelegateQueryHandler>(bean,
instance, cc));
-                    return instance;
+                    selectedBean = bean;
                 }
-                return (DelegateQueryHandler) BeanProvider.getContextualReference(bean.getBeanClass());
+            }
+            
+            if (selectedBean != null)
+            {
+                lookupCache.put(context.getMethod(), selectedBean);
             }
         }
+        
+        
+        if (selectedBean != null)
+        {
+            CreationalContext<DelegateQueryHandler> cc = beanManager.createCreationalContext(selectedBean);
+            DelegateQueryHandler instance = (DelegateQueryHandler) beanManager.getReference(
+                    selectedBean, DelegateQueryHandler.class, cc);
+            
+            if (selectedBean.getScope().equals(Dependent.class))
+            {
+                context.addDestroyable(new BeanDestroyable<DelegateQueryHandler>(selectedBean,
instance, cc));
+            }
+
+            return instance;
+        }
         return null;
     }
 
@@ -109,7 +131,8 @@ public class DelegateQueryBuilder extends QueryBuilder
     {
         try
         {
-            return invoke(delegate, context.getMethod(), context.getMethodParameters());
+            Method extract = ClassUtils.extractPossiblyGenericMethod(delegate.getClass(),
context.getMethod());
+            return extract.invoke(delegate, context.getMethodParameters());
         }
         catch (InvocationTargetException e)
         {
@@ -124,12 +147,4 @@ public class DelegateQueryBuilder extends QueryBuilder
             throw new RuntimeException(e);
         }
     }
-
-    protected Object invoke(Object target, Method method, Object[] args) throws InvocationTargetException,
-            IllegalAccessException
-    {
-        Method extract = ClassUtils.extractPossiblyGenericMethod(target.getClass(), method);
-        return extract.invoke(target, args);
-    }
-
 }


Mime
View raw message