Search in sources :

Example 1 with GroupCounts

use of uk.gov.gchq.gaffer.data.GroupCounts in project Gaffer by gchq.

the class CountGroupsHandler method doOperation.

@Override
public GroupCounts doOperation(final CountGroups operation, final Context context, final Store store) throws OperationException {
    int count = 0;
    final GroupCounts groupCounts = new GroupCounts();
    if (null != operation.getElements()) {
        for (final Element element : operation.getElements()) {
            if (null != operation.getLimit()) {
                count++;
                if (count > operation.getLimit()) {
                    groupCounts.setLimitHit(true);
                    break;
                }
            }
            if (element instanceof Entity) {
                groupCounts.addEntityGroup(element.getGroup());
            } else {
                groupCounts.addEdgeGroup(element.getGroup());
            }
        }
    }
    return groupCounts;
}
Also used : Entity(uk.gov.gchq.gaffer.data.element.Entity) Element(uk.gov.gchq.gaffer.data.element.Element) GroupCounts(uk.gov.gchq.gaffer.data.GroupCounts)

Example 2 with GroupCounts

use of uk.gov.gchq.gaffer.data.GroupCounts in project Gaffer by gchq.

the class CountGroupsHandlerTest method shouldReturnGroupCountsWithoutLimit.

@Test
public void shouldReturnGroupCountsWithoutLimit() throws OperationException {
    // Given
    final CountGroupsHandler handler = new CountGroupsHandler();
    final Store store = mock(Store.class);
    final CountGroups countGroups = mock(CountGroups.class);
    final CloseableIterable<Element> elements = getElements();
    final Context context = new Context();
    given(countGroups.getLimit()).willReturn(null);
    given(countGroups.getElements()).willReturn(elements);
    // When
    final GroupCounts counts = handler.doOperation(countGroups, context, store);
    // Then
    assertFalse(counts.isLimitHit());
    assertEquals(2, counts.getEntityGroups().size());
    assertEquals(3, (int) counts.getEntityGroups().get(GROUP1));
    assertEquals(1, (int) counts.getEntityGroups().get(GROUP2));
    assertEquals(2, counts.getEdgeGroups().size());
    assertEquals(1, (int) counts.getEdgeGroups().get(GROUP1));
    assertEquals(3, (int) counts.getEdgeGroups().get(GROUP2));
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) CountGroups(uk.gov.gchq.gaffer.operation.impl.CountGroups) Element(uk.gov.gchq.gaffer.data.element.Element) CountGroupsHandler(uk.gov.gchq.gaffer.store.operation.handler.CountGroupsHandler) Store(uk.gov.gchq.gaffer.store.Store) GroupCounts(uk.gov.gchq.gaffer.data.GroupCounts) Test(org.junit.Test)

Example 3 with GroupCounts

use of uk.gov.gchq.gaffer.data.GroupCounts in project Gaffer by gchq.

the class CountGroupsHandlerTest method shouldReturnAllGroupCountsWhenLessThanLimit.

@Test
public void shouldReturnAllGroupCountsWhenLessThanLimit() throws OperationException {
    // Given
    final CountGroupsHandler handler = new CountGroupsHandler();
    final Store store = mock(Store.class);
    final CountGroups countGroups = mock(CountGroups.class);
    final CloseableIterable<Element> elements = getElements();
    final Integer limit = 10;
    final Context context = new Context();
    given(countGroups.getLimit()).willReturn(limit);
    given(countGroups.getElements()).willReturn(elements);
    // When
    final GroupCounts counts = handler.doOperation(countGroups, context, store);
    // Then
    assertFalse(counts.isLimitHit());
    assertEquals(2, counts.getEntityGroups().size());
    assertEquals(3, (int) counts.getEntityGroups().get(GROUP1));
    assertEquals(1, (int) counts.getEntityGroups().get(GROUP2));
    assertEquals(2, counts.getEdgeGroups().size());
    assertEquals(1, (int) counts.getEdgeGroups().get(GROUP1));
    assertEquals(3, (int) counts.getEdgeGroups().get(GROUP2));
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) CountGroups(uk.gov.gchq.gaffer.operation.impl.CountGroups) Element(uk.gov.gchq.gaffer.data.element.Element) CountGroupsHandler(uk.gov.gchq.gaffer.store.operation.handler.CountGroupsHandler) Store(uk.gov.gchq.gaffer.store.Store) GroupCounts(uk.gov.gchq.gaffer.data.GroupCounts) Test(org.junit.Test)

