Search in sources :

Example 1 with HashMapGraphLibrary

use of uk.gov.gchq.gaffer.store.library.HashMapGraphLibrary in project gaffer-doc by gchq.

the class FederatedStoreWalkThrough method run.

public CloseableIterable<? extends Element> run() throws Exception {
    final Schema schema = new Schema.Builder().json(StreamUtil.openStreams(getClass(), schemaPath)).build();
    final HashMapGraphLibrary library = new HashMapGraphLibrary();
    library.addProperties("mapStore", getMapStoreProperties());
    library.addProperties("accumuloStore", getAccumuloStoreProperties());
    library.addSchema("roadTraffic", schema);
    // [creating a federatedstore] create a store that federates to a MapStore and AccumuloStore
    // ---------------------------------------------------------
    final Graph federatedGraph = new Graph.Builder().config(new GraphConfig.Builder().graphId(getClass().getSimpleName()).library(library).build()).storeProperties(getFederatedStoreProperties()).build();
    // ---------------------------------------------------------
    // [federatedstore properties]
    // ---------------------------------------------------------
    final FederatedStoreProperties exampleFederatedProperty = new FederatedStoreProperties();
    exampleFederatedProperty.setCacheProperties(HashMapCacheService.class.getName());
    // ---------------------------------------------------------
    printJson("FED_PROP", getFederatedStoreProperties());
    final User user = new User("user01");
    final AddGraph addMapGraph = new AddGraph.Builder().graphId("mapGraph").schema(new Schema.Builder().json(StreamUtil.openStream(getClass(), "RoadAndRoadUseWithTimesAndCardinalitiesForFederatedStore/schema/entities.json")).json(StreamUtil.openStream(getClass(), "RoadAndRoadUseWithTimesAndCardinalitiesForFederatedStore/schema/types.json")).build()).storeProperties(getMapStoreProperties()).isPublic(true).build();
    federatedGraph.execute(addMapGraph, user);
    final AddGraph addAccumuloGraph = new AddGraph.Builder().graphId("accumuloGraph").schema(new Schema.Builder().json(StreamUtil.openStream(getClass(), "RoadAndRoadUseWithTimesAndCardinalitiesForFederatedStore/schema/edges.json")).json(StreamUtil.openStream(getClass(), "RoadAndRoadUseWithTimesAndCardinalitiesForFederatedStore/schema/types.json")).build()).storeProperties(getAccumuloStoreProperties()).isPublic(true).build();
    federatedGraph.execute(addAccumuloGraph, user);
    // [add another graph] add a graph to the federated store.
    // ---------------------------------------------------------
    AddGraph addAnotherGraph = new AddGraph.Builder().graphId("AnotherGraph").schema(schema).storeProperties(getMapStoreProperties()).build();
    federatedGraph.execute(addAnotherGraph, user);
    // ---------------------------------------------------------
    improveReadabilityOfJson(addAnotherGraph);
    addAccumuloGraph.setGraphAuths(null);
    printJson("ADD_GRAPH", addAnotherGraph);
    // [remove graph] remove a graph from the federated store.
    // ---------------------------------------------------------
    RemoveGraph removeGraph = new RemoveGraph.Builder().graphId("AnotherGraph").build();
    federatedGraph.execute(removeGraph, user);
    // ---------------------------------------------------------
    improveReadabilityOfJson(removeGraph);
    printJson("REMOVE_GRAPH", removeGraph);
    // [get all graph ids] Get a list of all the graphId within the FederatedStore.
    // ---------------------------------------------------------
    final GetAllGraphIds getAllGraphIDs = new GetAllGraphIds();
    Iterable<? extends String> graphIds = federatedGraph.execute(getAllGraphIDs, user);
    // ---------------------------------------------------------
    improveReadabilityOfJson(getAllGraphIDs);
    printJson("GET_ALL_GRAPH_IDS", getAllGraphIDs);
    print("GRAPH_IDS", graphIds.toString());
    // [add elements] Create a data generator and add the edges to the federated graphs using an operation chain consisting of:
    // generateElements - generating edges from the data (note these are directed edges)
    // addElements - add the edges to the graph
    // ---------------------------------------------------------
    final OperationChain<Void> addOpChain = new OperationChain.Builder().first(new GenerateElements.Builder<String>().generator(new RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator()).input(IOUtils.readLines(StreamUtil.openStream(getClass(), "RoadAndRoadUseWithTimesAndCardinalities/data.txt"))).build()).then(new AddElements()).build();
    federatedGraph.execute(addOpChain, user);
    // ---------------------------------------------------------
    printJsonAndPython("ADD_ELEMENTS", addOpChain);
    // [get elements]
    // ---------------------------------------------------------
    final OperationChain<CloseableIterable<? extends Element>> getOpChain = new OperationChain.Builder().first(new GetElements.Builder().input(new EntitySeed("10")).build()).build();
    CloseableIterable<? extends Element> elements = federatedGraph.execute(getOpChain, user);
    for (final Element element : elements) {
        print("ELEMENTS", element.toString());
    }
    printJsonAndPython("GET_ELEMENTS", getOpChain);
    // [get elements from accumulo graph]
    // ---------------------------------------------------------
    final OperationChain<CloseableIterable<? extends Element>> getOpChainOnAccumuloGraph = new OperationChain.Builder().first(new GetElements.Builder().input(new EntitySeed("10")).option(FederatedStoreConstants.KEY_OPERATION_OPTIONS_GRAPH_IDS, "accumuloGraph").build()).build();
    CloseableIterable<? extends Element> elementsFromAccumuloGraph = federatedGraph.execute(getOpChainOnAccumuloGraph, user);
    for (final Element element : elementsFromAccumuloGraph) {
        print("ELEMENTS_FROM_ACCUMULO_GRAPH", element.toString());
    }
    // [select graphs for operations]
    // ---------------------------------------------------------
    GetAllElements selectGraphsForOperations = new Builder().option(FederatedStoreConstants.KEY_OPERATION_OPTIONS_GRAPH_IDS, "graphId1, graphId2").build();
    // ---------------------------------------------------------
    printJsonAndPython("SELECT_GRAPHS_FOR_OPERATIONS", selectGraphsForOperations);
    // [do not skip failed execution]
    // ---------------------------------------------------------
    GetAllElements doNotSkipFailedExecution = new Builder().option(FederatedStoreConstants.KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE, "false").build();
    // ---------------------------------------------------------
    printJsonAndPython("DO_NOT_SKIP_FAILED_EXECUTION", doNotSkipFailedExecution);
    // [add public graph] add a graph to the federated store.
    // ---------------------------------------------------------
    AddGraph publicGraph = new AddGraph.Builder().graphId("publicGraph").parentSchemaIds(Lists.newArrayList("roadTraffic")).parentPropertiesId("mapStore").isPublic(// <-- public access
    true).graphAuths(// <-- used but irrelevant as graph has public access
    "Auth1").build();
    federatedGraph.execute(addAnotherGraph, user);
    // ---------------------------------------------------------
    improveReadabilityOfJson(publicGraph);
    printJson("ADD_PUBLIC_GRAPH", publicGraph);
    // [add private graph] add a graph to the federated store.
    // ---------------------------------------------------------
    AddGraph privateGraph = new AddGraph.Builder().graphId("privateGraph").parentSchemaIds(Lists.newArrayList("roadTraffic")).parentPropertiesId("mapStore").build();
    federatedGraph.execute(addAnotherGraph, user);
    // ---------------------------------------------------------
    improveReadabilityOfJson(privateGraph);
    printJson("ADD_PRIVATE_GRAPH", privateGraph);
    // [add secure graph] add a graph to the federated store.
    // ---------------------------------------------------------
    AddGraph addSecureGraph = new AddGraph.Builder().graphId("SecureGraph").parentSchemaIds(Lists.newArrayList("roadTraffic")).parentPropertiesId("mapStore").graphAuths("Auth1", "Auth2", "Auth3").build();
    federatedGraph.execute(addSecureGraph, user);
    // ---------------------------------------------------------
    improveReadabilityOfJson(addSecureGraph);
    printJson("ADD_SECURE_GRAPH", addSecureGraph);
    // [disallow public access]
    // ---------------------------------------------------------
    FederatedStoreProperties disallowPublicProperties = new FederatedStoreProperties();
    disallowPublicProperties.setGraphsCanHavePublicAccess(false);
    // ---------------------------------------------------------
    printJson("DISALLOW_PUBLIC_ACCESS", disallowPublicProperties.getProperties());
    // [limit custom properties]
    // ---------------------------------------------------------
    FederatedStoreProperties limitCustomProperties = new FederatedStoreProperties();
    limitCustomProperties.setCustomPropertyAuths("Auth1, Auth2, Auth3");
    // ---------------------------------------------------------
    printJson("LIMIT_CUSTOM_PROPERTIES", limitCustomProperties.getProperties());
    return elements;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User) FederatedStoreProperties(uk.gov.gchq.gaffer.federatedstore.FederatedStoreProperties) Schema(uk.gov.gchq.gaffer.store.schema.Schema) Builder(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements.Builder) Element(uk.gov.gchq.gaffer.data.element.Element) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) HashMapCacheService(uk.gov.gchq.gaffer.cache.impl.HashMapCacheService) GetAllGraphIds(uk.gov.gchq.gaffer.federatedstore.operation.GetAllGraphIds) AddGraph(uk.gov.gchq.gaffer.federatedstore.operation.AddGraph) RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator(uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) RemoveGraph(uk.gov.gchq.gaffer.federatedstore.operation.RemoveGraph) HashMapGraphLibrary(uk.gov.gchq.gaffer.store.library.HashMapGraphLibrary) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) Graph(uk.gov.gchq.gaffer.graph.Graph) RemoveGraph(uk.gov.gchq.gaffer.federatedstore.operation.RemoveGraph) AddGraph(uk.gov.gchq.gaffer.federatedstore.operation.AddGraph) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed)

