Search in sources :

Example 11 with Serialisation

use of uk.gov.gchq.gaffer.serialisation.Serialisation in project Gaffer by gchq.

the class SchemaTest method shouldMergeDifferentSchemasOppositeWayAround.

@Test
public void shouldMergeDifferentSchemasOppositeWayAround() {
    // Given
    final String type1 = "type1";
    final String type2 = "type2";
    final Serialisation vertexSerialiser = mock(Serialisation.class);
    final Schema schema1 = new Schema.Builder().edge(TestGroups.EDGE).entity(TestGroups.ENTITY).vertexSerialiser(vertexSerialiser).type(type1, Integer.class).visibilityProperty(TestPropertyNames.VISIBILITY).build();
    final Schema schema2 = new Schema.Builder().entity(TestGroups.ENTITY_2).edge(TestGroups.EDGE_2).type(type2, String.class).build();
    // When
    final Schema mergedSchema = new Schema.Builder().merge(schema2).merge(schema1).build();
    // Then
    assertEquals(2, mergedSchema.getEdges().size());
    assertNotNull(mergedSchema.getEdge(TestGroups.EDGE));
    assertNotNull(mergedSchema.getEdge(TestGroups.EDGE_2));
    assertEquals(2, mergedSchema.getEntities().size());
    assertNotNull(mergedSchema.getEntity(TestGroups.ENTITY));
    assertNotNull(mergedSchema.getEntity(TestGroups.ENTITY_2));
    assertEquals(Integer.class, mergedSchema.getType(type1).getClazz());
    assertEquals(String.class, mergedSchema.getType(type2).getClazz());
    assertSame(vertexSerialiser, mergedSchema.getVertexSerialiser());
    assertEquals(TestPropertyNames.VISIBILITY, mergedSchema.getVisibilityProperty());
}
Also used : Serialisation(uk.gov.gchq.gaffer.serialisation.Serialisation) Test(org.junit.Test)

Example 12 with Serialisation

use of uk.gov.gchq.gaffer.serialisation.Serialisation in project Gaffer by gchq.

the class SchemaTest method shouldMergeDifferentSchemas.

@Test
public void shouldMergeDifferentSchemas() {
    // Given
    final String type1 = "type1";
    final String type2 = "type2";
    final Serialisation vertexSerialiser = mock(Serialisation.class);
    final Schema schema1 = new Schema.Builder().edge(TestGroups.EDGE).entity(TestGroups.ENTITY).vertexSerialiser(vertexSerialiser).type(type1, Integer.class).visibilityProperty(TestPropertyNames.VISIBILITY).build();
    final Schema schema2 = new Schema.Builder().entity(TestGroups.ENTITY_2).edge(TestGroups.EDGE_2).type(type2, String.class).build();
    // When
    final Schema mergedSchema = new Schema.Builder().merge(schema1).merge(schema2).build();
    // Then
    assertEquals(2, mergedSchema.getEdges().size());
    assertNotNull(mergedSchema.getEdge(TestGroups.EDGE));
    assertNotNull(mergedSchema.getEdge(TestGroups.EDGE_2));
    assertEquals(2, mergedSchema.getEntities().size());
    assertNotNull(mergedSchema.getEntity(TestGroups.ENTITY));
    assertNotNull(mergedSchema.getEntity(TestGroups.ENTITY_2));
    assertEquals(Integer.class, mergedSchema.getType(type1).getClazz());
    assertEquals(String.class, mergedSchema.getType(type2).getClazz());
    assertSame(vertexSerialiser, mergedSchema.getVertexSerialiser());
    assertEquals(TestPropertyNames.VISIBILITY, mergedSchema.getVisibilityProperty());
}
Also used : Serialisation(uk.gov.gchq.gaffer.serialisation.Serialisation) Test(org.junit.Test)

Example 13 with Serialisation

use of uk.gov.gchq.gaffer.serialisation.Serialisation in project Gaffer by gchq.

the class Store method validateSchemas.