Example 4 with GroupCounts

use of uk.gov.gchq.gaffer.data.GroupCounts in project Gaffer by gchq.

the class OperationServiceIT method shouldReturnGroupCounts.

@Test
public void shouldReturnGroupCounts() throws IOException {
    // Given
    RestApiTestUtil.addElements(DEFAULT_ELEMENTS);
    // When
    final Response response = RestApiTestUtil.executeOperationChainChunked(new OperationChain.Builder().first(new GetAllElements<>()).then(new CountGroups()).build());
    // Then
    final GroupCounts groupCounts = response.readEntity(new GenericType<GroupCounts>() {
    });
    verifyGroupCounts(groupCounts);
}
Also used : Response(javax.ws.rs.core.Response) CountGroups(uk.gov.gchq.gaffer.operation.impl.CountGroups) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) GroupCounts(uk.gov.gchq.gaffer.data.GroupCounts) Test(org.junit.Test)

Example 5 with GroupCounts

use of uk.gov.gchq.gaffer.data.GroupCounts in project Gaffer by gchq.

the class CountGroupsIT method shouldCountGroupsOfElementsWhenMoreElementsThanLimit.

@Test
public void shouldCountGroupsOfElementsWhenMoreElementsThanLimit() throws OperationException, InterruptedException {
    // Given
    final User user = new User();
    final int limit = 5;
    final Entity entity = new Entity(TestGroups.ENTITY_2, VERTEX);
    entity.putProperty(TestPropertyNames.INT, 100);
    // When
    final GroupCounts counts = graph.execute(new Builder().first(new GetAllElements<>()).then(new CountGroups(limit)).build(), user);
    // Then
    int totalCount = (null != counts.getEntityGroups().get(TestGroups.ENTITY) ? counts.getEntityGroups().get(TestGroups.ENTITY) : 0);
    totalCount += (null != counts.getEdgeGroups().get(TestGroups.EDGE) ? counts.getEdgeGroups().get(TestGroups.EDGE) : 0);
    assertEquals(limit, totalCount);
}
Also used : Entity(uk.gov.gchq.gaffer.data.element.Entity) User(uk.gov.gchq.gaffer.user.User) CountGroups(uk.gov.gchq.gaffer.operation.impl.CountGroups) Builder(uk.gov.gchq.gaffer.operation.OperationChain.Builder) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) GroupCounts(uk.gov.gchq.gaffer.data.GroupCounts) Test(org.junit.Test)

Aggregations

GroupCounts (uk.gov.gchq.gaffer.data.GroupCounts)10 Test (org.junit.Test)9 CountGroups (uk.gov.gchq.gaffer.operation.impl.CountGroups)9 GetAllElements (uk.gov.gchq.gaffer.operation.impl.get.GetAllElements)5 Element (uk.gov.gchq.gaffer.data.element.Element)4 Entity (uk.gov.gchq.gaffer.data.element.Entity)4 Context (uk.gov.gchq.gaffer.store.Context)4 Store (uk.gov.gchq.gaffer.store.Store)4 CountGroupsHandler (uk.gov.gchq.gaffer.store.operation.handler.CountGroupsHandler)4 Builder (uk.gov.gchq.gaffer.operation.OperationChain.Builder)3 User (uk.gov.gchq.gaffer.user.User)3 Response (javax.ws.rs.core.Response)2 ChunkedInput (org.glassfish.jersey.client.ChunkedInput)1