Example 2 with HashMapGraphLibrary

use of uk.gov.gchq.gaffer.store.library.HashMapGraphLibrary in project Gaffer by gchq.

the class FederatedAddGraphWithHooksHandlerTest method shouldAddGraphUsingLibrary.

@Test
public void shouldAddGraphUsingLibrary() throws Exception {
    store.initialise(FEDERATEDSTORE_GRAPH_ID, null, federatedStoreProperties);
    Schema expectedSchema = new Schema.Builder().build();
    assertEquals(0, store.getGraphs(testUser, null, ignore).size());
    assertEquals(0, store.getGraphs(testUser, null, ignore).size());
    FederatedAddGraphWithHooksHandler federatedAddGraphWithHooksHandler = new FederatedAddGraphWithHooksHandler();
    federatedAddGraphWithHooksHandler.doOperation(new AddGraphWithHooks.Builder().graphId(EXPECTED_GRAPH_ID).schema(expectedSchema).storeProperties(PROPERTIES).build(), new Context(testUser), store);
    Collection<Graph> graphs = store.getGraphs(testUser, null, ignore);
    assertThat(graphs).hasSize(1);
    Graph next = graphs.iterator().next();
    assertEquals(EXPECTED_GRAPH_ID, next.getGraphId());
    assertEquals(expectedSchema, next.getSchema());
    final GraphLibrary library = new HashMapGraphLibrary();
    library.add(EXPECTED_GRAPH_ID_3, expectedSchema, PROPERTIES);
    store.setGraphLibrary(library);
    federatedAddGraphWithHooksHandler.doOperation(new AddGraphWithHooks.Builder().graphId(EXPECTED_GRAPH_ID_3).build(), new Context(testUser), store);
    graphs = store.getGraphs(testUser, null, ignore);
    assertThat(graphs).hasSize(2);
    Iterator<Graph> iterator = graphs.iterator();
    final HashSet<String> set = Sets.newHashSet();
    while (iterator.hasNext()) {
        set.add(iterator.next().getGraphId());
    }
    assertThat(set).contains(EXPECTED_GRAPH_ID, EXPECTED_GRAPH_ID_3);
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) HashMapGraphLibrary(uk.gov.gchq.gaffer.store.library.HashMapGraphLibrary) Schema(uk.gov.gchq.gaffer.store.schema.Schema) AddGraphWithHooks(uk.gov.gchq.gaffer.federatedstore.operation.AddGraphWithHooks) Graph(uk.gov.gchq.gaffer.graph.Graph) GraphLibrary(uk.gov.gchq.gaffer.store.library.GraphLibrary) HashMapGraphLibrary(uk.gov.gchq.gaffer.store.library.HashMapGraphLibrary) Test(org.junit.jupiter.api.Test)

