Search in sources :

Example 6 with Sort

use of uk.gov.gchq.gaffer.operation.impl.compare.Sort in project Gaffer by gchq.

the class SortHandlerTest method shouldSortLargeNumberOfElements.

@Test
public void shouldSortLargeNumberOfElements() throws OperationException {
    // Given
    final int streamSize = 10000;
    final int resultLimit = 5000;
    final Stream<Element> stream = // generate a few extra in case there
    new Random().ints(streamSize * 2).distinct().limit(streamSize).mapToObj(i -> new Entity.Builder().group(TestGroups.ENTITY).property("property", i).build());
    final Sort sort = new Sort.Builder().input(() -> stream.iterator()).comparators(new ElementPropertyComparator.Builder().groups(TestGroups.ENTITY).property("property").reverse(false).build()).resultLimit(resultLimit).deduplicate(true).build();
    final SortHandler handler = new SortHandler();
    // When
    final Iterable<? extends Element> result = handler.doOperation(sort, null, null);
    // Then
    final ArrayList<? extends Element> elements = Lists.newArrayList(result);
    final ArrayList<? extends Element> sortedElements = Lists.newArrayList(result);
    sortedElements.sort(new ElementPropertyComparator.Builder().groups(TestGroups.ENTITY).property("property").reverse(false).build());
    assertEquals(elements, sortedElements);
    assertNotNull(result);
    assertEquals(resultLimit, Iterables.size(result));
}
Also used : Random(java.util.Random) Element(uk.gov.gchq.gaffer.data.element.Element) Sort(uk.gov.gchq.gaffer.operation.impl.compare.Sort) ElementPropertyComparator(uk.gov.gchq.gaffer.data.element.comparison.ElementPropertyComparator) Test(org.junit.jupiter.api.Test)

Example 7 with Sort

use of uk.gov.gchq.gaffer.operation.impl.compare.Sort in project Gaffer by gchq.

the class SortHandlerTest method shouldSortBasedOnPropertyIncludingNulls.

@Test
public void shouldSortBasedOnPropertyIncludingNulls() throws OperationException, JsonProcessingException {
    // Given
    final Entity entity1 = new Entity.Builder().group(TestGroups.ENTITY).property("property", 1).build();
    final Entity entity2 = new Entity.Builder().group(TestGroups.ENTITY).property("property", 2).build();
    final Entity entity3 = new Entity.Builder().group(TestGroups.ENTITY).property("property", 3).build();
    final Entity entity4 = new Entity.Builder().group(TestGroups.ENTITY).build();
    final Entity entity5 = new Entity.Builder().group(TestGroups.ENTITY).build();
    final List<Entity> input = Lists.newArrayList(entity1, entity3, entity4, entity2, entity5);
    final Sort sort = new Sort.Builder().input(input).comparators(new ElementPropertyComparator.Builder().property("property").groups(TestGroups.ENTITY).comparator(new PropertyComparatorImpl()).build()).deduplicate(true).build();
    final SortHandler handler = new SortHandler();
    // When
    final Iterable<? extends Element> result = handler.doOperation(sort, null, null);
    // Then
    assertEquals(Arrays.asList(entity1, entity2, entity3, entity4), Lists.newArrayList(result));
}
Also used : Entity(uk.gov.gchq.gaffer.data.element.Entity) Sort(uk.gov.gchq.gaffer.operation.impl.compare.Sort) ElementPropertyComparator(uk.gov.gchq.gaffer.data.element.comparison.ElementPropertyComparator) Test(org.junit.jupiter.api.Test)

Example 8 with Sort

use of uk.gov.gchq.gaffer.operation.impl.compare.Sort in project Gaffer by gchq.

the class SortHandlerTest method shouldReturnNullIfOperationInputIsNull.

@Test
public void shouldReturnNullIfOperationInputIsNull() throws OperationException {
    // Given
    final Sort sort = new Sort.Builder().build();
    final SortHandler handler = new SortHandler();
    // When
    final Iterable<? extends Element> result = handler.doOperation(sort, null, null);
    // Then
    assertNull(result);
}
Also used : Sort(uk.gov.gchq.gaffer.operation.impl.compare.Sort) Test(org.junit.jupiter.api.Test)

Example 9 with Sort

use of uk.gov.gchq.gaffer.operation.impl.compare.Sort in project Gaffer by gchq.

the class SortHandlerTest method shouldSortBasedOnProperty.

