use of uk.gov.gchq.gaffer.data.element.id.EntityId in project Gaffer by gchq.
the class FilterToOperationConverterTest method testSpecifySourceOrDestinationAndPropertyFilter.
@Test
public void testSpecifySourceOrDestinationAndPropertyFilter() {
final Schema schema = getSchema();
final SparkSession sparkSession = SparkSessionProvider.getSparkSession();
// Specify src and a filter on property1
Filter[] filters = new Filter[2];
filters[0] = new GreaterThan("property1", 5);
filters[1] = new EqualTo(SchemaToStructTypeConverter.SRC_COL_NAME, "0");
FiltersToOperationConverter converter = new FiltersToOperationConverter(getViewFromSchema(schema), schema, filters);
Operation operation = converter.getOperation();
assertTrue(operation instanceof GetRDDOfElements);
assertEquals(0, ((GraphFilters) operation).getView().getEntityGroups().size());
assertEquals(2, ((GraphFilters) operation).getView().getEdgeGroups().size());
final Set<EntityId> seeds = new HashSet<>();
for (final Object seed : ((GetRDDOfElements) operation).getInput()) {
seeds.add((EntitySeed) seed);
}
assertEquals(Collections.singleton(new EntitySeed("0")), seeds);
View opView = ((GraphFilters) operation).getView();
for (final String edgeGroup : EDGE_GROUPS) {
final List<TupleAdaptedPredicate<String, ?>> edgePostAggFilters = opView.getEdge(edgeGroup).getPostAggregationFilterFunctions();
assertThat(edgePostAggFilters).hasSize(1);
assertArrayEquals(new String[] { "property1" }, edgePostAggFilters.get(0).getSelection());
assertEquals(new IsMoreThan(5, false), edgePostAggFilters.get(0).getPredicate());
}
// Specify src and filters on property1 and property4
filters = new Filter[3];
filters[0] = new GreaterThan("property1", 5);
filters[1] = new EqualTo(SchemaToStructTypeConverter.SRC_COL_NAME, "0");
filters[2] = new LessThan("property4", 8);
converter = new FiltersToOperationConverter(getViewFromSchema(schema), schema, filters);
operation = converter.getOperation();
assertTrue(operation instanceof GetRDDOfElements);
assertEquals(0, ((GraphFilters) operation).getView().getEntityGroups().size());
assertEquals(1, ((GraphFilters) operation).getView().getEdgeGroups().size());
seeds.clear();
for (final Object seed : ((GetRDDOfElements) operation).getInput()) {
seeds.add((EntitySeed) seed);
}
assertEquals(Collections.singleton(new EntitySeed("0")), seeds);
opView = ((GraphFilters) operation).getView();
final List<TupleAdaptedPredicate<String, ?>> entityPostAggFilters = opView.getEdge(EDGE_GROUP).getPostAggregationFilterFunctions();
assertThat(entityPostAggFilters).hasSize(2);
final List<String> expectedProperties = new ArrayList<>();
expectedProperties.add("property1");
expectedProperties.add("property4");
assertThat(entityPostAggFilters.get(0).getSelection()).hasSize(1);
assertEquals(expectedProperties.get(0), entityPostAggFilters.get(0).getSelection()[0]);
assertEquals(new IsMoreThan(5, false), entityPostAggFilters.get(0).getPredicate());
assertThat(entityPostAggFilters.get(1).getSelection()).hasSize(1);
assertEquals(expectedProperties.get(1), entityPostAggFilters.get(1).getSelection()[0]);
assertEquals(new IsLessThan(8, false), entityPostAggFilters.get(1).getPredicate());
}
use of uk.gov.gchq.gaffer.data.element.id.EntityId in project Gaffer by gchq.
the class AccumuloStoreTest method testAbleToInsertAndRetrieveEntityQueryingEqualAndRelated.
private void testAbleToInsertAndRetrieveEntityQueryingEqualAndRelated(final AccumuloStore store) throws OperationException {
final Entity e = new Entity(TestGroups.ENTITY, "1");
e.putProperty(TestPropertyNames.PROP_1, 1);
e.putProperty(TestPropertyNames.PROP_2, 2);
e.putProperty(TestPropertyNames.PROP_3, 3);
e.putProperty(TestPropertyNames.PROP_4, 4);
e.putProperty(TestPropertyNames.COUNT, 1);
final User user = new User();
final AddElements add = new AddElements.Builder().input(e).build();
store.execute(add, new Context(user));
final EntityId entityId1 = new EntitySeed("1");
final GetElements getBySeed = new GetElements.Builder().view(new View.Builder().entity(TestGroups.ENTITY).build()).input(entityId1).build();
CloseableIterable<? extends Element> results = null;
try {
results = store.execute(getBySeed, new Context(user));
assertEquals(1, Iterables.size(results));
assertTrue(Iterables.contains(results, e));
} finally {
if (results != null) {
results.close();
}
}
final GetElements getRelated = new GetElements.Builder().view(new View.Builder().entity(TestGroups.ENTITY).build()).input(entityId1).build();
CloseableIterable<? extends Element> relatedResults = null;
try {
relatedResults = store.execute(getRelated, store.createContext(user));
assertEquals(1, Iterables.size(relatedResults));
assertTrue(Iterables.contains(relatedResults, e));
final GetElements getRelatedWithPostAggregationFilter = new GetElements.Builder().view(new View.Builder().entity(TestGroups.ENTITY, new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select(TestPropertyNames.PROP_1).execute(new IsMoreThan(0)).build()).postAggregationFilter(new ElementFilter.Builder().select(TestPropertyNames.COUNT).execute(new IsMoreThan(6)).build()).build()).build()).input(entityId1).build();
relatedResults = store.execute(getRelatedWithPostAggregationFilter, store.createContext(user));
assertEquals(0, Iterables.size(relatedResults));
} finally {
if (relatedResults != null) {
relatedResults.close();
}
}
}
use of uk.gov.gchq.gaffer.data.element.id.EntityId in project Gaffer by gchq.
the class BloomFilterIT method calculateRandomLookUpRate.
private double calculateRandomLookUpRate(final FileSKVIterator reader, final HashSet<Entity> dataSet, final Random random, final RangeFactory rangeFactory) throws IOException, RangeFactoryException {
final EntityId[] randomData = new EntitySeed[5000];
for (int i = 0; i < 5000; i++) {
randomData[i] = new EntitySeed("type" + random.nextInt(Integer.MAX_VALUE));
}
final long start = System.currentTimeMillis();
for (int i = 0; i < 5000; i++) {
seek(reader, randomData[i], rangeFactory);
if (dataSet.contains(randomData[i])) {
assertThat(reader.hasTop()).isTrue();
}
}
final long end = System.currentTimeMillis();
final double randomRate = 5000 / ((end - start) / 1000.0);
LOGGER.info("Random look up rate = {}", randomRate);
return randomRate;
}
use of uk.gov.gchq.gaffer.data.element.id.EntityId in project Gaffer by gchq.
the class QueryGenerator method seedToPredicate.
private FilterPredicate seedToPredicate(final ParquetElementSeed seed, final SeededGraphFilters.IncludeIncomingOutgoingType includeIncomingOutgoingType, final SeedMatching.SeedMatchingType seedMatchingType, final String group, final boolean reversed) {
final boolean isEntityGroup = schemaUtils.getEntityGroups().contains(group);
FilterPredicate filter = null;
final ElementId elementId = seed.getElementId();
// Is it an entity group?
if (isEntityGroup) {
// EntityId case
if (elementId instanceof EntityId) {
filter = getIsEqualFilter(ParquetStore.VERTEX, ((ParquetEntitySeed) seed).getSeed(), group);
} else {
// EdgeId case
// Does the seed type need to match the group type?
final ParquetEdgeSeed edgeSeed = (ParquetEdgeSeed) seed;
if (seedMatchingType != SeedMatching.SeedMatchingType.EQUAL) {
// Vertex = source of edge seed or Vertex = destination of edge seed
// look in partition 0 with filter src = A and partition 1 with filter src = B
filter = getIsEqualFilter(ParquetStore.VERTEX, edgeSeed.getSource(), group);
if (null != ((ParquetEdgeSeed) seed).getDestination()) {
filter = FilterPredicateUtils.or(filter, getIsEqualFilter(ParquetStore.VERTEX, edgeSeed.getDestination(), group));
}
}
}
} else {
// EntityId case
if (elementId instanceof EntityId) {
// If seedMatchingType is EQUAL then we can't find anything in an edge group
if (seedMatchingType != SeedMatching.SeedMatchingType.EQUAL) {
if (includeIncomingOutgoingType == SeededGraphFilters.IncludeIncomingOutgoingType.INCOMING) {
if (reversed) {
// Dst is seed
filter = getIsEqualFilter(ParquetStore.DESTINATION, ((ParquetEntitySeed) seed).getSeed(), group);
} else {
// Src is seed and edge is undirected
filter = getIsEqualFilter(ParquetStore.SOURCE, ((ParquetEntitySeed) seed).getSeed(), group);
filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DIRECTED, new Object[] { false }, group));
}
} else if (includeIncomingOutgoingType == SeededGraphFilters.IncludeIncomingOutgoingType.OUTGOING) {
if (reversed) {
// Dst is seed and edge is undirected
filter = getIsEqualFilter(ParquetStore.DESTINATION, ((ParquetEntitySeed) seed).getSeed(), group);
filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DIRECTED, new Object[] { false }, group));
} else {
// Src is seed
filter = getIsEqualFilter(ParquetStore.SOURCE, ((ParquetEntitySeed) seed).getSeed(), group);
}
} else {
if (reversed) {
// Dst is seed
filter = getIsEqualFilter(ParquetStore.DESTINATION, ((ParquetEntitySeed) seed).getSeed(), group);
} else {
// Src is seed
filter = getIsEqualFilter(ParquetStore.SOURCE, ((ParquetEntitySeed) seed).getSeed(), group);
}
}
}
} else {
// EdgeId case
final ParquetEdgeSeed edgeSeed = (ParquetEdgeSeed) seed;
if (!reversed) {
// Src is source of edge seed and destination is destination of edge seed
filter = getIsEqualFilter(ParquetStore.SOURCE, edgeSeed.getSource(), group);
// WRONG seed is already serialised source and dest - now fixed?
filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DESTINATION, edgeSeed.getDestination(), group));
final DirectedType directedType = edgeSeed.getDirectedType();
if (directedType == DirectedType.DIRECTED) {
filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DIRECTED, new Object[] { true }, group));
} else if (directedType == DirectedType.UNDIRECTED) {
filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DIRECTED, new Object[] { false }, group));
}
} else {
// TODO Optimise this - there are times this is unnecessary
filter = getIsEqualFilter(ParquetStore.DESTINATION, edgeSeed.getSource(), group);
filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.SOURCE, edgeSeed.getDestination(), group));
final DirectedType directedType = edgeSeed.getDirectedType();
if (directedType == DirectedType.DIRECTED) {
filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DIRECTED, new Object[] { true }, group));
} else if (directedType == DirectedType.UNDIRECTED) {
filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DIRECTED, new Object[] { false }, group));
}
}
}
}
LOGGER.debug("Returning {} from seedToPredicate", filter);
return filter;
}
use of uk.gov.gchq.gaffer.data.element.id.EntityId in project Gaffer by gchq.
the class QueryGenerator method seedToParquetObject.
private ParquetElementSeed seedToParquetObject(final ElementId seed, final String group, final boolean isEntityGroup) throws SerialisationException {
final GafferGroupObjectConverter converter = schemaUtils.getConverter(group);
final String column;
if (isEntityGroup) {
column = ParquetStore.VERTEX;
} else {
column = ParquetStore.SOURCE;
}
if (seed instanceof EntityId) {
return new ParquetEntitySeed(seed, converter.gafferObjectToParquetObjects(column, ((EntityId) seed).getVertex()));
} else {
return converter.edgeIdToParquetObjects((EdgeId) seed);
}
}
Aggregations