Example 3 with HashMapGraphLibrary

use of uk.gov.gchq.gaffer.store.library.HashMapGraphLibrary in project Gaffer by gchq.

the class GraphTest method shouldBuildGraphUsingGraphIdAndLookupSchema.

@Test
public void shouldBuildGraphUsingGraphIdAndLookupSchema() throws Exception {
    // Given
    final StoreProperties storeProperties = new StoreProperties();
    storeProperties.setStoreClass(TestStoreImpl.class.getName());
    final Schema schemaModule1 = new Schema.Builder().type(TestTypes.PROP_STRING, new TypeDefinition.Builder().clazz(String.class).build()).type("vertex", new TypeDefinition.Builder().clazz(String.class).build()).edge(TestGroups.EDGE, new SchemaEdgeDefinition.Builder().property(TestPropertyNames.PROP_1, TestTypes.PROP_STRING).aggregate(false).source("vertex").destination("vertex").directed(DIRECTED_EITHER).build()).build();
    final Schema schemaModule2 = new Schema.Builder().type(TestTypes.PROP_INTEGER, new TypeDefinition.Builder().clazz(Integer.class).build()).type("vertex2", new TypeDefinition.Builder().clazz(String.class).build()).edge(TestGroups.EDGE_2, new SchemaEdgeDefinition.Builder().property(TestPropertyNames.PROP_2, TestTypes.PROP_INTEGER).aggregate(false).source("vertex2").destination("vertex2").directed(DIRECTED_EITHER).build()).build();
    final Schema schemaModule3 = new Schema.Builder().entity(TestGroups.ENTITY, new SchemaEntityDefinition.Builder().property(TestPropertyNames.PROP_1, TestTypes.PROP_STRING).aggregate(false).vertex("vertex3").build()).type("vertex3", new TypeDefinition.Builder().clazz(String.class).build()).build();
    final Schema schemaModule4 = new Schema.Builder().entity(TestGroups.ENTITY_2, new SchemaEntityDefinition.Builder().property(TestPropertyNames.PROP_2, TestTypes.PROP_INTEGER).aggregate(false).vertex("vertex4").build()).type("vertex4", new TypeDefinition.Builder().clazz(String.class).build()).type(DIRECTED_EITHER, Boolean.class).build();
    // When
    final Graph graph = new Graph.Builder().config(new GraphConfig.Builder().graphId(GRAPH_ID).library(new HashMapGraphLibrary()).build()).addSchema(schemaModule1).addSchema(schemaModule2).addSchema(schemaModule3).addSchema(schemaModule4).storeProperties(storeProperties).build();
    final Graph graph2 = new Graph.Builder().config(new GraphConfig.Builder().graphId(GRAPH_ID).library(new HashMapGraphLibrary()).build()).storeProperties(storeProperties).build();
    // Then
    JsonAssert.assertEquals(graph.getSchema().toJson(false), graph2.getSchema().toJson(false));
}
Also used : HashMapGraphLibrary(uk.gov.gchq.gaffer.store.library.HashMapGraphLibrary) Schema(uk.gov.gchq.gaffer.store.schema.Schema) SchemaEntityDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEntityDefinition) TypeDefinition(uk.gov.gchq.gaffer.store.schema.TypeDefinition) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) Test(org.junit.jupiter.api.Test)

