Search in sources :

Example 16 with Serialiser

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());
}
Also used : StringSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.StringSerialiser) StringToStringSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.tostring.StringToStringSerialiser) RawLongSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.raw.RawLongSerialiser) MapSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.MapSerialiser) Serialiser(uk.gov.gchq.gaffer.serialisation.Serialiser) StringSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.StringSerialiser) JavaSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.JavaSerialiser) ToBytesSerialiser(uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser) StringToStringSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.tostring.StringToStringSerialiser) Test(org.junit.jupiter.api.Test)

Example 17 with Serialiser

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());
}
Also used : TupleAdaptedPredicate(uk.gov.gchq.koryphe.tuple.predicate.TupleAdaptedPredicate) RawLongSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.raw.RawLongSerialiser) MapSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.MapSerialiser) Serialiser(uk.gov.gchq.gaffer.serialisation.Serialiser) StringSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.StringSerialiser) JavaSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.JavaSerialiser) ToBytesSerialiser(uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser) StringToStringSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.tostring.StringToStringSerialiser) MapSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.MapSerialiser) IsA(uk.gov.gchq.koryphe.impl.predicate.IsA) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) ExampleAggregateFunction(uk.gov.gchq.gaffer.function.ExampleAggregateFunction) TupleAdaptedBinaryOperator(uk.gov.gchq.koryphe.tuple.binaryoperator.TupleAdaptedBinaryOperator) ExampleFilterFunction(uk.gov.gchq.gaffer.function.ExampleFilterFunction) IsXMoreThanY(uk.gov.gchq.koryphe.impl.predicate.IsXMoreThanY) ElementAggregator(uk.gov.gchq.gaffer.data.element.function.ElementAggregator) Test(org.junit.jupiter.api.Test)

Example 18 with Serialiser

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());
}
Also used : Serialiser(uk.gov.gchq.gaffer.serialisation.Serialiser) StringSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.StringSerialiser) RawDoubleSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.raw.RawDoubleSerialiser) OrderedIntegerSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.ordered.OrderedIntegerSerialiser) RawDateSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.raw.RawDateSerialiser) JavaSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.JavaSerialiser) BooleanSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.BooleanSerialiser) RawFloatSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.raw.RawFloatSerialiser) Test(org.junit.jupiter.api.Test)

Example 19 with Serialiser

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());
}
Also used : RawDateSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.raw.RawDateSerialiser) RawDoubleSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.raw.RawDoubleSerialiser) Serialiser(uk.gov.gchq.gaffer.serialisation.Serialiser) StringSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.StringSerialiser) RawDoubleSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.raw.RawDoubleSerialiser) OrderedIntegerSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.ordered.OrderedIntegerSerialiser) RawDateSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.raw.RawDateSerialiser) JavaSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.JavaSerialiser) BooleanSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.BooleanSerialiser) RawFloatSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.raw.RawFloatSerialiser) RawFloatSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.raw.RawFloatSerialiser) Test(org.junit.jupiter.api.Test)

Example 20 with Serialiser

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;
}
Also used : Serialiser(uk.gov.gchq.gaffer.serialisation.Serialiser) HashSet(java.util.HashSet)

Aggregations

Serialiser (uk.gov.gchq.gaffer.serialisation.Serialiser)28 Test (org.junit.jupiter.api.Test)18 JavaSerialiser (uk.gov.gchq.gaffer.serialisation.implementation.JavaSerialiser)18 StringSerialiser (uk.gov.gchq.gaffer.serialisation.implementation.StringSerialiser)16 ToBytesSerialiser (uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser)10 BooleanSerialiser (uk.gov.gchq.gaffer.serialisation.implementation.BooleanSerialiser)8 OrderedIntegerSerialiser (uk.gov.gchq.gaffer.serialisation.implementation.ordered.OrderedIntegerSerialiser)8 RawDateSerialiser (uk.gov.gchq.gaffer.serialisation.implementation.raw.RawDateSerialiser)8 RawDoubleSerialiser (uk.gov.gchq.gaffer.serialisation.implementation.raw.RawDoubleSerialiser)8 RawFloatSerialiser (uk.gov.gchq.gaffer.serialisation.implementation.raw.RawFloatSerialiser)8 StringToStringSerialiser (uk.gov.gchq.gaffer.serialisation.implementation.tostring.StringToStringSerialiser)6 MapSerialiser (uk.gov.gchq.gaffer.serialisation.implementation.MapSerialiser)5 RawLongSerialiser (uk.gov.gchq.gaffer.serialisation.implementation.raw.RawLongSerialiser)5 SerialisationException (uk.gov.gchq.gaffer.exception.SerialisationException)4 Schema (uk.gov.gchq.gaffer.store.schema.Schema)4 ArrayList (java.util.ArrayList)3 JSONSerialiser (uk.gov.gchq.gaffer.jsonserialisation.JSONSerialiser)3 AvroSerialiser (uk.gov.gchq.gaffer.serialisation.AvroSerialiser)3 SchemaElementDefinition (uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition)3 HyperLogLogPlus (com.clearspring.analytics.stream.cardinality.HyperLogLogPlus)2