use of com.thinkaurelius.titan.graphdb.internal.InternalRelation 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.graphdb.internal.InternalRelation in project titan by thinkaurelius.
the class IndexRepairJob method process.
@Override
public void process(TitanVertex vertex, ScanMetrics metrics) {
try {
BackendTransaction mutator = writeTx.getTxHandle();
if (index instanceof RelationTypeIndex) {
RelationTypeIndexWrapper wrapper = (RelationTypeIndexWrapper) index;
InternalRelationType wrappedType = wrapper.getWrappedType();
EdgeSerializer edgeSerializer = writeTx.getEdgeSerializer();
List<Entry> additions = new ArrayList<>();
for (TitanRelation relation : vertex.query().types(indexRelationTypeName).direction(Direction.OUT).relations()) {
InternalRelation titanRelation = (InternalRelation) relation;
for (int pos = 0; pos < titanRelation.getArity(); pos++) {
if (!wrappedType.isUnidirected(Direction.BOTH) && !wrappedType.isUnidirected(EdgeDirection.fromPosition(pos)))
//Directionality is not covered
continue;
Entry entry = edgeSerializer.writeRelation(titanRelation, wrappedType, pos, writeTx);
additions.add(entry);
}
}
StaticBuffer vertexKey = writeTx.getIdInspector().getKey(vertex.longId());
mutator.mutateEdges(vertexKey, additions, KCVSCache.NO_DELETIONS);
metrics.incrementCustom(ADDED_RECORDS_COUNT, additions.size());
} else if (index instanceof TitanGraphIndex) {
IndexType indexType = mgmt.getSchemaVertex(index).asIndexType();
assert indexType != null;
IndexSerializer indexSerializer = graph.getIndexSerializer();
//Gather elements to index
List<TitanElement> elements;
switch(indexType.getElement()) {
case VERTEX:
elements = ImmutableList.of(vertex);
break;
case PROPERTY:
elements = Lists.newArrayList();
for (TitanVertexProperty p : addIndexSchemaConstraint(vertex.query(), indexType).properties()) {
elements.add(p);
}
break;
case EDGE:
elements = Lists.newArrayList();
for (TitanEdge e : addIndexSchemaConstraint(vertex.query().direction(Direction.OUT), indexType).edges()) {
elements.add(e);
}
break;
default:
throw new AssertionError("Unexpected category: " + indexType.getElement());
}
if (indexType.isCompositeIndex()) {
for (TitanElement element : elements) {
Set<IndexSerializer.IndexUpdate<StaticBuffer, Entry>> updates = indexSerializer.reindexElement(element, (CompositeIndexType) indexType);
for (IndexSerializer.IndexUpdate<StaticBuffer, Entry> update : updates) {
log.debug("Mutating index {}: {}", indexType, update.getEntry());
mutator.mutateIndex(update.getKey(), Lists.newArrayList(update.getEntry()), KCVSCache.NO_DELETIONS);
metrics.incrementCustom(ADDED_RECORDS_COUNT);
}
}
} else {
assert indexType.isMixedIndex();
Map<String, Map<String, List<IndexEntry>>> documentsPerStore = new HashMap<>();
for (TitanElement element : elements) {
indexSerializer.reindexElement(element, (MixedIndexType) indexType, documentsPerStore);
metrics.incrementCustom(DOCUMENT_UPDATES_COUNT);
}
mutator.getIndexTransaction(indexType.getBackingIndexName()).restore(documentsPerStore);
}
} else
throw new UnsupportedOperationException("Unsupported index found: " + index);
} catch (final Exception e) {
mgmt.rollback();
writeTx.rollback();
metrics.incrementCustom(FAILED_TX);
throw new TitanException(e.getMessage(), e);
}
}
use of com.thinkaurelius.titan.graphdb.internal.InternalRelation in project titan by thinkaurelius.
the class CacheVertexProperty method it.
@Override
public InternalRelation it() {
InternalRelation it = null;
InternalVertex startVertex = getVertex(0);
if (startVertex.hasAddedRelations() && startVertex.hasRemovedRelations()) {
//Test whether this relation has been replaced
final long id = super.longId();
it = Iterables.getOnlyElement(startVertex.getAddedRelations(new Predicate<InternalRelation>() {
@Override
public boolean apply(@Nullable InternalRelation internalRelation) {
return (internalRelation instanceof StandardVertexProperty) && ((StandardVertexProperty) internalRelation).getPreviousID() == id;
}
}), null);
}
return (it != null) ? it : super.it();
}
use of com.thinkaurelius.titan.graphdb.internal.InternalRelation in project titan by thinkaurelius.
the class TransactionLogHeader method logRelations.
private static void logRelations(DataOutput out, final Collection<InternalRelation> relations, StandardTitanTx tx) {
VariableLong.writePositive(out, relations.size());
for (InternalRelation rel : relations) {
VariableLong.writePositive(out, rel.getVertex(0).longId());
com.thinkaurelius.titan.diskstorage.Entry entry = tx.getEdgeSerializer().writeRelation(rel, 0, tx);
BufferUtil.writeEntry(out, entry);
}
}
Aggregations