use of org.janusgraph.core.schema.JanusGraphManagement in project janusgraph by JanusGraph.
the class GraphOfTheGodsFactory method load.
public static void load(final JanusGraph graph, String mixedIndexName, boolean uniqueNameCompositeIndex) {
if (graph instanceof StandardJanusGraph) {
Preconditions.checkState(mixedIndexNullOrExists((StandardJanusGraph) graph, mixedIndexName), ERR_NO_INDEXING_BACKEND, mixedIndexName);
}
// Create Schema
JanusGraphManagement management = graph.openManagement();
final PropertyKey name = management.makePropertyKey("name").dataType(String.class).make();
JanusGraphManagement.IndexBuilder nameIndexBuilder = management.buildIndex("name", Vertex.class).addKey(name);
if (uniqueNameCompositeIndex)
nameIndexBuilder.unique();
JanusGraphIndex nameIndex = nameIndexBuilder.buildCompositeIndex();
management.setConsistency(nameIndex, ConsistencyModifier.LOCK);
final PropertyKey age = management.makePropertyKey("age").dataType(Integer.class).make();
if (null != mixedIndexName)
management.buildIndex("vertices", Vertex.class).addKey(age).buildMixedIndex(mixedIndexName);
final PropertyKey time = management.makePropertyKey("time").dataType(Integer.class).make();
final PropertyKey reason = management.makePropertyKey("reason").dataType(String.class).make();
final PropertyKey place = management.makePropertyKey("place").dataType(Geoshape.class).make();
if (null != mixedIndexName)
management.buildIndex("edges", Edge.class).addKey(reason).addKey(place).buildMixedIndex(mixedIndexName);
management.makeEdgeLabel("father").multiplicity(Multiplicity.MANY2ONE).make();
management.makeEdgeLabel("mother").multiplicity(Multiplicity.MANY2ONE).make();
EdgeLabel battled = management.makeEdgeLabel("battled").signature(time).make();
management.buildEdgeIndex(battled, "battlesByTime", Direction.BOTH, Order.decr, time);
management.makeEdgeLabel("lives").signature(reason).make();
management.makeEdgeLabel("pet").make();
management.makeEdgeLabel("brother").make();
management.makeVertexLabel("titan").make();
management.makeVertexLabel("location").make();
management.makeVertexLabel("god").make();
management.makeVertexLabel("demigod").make();
management.makeVertexLabel("human").make();
management.makeVertexLabel("monster").make();
management.commit();
JanusGraphTransaction tx = graph.newTransaction();
// vertices
Vertex saturn = tx.addVertex(T.label, "titan", "name", "saturn", "age", 10000);
Vertex sky = tx.addVertex(T.label, "location", "name", "sky");
Vertex sea = tx.addVertex(T.label, "location", "name", "sea");
Vertex jupiter = tx.addVertex(T.label, "god", "name", "jupiter", "age", 5000);
Vertex neptune = tx.addVertex(T.label, "god", "name", "neptune", "age", 4500);
Vertex hercules = tx.addVertex(T.label, "demigod", "name", "hercules", "age", 30);
Vertex alcmene = tx.addVertex(T.label, "human", "name", "alcmene", "age", 45);
Vertex pluto = tx.addVertex(T.label, "god", "name", "pluto", "age", 4000);
Vertex nemean = tx.addVertex(T.label, "monster", "name", "nemean");
Vertex hydra = tx.addVertex(T.label, "monster", "name", "hydra");
Vertex cerberus = tx.addVertex(T.label, "monster", "name", "cerberus");
Vertex tartarus = tx.addVertex(T.label, "location", "name", "tartarus");
// edges
jupiter.addEdge("father", saturn);
jupiter.addEdge("lives", sky, "reason", "loves fresh breezes");
jupiter.addEdge("brother", neptune);
jupiter.addEdge("brother", pluto);
neptune.addEdge("lives", sea).property("reason", "loves waves");
neptune.addEdge("brother", jupiter);
neptune.addEdge("brother", pluto);
hercules.addEdge("father", jupiter);
hercules.addEdge("mother", alcmene);
hercules.addEdge("battled", nemean, "time", 1, "place", Geoshape.point(38.1f, 23.7f));
hercules.addEdge("battled", hydra, "time", 2, "place", Geoshape.point(37.7f, 23.9f));
hercules.addEdge("battled", cerberus, "time", 12, "place", Geoshape.point(39f, 22f));
pluto.addEdge("brother", jupiter);
pluto.addEdge("brother", neptune);
pluto.addEdge("lives", tartarus, "reason", "no fear of death");
pluto.addEdge("pet", cerberus);
cerberus.addEdge("lives", tartarus);
// commit the transaction to disk
tx.commit();
}
use of org.janusgraph.core.schema.JanusGraphManagement in project grakn by graknlabs.
the class TxFactoryJanus method makeIndicesVertexCentric.
private static void makeIndicesVertexCentric(JanusGraphManagement management) {
ResourceBundle keys = ResourceBundle.getBundle("indices-edges");
Set<String> edgeLabels = keys.keySet();
for (String edgeLabel : edgeLabels) {
String[] propertyKeyStrings = keys.getString(edgeLabel).split(",");
// Get all the property keys we need
Set<PropertyKey> propertyKeys = stream(propertyKeyStrings).map(keyId -> {
PropertyKey key = management.getPropertyKey(keyId);
if (key == null) {
throw new RuntimeException("Trying to create edge index on label [" + edgeLabel + "] but the property [" + keyId + "] does not exist");
}
return key;
}).collect(Collectors.toSet());
// Get the edge and indexing information
RelationType relationType = management.getRelationType(edgeLabel);
EdgeLabel label = management.getEdgeLabel(edgeLabel);
// Create index on each property key
for (PropertyKey key : propertyKeys) {
if (management.getRelationIndex(relationType, edgeLabel + "by" + key.name()) == null) {
management.buildEdgeIndex(label, edgeLabel + "by" + key.name(), Direction.BOTH, Order.decr, key);
}
}
// Create index on all property keys
String propertyKeyId = propertyKeys.stream().map(Namifiable::name).collect(Collectors.joining("_"));
if (management.getRelationIndex(relationType, edgeLabel + "by" + propertyKeyId) == null) {
PropertyKey[] allKeys = propertyKeys.toArray(new PropertyKey[propertyKeys.size()]);
management.buildEdgeIndex(label, edgeLabel + "by" + propertyKeyId, Direction.BOTH, Order.decr, allKeys);
}
}
}
use of org.janusgraph.core.schema.JanusGraphManagement in project grakn by graknlabs.
the class TxFactoryJanus method makeIndicesComposite.
private static void makeIndicesComposite(JanusGraphManagement management) {
ResourceBundle keys = ResourceBundle.getBundle("indices-composite");
Set<String> keyString = keys.keySet();
for (String propertyKeyLabel : keyString) {
String indexLabel = "by" + propertyKeyLabel;
JanusGraphIndex index = management.getGraphIndex(indexLabel);
if (index == null) {
boolean isUnique = Boolean.parseBoolean(keys.getString(propertyKeyLabel));
PropertyKey key = management.getPropertyKey(propertyKeyLabel);
JanusGraphManagement.IndexBuilder indexBuilder = management.buildIndex(indexLabel, Vertex.class).addKey(key);
if (isUnique) {
indexBuilder.unique();
}
indexBuilder.buildCompositeIndex();
}
}
}
use of org.janusgraph.core.schema.JanusGraphManagement in project grakn by graknlabs.
the class TxFactoryJanus method buildJanusIndexes.
private static void buildJanusIndexes(JanusGraph graph) {
JanusGraphManagement management = graph.openManagement();
makeVertexLabels(management);
makeEdgeLabels(management);
makePropertyKeys(management);
makeIndicesVertexCentric(management);
makeIndicesComposite(management);
management.commit();
}
use of org.janusgraph.core.schema.JanusGraphManagement in project grakn by graknlabs.
the class TxFactoryJanusTest method testGraphConfig.
@Test
public void testGraphConfig() throws InterruptedException {
JanusGraphManagement management = sharedGraph.openManagement();
// Test Composite Indices
String byId = "by" + Schema.VertexProperty.ID.name();
String byIndex = "by" + Schema.VertexProperty.INDEX.name();
String byValueString = "by" + Schema.VertexProperty.VALUE_STRING.name();
String byValueLong = "by" + Schema.VertexProperty.VALUE_LONG.name();
String byValueDouble = "by" + Schema.VertexProperty.VALUE_DOUBLE.name();
String byValueBoolean = "by" + Schema.VertexProperty.VALUE_BOOLEAN.name();
assertEquals(byId, management.getGraphIndex(byId).toString());
assertEquals(byIndex, management.getGraphIndex(byIndex).toString());
assertEquals(byValueString, management.getGraphIndex(byValueString).toString());
assertEquals(byValueLong, management.getGraphIndex(byValueLong).toString());
assertEquals(byValueDouble, management.getGraphIndex(byValueDouble).toString());
assertEquals(byValueBoolean, management.getGraphIndex(byValueBoolean).toString());
// Text Edge Indices
ResourceBundle keys = ResourceBundle.getBundle("indices-edges");
Set<String> keyString = keys.keySet();
for (String label : keyString) {
assertNotNull(management.getEdgeLabel(label));
}
// Test Properties
Arrays.stream(Schema.VertexProperty.values()).forEach(property -> assertNotNull(management.getPropertyKey(property.name())));
Arrays.stream(Schema.EdgeProperty.values()).forEach(property -> assertNotNull(management.getPropertyKey(property.name())));
// Test Labels
Arrays.stream(Schema.BaseType.values()).forEach(label -> assertNotNull(management.getVertexLabel(label.name())));
}
Aggregations