use of uk.gov.gchq.gaffer.store.operation.GetSchema in project Gaffer by gchq.
the class Store method validateSchemas.
public void validateSchemas() {
final ValidationResult validationResult = new ValidationResult();
if (null == schema) {
validationResult.addError("Schema is missing");
} else {
validationResult.add(schema.validate());
getSchemaElements().forEach((key, value) -> value.getProperties().forEach(propertyName -> {
final Class propertyClass = value.getPropertyClass(propertyName);
final Serialiser serialisation = value.getPropertyTypeDef(propertyName).getSerialiser();
if (null == serialisation) {
validationResult.addError(String.format("Could not find a serialiser for property '%s' in the group '%s'.", propertyName, key));
} else if (!serialisation.canHandle(propertyClass)) {
validationResult.addError(String.format("Schema serialiser (%s) for property '%s' in the group '%s' cannot handle property found in the schema", serialisation.getClass().getName(), propertyName, key));
}
}));
validateSchema(validationResult, getSchema().getVertexSerialiser());
getSchema().getTypes().forEach((k, v) -> validateSchema(validationResult, v.getSerialiser()));
}
if (!validationResult.isValid()) {
throw new SchemaException("Schema is not valid. " + validationResult.getErrorString());
}
}
use of uk.gov.gchq.gaffer.store.operation.GetSchema in project Gaffer by gchq.
the class FederatedGraphStorage method getSchema.
public Schema getSchema(final Map<String, String> config, final User user) {
if (null == user) {
// no user then return an empty schema
return new Schema();
}
final List<String> graphIds = FederatedStoreUtil.getGraphIds(config);
final Stream<Graph> graphs = getStream(user, graphIds);
final Builder schemaBuilder = new Builder();
try {
graphs.forEach(g -> schemaBuilder.merge(g.getSchema()));
} catch (final SchemaException e) {
final List<String> resultGraphIds = getStream(user, graphIds).map(Graph::getGraphId).collect(Collectors.toList());
throw new SchemaException(String.format(UNABLE_TO_MERGE_THE_SCHEMAS_FOR_ALL_OF_YOUR_FEDERATED_GRAPHS, resultGraphIds, KEY_OPERATION_OPTIONS_GRAPH_IDS), e);
}
return schemaBuilder.build();
}
use of uk.gov.gchq.gaffer.store.operation.GetSchema in project Gaffer by gchq.
the class FederatedGraphStorage method getSchema.
public Schema getSchema(final GetSchema operation, final Context context) {
if (null == context || null == context.getUser()) {
// no user then return an empty schema
return new Schema();
}
if (null == operation) {
return getSchema((Map<String, String>) null, context);
}
final List<String> graphIds = FederatedStoreUtil.getGraphIds(operation.getOptions());
final Stream<Graph> graphs = getStream(context.getUser(), graphIds);
final Builder schemaBuilder = new Builder();
try {
if (operation.isCompact()) {
final GetSchema getSchema = new GetSchema.Builder().compact(true).build();
graphs.forEach(g -> {
try {
schemaBuilder.merge(g.execute(getSchema, context));
} catch (final OperationException e) {
throw new RuntimeException("Unable to fetch schema from graph " + g.getGraphId(), e);
}
});
} else {
graphs.forEach(g -> schemaBuilder.merge(g.getSchema()));
}
} catch (final SchemaException e) {
final List<String> resultGraphIds = getStream(context.getUser(), graphIds).map(Graph::getGraphId).collect(Collectors.toList());
throw new SchemaException("Unable to merge the schemas for all of your federated graphs: " + resultGraphIds + ". You can limit which graphs to query for using the operation option: " + KEY_OPERATION_OPTIONS_GRAPH_IDS, e);
}
return schemaBuilder.build();
}
use of uk.gov.gchq.gaffer.store.operation.GetSchema in project Gaffer by gchq.
the class FederatedGetSchemaHandlerTest method shouldReturnSchemaOnlyForEnabledGraphs.
@Test
public void shouldReturnSchemaOnlyForEnabledGraphs() throws OperationException {
library.addProperties(ACC_PROP_ID, PROPERTIES);
fStore.setGraphLibrary(library);
final Schema edgeSchema1 = new Schema.Builder().edge("edge", new SchemaEdgeDefinition.Builder().source("string").destination("string").property("prop1", "string").directed(DIRECTED_EITHER).build()).vertexSerialiser(new StringSerialiser()).type(DIRECTED_EITHER, Boolean.class).merge(STRING_SCHEMA).build();
library.addSchema("edgeSchema1", edgeSchema1);
final Schema edgeSchema2 = new Schema.Builder().edge("edge", new SchemaEdgeDefinition.Builder().source("string").destination("string").property("prop2", "string").directed(DIRECTED_EITHER).build()).vertexSerialiser(new StringSerialiser()).type(DIRECTED_EITHER, Boolean.class).merge(STRING_SCHEMA).build();
library.addSchema("edgeSchema2", edgeSchema2);
fStore.execute(Operation.asOperationChain(new AddGraph.Builder().graphId("schemaEnabled").parentPropertiesId(ACC_PROP_ID).parentSchemaIds(Lists.newArrayList("edgeSchema1")).disabledByDefault(false).build()), context);
fStore.execute(Operation.asOperationChain(new AddGraph.Builder().graphId("schemaDisabled").parentPropertiesId(ACC_PROP_ID).parentSchemaIds(Lists.newArrayList("edgeSchema2")).disabledByDefault(true).build()), context);
final GetSchema operation = new GetSchema.Builder().compact(true).build();
// When
final Schema result = handler.doOperation(operation, context, fStore);
// Then
assertNotNull(result);
JsonAssert.assertEquals(edgeSchema1.toJson(true), result.toJson(true));
}
use of uk.gov.gchq.gaffer.store.operation.GetSchema in project Gaffer by gchq.
the class FederatedGetSchemaHandlerTest method shouldThrowExceptionForANullOperation.
@Test
public void shouldThrowExceptionForANullOperation() throws OperationException {
library.addProperties(ACC_PROP_ID, PROPERTIES);
fStore.setGraphLibrary(library);
final GetSchema operation = null;
try {
handler.doOperation(operation, context, fStore);
} catch (final OperationException e) {
assertTrue(e.getMessage().contains("Operation cannot be null"));
}
}
Aggregations