Search in sources :

Example 11 with StandardJanusGraph

use of org.janusgraph.graphdb.database.StandardJanusGraph in project janusgraph by JanusGraph.

the class JanusGraphManager method openGraph.

@Override
public Graph openGraph(String gName, Function<String, Graph> thunk) {
    Graph graph = graphs.get(gName);
    if (graph != null && !((StandardJanusGraph) graph).isClosed()) {
        if (null != gremlinExecutor) {
            this.gremlinExecutor.getScriptEngineManager().put(gName, graph);
            this.gremlinExecutor.getScriptEngineManager().put(gName + "_traversal", graph.traversal());
        }
        return graph;
    } else {
        synchronized (instantiateGraphLock) {
            graph = graphs.get(gName);
            if (graph == null || ((StandardJanusGraph) graph).isClosed()) {
                graph = thunk.apply(gName);
                graphs.put(gName, graph);
            }
        }
        if (null != gremlinExecutor) {
            this.gremlinExecutor.getScriptEngineManager().put(gName, graph);
            this.gremlinExecutor.getScriptEngineManager().put(gName + "_traversal", graph.traversal());
        }
        return graph;
    }
}
Also used : StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph) Graph(org.apache.tinkerpop.gremlin.structure.Graph) StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph)

Example 12 with StandardJanusGraph

use of org.janusgraph.graphdb.database.StandardJanusGraph in project janusgraph by JanusGraph.

the class JanusGraphFactory method open.

/**
 * Opens a {@link JanusGraph} database configured according to the provided configuration.
 * This method shouldn't be called by end users; it is used by internal server processes to
 * open graphs defined at server start that do not include the graphname property.
 *
 * @param configuration Configuration for the graph database
 * @param backupName Backup name for graph
 * @return JanusGraph graph database
 */
public static JanusGraph open(ReadConfiguration configuration, String backupName) {
    final ModifiableConfiguration config = new ModifiableConfiguration(ROOT_NS, (WriteConfiguration) configuration, BasicConfiguration.Restriction.NONE);
    final String graphName = config.has(GRAPH_NAME) ? config.get(GRAPH_NAME) : backupName;
    final JanusGraphManager jgm = JanusGraphManagerUtility.getInstance();
    if (null != graphName) {
        Preconditions.checkState(jgm != null, JANUS_GRAPH_MANAGER_EXPECTED_STATE_MSG);
        return (JanusGraph) jgm.openGraph(graphName, gName -> new StandardJanusGraph(new GraphDatabaseConfiguration(configuration)));
    } else {
        if (jgm != null) {
            log.warn("You should supply \"graph.graphname\" in your .properties file configuration if you are opening " + "a graph that has not already been opened at server start, i.e. it was " + "defined in your YAML file. This will ensure the graph is tracked by the JanusGraphManager, " + "which will enable autocommit and rollback functionality upon all gremlin script executions. " + "Note that JanusGraphFactory#open(String === shortcut notation) does not support consuming the property " + "\"graph.graphname\" so these graphs should be accessed dynamically by supplying a .properties file here " + "or by using the ConfiguredGraphFactory.");
        }
        return new StandardJanusGraph(new GraphDatabaseConfiguration(configuration));
    }
}
Also used : Configuration(org.apache.commons.configuration.Configuration) StringUtils(org.apache.commons.lang.StringUtils) java.util(java.util) StandardStoreManager(org.janusgraph.diskstorage.StandardStoreManager) Graph(org.apache.tinkerpop.gremlin.structure.Graph) LoggerFactory(org.slf4j.LoggerFactory) Backend(org.janusgraph.diskstorage.Backend) Iterators(com.google.common.collect.Iterators) StandardTransactionLogProcessor(org.janusgraph.graphdb.log.StandardTransactionLogProcessor) org.janusgraph.diskstorage.configuration(org.janusgraph.diskstorage.configuration) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) JANUS_GRAPH_MANAGER_EXPECTED_STATE_MSG(org.janusgraph.graphdb.management.JanusGraphManager.JANUS_GRAPH_MANAGER_EXPECTED_STATE_MSG) BaseConfiguration(org.apache.commons.configuration.BaseConfiguration) TransactionRecovery(org.janusgraph.core.log.TransactionRecovery) BackendException(org.janusgraph.diskstorage.BackendException) Logger(org.slf4j.Logger) LoggerUtil.sanitizeAndLaunder(org.janusgraph.util.system.LoggerUtil.sanitizeAndLaunder) StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph) CommonsConfiguration(org.janusgraph.diskstorage.configuration.backend.CommonsConfiguration) IOUtils(org.janusgraph.util.system.IOUtils) Instant(java.time.Instant) File(java.io.File) LogProcessorFramework(org.janusgraph.core.log.LogProcessorFramework) JanusGraphManager(org.janusgraph.graphdb.management.JanusGraphManager) GraphDatabaseConfiguration(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration) Preconditions(com.google.common.base.Preconditions) Pattern(java.util.regex.Pattern) StandardLogProcessorFramework(org.janusgraph.graphdb.log.StandardLogProcessorFramework) ConfigurationException(org.apache.commons.configuration.ConfigurationException) StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph) JanusGraphManager(org.janusgraph.graphdb.management.JanusGraphManager) GraphDatabaseConfiguration(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration) StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph)

