Search in sources :

Example 6 with SchemaElementDefinition

use of uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition in project Gaffer by gchq.

the class AbstractCoreKeyAccumuloElementConverter method getPropertiesFromColumnQualifier.

@Override
public Properties getPropertiesFromColumnQualifier(final String group, final byte[] bytes) throws AccumuloElementConversionException {
    final SchemaElementDefinition elementDefinition = schema.getElement(group);
    if (null == elementDefinition) {
        throw new AccumuloElementConversionException("No SchemaElementDefinition found for group " + group + ", is this group in your schema or do your table iterators need updating?");
    }
    final Properties properties = new Properties();
    if (bytes == null || bytes.length == 0) {
        return properties;
    }
    int lastDelimiter = 0;
    final int arrayLength = bytes.length;
    long currentPropLength;
    final Iterator<String> propertyNames = elementDefinition.getGroupBy().iterator();
    while (propertyNames.hasNext() && lastDelimiter < arrayLength) {
        final String propertyName = propertyNames.next();
        final TypeDefinition typeDefinition = elementDefinition.getPropertyTypeDef(propertyName);
        final Serialisation<?> serialiser = (typeDefinition != null) ? typeDefinition.getSerialiser() : null;
        if (null != serialiser) {
            final int numBytesForLength = CompactRawSerialisationUtils.decodeVIntSize(bytes[lastDelimiter]);
            final byte[] length = new byte[numBytesForLength];
            System.arraycopy(bytes, lastDelimiter, length, 0, numBytesForLength);
            try {
                currentPropLength = CompactRawSerialisationUtils.readLong(length);
            } catch (final SerialisationException e) {
                throw new AccumuloElementConversionException("Exception reading length of property", e);
            }
            lastDelimiter += numBytesForLength;
            if (currentPropLength > 0) {
                try {
                    properties.put(propertyName, serialiser.deserialise(Arrays.copyOfRange(bytes, lastDelimiter, lastDelimiter += currentPropLength)));
                } catch (SerialisationException e) {
                    throw new AccumuloElementConversionException("Failed to deserialise property " + propertyName, e);
                }
            }
        }
    }
    return properties;
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) Properties(uk.gov.gchq.gaffer.data.element.Properties) SchemaElementDefinition(uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition) AccumuloElementConversionException(uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException) TypeDefinition(uk.gov.gchq.gaffer.store.schema.TypeDefinition)

Example 7 with SchemaElementDefinition

use of uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition in project Gaffer by gchq.

the class ExamplesService method generateElements.

@Override
public GenerateElements generateElements() {
    final GenerateElements<ExampleDomainObject> op = new GenerateElements<>(new ExampleDomainObjectGenerator());
    final ArrayList<ExampleDomainObject> objs = new ArrayList<>();
    if (hasEntities()) {
        final SchemaElementDefinition entityDef = getSchema().getEntity(getAnEntityGroup());
        objs.add(new ExampleDomainObject(getAnEntityGroup(), getExampleVertex(entityDef.getIdentifierClass(IdentifierType.VERTEX), 1)));
        objs.add(new ExampleDomainObject(getAnEntityGroup(), getExampleVertex(entityDef.getIdentifierClass(IdentifierType.VERTEX), 2)));
    }
    if (hasEdges()) {
        final SchemaElementDefinition edgeDef = getSchema().getEdge(getAnEdgeGroup());
        objs.add(new ExampleDomainObject(getAnEdgeGroup(), getExampleVertex(edgeDef.getIdentifierClass(IdentifierType.SOURCE), 1), getExampleVertex(edgeDef.getIdentifierClass(IdentifierType.DESTINATION), 1), isAnEdgeDirected()));
    }
    op.setInput(objs);
    populateOperation(op);
    return op;
}
Also used : GenerateElements(uk.gov.gchq.gaffer.operation.impl.generate.GenerateElements) ArrayList(java.util.ArrayList) ExampleDomainObjectGenerator(uk.gov.gchq.gaffer.rest.example.ExampleDomainObjectGenerator) ExampleDomainObject(uk.gov.gchq.gaffer.rest.example.ExampleDomainObject) SchemaElementDefinition(uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition)

Example 8 with SchemaElementDefinition

use of uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition in project Gaffer by gchq.

the class ValidatedElementsTest method setup.

