Search in sources :

Example 1 with GraphConfig

use of uk.gov.gchq.gaffer.graph.GraphConfig in project gaffer-doc by gchq.

the class TheBasics method run.

@Override
public CloseableIterable<? extends Element> run() throws OperationException, IOException {
    // [generate] Create some edges from the simple data file using our Road Use generator class
    // ---------------------------------------------------------
    final List<Element> elements = new ArrayList<>();
    final RoadUseElementGenerator dataGenerator = new RoadUseElementGenerator();
    for (final String line : IOUtils.readLines(StreamUtil.openStream(getClass(), dataPath))) {
        elements.add(dataGenerator._apply(line));
    }
    // ---------------------------------------------------------
    print("Elements generated from the data file.");
    for (final Element element : elements) {
        print("GENERATED_EDGES", element.toString());
    }
    print("");
    Graph graph;
    // [graph from files] Create a graph using config, schema and store properties files
    // ---------------------------------------------------------
    graph = new Graph.Builder().config(StreamUtil.openStream(getClass(), graphConfigPath)).addSchemas(StreamUtil.openStreams(getClass(), schemaPath)).storeProperties(StreamUtil.openStream(getClass(), storePropertiesPath)).build();
    // ---------------------------------------------------------
    // [graph] Create a graph using config, schema and store properties from java
    // ---------------------------------------------------------
    final GraphConfig config = new GraphConfig.Builder().graphId(getClass().getSimpleName()).build();
    final Schema schema = new Schema.Builder().edge("RoadUse", new SchemaEdgeDefinition.Builder().description("A directed edge representing vehicles moving from junction A to junction B.").source("junction").destination("junction").directed("true").property("count", "count.long").build()).type("junction", new TypeDefinition.Builder().description("A road junction represented by a String.").clazz(String.class).build()).type("count.long", new TypeDefinition.Builder().description("A long count that must be greater than or equal to 0.").clazz(Long.class).validateFunctions(new IsMoreThan(0L, true)).aggregateFunction(new Sum()).build()).type("true", new TypeDefinition.Builder().description("A simple boolean that must always be true.").clazz(Boolean.class).validateFunctions(new IsTrue()).build()).build();
    final AccumuloProperties properties = new AccumuloProperties();
    properties.setStoreClass(SingleUseMockAccumuloStore.class);
    properties.setInstance("instance1");
    properties.setZookeepers("zookeeper1");
    properties.setUser("user01");
    properties.setPassword("password");
    graph = new Graph.Builder().config(config).addSchema(schema).storeProperties(properties).build();
    // ---------------------------------------------------------
    // [user] Create a user
    // ---------------------------------------------------------
    final User user = new User("user01");
    // ---------------------------------------------------------
    // [add] Add the edges to the graph
    // ---------------------------------------------------------
    final AddElements addElements = new AddElements.Builder().input(elements).build();
    graph.execute(addElements, user);
    // ---------------------------------------------------------
    print("The elements have been added.");
    // [get] Get all the edges that contain the vertex "10"
    // ---------------------------------------------------------
    final GetElements query = new GetElements.Builder().input(new EntitySeed("10")).view(new View.Builder().edge("RoadUse").build()).build();
    final CloseableIterable<? extends Element> results = graph.execute(query, user);
    // ---------------------------------------------------------
    print("\nAll edges containing the vertex 10. The counts have been aggregated.");
    for (final Element e : results) {
        print("GET_ELEMENTS_RESULT", e.toString());
    }
    return results;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User) AccumuloProperties(uk.gov.gchq.gaffer.accumulostore.AccumuloProperties) Element(uk.gov.gchq.gaffer.data.element.Element) Schema(uk.gov.gchq.gaffer.store.schema.Schema) ArrayList(java.util.ArrayList) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) TypeDefinition(uk.gov.gchq.gaffer.store.schema.TypeDefinition) GraphConfig(uk.gov.gchq.gaffer.graph.GraphConfig) Sum(uk.gov.gchq.koryphe.impl.binaryoperator.Sum) Graph(uk.gov.gchq.gaffer.graph.Graph) IsTrue(uk.gov.gchq.koryphe.impl.predicate.IsTrue) RoadUseElementGenerator(uk.gov.gchq.gaffer.doc.user.generator.RoadUseElementGenerator) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) SchemaEdgeDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan)