Example 13 with StandardJanusGraph

use of org.janusgraph.graphdb.database.StandardJanusGraph in project janusgraph by JanusGraph.

the class ConfiguredGraphFactory method drop.

/**
 * Drop graph database, deleting all data in storage and indexing backends. Graph can be open or closed (will be
 * closed as part of the drop operation). The graph is removed from the {@link
 * JanusGraphManager} graph reference tracker, if there. Finally, if a configuration for this
 * graph exists on the {@link ConfigurationManagementGraph}, then said configuration will be
 * removed.
 *
 * <p><b>WARNING: This is an irreversible operation that will delete all graph and index data.</b></p>
 * @param graphName String graphName. Corresponding graph can be open or closed.
 * @throws BackendException If an error occurs during deletion
 * @throws ConfigurationManagementGraphNotEnabledException If ConfigurationManagementGraph not
 */
public static void drop(String graphName) throws BackendException, ConfigurationManagementGraphNotEnabledException, Exception {
    final StandardJanusGraph graph = (StandardJanusGraph) ConfiguredGraphFactory.close(graphName);
    JanusGraphFactory.drop(graph);
    removeConfiguration(graphName);
}
Also used : StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph)

Example 14 with StandardJanusGraph

use of org.janusgraph.graphdb.database.StandardJanusGraph in project janusgraph by JanusGraph.

the class ConfiguredGraphFactory method create.

/**
 * Creates a {@link JanusGraph} configuration stored in the {@ConfigurationGraphManagament}
 * graph and a {@link JanusGraph} graph reference according to the single
 * Template Configuration  previously created by the {@link ConfigurationManagementGraph} API;
 * A configuration for this graph must not already exist, and a Template Configuration must
 * exist. If the Template Configuration does not include its
 * backend's respective keyspace/table/storage_directory parameter, we set the keyspace/table
 * to the supplied graphName or we append the graphName to the supplied
 * storage_root parameter.
 *
 * @param graphName
 *
 * @return JanusGraph
 */
public static synchronized JanusGraph create(final String graphName) {
    final ConfigurationManagementGraph configManagementGraph = getConfigGraphManagementInstance();
    final Map<String, Object> graphConfigMap = configManagementGraph.getConfiguration(graphName);
    Preconditions.checkState(null == graphConfigMap, String.format("Configuration for graph %s already exists.", graphName));
    final Map<String, Object> templateConfigMap = configManagementGraph.getTemplateConfiguration();
    Preconditions.checkState(null != templateConfigMap, "Please create a template Configuration using the ConfigurationManagementGraph#createTemplateConfiguration API.");
    templateConfigMap.put(ConfigurationManagementGraph.PROPERTY_GRAPH_NAME, graphName);
    templateConfigMap.put(ConfigurationManagementGraph.PROPERTY_CREATED_USING_TEMPLATE, true);
    final JanusGraphManager jgm = JanusGraphManagerUtility.getInstance();
    Preconditions.checkState(jgm != null, JANUS_GRAPH_MANAGER_EXPECTED_STATE_MSG);
    final CommonsConfiguration config = new CommonsConfiguration(new MapConfiguration(templateConfigMap));
    final JanusGraph g = (JanusGraph) jgm.openGraph(graphName, (String gName) -> new StandardJanusGraph(new GraphDatabaseConfiguration(config)));
    configManagementGraph.createConfiguration(new MapConfiguration(templateConfigMap));
    return g;
}
Also used : MapConfiguration(org.apache.commons.configuration.MapConfiguration) StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph) JanusGraphManager(org.janusgraph.graphdb.management.JanusGraphManager) CommonsConfiguration(org.janusgraph.diskstorage.configuration.backend.CommonsConfiguration) GraphDatabaseConfiguration(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration) ConfigurationManagementGraph(org.janusgraph.graphdb.management.ConfigurationManagementGraph) StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph)

