ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject git commit: AMBARI-5981 Hosts API should provide count of filtered hosts. Additional Patch. (dsen)
Date Wed, 04 Jun 2014 12:32:23 GMT
Repository: ambari
Updated Branches:
  refs/heads/trunk c34090aea -> e37f9657e


AMBARI-5981 Hosts API should provide count of filtered hosts. Additional Patch. (dsen)


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

Branch: refs/heads/trunk
Commit: e37f9657ed67d20285f8eba7b5cb03568aec96ad
Parents: c34090a
Author: Dmitry Sen <dsen@hortonworks.com>
Authored: Wed Jun 4 15:02:18 2014 +0300
Committer: Dmitry Sen <dsen@hortonworks.com>
Committed: Wed Jun 4 15:06:53 2014 +0300

----------------------------------------------------------------------
 .../internal/ClusterControllerImpl.java         | 41 ++++++++----
 .../internal/ClusterControllerImplTest.java     | 67 ++++++++++++++++++++
 2 files changed, 97 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/e37f9657/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterControllerImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterControllerImpl.java
b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterControllerImpl.java
index a54b303..cde9e22 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterControllerImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterControllerImpl.java
@@ -535,6 +535,23 @@ public class ClusterControllerImpl implements ClusterController {
   }
 
   /**
+   * Evaluate the predicate and create a list of filtered resources
+   *
+   * @param resourceIterable @ResourceIterable
+   * @return @NavigableSet of filtered resources
+   */
+  private LinkedList<Resource> getEvaluatedResources(ResourceIterable
+                                              resourceIterable) {
+    LinkedList<Resource> resources = new LinkedList<Resource>();
+    if (resourceIterable != null) {
+      for (Resource resource : resourceIterable) {
+        resources.add(resource);
+      }
+    }
+    return resources;
+  }
+
+  /**
    * Get one page of resources from the given set of resources starting at the given offset.
    *
    * @param pageSize   the page size
@@ -549,10 +566,12 @@ public class ClusterControllerImpl implements ClusterController {
                                          Predicate predicate,
                                          ResourcePredicateEvaluator evaluator) {
 
-    int                currentOffset = 0;
-    Resource           previous      = null;
-    Set<Resource>      pageResources = new LinkedHashSet<Resource>();
-    Iterator<Resource> iterator      = resources.iterator();
+    int currentOffset = 0;
+    Resource previous      = null;
+    Set<Resource> pageResources = new LinkedHashSet<Resource>();
+    LinkedList<Resource> filteredResources =
+      getEvaluatedResources(new ResourceIterable(resources, predicate, evaluator));
+    Iterator<Resource> iterator = filteredResources.iterator();
 
     // skip till offset
     while (currentOffset < offset && iterator.hasNext()) {
@@ -565,12 +584,11 @@ public class ClusterControllerImpl implements ClusterController {
       pageResources.add(iterator.next());
     }
 
-    return new PageResponseImpl(new ResourceIterable(pageResources,
-        predicate, evaluator),
+    return new PageResponseImpl(pageResources,
         currentOffset,
         previous,
         iterator.hasNext() ? iterator.next() : null,
-        resources.size()
+        filteredResources.size()
       );
   }
 
@@ -592,7 +610,9 @@ public class ClusterControllerImpl implements ClusterController {
     int                currentOffset = resources.size() - 1;
     Resource           next          = null;
     List<Resource>     pageResources = new LinkedList<Resource>();
-    Iterator<Resource> iterator      = resources.descendingIterator();
+    LinkedList<Resource> filteredResources =
+      getEvaluatedResources(new ResourceIterable(resources, predicate, evaluator));
+    Iterator<Resource> iterator = filteredResources.descendingIterator();
 
     if (offset != -1) {
       // skip till offset
@@ -608,12 +628,11 @@ public class ClusterControllerImpl implements ClusterController {
       --currentOffset;
     }
 
-    return new PageResponseImpl(new ResourceIterable(new
-        LinkedHashSet<Resource>(pageResources), predicate, evaluator),
+    return new PageResponseImpl(pageResources,
         currentOffset + 1,
         iterator.hasNext() ? iterator.next() : null,
         next,
-        resources.size()
+        filteredResources.size()
       );
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/e37f9657/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterControllerImplTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterControllerImplTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterControllerImplTest.java
index 4458799..9e6d859 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterControllerImplTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterControllerImplTest.java
@@ -151,6 +151,8 @@ public class ClusterControllerImplTest {
     Assert.assertEquals(Resource.Type.Host, list.get(0).getType());
     Assert.assertEquals("host:1", (String) list.get(1).getPropertyValue(PropertyHelper.getPropertyId("Hosts",
"host_name")));
     Assert.assertEquals(Resource.Type.Host, list.get(1).getType());
+    //total hosts created in TestHostResourceProvider, not only on this page
+    Assert.assertEquals(4, pageResponse.getTotalResourceCount().intValue());
 
     // get the first three
     pageRequest = new PageRequestImpl(PageRequest.StartingPoint.Beginning, 3, 0, null, null);
@@ -169,6 +171,8 @@ public class ClusterControllerImplTest {
     Assert.assertEquals(Resource.Type.Host, list.get(1).getType());
     Assert.assertEquals("host:2", (String) list.get(2).getPropertyValue(PropertyHelper.getPropertyId("Hosts",
"host_name")));
     Assert.assertEquals(Resource.Type.Host, list.get(2).getType());
+    //total hosts created in TestHostResourceProvider, not only on this page
+    Assert.assertEquals(4, pageResponse.getTotalResourceCount().intValue());
   }
 
   @Test
@@ -287,6 +291,8 @@ public class ClusterControllerImplTest {
     Assert.assertEquals(Resource.Type.Host, list.get(0).getType());
     Assert.assertEquals("host:2", (String) list.get(1).getPropertyValue(PropertyHelper.getPropertyId("Hosts",
"host_name")));
     Assert.assertEquals(Resource.Type.Host, list.get(1).getType());
+    //total hosts created in TestHostResourceProvider, not only on this page
+    Assert.assertEquals(4, pageResponse.getTotalResourceCount().intValue());
 
     // get the last three (0 - 2)
     pageRequest = new PageRequestImpl(PageRequest.StartingPoint.OffsetStart, 3, 0, null,
null);
@@ -336,6 +342,8 @@ public class ClusterControllerImplTest {
     Assert.assertEquals(Resource.Type.Host, list.get(0).getType());
     Assert.assertEquals("host:3", (String) list.get(1).getPropertyValue(PropertyHelper.getPropertyId("Hosts",
"host_name")));
     Assert.assertEquals(Resource.Type.Host, list.get(1).getType());
+    //total hosts created in TestHostResourceProvider, not only on this page
+    Assert.assertEquals(4, pageResponse.getTotalResourceCount().intValue());
 
     // get the last three
     pageRequest = new PageRequestImpl(PageRequest.StartingPoint.End, 3, 0, null, null);
@@ -373,6 +381,8 @@ public class ClusterControllerImplTest {
     Assert.assertEquals(1, pageResponse.getOffset());
     Assert.assertEquals("host:0", pageResponse.getPreviousResource().getPropertyValue(PropertyHelper.getPropertyId("Hosts",
"host_name")));
     Assert.assertEquals("host:3", pageResponse.getNextResource().getPropertyValue(PropertyHelper.getPropertyId("Hosts",
"host_name")));
+    //total hosts created in TestHostResourceProvider, not only on this page
+    Assert.assertEquals(4, pageResponse.getTotalResourceCount().intValue());
 
     Iterable<Resource> iterable = pageResponse.getIterable();
     List<Resource> list = new LinkedList<Resource>();
@@ -527,6 +537,63 @@ public class ClusterControllerImplTest {
   }
 
   @Test
+  public void testGetResourcesSortedWithPredicateWithItemsTotal() throws Exception{
+
+    ClusterControllerImpl controller =
+      new ClusterControllerImpl(new TestProviderModule());
+
+    Set<String> propertyIds = new HashSet<String>();
+
+    Request request = PropertyHelper.getReadRequest(propertyIds);
+
+    Predicate predicate =
+      new PredicateBuilder().property("c1/p2").equals(1).toPredicate();
+    List<SortRequestProperty> sortRequestProperties = Collections.singletonList(
+      new SortRequestProperty("Hosts/host_name", SortRequest.Order.DESC));
+    SortRequest sortRequest = new SortRequestImpl(sortRequestProperties);
+
+    // get the first one
+    PageRequest pageRequest =
+      new PageRequestImpl(PageRequest.StartingPoint.Beginning, 1, 0, null, null);
+    PageResponse pageResponse =
+      controller.getResources(Resource.Type.Host, request, predicate, pageRequest, sortRequest);
+
+    Iterable<Resource> iterable = pageResponse.getIterable();
+    List<Resource> list = new LinkedList<Resource>();
+
+    for (Resource resource : iterable) {
+      list.add(resource);
+    }
+    Assert.assertEquals(1, list.size());
+    //total hosts after applying the filter, not only on this page
+    Assert.assertEquals(2, pageResponse.getTotalResourceCount().intValue());
+    // DESC sorted
+    Assert.assertEquals("host:3", (String) list.get(0).getPropertyValue(
+      PropertyHelper.getPropertyId("Hosts", "host_name")));
+    Assert.assertEquals(Resource.Type.Host, list.get(0).getType());
+
+    pageRequest =
+      new PageRequestImpl(PageRequest.StartingPoint.OffsetStart, 1, 1, null, null);
+    pageResponse = controller.getResources(
+      Resource.Type.Host, request, predicate, pageRequest, sortRequest);
+
+    iterable = pageResponse.getIterable();
+    list.clear();
+
+    for (Resource resource : iterable) {
+      list.add(resource);
+    }
+    Assert.assertEquals(1, list.size());
+    //total hosts after applying the filter, not only on this page
+    Assert.assertEquals(2, pageResponse.getTotalResourceCount().intValue());
+    // DESC sorted
+    Assert.assertEquals("host:1", (String) list.get(0).getPropertyValue(
+      PropertyHelper.getPropertyId("Hosts", "host_name")));
+    Assert.assertEquals(Resource.Type.Host, list.get(0).getType());
+
+  }
+
+  @Test
   public void testCreateResources() throws Exception{
     TestProviderModule providerModule = new TestProviderModule();
     TestHostResourceProvider resourceProvider = (TestHostResourceProvider) providerModule.getResourceProvider(Resource.Type.Host);


Mime
View raw message