Example 2 with GraphConfig

use of uk.gov.gchq.gaffer.graph.GraphConfig in project Gaffer by gchq.

the class FederatedGraphStorage method changeGraphId.

private boolean changeGraphId(final String graphId, final String newGraphId, final Predicate<FederatedAccess> accessPredicate) throws StorageException {
    boolean rtn;
    final Graph graphToMove = getGraphToMove(graphId, accessPredicate);
    if (nonNull(graphToMove)) {
        FederatedAccess key = null;
        // remove graph to be moved from storage
        for (final Entry<FederatedAccess, Set<Graph>> entry : storage.entrySet()) {
            final boolean removed = entry.getValue().removeIf(graph -> graph.getGraphId().equals(graphId));
            if (removed) {
                key = entry.getKey();
                break;
            }
        }
        // Update Tables
        String storeClass = graphToMove.getStoreProperties().getStoreClass();
        if (nonNull(storeClass) && storeClass.startsWith(AccumuloStore.class.getPackage().getName())) {
            /*
                 * This logic is only for Accumulo derived stores Only.
                 * For updating table names to match graphs names.
                 *
                 * uk.gov.gchq.gaffer.accumulostore.[AccumuloStore, SingleUseAccumuloStore,
                 * SingleUseMockAccumuloStore, MockAccumuloStore, MiniAccumuloStore]
                 */
            try {
                AccumuloProperties tmpAccumuloProps = (AccumuloProperties) graphToMove.getStoreProperties();
                Connector connection = TableUtils.getConnector(tmpAccumuloProps.getInstance(), tmpAccumuloProps.getZookeepers(), tmpAccumuloProps.getUser(), tmpAccumuloProps.getPassword());
                if (connection.tableOperations().exists(graphId)) {
                    connection.tableOperations().offline(graphId);
                    connection.tableOperations().rename(graphId, newGraphId);
                    connection.tableOperations().online(newGraphId);
                }
            } catch (final Exception e) {
                LOGGER.warn("Error trying to update tables for graphID:{} graphToMove:{}", graphId, graphToMove);
                LOGGER.warn("Error trying to update tables.", e);
            }
        }
        final GraphConfig configWithNewGraphId = cloneGraphConfigWithNewGraphId(newGraphId, graphToMove);
        // add the graph being renamed.
        GraphSerialisable newGraphSerialisable = new GraphSerialisable.Builder().graph(graphToMove).config(configWithNewGraphId).build();
        this.put(newGraphSerialisable, key);
        // Update cache
        if (isCacheEnabled()) {
            try {
                // Overwrite cache = true because the graphLibrary should have thrown an error before this point.
                federatedStoreCache.addGraphToCache(newGraphSerialisable, key, true);
            } catch (final CacheOperationException e) {
                String s = "Contact Admin for recovery. Error occurred updating graphId. GraphStorage=updated, Cache=outdated graphId.";
                LOGGER.error(s + " graphStorage graphId:{} cache graphId:{}", newGraphId, graphId);
                throw new StorageException(s, e);
            }
            federatedStoreCache.deleteGraphFromCache(graphId);
        }
        rtn = true;
    } else {
        rtn = false;
    }
    return rtn;
}
Also used : Connector(org.apache.accumulo.core.client.Connector) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) AccumuloProperties(uk.gov.gchq.gaffer.accumulostore.AccumuloProperties) StorageException(uk.gov.gchq.gaffer.federatedstore.exception.StorageException) SchemaException(uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException) OverwritingException(uk.gov.gchq.gaffer.commonutil.exception.OverwritingException) CacheOperationException(uk.gov.gchq.gaffer.cache.exception.CacheOperationException) OperationException(uk.gov.gchq.gaffer.operation.OperationException) GraphConfig(uk.gov.gchq.gaffer.graph.GraphConfig) Graph(uk.gov.gchq.gaffer.graph.Graph) GraphSerialisable(uk.gov.gchq.gaffer.graph.GraphSerialisable) CacheOperationException(uk.gov.gchq.gaffer.cache.exception.CacheOperationException) AccumuloStore(uk.gov.gchq.gaffer.accumulostore.AccumuloStore) StorageException(uk.gov.gchq.gaffer.federatedstore.exception.StorageException)