Example 15 with StandardJanusGraph

use of org.janusgraph.graphdb.database.StandardJanusGraph in project janusgraph by JanusGraph.

the class ManagementUtil method awaitIndexUpdate.

private static void awaitIndexUpdate(JanusGraph g, String indexName, String relationTypeName, long time, TemporalUnit unit) {
    Preconditions.checkArgument(g != null && g.isOpen(), "Need to provide valid, open graph instance");
    Preconditions.checkArgument(time > 0 && unit != null, "Need to provide valid time interval");
    Preconditions.checkArgument(StringUtils.isNotBlank(indexName), "Need to provide an index name");
    StandardJanusGraph graph = (StandardJanusGraph) g;
    TimestampProvider times = graph.getConfiguration().getTimestampProvider();
    Instant end = times.getTime().plus(Duration.of(time, unit));
    boolean isStable = false;
    while (times.getTime().isBefore(end)) {
        JanusGraphManagement management = graph.openManagement();
        try {
            if (StringUtils.isNotBlank(relationTypeName)) {
                RelationTypeIndex idx = management.getRelationIndex(management.getRelationType(relationTypeName), indexName);
                Preconditions.checkArgument(idx != null, "Index could not be found: %s @ %s", indexName, relationTypeName);
                isStable = idx.getIndexStatus().isStable();
            } else {
                JanusGraphIndex idx = management.getGraphIndex(indexName);
                Preconditions.checkArgument(idx != null, "Index could not be found: %s", indexName);
                isStable = true;
                for (PropertyKey key : idx.getFieldKeys()) {
                    if (!idx.getIndexStatus(key).isStable())
                        isStable = false;
                }
            }
        } finally {
            management.rollback();
        }
        if (isStable)
            break;
        try {
            times.sleepFor(Duration.ofMillis(500));
        } catch (InterruptedException ignored) {
        }
    }
    if (!isStable)
        throw new JanusGraphException("Index did not stabilize within the given amount of time. For sufficiently long " + "wait periods this is most likely caused by a failed/incorrectly shut down JanusGraph instance or a lingering transaction.");
}
Also used : JanusGraphManagement(org.janusgraph.core.schema.JanusGraphManagement) TimestampProvider(org.janusgraph.diskstorage.util.time.TimestampProvider) Instant(java.time.Instant) JanusGraphException(org.janusgraph.core.JanusGraphException) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) RelationTypeIndex(org.janusgraph.core.schema.RelationTypeIndex) PropertyKey(org.janusgraph.core.PropertyKey) StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph)

Aggregations

StandardJanusGraph (org.janusgraph.graphdb.database.StandardJanusGraph)29 Test (org.junit.Test)18 MapConfiguration (org.apache.commons.configuration.MapConfiguration)16 HashMap (java.util.HashMap)14 GraphDatabaseConfiguration (org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration)11 CommonsConfiguration (org.janusgraph.diskstorage.configuration.backend.CommonsConfiguration)9 JanusGraphManagement (org.janusgraph.core.schema.JanusGraphManagement)4 JanusGraphManager (org.janusgraph.graphdb.management.JanusGraphManager)4 Graph (org.apache.tinkerpop.gremlin.structure.Graph)3 JanusGraphTransaction (org.janusgraph.core.JanusGraphTransaction)3 PropertyKey (org.janusgraph.core.PropertyKey)3 Instant (java.time.Instant)2 JanusGraph (org.janusgraph.core.JanusGraph)2 JanusGraphIndex (org.janusgraph.core.schema.JanusGraphIndex)2 Backend (org.janusgraph.diskstorage.Backend)2 TimestampProvider (org.janusgraph.diskstorage.util.time.TimestampProvider)2 ManagementSystem (org.janusgraph.graphdb.database.management.ManagementSystem)2 ElementCategory (org.janusgraph.graphdb.internal.ElementCategory)2 GraknTxJanus (ai.grakn.kb.internal.GraknTxJanus)1 Preconditions (com.google.common.base.Preconditions)1