use of org.janusgraph.core.EdgeLabel in project janusgraph by JanusGraph.
the class JanusGraphAppTest method createSchema.
@Test
public void createSchema() throws ConfigurationException {
final JanusGraphApp app = new JanusGraphApp(CONF_FILE);
final GraphTraversalSource g = app.openGraph();
app.createSchema();
final JanusGraph janusGraph = (JanusGraph) g.getGraph();
final JanusGraphManagement management = janusGraph.openManagement();
final List<String> vertexLabels = StreamSupport.stream(management.getVertexLabels().spliterator(), false).map(Namifiable::name).collect(Collectors.toList());
final List<String> expectedVertexLabels = Stream.of("titan", "location", "god", "demigod", "human", "monster").collect(Collectors.toList());
assertTrue(vertexLabels.containsAll(expectedVertexLabels));
final List<String> edgeLabels = StreamSupport.stream(management.getRelationTypes(EdgeLabel.class).spliterator(), false).map(Namifiable::name).collect(Collectors.toList());
final List<String> expectedEdgeLabels = Stream.of("father", "mother", "brother", "pet", "lives", "battled").collect(Collectors.toList());
assertTrue(edgeLabels.containsAll(expectedEdgeLabels));
final EdgeLabel father = management.getEdgeLabel("father");
assertTrue(father.isDirected());
assertFalse(father.isUnidirected());
assertEquals(Multiplicity.MANY2ONE, father.multiplicity());
final List<String> propertyKeys = StreamSupport.stream(management.getRelationTypes(PropertyKey.class).spliterator(), false).map(Namifiable::name).collect(Collectors.toList());
final List<String> expectedPropertyKeys = Stream.of("name", "age", "time", "place", "reason").collect(Collectors.toList());
assertTrue(propertyKeys.containsAll(expectedPropertyKeys));
final PropertyKey place = management.getPropertyKey("place");
assertEquals(Cardinality.SINGLE, place.cardinality());
assertEquals(Geoshape.class, place.dataType());
final JanusGraphIndex nameIndex = management.getGraphIndex("nameIndex");
assertTrue(nameIndex.isCompositeIndex());
assertTrue(nameIndex.getIndexedElement().equals(JanusGraphVertex.class));
final PropertyKey[] nameIndexKeys = nameIndex.getFieldKeys();
assertEquals(1, nameIndexKeys.length);
assertEquals("name", nameIndexKeys[0].name());
}
use of org.janusgraph.core.EdgeLabel in project janusgraph by JanusGraph.
the class AbstractIndexManagementIT method testRepairRelationIndex.
@Test
public void testRepairRelationIndex() throws InterruptedException, BackendException, ExecutionException {
tx.commit();
mgmt.commit();
// Load the "Graph of the Gods" sample data (WITHOUT mixed index coverage)
GraphOfTheGodsFactory.loadWithoutMixedIndex(graph, true);
// Create and enable a relation index on lives edges by reason
JanusGraphManagement m = graph.openManagement();
PropertyKey reason = m.getPropertyKey("reason");
EdgeLabel lives = m.getEdgeLabel("lives");
m.buildEdgeIndex(lives, "livesByReason", Direction.BOTH, Order.decr, reason);
m.commit();
graph.tx().commit();
// Block until the SchemaStatus transitions to REGISTERED
assertTrue(ManagementSystem.awaitRelationIndexStatus(graph, "livesByReason", "lives").status(SchemaStatus.REGISTERED).call().getSucceeded());
m = graph.openManagement();
RelationTypeIndex index = m.getRelationIndex(m.getRelationType("lives"), "livesByReason");
m.updateIndex(index, SchemaAction.ENABLE_INDEX);
m.commit();
graph.tx().commit();
// Block until the SchemaStatus transitions to ENABLED
assertTrue(ManagementSystem.awaitRelationIndexStatus(graph, "livesByReason", "lives").status(SchemaStatus.ENABLED).call().getSucceeded());
// Run a query that hits the index but erroneously returns nothing because we haven't repaired yet
// assertFalse(graph.query().has("reason", "no fear of death").edges().iterator().hasNext());
// Repair
MapReduceIndexManagement mri = new MapReduceIndexManagement(graph);
m = graph.openManagement();
index = m.getRelationIndex(m.getRelationType("lives"), "livesByReason");
ScanMetrics metrics = mri.updateIndex(index, SchemaAction.REINDEX).get();
assertEquals(8, metrics.getCustom(IndexRepairJob.ADDED_RECORDS_COUNT));
}
use of org.janusgraph.core.EdgeLabel 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.EdgeLabel in project janusgraph by JanusGraph.
the class JanusGraphTest method testConsistencyEnforcement.
/* ==================================================================================
CONSISTENCY
==================================================================================*/
/**
* Tests the correct application of ConsistencyModifiers across transactional boundaries
*/
@Test
public void testConsistencyEnforcement() {
PropertyKey uid = makeVertexIndexedUniqueKey("uid", Integer.class);
PropertyKey name = makeKey("name", String.class);
mgmt.setConsistency(uid, ConsistencyModifier.LOCK);
mgmt.setConsistency(name, ConsistencyModifier.LOCK);
mgmt.setConsistency(mgmt.getGraphIndex("uid"), ConsistencyModifier.LOCK);
EdgeLabel knows = mgmt.makeEdgeLabel("knows").multiplicity(Multiplicity.SIMPLE).make();
EdgeLabel spouse = mgmt.makeEdgeLabel("spouse").multiplicity(Multiplicity.ONE2ONE).make();
EdgeLabel connect = mgmt.makeEdgeLabel("connect").multiplicity(Multiplicity.MULTI).make();
EdgeLabel related = mgmt.makeEdgeLabel("related").multiplicity(Multiplicity.MULTI).make();
mgmt.setConsistency(knows, ConsistencyModifier.LOCK);
mgmt.setConsistency(spouse, ConsistencyModifier.LOCK);
mgmt.setConsistency(related, ConsistencyModifier.FORK);
finishSchema();
name = tx.getPropertyKey("name");
connect = tx.getEdgeLabel("connect");
related = tx.getEdgeLabel("related");
JanusGraphVertex v1 = tx.addVertex("uid", 1);
JanusGraphVertex v2 = tx.addVertex("uid", 2);
JanusGraphVertex v3 = tx.addVertex("uid", 3);
Edge e1 = v1.addEdge(connect.name(), v2, name.name(), "e1");
Edge e2 = v1.addEdge(related.name(), v2, name.name(), "e2");
newTx();
v1 = getV(tx, v1);
/*
==== check fork, no fork behavior
*/
long e1id = getId(e1);
long e2id = getId(e2);
e1 = Iterables.getOnlyElement(v1.query().direction(Direction.OUT).labels("connect").edges());
assertEquals("e1", e1.value("name"));
assertEquals(e1id, getId(e1));
e2 = Iterables.getOnlyElement(v1.query().direction(Direction.OUT).labels("related").edges());
assertEquals("e2", e2.value("name"));
assertEquals(e2id, getId(e2));
// Update edges - one should simply update, the other fork
e1.property("name", "e1.2");
e2.property("name", "e2.2");
newTx();
v1 = getV(tx, v1);
e1 = Iterables.getOnlyElement(v1.query().direction(Direction.OUT).labels("connect").edges());
assertEquals("e1.2", e1.value("name"));
// should have same id
assertEquals(e1id, getId(e1));
e2 = Iterables.getOnlyElement(v1.query().direction(Direction.OUT).labels("related").edges());
assertEquals("e2.2", e2.value("name"));
// should have different id since forked
assertNotEquals(e2id, getId(e2));
clopen();
/*
=== check cross transaction
*/
final Random random = new Random();
final long[] vertexIds = { getId(v1), getId(v2), getId(v3) };
// 1) Index uniqueness
executeLockConflictingTransactionJobs(graph, new TransactionJob() {
private int pos = 0;
@Override
public void run(JanusGraphTransaction tx) {
JanusGraphVertex u = getV(tx, vertexIds[pos++]);
u.property(VertexProperty.Cardinality.single, "uid", 5);
}
});
// 2) Property out-uniqueness
executeLockConflictingTransactionJobs(graph, tx -> {
final JanusGraphVertex u = getV(tx, vertexIds[0]);
u.property(VertexProperty.Cardinality.single, "name", "v" + random.nextInt(10));
});
// 3) knows simpleness
executeLockConflictingTransactionJobs(graph, tx -> {
final JanusGraphVertex u1 = getV(tx, vertexIds[0]), u2 = getV(tx, vertexIds[1]);
u1.addEdge("knows", u2);
});
// 4) knows one2one (in 2 separate configurations)
executeLockConflictingTransactionJobs(graph, new TransactionJob() {
private int pos = 1;
@Override
public void run(JanusGraphTransaction tx) {
final JanusGraphVertex u1 = getV(tx, vertexIds[0]), u2 = getV(tx, vertexIds[pos++]);
u1.addEdge("spouse", u2);
}
});
executeLockConflictingTransactionJobs(graph, new TransactionJob() {
private int pos = 1;
@Override
public void run(JanusGraphTransaction tx) {
final JanusGraphVertex u1 = getV(tx, vertexIds[pos++]), u2 = getV(tx, vertexIds[0]);
u1.addEdge("spouse", u2);
}
});
// ######### TRY INVALID CONSISTENCY
try {
// Fork does not apply to constrained types
mgmt.setConsistency(mgmt.getPropertyKey("name"), ConsistencyModifier.FORK);
fail();
} catch (IllegalArgumentException ignored) {
}
}
use of org.janusgraph.core.EdgeLabel in project janusgraph by JanusGraph.
the class JanusGraphTest method testConcurrentConsistencyEnforcement.
@Test
public void testConcurrentConsistencyEnforcement() throws Exception {
PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.SINGLE).make();
JanusGraphIndex nameIndex = mgmt.buildIndex("name", Vertex.class).addKey(name).unique().buildCompositeIndex();
mgmt.setConsistency(nameIndex, ConsistencyModifier.LOCK);
EdgeLabel married = mgmt.makeEdgeLabel("married").multiplicity(Multiplicity.ONE2ONE).make();
mgmt.setConsistency(married, ConsistencyModifier.LOCK);
mgmt.makeEdgeLabel("friend").multiplicity(Multiplicity.MULTI).make();
finishSchema();
JanusGraphVertex baseV = tx.addVertex("name", "base");
newTx();
final long baseVid = getId(baseV);
final String nameA = "a", nameB = "b";
final int parallelThreads = 4;
int numSuccess = executeParallelTransactions(tx -> {
final JanusGraphVertex a = tx.addVertex();
final JanusGraphVertex base = getV(tx, baseVid);
base.addEdge("married", a);
}, parallelThreads);
assertTrue("At most 1 tx should succeed: " + numSuccess, numSuccess <= 1);
numSuccess = executeParallelTransactions(tx -> {
tx.addVertex("name", nameA);
final JanusGraphVertex b = tx.addVertex("name", nameB);
b.addEdge("friend", b);
}, parallelThreads);
newTx();
final long numA = Iterables.size(tx.query().has("name", nameA).vertices());
final long numB = Iterables.size(tx.query().has("name", nameB).vertices());
// System.out.println(numA + " - " + numB);
assertTrue("At most 1 tx should succeed: " + numSuccess, numSuccess <= 1);
assertTrue(numA <= 1);
assertTrue(numB <= 1);
}
Aggregations