Example 4 with HashMapGraphLibrary

use of uk.gov.gchq.gaffer.store.library.HashMapGraphLibrary in project Gaffer by gchq.

the class GraphTest method shouldBuildGraphFromConfigAndSetIdsToGraphsWhenDifferent.

@Test
public void shouldBuildGraphFromConfigAndSetIdsToGraphsWhenDifferent() {
    // Given
    final StoreProperties libraryStoreProperties = new StoreProperties();
    libraryStoreProperties.setStoreClass(TestStoreImpl.class.getName());
    final StoreProperties graphStoreProperties = new StoreProperties();
    graphStoreProperties.setStoreClass(TestStoreImpl.class.getName());
    final Schema librarySchema = new Schema.Builder().build();
    final Schema graphSchema = new Schema.Builder().build();
    final String graphId1 = "graphId1";
    final HashMapGraphLibrary library = new HashMapGraphLibrary();
    library.addSchema(SCHEMA_ID_1, librarySchema);
    library.addProperties(STORE_PROPERTIES_ID_1, libraryStoreProperties);
    // When
    final GraphConfig config = new GraphConfig.Builder().graphId(graphId1).library(library).build();
    final Graph graph1 = new Graph.Builder().config(config).addToLibrary(true).parentStorePropertiesId("storePropertiesId1").storeProperties(graphStoreProperties).addParentSchemaIds(SCHEMA_ID_1).addSchemas(graphSchema).build();
    // Then
    assertEquals(graphId1, graph1.getGraphId());
    JsonAssert.assertEquals(library.getSchema(SCHEMA_ID_1).toJson(false), librarySchema.toJson(false));
    final Pair<String, String> ids = library.getIds(graphId1);
    // Check that the schemaIds are different between the parent and supplied schema
    assertEquals(graphId1, ids.getFirst());
    // Check that the storePropsIds are different between the parent and supplied storeProps
    assertEquals(graphId1, ids.getSecond());
}
Also used : HashMapGraphLibrary(uk.gov.gchq.gaffer.store.library.HashMapGraphLibrary) Schema(uk.gov.gchq.gaffer.store.schema.Schema) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) Test(org.junit.jupiter.api.Test)