@Before
public void setup() {
    elements = new ArrayList<>();
    filters = new ArrayList<>();
    schema = mock(Schema.class);
    for (int i = 0; i < 3; i++) {
        elements.add(mock(Element.class));
        filters.add(mock(ElementFilter.class));
        final String group = "group " + i;
        given(elements.get(i).getGroup()).willReturn(group);
        given(filters.get(i).filter(elements.get(i))).willReturn(true);
        final SchemaElementDefinition elementDef = mock(SchemaElementDefinition.class);
        given(schema.getElement(group)).willReturn(elementDef);
        given(elementDef.getValidator(true)).willReturn(filters.get(i));
    }
    given(filters.get(1).filter(elements.get(1))).willReturn(false);
}
Also used : Schema(uk.gov.gchq.gaffer.store.schema.Schema) Element(uk.gov.gchq.gaffer.data.element.Element) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) SchemaElementDefinition(uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition) Before(org.junit.Before)

Example 9 with SchemaElementDefinition

use of uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition in project Gaffer by gchq.

the class ValidateHandlerTest method shouldValidatedElements.

@Test
public void shouldValidatedElements() throws OperationException {
    // Given
    final ValidateHandler handler = new ValidateHandler();
    final Store store = mock(Store.class);
    final Validate validate = mock(Validate.class);
    final Element elm1 = mock(Element.class);
    final CloseableIterable<Element> elements = new WrappedCloseableIterable<>(Collections.singletonList(elm1));
    final Schema schema = mock(Schema.class);
    final Context context = new Context();
    given(validate.getElements()).willReturn(elements);
    given(validate.isSkipInvalidElements()).willReturn(false);
    given(store.getSchema()).willReturn(schema);
    final String group = "group";
    given(elm1.getGroup()).willReturn(group);
    final SchemaElementDefinition elementDef = mock(SchemaElementDefinition.class);
    final ElementFilter validator = mock(ElementFilter.class);
    given(validator.filter(elm1)).willReturn(true);
    given(elementDef.getValidator(true)).willReturn(validator);
    given(schema.getElement(group)).willReturn(elementDef);
    // When
    final Iterable<Element> result = handler.doOperation(validate, context, store);
    // Then
    final Iterator<Element> itr = result.iterator();
    final Element elm1Result = itr.next();
    assertSame(elm1, elm1Result);
    assertFalse(itr.hasNext());
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) WrappedCloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.WrappedCloseableIterable) Element(uk.gov.gchq.gaffer.data.element.Element) Schema(uk.gov.gchq.gaffer.store.schema.Schema) Store(uk.gov.gchq.gaffer.store.Store) Validate(uk.gov.gchq.gaffer.operation.impl.Validate) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) SchemaElementDefinition(uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition) Test(org.junit.Test)

Example 10 with SchemaElementDefinition

use of uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition in project Gaffer by gchq.

the class ElementValidatorTest method shouldReturnTrueWhenSchemaValidateWithoutIsAWithValidElement.

@Test
public void shouldReturnTrueWhenSchemaValidateWithoutIsAWithValidElement() {
    // Given
    final Schema schema = mock(Schema.class);
    final String group = TestGroups.EDGE;
    final Element elm = mock(Element.class);
    final SchemaElementDefinition elementDef = mock(SchemaElementDefinition.class);
    final ElementFilter filter = mock(ElementFilter.class);
    final boolean includeIsA = false;
    final ElementValidator validator = new ElementValidator(schema, includeIsA);
    given(elm.getGroup()).willReturn(group);
    given(schema.getElement(group)).willReturn(elementDef);
    given(elementDef.getValidator(includeIsA)).willReturn(filter);
    given(filter.filter(elm)).willReturn(true);
    // When
    final boolean isValid = validator.validate(elm);
    // Then
    assertTrue(isValid);
}
Also used : Schema(uk.gov.gchq.gaffer.store.schema.Schema) Element(uk.gov.gchq.gaffer.data.element.Element) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) SchemaElementDefinition(uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition) Test(org.junit.Test)

Aggregations

SchemaElementDefinition (uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition)19 AccumuloElementConversionException (uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException)7 Element (uk.gov.gchq.gaffer.data.element.Element)5 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)5 Schema (uk.gov.gchq.gaffer.store.schema.Schema)5 TypeDefinition (uk.gov.gchq.gaffer.store.schema.TypeDefinition)5 Test (org.junit.Test)4 Properties (uk.gov.gchq.gaffer.data.element.Properties)4 SerialisationException (uk.gov.gchq.gaffer.exception.SerialisationException)4 Serialisation (uk.gov.gchq.gaffer.serialisation.Serialisation)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 Value (org.apache.accumulo.core.data.Value)1 DataType (org.apache.spark.sql.types.DataType)1