@Test
public void shouldSortBasedOnProperty() throws OperationException, JsonProcessingException {
    // Given
    final Entity entity1 = new Entity.Builder().group(TestGroups.ENTITY).property("property", 1).build();
    final Entity entity2 = new Entity.Builder().group(TestGroups.ENTITY).property("property", 2).build();
    final Entity entity3a = new Entity.Builder().group(TestGroups.ENTITY).property("property", 3).property("otherProp", "a").build();
    final Entity entity3b = new Entity.Builder().group(TestGroups.ENTITY).property("property", 3).property("otherProp", "b").build();
    final Entity entity4 = new Entity.Builder().group(TestGroups.ENTITY).property("property", 4).build();
    final List<Entity> input = Lists.newArrayList(entity1, entity4, entity3a, entity3b, entity2);
    final Sort sort = new Sort.Builder().input(input).comparators(new ElementPropertyComparator.Builder().groups(TestGroups.ENTITY).property("property").build()).build();
    final SortHandler handler = new SortHandler();
    // When
    final Iterable<? extends Element> result = handler.doOperation(sort, null, null);
    // Then
    final List<? extends Element> resultList = Lists.newArrayList(result);
    assertTrue(Arrays.asList(entity1, entity2, entity3a, entity3b, entity4).equals(resultList) || Arrays.asList(entity1, entity2, entity3b, entity3a, entity4).equals(resultList), "Expected: \n" + Arrays.asList(entity1, entity2, entity3a, entity3b, entity4) + "\n but got: \n" + resultList);
}
Also used : Entity(uk.gov.gchq.gaffer.data.element.Entity) Sort(uk.gov.gchq.gaffer.operation.impl.compare.Sort) ElementPropertyComparator(uk.gov.gchq.gaffer.data.element.comparison.ElementPropertyComparator) Test(org.junit.jupiter.api.Test)

Example 10 with Sort

use of uk.gov.gchq.gaffer.operation.impl.compare.Sort in project Gaffer by gchq.

the class SortHandlerTest method shouldSortBasedOn2Properties.

@Test
public void shouldSortBasedOn2Properties() throws OperationException, JsonProcessingException {
    // Given
    final Entity entity1 = new Entity.Builder().group(TestGroups.ENTITY).property("property1", 1).property("property2", 1).build();
    final Entity entity2 = new Entity.Builder().group(TestGroups.ENTITY).property("property1", 1).property("property2", 2).build();
    final Entity entity3 = new Entity.Builder().group(TestGroups.ENTITY).property("property1", 2).property("property2", 2).build();
    final Entity entity4 = new Entity.Builder().group(TestGroups.ENTITY).property("property1", 2).property("property2", 1).build();
    final List<Entity> input = Lists.newArrayList(entity1, entity3, entity2, entity4);
    final Sort sort = new Sort.Builder().input(input).comparators(new ElementPropertyComparator.Builder().groups(TestGroups.ENTITY).property("property1").build(), new ElementPropertyComparator.Builder().groups(TestGroups.ENTITY).property("property2").build()).build();
    final SortHandler handler = new SortHandler();
    // When
    final Iterable<? extends Element> result = handler.doOperation(sort, null, null);
    // Then
    assertEquals(Arrays.asList(entity1, entity2, entity4, entity3), Lists.newArrayList(result));
}
Also used : Entity(uk.gov.gchq.gaffer.data.element.Entity) Sort(uk.gov.gchq.gaffer.operation.impl.compare.Sort) ElementPropertyComparator(uk.gov.gchq.gaffer.data.element.comparison.ElementPropertyComparator) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)12 Sort (uk.gov.gchq.gaffer.operation.impl.compare.Sort)12 Entity (uk.gov.gchq.gaffer.data.element.Entity)8 ElementPropertyComparator (uk.gov.gchq.gaffer.data.element.comparison.ElementPropertyComparator)7 Element (uk.gov.gchq.gaffer.data.element.Element)2 Random (java.util.Random)1 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)1 TestOperationsImpl (uk.gov.gchq.gaffer.operation.TestOperationsImpl)1 DiscardOutput (uk.gov.gchq.gaffer.operation.impl.DiscardOutput)1 GenerateObjects (uk.gov.gchq.gaffer.operation.impl.generate.GenerateObjects)1 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)1 Context (uk.gov.gchq.gaffer.store.Context)1 User (uk.gov.gchq.gaffer.user.User)1