Example 3 with GraphConfig

use of uk.gov.gchq.gaffer.graph.GraphConfig in project Gaffer by gchq.

the class OperationControllerIT method shouldCorrectlyStreamExecuteChunked.

@Test
public void shouldCorrectlyStreamExecuteChunked() throws Exception {
    // Given
    final Schema schema = new Schema.Builder().entity("g1", new SchemaEntityDefinition.Builder().vertex("string").build()).type("string", new TypeDefinition.Builder().clazz(String.class).aggregateFunction(new StringConcat()).build()).build();
    Graph graph = new Graph.Builder().config(new GraphConfig("id")).storeProperties(new MapStoreProperties()).addSchema(schema).build();
    when(getGraphFactory().getGraph()).thenReturn(graph);
    Entity ent1 = new Entity.Builder().group("g1").vertex("v1").build();
    Entity ent2 = new Entity.Builder().group("g1").vertex("v2").build();
    final ObjectMapper mapper = createDefaultMapper();
    graph.execute(new AddElements.Builder().input(ent1).build(), new Context());
    graph.execute(new AddElements.Builder().input(ent2).build(), new Context());
    // When
    final ResponseEntity<String> response = post("/graph/operations/execute/chunked", new GetAllElements.Builder().build(), String.class);
    // Then
    String expected = mapper.writeValueAsString(ent1) + "\r\n" + mapper.writeValueAsString(ent2) + "\r\n";
    assertEquals(expected, response.getBody());
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) Context(uk.gov.gchq.gaffer.store.Context) Entity(uk.gov.gchq.gaffer.data.element.Entity) HttpEntity(org.springframework.http.HttpEntity) ResponseEntity(org.springframework.http.ResponseEntity) StringConcat(uk.gov.gchq.koryphe.impl.binaryoperator.StringConcat) Schema(uk.gov.gchq.gaffer.store.schema.Schema) SchemaEntityDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEntityDefinition) TypeDefinition(uk.gov.gchq.gaffer.store.schema.TypeDefinition) MapStoreProperties(uk.gov.gchq.gaffer.mapstore.MapStoreProperties) GraphConfig(uk.gov.gchq.gaffer.graph.GraphConfig) Graph(uk.gov.gchq.gaffer.graph.Graph) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 4 with GraphConfig

use of uk.gov.gchq.gaffer.graph.GraphConfig in project Gaffer by gchq.

the class GraphConfigurationControllerTest method shouldReturnGraphId.

@Test
public void shouldReturnGraphId() {
    // Given
    when(graphFactory.getGraph()).thenReturn(new Graph.Builder().config(new GraphConfig("id")).addSchema(new Schema()).storeProperties(new MapStoreProperties()).description("test graph").build());
    // When
    GraphConfigurationController controller = new GraphConfigurationController(graphFactory);
    final String graphId = controller.getGraphId();
    // Then
    assertEquals("id", graphId);
}
Also used : GraphConfig(uk.gov.gchq.gaffer.graph.GraphConfig) Schema(uk.gov.gchq.gaffer.store.schema.Schema) MapStoreProperties(uk.gov.gchq.gaffer.mapstore.MapStoreProperties) Test(org.junit.jupiter.api.Test)

