use of com.thinkaurelius.titan.core.TitanVertex in project titan by thinkaurelius.
the class RelationIdentifier method findRelation.
TitanRelation findRelation(TitanTransaction tx) {
TitanVertex v = ((StandardTitanTx) tx).getInternalVertex(outVertexId);
if (v == null || v.isRemoved())
return null;
TitanVertex typeVertex = tx.getVertex(typeId);
if (typeVertex == null)
return null;
if (!(typeVertex instanceof RelationType))
throw new IllegalArgumentException("Invalid RelationIdentifier: typeID does not reference a type");
RelationType type = (RelationType) typeVertex;
Iterable<? extends TitanRelation> rels;
if (((RelationType) typeVertex).isEdgeLabel()) {
Direction dir = Direction.OUT;
TitanVertex other = ((StandardTitanTx) tx).getInternalVertex(inVertexId);
if (other == null || other.isRemoved())
return null;
if (((StandardTitanTx) tx).isPartitionedVertex(v) && !((StandardTitanTx) tx).isPartitionedVertex(other)) {
//Swap for likely better performance
TitanVertex tmp = other;
other = v;
v = tmp;
dir = Direction.IN;
}
rels = ((VertexCentricQueryBuilder) v.query()).noPartitionRestriction().types((EdgeLabel) type).direction(dir).adjacent(other).edges();
} else {
rels = ((VertexCentricQueryBuilder) v.query()).noPartitionRestriction().types((PropertyKey) type).properties();
}
for (TitanRelation r : rels) {
//Find current or previous relation
if (r.longId() == relationId || ((r instanceof StandardRelation) && ((StandardRelation) r).getPreviousID() == relationId))
return r;
}
return null;
}
use of com.thinkaurelius.titan.core.TitanVertex in project titan by thinkaurelius.
the class AdjacentVertexFilterOptimizerStrategy method apply.
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
TraversalHelper.getStepsOfClass(TraversalFilterStep.class, traversal).forEach(originalStep -> {
Traversal.Admin<?, ?> filterTraversal = (Traversal.Admin<?, ?>) originalStep.getLocalChildren().get(0);
List<Step> steps = filterTraversal.getSteps();
if (steps.size() == 2 && (steps.get(0) instanceof EdgeVertexStep || steps.get(0) instanceof EdgeOtherVertexStep) && (steps.get(1) instanceof IsStep)) {
Direction direction = null;
if (steps.get(0) instanceof EdgeVertexStep) {
EdgeVertexStep evs = (EdgeVertexStep) steps.get(0);
if (evs.getDirection() != Direction.BOTH)
direction = evs.getDirection();
} else {
assert steps.get(0) instanceof EdgeOtherVertexStep;
direction = Direction.BOTH;
}
P predicate = ((IsStep) steps.get(1)).getPredicate();
if (direction != null && predicate.getBiPredicate() == Compare.eq && predicate.getValue() instanceof Vertex) {
TitanVertex vertex = TitanTraversalUtil.getTitanVertex((Vertex) predicate.getValue());
Step<?, ?> currentStep = originalStep.getPreviousStep();
while (true) {
if (currentStep instanceof HasStep || currentStep instanceof IdentityStep) {
} else
break;
}
if (currentStep instanceof VertexStep) {
VertexStep vstep = (VertexStep) currentStep;
if (vstep.returnsEdge() && (direction == Direction.BOTH || direction.equals(vstep.getDirection().opposite()))) {
TraversalHelper.replaceStep(originalStep, new HasStep(traversal, HasContainer.makeHasContainers(ImplicitKey.ADJACENT_ID.name(), P.eq(vertex))), traversal);
}
}
}
}
});
}
use of com.thinkaurelius.titan.core.TitanVertex in project titan by thinkaurelius.
the class EdgeSerializerTest method testValueOrdering.
@Test
public void testValueOrdering() {
StandardTitanGraph graph = (StandardTitanGraph) StorageSetup.getInMemoryGraph();
TitanManagement mgmt = graph.openManagement();
EdgeLabel father = mgmt.makeEdgeLabel("father").multiplicity(Multiplicity.MANY2ONE).make();
for (int i = 1; i <= 5; i++) mgmt.makePropertyKey("key" + i).dataType(Integer.class).make();
mgmt.commit();
TitanVertex v1 = graph.addVertex(), v2 = graph.addVertex();
TitanEdge e1 = v1.addEdge("father", v2);
for (int i = 1; i <= 5; i++) e1.property("key" + i, i);
graph.tx().commit();
e1.remove();
graph.tx().commit();
}
use of com.thinkaurelius.titan.core.TitanVertex in project titan by thinkaurelius.
the class VertexIDAssignerTest method testIDAssignment.
@Test
public void testIDAssignment() {
LongSet vertexIds = new LongHashSet();
LongSet relationIds = new LongHashSet();
int totalRelations = 0;
int totalVertices = 0;
for (int trial = 0; trial < 10; trial++) {
for (boolean flush : new boolean[] { true, false }) {
TitanGraph graph = getInMemoryGraph();
int numVertices = 1000;
List<TitanVertex> vertices = new ArrayList<TitanVertex>(numVertices);
List<InternalRelation> relations = new ArrayList<InternalRelation>();
TitanVertex old = null;
totalRelations += 2 * numVertices;
totalVertices += numVertices;
try {
for (int i = 0; i < numVertices; i++) {
TitanVertex next = graph.addVertex();
InternalRelation edge = null;
if (old != null) {
edge = (InternalRelation) old.addEdge("knows", next);
}
InternalRelation property = (InternalRelation) next.property("age", 25);
if (flush) {
idAssigner.assignID((InternalVertex) next, next.vertexLabel());
idAssigner.assignID(property);
if (edge != null)
idAssigner.assignID(edge);
}
relations.add(property);
if (edge != null)
relations.add(edge);
vertices.add(next);
old = next;
}
if (!flush)
idAssigner.assignIDs(relations);
//Check if we should have exhausted the id pools
if (totalRelations > maxIDAssignments || totalVertices > maxIDAssignments)
fail();
//Verify that ids are set and unique
for (TitanVertex v : vertices) {
assertTrue(v.hasId());
long id = v.longId();
assertTrue(id > 0 && id < Long.MAX_VALUE);
assertTrue(vertexIds.add(id));
}
for (InternalRelation r : relations) {
assertTrue(r.hasId());
long id = r.longId();
assertTrue(id > 0 && id < Long.MAX_VALUE);
assertTrue(relationIds.add(id));
}
} catch (IDPoolExhaustedException e) {
//Since the id assignment process is randomized, we divide by 3/2 to account for minor variations
assertTrue("Max Avail: " + maxIDAssignments + " vs. [" + totalVertices + "," + totalRelations + "]", totalRelations >= maxIDAssignments / 3 * 2 || totalVertices >= maxIDAssignments / 3 * 2);
} finally {
graph.tx().rollback();
graph.close();
}
}
}
}
use of com.thinkaurelius.titan.core.TitanVertex in project titan by thinkaurelius.
the class AbstractIndexManagementIT method testRepairGraphIndex.
@Test
public void testRepairGraphIndex() 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 graph index on age
TitanManagement m = graph.openManagement();
PropertyKey age = m.getPropertyKey("age");
m.buildIndex("verticesByAge", Vertex.class).addKey(age).buildCompositeIndex();
m.commit();
graph.tx().commit();
// Block until the SchemaStatus transitions to REGISTERED
assertTrue(ManagementSystem.awaitGraphIndexStatus(graph, "verticesByAge").status(SchemaStatus.REGISTERED).call().getSucceeded());
m = graph.openManagement();
TitanGraphIndex index = m.getGraphIndex("verticesByAge");
m.updateIndex(index, SchemaAction.ENABLE_INDEX);
m.commit();
graph.tx().commit();
// Block until the SchemaStatus transitions to ENABLED
assertTrue(ManagementSystem.awaitGraphIndexStatus(graph, "verticesByAge").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("age", 10000).vertices().iterator().hasNext());
// Repair
MapReduceIndexManagement mri = new MapReduceIndexManagement(graph);
m = graph.openManagement();
index = m.getGraphIndex("verticesByAge");
ScanMetrics metrics = mri.updateIndex(index, SchemaAction.REINDEX).get();
assertEquals(6, metrics.getCustom(IndexRepairJob.ADDED_RECORDS_COUNT));
// Test the index
Iterable<TitanVertex> hits = graph.query().has("age", 4500).vertices();
assertNotNull(hits);
assertEquals(1, Iterables.size(hits));
TitanVertex v = Iterables.getOnlyElement(hits);
assertNotNull(v);
assertEquals("neptune", v.value("name"));
}
Aggregations