use of uk.gov.gchq.gaffer.serialisation.Serialiser in project Gaffer by gchq.
the class SchemaTest method shouldMergeDifferentSchemasOppositeWayAround.
@Test
public void shouldMergeDifferentSchemasOppositeWayAround() {
// Given
// Given
final String typeShared = "typeShared";
final String type1 = "type1";
final String type2 = "type2";
final Serialiser vertexSerialiser = new StringSerialiser();
final Schema schema1 = new Schema.Builder().edge(TestGroups.EDGE, new SchemaEdgeDefinition.Builder().property(TestPropertyNames.PROP_1, type1).build()).entity(TestGroups.ENTITY, new SchemaEntityDefinition.Builder().property(TestPropertyNames.COUNT, typeShared).build()).vertexSerialiser(vertexSerialiser).type(typeShared, Long.class).type(type1, Integer.class).visibilityProperty(TestPropertyNames.VISIBILITY).build();
final Schema schema2 = new Schema.Builder().entity(TestGroups.ENTITY_2, new SchemaEntityDefinition.Builder().property(TestPropertyNames.COUNT, typeShared).build()).edge(TestGroups.EDGE_2, new SchemaEdgeDefinition.Builder().property(TestPropertyNames.PROP_2, type2).build()).type(type2, String.class).type(typeShared, Long.class).build();
// When
final Schema mergedSchema = new Schema.Builder().merge(schema2).merge(schema2).merge(schema1).merge(// should be able to merge a duplicate schema
schema1).build();
// Then
assertEquals(2, mergedSchema.getEdges().size());
assertEquals(1, mergedSchema.getEdge(TestGroups.EDGE).getPropertyMap().size());
assertEquals(type1, mergedSchema.getEdge(TestGroups.EDGE).getPropertyMap().get(TestPropertyNames.PROP_1));
assertEquals(1, mergedSchema.getEdge(TestGroups.EDGE_2).getPropertyMap().size());
assertEquals(type2, mergedSchema.getEdge(TestGroups.EDGE_2).getPropertyMap().get(TestPropertyNames.PROP_2));
assertEquals(2, mergedSchema.getEntities().size());
assertEquals(1, mergedSchema.getEntity(TestGroups.ENTITY).getPropertyMap().size());
assertEquals(typeShared, mergedSchema.getEntity(TestGroups.ENTITY).getPropertyMap().get(TestPropertyNames.COUNT));
assertEquals(1, mergedSchema.getEntity(TestGroups.ENTITY_2).getPropertyMap().size());
assertEquals(typeShared, mergedSchema.getEntity(TestGroups.ENTITY_2).getPropertyMap().get(TestPropertyNames.COUNT));
assertEquals(Integer.class, mergedSchema.getType(type1).getClazz());
assertEquals(String.class, mergedSchema.getType(type2).getClazz());
assertEquals(vertexSerialiser, mergedSchema.getVertexSerialiser());
assertEquals(TestPropertyNames.VISIBILITY, mergedSchema.getVisibilityProperty());
}
use of uk.gov.gchq.gaffer.serialisation.Serialiser in project Gaffer by gchq.
the class SchemaTest method testLoadingSchemaFromJson.
@Test
public void testLoadingSchemaFromJson() {
// Edge definitions
SchemaElementDefinition edgeDefinition = schema.getEdge(TestGroups.EDGE);
assertNotNull(edgeDefinition);
assertEquals(EDGE_DESCRIPTION, edgeDefinition.getDescription());
final Map<String, String> propertyMap = edgeDefinition.getPropertyMap();
assertThat(propertyMap).hasSize(3);
assertEquals("prop.string", propertyMap.get(TestPropertyNames.PROP_2));
assertEquals("prop.date", propertyMap.get(TestPropertyNames.DATE));
assertEquals("timestamp", propertyMap.get(TestPropertyNames.TIMESTAMP));
assertEquals(Sets.newLinkedHashSet(Collections.singletonList(TestPropertyNames.DATE)), edgeDefinition.getGroupBy());
// Check validator
ElementFilter validator = edgeDefinition.getValidator();
List<TupleAdaptedPredicate<String, ?>> valContexts = validator.getComponents();
int index = 0;
TupleAdaptedPredicate<String, ?> tuplePredicate = valContexts.get(index++);
assertTrue(tuplePredicate.getPredicate() instanceof IsA);
assertThat(tuplePredicate.getSelection()).hasSize(1);
assertEquals(IdentifierType.SOURCE.name(), tuplePredicate.getSelection()[0]);
tuplePredicate = valContexts.get(index++);
assertTrue(tuplePredicate.getPredicate() instanceof IsA);
assertThat(tuplePredicate.getSelection()).hasSize(1);
assertEquals(IdentifierType.DESTINATION.name(), tuplePredicate.getSelection()[0]);
tuplePredicate = valContexts.get(index++);
assertTrue(tuplePredicate.getPredicate() instanceof IsA);
assertThat(tuplePredicate.getSelection()).hasSize(1);
assertEquals(IdentifierType.DIRECTED.name(), tuplePredicate.getSelection()[0]);
tuplePredicate = valContexts.get(index++);
assertTrue(tuplePredicate.getPredicate() instanceof ExampleFilterFunction);
assertThat(tuplePredicate.getSelection()).hasSize(1);
assertEquals(IdentifierType.DIRECTED.name(), tuplePredicate.getSelection()[0]);
tuplePredicate = valContexts.get(index++);
assertTrue(tuplePredicate.getPredicate() instanceof IsA);
assertThat(tuplePredicate.getSelection()).hasSize(1);
assertEquals(TestPropertyNames.PROP_2, tuplePredicate.getSelection()[0]);
tuplePredicate = valContexts.get(index++);
assertTrue(tuplePredicate.getPredicate() instanceof ExampleFilterFunction);
assertThat(tuplePredicate.getSelection()).hasSize(1);
assertEquals(TestPropertyNames.PROP_2, tuplePredicate.getSelection()[0]);
tuplePredicate = valContexts.get(index++);
assertTrue(tuplePredicate.getPredicate() instanceof IsA);
assertThat(tuplePredicate.getSelection()).hasSize(1);
assertEquals(TestPropertyNames.DATE, tuplePredicate.getSelection()[0]);
tuplePredicate = valContexts.get(index++);
assertTrue(tuplePredicate.getPredicate() instanceof IsA);
assertThat(tuplePredicate.getSelection()).hasSize(1);
assertEquals(TestPropertyNames.TIMESTAMP, tuplePredicate.getSelection()[0]);
assertEquals(index, valContexts.size());
TypeDefinition type = edgeDefinition.getPropertyTypeDef(TestPropertyNames.DATE);
assertEquals(Date.class, type.getClazz());
assertEquals(DATE_TYPE_DESCRIPTION, type.getDescription());
assertNull(type.getSerialiser());
assertTrue(type.getAggregateFunction() instanceof ExampleAggregateFunction);
// Entity definitions
SchemaElementDefinition entityDefinition = schema.getEntity(TestGroups.ENTITY);
assertNotNull(entityDefinition);
assertEquals(ENTITY_DESCRIPTION, entityDefinition.getDescription());
assertTrue(entityDefinition.containsProperty(TestPropertyNames.PROP_1));
type = entityDefinition.getPropertyTypeDef(TestPropertyNames.PROP_1);
assertEquals(0, entityDefinition.getGroupBy().size());
assertEquals(STRING_TYPE_DESCRIPTION, type.getDescription());
assertEquals(String.class, type.getClazz());
assertNull(type.getSerialiser());
assertTrue(type.getAggregateFunction() instanceof ExampleAggregateFunction);
validator = entityDefinition.getValidator();
valContexts = validator.getComponents();
index = 0;
tuplePredicate = valContexts.get(index++);
assertTrue(tuplePredicate.getPredicate() instanceof IsXMoreThanY);
assertThat(tuplePredicate.getSelection()).hasSize(2);
assertEquals(TestPropertyNames.PROP_1, tuplePredicate.getSelection()[0]);
assertEquals(TestPropertyNames.VISIBILITY, tuplePredicate.getSelection()[1]);
tuplePredicate = valContexts.get(index++);
assertTrue(tuplePredicate.getPredicate() instanceof IsA);
assertThat(tuplePredicate.getSelection()).hasSize(1);
assertEquals(IdentifierType.VERTEX.name(), tuplePredicate.getSelection()[0]);
tuplePredicate = valContexts.get(index++);
assertTrue(tuplePredicate.getPredicate() instanceof IsA);
assertThat(tuplePredicate.getSelection()).hasSize(1);
assertEquals(TestPropertyNames.PROP_1, tuplePredicate.getSelection()[0]);
final ElementAggregator aggregator = edgeDefinition.getFullAggregator();
final List<TupleAdaptedBinaryOperator<String, ?>> aggContexts = aggregator.getComponents();
assertThat(aggContexts).hasSize(3);
TupleAdaptedBinaryOperator<String, ?> aggContext = aggContexts.get(0);
assertTrue(aggContext.getBinaryOperator() instanceof ExampleAggregateFunction);
assertThat(aggContext.getSelection()).hasSize(1);
assertEquals(TestPropertyNames.PROP_2, aggContext.getSelection()[0]);
aggContext = aggContexts.get(1);
assertTrue(aggContext.getBinaryOperator() instanceof ExampleAggregateFunction);
assertThat(aggContext.getSelection()).hasSize(1);
assertEquals(TestPropertyNames.DATE, aggContext.getSelection()[0]);
TypeDefinition mapTypeDef = schema.getType(TestTypes.PROP_MAP);
assertEquals(LinkedHashMap.class, mapTypeDef.getClazz());
assertEquals(MAP_TYPE_DESCRIPTION, mapTypeDef.getDescription());
Serialiser serialiser = mapTypeDef.getSerialiser();
assertEquals(MapSerialiser.class, serialiser.getClass());
MapSerialiser mapSerialiser = (MapSerialiser) serialiser;
assertEquals(StringSerialiser.class, mapSerialiser.getKeySerialiser().getClass());
assertEquals(RawLongSerialiser.class, mapSerialiser.getValueSerialiser().getClass());
assertNull(mapSerialiser.getMapClass());
}
use of uk.gov.gchq.gaffer.serialisation.Serialiser in project Gaffer by gchq.
the class SerialisationFactoryTest method shouldReturnOrderedSerialiserForAnInteger.
@Test
public void shouldReturnOrderedSerialiserForAnInteger() throws SerialisationException {
// Given
final SerialisationFactory factory = new SerialisationFactory();
final Class<?> clazz = Integer.class;
final boolean ordered = true;
// When
final Serialiser serialiser = factory.getSerialiser(clazz, ordered, true);
// Then
assertTrue(serialiser.canHandle(clazz));
assertEquals(OrderedIntegerSerialiser.class, serialiser.getClass());
}
use of uk.gov.gchq.gaffer.serialisation.Serialiser in project Gaffer by gchq.
the class SerialisationFactoryTest method shouldReturnJavaSerialiserIfNoCustomSerialiserFound.
@Test
public void shouldReturnJavaSerialiserIfNoCustomSerialiserFound() throws SerialisationException {
// Given
final Serialiser[] serialisers = new Serialiser[] { new RawDateSerialiser(), new RawDoubleSerialiser(), new RawFloatSerialiser() };
final SerialisationFactory factory = new SerialisationFactory(serialisers);
final Class<?> clazz = String.class;
// When
final Serialiser serialiser = factory.getSerialiser(clazz);
// Then
assertTrue(serialiser.canHandle(clazz));
assertEquals(JavaSerialiser.class, serialiser.getClass());
}
use of uk.gov.gchq.gaffer.serialisation.Serialiser in project Gaffer by gchq.
the class SchemaOptimiser method getDefaultVertexSerialiser.
private Serialiser getDefaultVertexSerialiser(final Schema schema, final boolean isStoreOrdered) {
if (null != schema.getVertexSerialiser()) {
return schema.getVertexSerialiser();
}
final Set<Class<?>> vertexClasses = new HashSet<>();
for (final SchemaEntityDefinition definition : schema.getEntities().values()) {
vertexClasses.add(definition.getIdentifierClass(IdentifierType.VERTEX));
}
for (final SchemaEdgeDefinition definition : schema.getEdges().values()) {
vertexClasses.add(definition.getIdentifierClass(IdentifierType.SOURCE));
vertexClasses.add(definition.getIdentifierClass(IdentifierType.DESTINATION));
}
vertexClasses.remove(null);
if (!vertexClasses.isEmpty()) {
Serialiser serialiser = null;
if (vertexClasses.size() == 1) {
serialiser = serialisationFactory.getSerialiser(vertexClasses.iterator().next(), isStoreOrdered, true);
} else {
for (final Class<?> clazz : vertexClasses) {
serialiser = serialisationFactory.getSerialiser(clazz, isStoreOrdered, true);
boolean canHandlerAll = true;
for (final Class<?> clazz2 : vertexClasses) {
if (!serialiser.canHandle(clazz2)) {
canHandlerAll = false;
serialiser = null;
break;
}
}
if (canHandlerAll) {
break;
}
}
}
if (null == serialiser) {
throw new IllegalArgumentException("No default serialiser could be found that would support all vertex class types " + vertexClasses.toString() + ", please implement your own or change your vertex class types.");
}
if (isStoreOrdered && !serialiser.preservesObjectOrdering()) {
LOGGER.info("{} serialiser is used for vertex serialisation in an ordered store and it does not preserve the order of bytes. See https://gchq.github.io/gaffer-doc/getting-started/developer-guide/schemas.html#serialisers.", serialiser.getClass().getName());
}
return serialiser;
}
return null;
}
Aggregations