Example 5 with GraphConfig

use of uk.gov.gchq.gaffer.graph.GraphConfig in project Gaffer by gchq.

the class GraphConfigurationControllerTest method shouldSerialiseAndDeserialiseGetStoreTraits.

@Test
public void shouldSerialiseAndDeserialiseGetStoreTraits() throws SerialisationException {
    // Given
    Store store = mock(Store.class);
    Schema schema = new Schema();
    StoreProperties props = new StoreProperties();
    Set<StoreTrait> storeTraits = Sets.newHashSet(INGEST_AGGREGATION, PRE_AGGREGATION_FILTERING, POST_AGGREGATION_FILTERING);
    when(store.getSchema()).thenReturn(schema);
    when(store.getProperties()).thenReturn(props);
    when(store.getTraits()).thenReturn(storeTraits);
    Graph graph = new Graph.Builder().config(new GraphConfig("id")).addSchema(new Schema()).store(store).build();
    when(graphFactory.getGraph()).thenReturn(graph);
    GraphConfigurationController controller = new GraphConfigurationController(graphFactory);
    // When
    byte[] bytes = JSONSerialiser.serialise(controller.getStoreTraits());
    final Set<String> traits = JSONSerialiser.deserialise(bytes, Set.class);
    // Then
    assertEquals(Sets.newHashSet(INGEST_AGGREGATION.name(), PRE_AGGREGATION_FILTERING.name(), POST_AGGREGATION_FILTERING.name()), traits);
}
Also used : GraphConfig(uk.gov.gchq.gaffer.graph.GraphConfig) Graph(uk.gov.gchq.gaffer.graph.Graph) StoreTrait(uk.gov.gchq.gaffer.store.StoreTrait) Schema(uk.gov.gchq.gaffer.store.schema.Schema) MapStore(uk.gov.gchq.gaffer.mapstore.MapStore) Store(uk.gov.gchq.gaffer.store.Store) MapStoreProperties(uk.gov.gchq.gaffer.mapstore.MapStoreProperties) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) Test(org.junit.jupiter.api.Test)

Aggregations

GraphConfig (uk.gov.gchq.gaffer.graph.GraphConfig)37 Graph (uk.gov.gchq.gaffer.graph.Graph)19 Schema (uk.gov.gchq.gaffer.store.schema.Schema)19 GraphSerialisable (uk.gov.gchq.gaffer.graph.GraphSerialisable)18 Test (org.junit.jupiter.api.Test)15 Context (uk.gov.gchq.gaffer.store.Context)8 MapStoreProperties (uk.gov.gchq.gaffer.mapstore.MapStoreProperties)7 BeforeEach (org.junit.jupiter.api.BeforeEach)6 StoreTrait (uk.gov.gchq.gaffer.store.StoreTrait)6 Test (org.junit.Test)5 AddGraph (uk.gov.gchq.gaffer.federatedstore.operation.AddGraph)5 FederatedGetTraitsHandlerTest (uk.gov.gchq.gaffer.federatedstore.operation.handler.impl.FederatedGetTraitsHandlerTest)5 NoAccessPredicate (uk.gov.gchq.gaffer.access.predicate.NoAccessPredicate)4 FederatedStore (uk.gov.gchq.gaffer.federatedstore.FederatedStore)4 FederatedStoreProperties (uk.gov.gchq.gaffer.federatedstore.FederatedStoreProperties)4 RemoveGraph (uk.gov.gchq.gaffer.federatedstore.operation.RemoveGraph)4 ProxyProperties (uk.gov.gchq.gaffer.proxystore.ProxyProperties)4 StoreProperties (uk.gov.gchq.gaffer.store.StoreProperties)4 SchemaEdgeDefinition (uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition)4 ArrayList (java.util.ArrayList)3