Alexander Wels has uploaded a new change for review.

Change subject: restapi: optimize getUriBuilder
......................................................................

restapi: optimize getUriBuilder

- Whenever we needed to build a Uri we would create a new UriBuilder from
  the passed in model. The structure of the builder is based on type of the
  model. This patch instead of creating a new one from scratch each time,
  caches the UriBuilder and returns a clone. Cloning the builder is much
  faster than creating a new one from scratch. Using a cloned builder ensures
  there are no concurrency issues associated with the builder.

Change-Id: I86221e8af24da28a7137c0cc0e7aec69943a65c5
Signed-off-by: Alexander Wels <[email protected]>
---
M 
backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/utils/LinkHelper.java
1 file changed, 31 insertions(+), 16 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/88/37988/1

diff --git 
a/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/utils/LinkHelper.java
 
b/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/utils/LinkHelper.java
index 49dfd1f7..aa97b21 100644
--- 
a/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/utils/LinkHelper.java
+++ 
b/backend/manager/modules/restapi/interface/definition/src/main/java/org/ovirt/engine/api/utils/LinkHelper.java
@@ -21,7 +21,9 @@
 import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.Map.Entry;
+import java.util.concurrent.ConcurrentHashMap;
 
 import javax.ws.rs.Path;
 import javax.ws.rs.core.UriBuilder;
@@ -796,6 +798,12 @@
     }
 
     /**
+     * Cache the {@code UriBuilder}s by the associated model so we don't have 
to create the builders each time.
+     */
+    private static Map<Class<? extends BaseResource>, UriBuilder> builderMap =
+            new ConcurrentHashMap<Class<? extends BaseResource>, UriBuilder>();
+
+    /**
      * Create a #UriBuilder which encapsulates the path to an object
      *
      * i.e. for a VM tag, return a UriBuilder which encapsulates
@@ -807,28 +815,35 @@
      * @return                     the #UriBuilder encapsulating the object's 
path
      */
     public static <R extends BaseResource> UriBuilder getUriBuilder(UriInfo 
uriInfo, R model, Class<? extends BaseResource> suggestedParentType) {
-        Collection collection = getCollection(model, suggestedParentType);
-        if (collection == null) {
-            return null;
-        }
+        UriBuilder uriBuilder = builderMap.get(model.getClass());
 
-        UriBuilder uriBuilder;
+        if (uriBuilder == null) {
+            Collection collection = getCollection(model, suggestedParentType);
+            if (collection == null) {
+                return null;
+            }
 
-        if (collection.getParentType() != NO_PARENT) {
-            BaseResource parent = getParentModel(model, 
collection.getParentType());
+            if (collection.getParentType() != NO_PARENT) {
+                BaseResource parent = getParentModel(model, 
collection.getParentType());
 
-            Collection parentCollection = getCollection(parent, 
suggestedParentType);
+                Collection parentCollection = getCollection(parent, 
suggestedParentType);
 
-            String path = getPath(collection.getCollectionType(),
-                                  parentCollection.getResourceType(),
-                                  model.getClass());
+                String path = getPath(collection.getCollectionType(),
+                                      parentCollection.getResourceType(),
+                                      model.getClass());
 
-            uriBuilder = getUriBuilder(uriInfo, parent).path(path);
+                uriBuilder = getUriBuilder(uriInfo, parent).path(path);
+            } else {
+                String path = getPath(collection.getCollectionType());
+                uriBuilder = uriInfo != null
+                             ? 
UriBuilder.fromPath(uriInfo.getBaseUri().getPath()).path(path)
+                             : UriBuilder.fromPath(path);
+            }
+            builderMap.put(model.getClass(), uriBuilder.clone());
         } else {
-            String path = getPath(collection.getCollectionType());
-            uriBuilder = uriInfo != null
-                         ? 
UriBuilder.fromPath(uriInfo.getBaseUri().getPath()).path(path)
-                         : UriBuilder.fromPath(path);
+            //We need to clone so we have our own copy to work with. Cloning 
is much faster than building a new one
+            //from scratch each time.
+            uriBuilder = uriBuilder.clone();
         }
 
         return uriBuilder.path(model.getId());


-- 
To view, visit http://gerrit.ovirt.org/37988
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I86221e8af24da28a7137c0cc0e7aec69943a65c5
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Alexander Wels <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to