public void validateSchemas() {
    boolean valid = schema.validate();
    final HashMap<String, SchemaElementDefinition> schemaElements = new HashMap<>();
    schemaElements.putAll(getSchema().getEdges());
    schemaElements.putAll(getSchema().getEntities());
    for (final Entry<String, SchemaElementDefinition> schemaElementDefinitionEntry : schemaElements.entrySet()) {
        for (final String propertyName : schemaElementDefinitionEntry.getValue().getProperties()) {
            Class propertyClass = schemaElementDefinitionEntry.getValue().getPropertyClass(propertyName);
            Serialisation serialisation = schemaElementDefinitionEntry.getValue().getPropertyTypeDef(propertyName).getSerialiser();
            if (null == serialisation) {
                valid = false;
                LOGGER.error("Could not find a serialiser for property '" + propertyName + "' in the group '" + schemaElementDefinitionEntry.getKey() + "'.");
            } else if (!serialisation.canHandle(propertyClass)) {
                valid = false;
                LOGGER.error("Schema serialiser (" + serialisation.getClass().getName() + ") for property '" + propertyName + "' in the group '" + schemaElementDefinitionEntry.getKey() + "' cannot handle property found in the schema");
            }
        }
    }
    if (!valid) {
        throw new SchemaException("Schema is not valid. Check the logs for more information.");
    }
}
Also used : SchemaException(uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Serialisation(uk.gov.gchq.gaffer.serialisation.Serialisation) SchemaElementDefinition(uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition)

Example 14 with Serialisation

use of uk.gov.gchq.gaffer.serialisation.Serialisation in project Gaffer by gchq.

the class SchemaOptimiser method getDefaultVertexSerialiser.

private Serialisation 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()) {
        Serialisation serialiser = null;
        if (vertexClasses.size() == 1) {
            serialiser = serialisationFactory.getSerialiser(vertexClasses.iterator().next(), isStoreOrdered);
        } else {
            for (final Class<?> clazz : vertexClasses) {
                serialiser = serialisationFactory.getSerialiser(clazz, isStoreOrdered);
                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 (serialiser instanceof JavaSerialiser) {
            LOGGER.warn("Java serialisation is not recommended for vertex serialisation - it may cause aggregation to fail. Please implement your own or change your vertex class types.");
        }
        return serialiser;
    }
    return null;
}
Also used : JavaSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.JavaSerialiser) Serialisation(uk.gov.gchq.gaffer.serialisation.Serialisation) HashSet(java.util.HashSet)

Example 15 with Serialisation

use of uk.gov.gchq.gaffer.serialisation.Serialisation in project Gaffer by gchq.

the class SerialisationFactoryTest method shouldReturnOrderedSerialiserForAString.

@Test
public void shouldReturnOrderedSerialiserForAString() throws SerialisationException {
    // Given
    final SerialisationFactory factory = new SerialisationFactory();
    final Class<?> clazz = String.class;
    final boolean ordered = true;
    // When
    final Serialisation serialiser = factory.getSerialiser(clazz, ordered);
    // Then
    assertTrue(serialiser.canHandle(clazz));
    assertEquals(StringSerialiser.class, serialiser.getClass());
}
Also used : Serialisation(uk.gov.gchq.gaffer.serialisation.Serialisation) Test(org.junit.Test)

Aggregations

Serialisation (uk.gov.gchq.gaffer.serialisation.Serialisation)15 Test (org.junit.Test)8 SchemaElementDefinition (uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition)4 AccumuloElementConversionException (uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException)3 SerialisationException (uk.gov.gchq.gaffer.exception.SerialisationException)3 TypeDefinition (uk.gov.gchq.gaffer.store.schema.TypeDefinition)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 Key (org.apache.accumulo.core.data.Key)2 RangeFactoryException (uk.gov.gchq.gaffer.accumulostore.key.exception.RangeFactoryException)2 SchemaException (uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Value (org.apache.accumulo.core.data.Value)1 Properties (uk.gov.gchq.gaffer.data.element.Properties)1 JavaSerialiser (uk.gov.gchq.gaffer.serialisation.implementation.JavaSerialiser)1