public void initializeGraph(Configuration config) { if (!provided) { this.graph = (StandardJanusGraph) JanusGraphFactory.open((BasicConfiguration) config); } }
/** * 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 also removed from the {@link org.janusgraph.graphdb.management.JanusGraphManager} * graph reference tracker, if there. * * <p><b>WARNING: This is an irreversible operation that will delete all graph and index data.</b></p> * * @param graph * @throws BackendException If an error occurs during deletion * @deprecated Use {@link org.janusgraph.core.JanusGraphFactory#drop(JanusGraph)} */ @Deprecated public static void clear(JanusGraph graph) throws BackendException { JanusGraphFactory.drop(graph); }
private static ReadConfiguration getLocalConfiguration(String shortcutOrFile) { File file = new File(shortcutOrFile); if (file.exists()) return getLocalConfiguration(file); else { int pos = shortcutOrFile.indexOf(':'); if (pos<0) pos = shortcutOrFile.length(); String backend = shortcutOrFile.substring(0,pos); Preconditions.checkArgument(StandardStoreManager.getAllManagerClasses().containsKey(backend.toLowerCase()), "Backend shorthand unknown: %s", backend); String secondArg = null; if (pos+1<shortcutOrFile.length()) secondArg = shortcutOrFile.substring(pos + 1).trim(); BaseConfiguration config = new BaseConfiguration(); ModifiableConfiguration writeConfig = new ModifiableConfiguration(ROOT_NS,new CommonsConfiguration(config), BasicConfiguration.Restriction.NONE); writeConfig.set(STORAGE_BACKEND,backend); ConfigOption option = Backend.getOptionForShorthand(backend); if (option==null) { Preconditions.checkArgument(secondArg==null); } else if (option==STORAGE_DIRECTORY || option==STORAGE_CONF_FILE) { Preconditions.checkArgument(StringUtils.isNotBlank(secondArg),"Need to provide additional argument to initialize storage backend"); writeConfig.set(option,getAbsolutePath(secondArg)); } else if (option==STORAGE_HOSTS) { Preconditions.checkArgument(StringUtils.isNotBlank(secondArg),"Need to provide additional argument to initialize storage backend"); writeConfig.set(option,new String[]{secondArg}); } else throw new IllegalArgumentException("Invalid configuration option for backend "+option); return new CommonsConfiguration(config); } }
/** * Opens a {@link JanusGraph} database. * <p> * If the argument points to a configuration file, the configuration file is loaded to configure the JanusGraph graph * If the string argument is a configuration short-cut, then the short-cut is parsed and used to configure the returned JanusGraph graph. * <p> * A configuration short-cut is of the form: * [STORAGE_BACKEND_NAME]:[DIRECTORY_OR_HOST] * * @param shortcutOrFile Configuration file name or configuration short-cut * @return JanusGraph graph database configured according to the provided configuration * @see <a href="http://docs.janusgraph.org/latest/configuration.html">"Configuration" manual chapter</a> * @see <a href="http://docs.janusgraph.org/latest/config-ref.html">Configuration Reference</a> */ public static JanusGraph open(String shortcutOrFile) { return open(getLocalConfiguration(shortcutOrFile)); }
public static JanusGraph create(final String directory) { JanusGraphFactory.Builder config = JanusGraphFactory.build(); config.set("storage.backend", "berkeleyje"); config.set("storage.directory", directory); config.set("index." + INDEX_NAME + ".backend", "elasticsearch"); JanusGraph graph = config.open(); GraphOfTheGodsFactory.load(graph); return graph; }
@Test public void testOrForceIndexUniqueMixedIndex() throws Exception { JanusGraph customGraph = null; try { customGraph = this.getForceIndexGraph(); final JanusGraphManagement management = customGraph.openManagement(); final PropertyKey nameProperty = management.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.SINGLE).make(); final PropertyKey ageProperty = management.makePropertyKey("age").dataType(Integer.class).cardinality(Cardinality.SINGLE).make(); final PropertyKey lengthProperty = management.makePropertyKey("length").dataType(Integer.class).cardinality(Cardinality.SINGLE).make(); management.buildIndex("oridx", Vertex.class).addKey(nameProperty, getStringMapping()).addKey(ageProperty).addKey(lengthProperty).buildMixedIndex(INDEX); management.commit(); customGraph.tx().commit(); testOr(customGraph); } finally { if (customGraph != null) { JanusGraphFactory.close(customGraph); } } }
private static String getAbsolutePath(String file) { return getAbsolutePath(new File(System.getProperty("user.dir")), file); }
/** * Opens a {@link JanusGraph} database. * <p> * If the argument points to a configuration file, the configuration file is loaded to configure the JanusGraph graph * If the string argument is a configuration short-cut, then the short-cut is parsed and used to configure the returned JanusGraph graph. * 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. * <p> * A configuration short-cut is of the form: * [STORAGE_BACKEND_NAME]:[DIRECTORY_OR_HOST] * * @param shortcutOrFile Configuration file name or configuration short-cut * @param backupName Backup name for graph * @return JanusGraph graph database configured according to the provided configuration * @see <a href="http://docs.janusgraph.org/latest/configuration.html">"Configuration" manual chapter</a> * @see <a href="http://docs.janusgraph.org/latest/config-ref.html">Configuration Reference</a> */ public static JanusGraph open(String shortcutOrFile, String backupName) { return open(getLocalConfiguration(shortcutOrFile), backupName); }
@Inject public JanusGraphBuilderCQLProvider(IJanusGraphConfig storageConfig, ICQLConfig config) { graphBuilder = JanusGraphFactory.build().set("storage.cql.keyspace", config.getKeyspace()) .set("storage.backend", "cql").set("storage.cql.cluster-name", config.getClusterName()) .set("storage.hostname", Optional.ofNullable(storageConfig.getStorageHostname()).orElse(Inet4Address.getLoopbackAddress().getHostAddress())) .set("storage.port", storageConfig.getStoragePort()).set("storage.lock.wait-time", 300) .set("cache.db-cache", false).set("query.batch", false).set("query.smart-limit", false) .set("query.force-index", false).set("query.fast-property", false); }
@Test public void testOrForceIndexPartialIndex() throws Exception { JanusGraph customGraph = null; try { customGraph = this.getForceIndexGraph(); final JanusGraphManagement management = customGraph.openManagement(); final PropertyKey stringProperty = management.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.SINGLE).make(); management.makePropertyKey("age").dataType(Integer.class).cardinality(Cardinality.SINGLE).make(); management.buildIndex("oridx", Vertex.class).addKey(stringProperty, getStringMapping()).buildMixedIndex(INDEX); management.commit(); customGraph.tx().commit(); final GraphTraversalSource g = customGraph.traversal(); g.addV().property("name", "Hiro").property("age", 2).next(); g.addV().property("name", "Totoro").property("age", 1).next(); customGraph.tx().commit(); g.V().or(__.has("name", "Totoro"),__.has("age", 2)).hasNext(); fail("should fail"); } catch (final JanusGraphException e){ assertTrue(e.getMessage().contains("Could not find a suitable index to answer graph query and graph scans are disabled")); } finally { if (customGraph != null) { JanusGraphFactory.close(customGraph); } } }
final String s = configuration.getString(k); Preconditions.checkArgument(StringUtils.isNotBlank(s),"Invalid Configuration: key %s has null empty value",k); configuration.setProperty(k,getAbsolutePath(configParent,s));
/** * Opens a {@link JanusGraph} database configured according to the provided configuration. * * @param configuration Configuration for the graph database * @return JanusGraph graph database */ public static JanusGraph open(ReadConfiguration configuration) { return open(configuration, null); }
/** * 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); }
private JanusGraph configureGraph(boolean batchLoading){ JanusGraphFactory.Builder builder = JanusGraphFactory.build(). set("storage.hostname", session().uri()). set("storage.cassandra.keyspace", session().keyspace().getValue()). set("storage.batch-loading", batchLoading); String storageBackend = "storage.backend"; //Load Defaults DEFAULT_PROPERTIES.forEach((key, value) -> builder.set(key.toString(), value)); //Load Passed in properties session().config().properties().forEach((key, value) -> { //Overwrite storage if(key.equals(storageBackend)){ value = storageBackendMapper.get(value); } //Inject properties into other default properties if(overrideMap.containsKey(key)){ builder.set(overrideMap.get(key), value); } builder.set(key.toString(), value); }); LOG.debug("Opening graph on {}", session().uri()); return builder.open(); }
@Test public void testOrForceIndexComposite() throws Exception { JanusGraph customGraph = null; try { customGraph = this.getForceIndexGraph(); final JanusGraphManagement management = customGraph.openManagement(); management.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.SINGLE).make(); final PropertyKey ageProperty = management.makePropertyKey("age").dataType(Integer.class).cardinality(Cardinality.SINGLE).make(); management.buildIndex("ageridx", Vertex.class).addKey(ageProperty).buildCompositeIndex(); management.commit(); customGraph.tx().commit(); final GraphTraversalSource g = customGraph.traversal(); g.addV().property("name", "Hiro").property("age", 2).next(); g.addV().property("name", "Totoro").property("age", 1).next(); customGraph.tx().commit(); g.V().has("age", P.gte(4).or(P.lt(2))).hasNext(); fail("should fail"); } catch (final JanusGraphException e){ assertTrue(e.getMessage().contains("Could not find a suitable index to answer graph query and graph scans are disabled")); } finally { if (customGraph != null) { JanusGraphFactory.close(customGraph); } } } }
/** * Opens a {@link JanusGraph} database configured according to the provided configuration. * * @param configuration Configuration for the graph database * @return JanusGraph graph database * @see <a href="http://docs.janusgraph.org/latest/configuration.html">"Configuration" manual chapter</a> * @see <a href="http://docs.janusgraph.org/latest/config-ref.html">Configuration Reference</a> */ public static JanusGraph open(Configuration configuration) { return open(new CommonsConfiguration(configuration)); }
/** * Very simple graph operation to ensure minimal functionality and cleanup */ @Test public void testBasic() throws BackendException { PropertyKey uid = makeVertexIndexedUniqueKey("name", String.class); finishSchema(); JanusGraphVertex n1 = tx.addVertex(); uid = tx.getPropertyKey("name"); n1.property(uid.name(), "abcd"); clopen(); long nid = n1.longId(); uid = tx.getPropertyKey("name"); assertTrue(getV(tx, nid) != null); assertTrue(getV(tx, uid.longId()) != null); assertMissing(tx, nid + 64); uid = tx.getPropertyKey(uid.name()); n1 = getV(tx, nid); assertEquals(n1, getOnlyVertex(tx.query().has(uid.name(), "abcd"))); assertEquals(1, Iterables.size(n1.query().relations())); //TODO: how to expose relations? assertEquals("abcd", n1.value(uid.name())); assertCount(1, tx.query().vertices()); close(); JanusGraphFactory.drop(graph); open(config); assertEmpty(tx.query().vertices()); }
@Test public void testOrForceIndexMixedAndCompositeIndex() throws Exception { JanusGraph customGraph = null; try { customGraph = this.getForceIndexGraph(); final JanusGraphManagement management = customGraph.openManagement(); final PropertyKey nameProperty = management.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.SINGLE).make(); final PropertyKey ageProperty = management.makePropertyKey("age").dataType(Integer.class).cardinality(Cardinality.SINGLE).make(); final PropertyKey lengthProperty = management.makePropertyKey("length").dataType(Integer.class).cardinality(Cardinality.SINGLE).make(); management.buildIndex("nameidx", Vertex.class).addKey(nameProperty, getStringMapping()).buildMixedIndex(INDEX); management.buildIndex("ageridx", Vertex.class).addKey(ageProperty).buildCompositeIndex(); management.buildIndex("lengthidx", Vertex.class).addKey(lengthProperty).buildMixedIndex(INDEX); management.commit(); customGraph.tx().commit(); testOr(customGraph); } finally { if (customGraph != null) { JanusGraphFactory.close(customGraph); } } }
/** * Opens a {@link JanusGraph} database configured according to the provided configuration. * * @param configuration Configuration for the graph database * @return JanusGraph graph database */ public static JanusGraph open(BasicConfiguration configuration) { return open(configuration.getConfiguration()); }
@Override public void clear() { if (this.graph == null) return; if (this.graph.isOpen()) close(); try { JanusGraphFactory.drop(this.graph); } catch (Exception e) { LOG.log(Level.WARNING, "Failed to delete graph due to: " + e.getMessage(), e); } }