Example 5 with HashMapGraphLibrary

use of uk.gov.gchq.gaffer.store.library.HashMapGraphLibrary in project Gaffer by gchq.

the class GraphTest method shouldBuildGraphFromConfigAndSetIdsToGraphsWhenIdentical.

@Test
public void shouldBuildGraphFromConfigAndSetIdsToGraphsWhenIdentical() {
    // Given
    final StoreProperties storeProperties = new StoreProperties();
    storeProperties.setStoreClass(TestStoreImpl.class.getName());
    String storePropertiesId1 = "storePropertiesId1";
    final Schema schema = new Schema.Builder().build();
    final String graphId1 = "graphId1";
    final HashMapGraphLibrary library = new HashMapGraphLibrary();
    library.addSchema(SCHEMA_ID_1, schema);
    library.addProperties(storePropertiesId1, storeProperties);
    // When
    final GraphConfig config = new GraphConfig.Builder().graphId(graphId1).library(library).build();
    final Graph graph1 = new Graph.Builder().config(config).addToLibrary(true).parentStorePropertiesId(storePropertiesId1).storeProperties(storeProperties).addParentSchemaIds(SCHEMA_ID_1).addSchemas(schema).build();
    // Then
    assertEquals(graphId1, graph1.getGraphId());
    JsonAssert.assertEquals(library.getSchema(SCHEMA_ID_1).toJson(false), schema.toJson(false));
    // Check that the schemaId = schemaId1 as both the parent and supplied schema have same id's
    // Check that the storePropsId = storePropertiesId1 as both parent and supplied storeProps have same id's
    assertThat(graphId1).isEqualTo(library.getIds(graphId1).getFirst()).isEqualTo(library.getIds(graphId1).getSecond());
}
Also used : HashMapGraphLibrary(uk.gov.gchq.gaffer.store.library.HashMapGraphLibrary) Schema(uk.gov.gchq.gaffer.store.schema.Schema) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) Test(org.junit.jupiter.api.Test)

Aggregations

HashMapGraphLibrary (uk.gov.gchq.gaffer.store.library.HashMapGraphLibrary)17 Test (org.junit.jupiter.api.Test)11 Schema (uk.gov.gchq.gaffer.store.schema.Schema)11 StoreProperties (uk.gov.gchq.gaffer.store.StoreProperties)8 Context (uk.gov.gchq.gaffer.store.Context)6 BeforeEach (org.junit.jupiter.api.BeforeEach)4 Graph (uk.gov.gchq.gaffer.graph.Graph)3 GraphLibrary (uk.gov.gchq.gaffer.store.library.GraphLibrary)3 AddGraph (uk.gov.gchq.gaffer.federatedstore.operation.AddGraph)2 AddSchemaToLibrary (uk.gov.gchq.gaffer.store.operation.add.AddSchemaToLibrary)2 HashMapCacheService (uk.gov.gchq.gaffer.cache.impl.HashMapCacheService)1 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)1 Element (uk.gov.gchq.gaffer.data.element.Element)1 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)1 RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator (uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator)1 FederatedStoreProperties (uk.gov.gchq.gaffer.federatedstore.FederatedStoreProperties)1 AddGraphWithHooks (uk.gov.gchq.gaffer.federatedstore.operation.AddGraphWithHooks)1 GetAllGraphIds (uk.gov.gchq.gaffer.federatedstore.operation.GetAllGraphIds)1 RemoveGraph (uk.gov.gchq.gaffer.federatedstore.operation.RemoveGraph)1 AddOperationsToChain (uk.gov.gchq.gaffer.graph.hook.AddOperationsToChain)1