Search in sources :

Example 11 with InternalRelation

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();
            }
        }
    }
}
Also used : TitanGraph(com.thinkaurelius.titan.core.TitanGraph) LongHashSet(com.carrotsearch.hppc.LongHashSet) TitanVertex(com.thinkaurelius.titan.core.TitanVertex) LongSet(com.carrotsearch.hppc.LongSet) ArrayList(java.util.ArrayList) IDPoolExhaustedException(com.thinkaurelius.titan.graphdb.database.idassigner.IDPoolExhaustedException) InternalRelation(com.thinkaurelius.titan.graphdb.internal.InternalRelation) Test(org.junit.Test)

Example 12 with InternalRelation

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);
    }
}
Also used : InternalRelation(com.thinkaurelius.titan.graphdb.internal.InternalRelation) Entry(com.thinkaurelius.titan.diskstorage.Entry) IndexEntry(com.thinkaurelius.titan.diskstorage.indexing.IndexEntry) EdgeSerializer(com.thinkaurelius.titan.graphdb.database.EdgeSerializer) RelationTypeIndexWrapper(com.thinkaurelius.titan.graphdb.database.management.RelationTypeIndexWrapper) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) ImmutableList(com.google.common.collect.ImmutableList) MixedIndexType(com.thinkaurelius.titan.graphdb.types.MixedIndexType) CompositeIndexType(com.thinkaurelius.titan.graphdb.types.CompositeIndexType) IndexType(com.thinkaurelius.titan.graphdb.types.IndexType) BackendTransaction(com.thinkaurelius.titan.diskstorage.BackendTransaction) MixedIndexType(com.thinkaurelius.titan.graphdb.types.MixedIndexType) IndexSerializer(com.thinkaurelius.titan.graphdb.database.IndexSerializer) CompositeIndexType(com.thinkaurelius.titan.graphdb.types.CompositeIndexType) InternalRelationType(com.thinkaurelius.titan.graphdb.internal.InternalRelationType)

Example 13 with InternalRelation

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();
}
Also used : InternalVertex(com.thinkaurelius.titan.graphdb.internal.InternalVertex) InternalRelation(com.thinkaurelius.titan.graphdb.internal.InternalRelation)

Example 14 with InternalRelation

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);
    }
}
Also used : InternalRelation(com.thinkaurelius.titan.graphdb.internal.InternalRelation)

Aggregations

InternalRelation (com.thinkaurelius.titan.graphdb.internal.InternalRelation)14 InternalVertex (com.thinkaurelius.titan.graphdb.internal.InternalVertex)7 InternalRelationType (com.thinkaurelius.titan.graphdb.internal.InternalRelationType)5 MixedIndexType (com.thinkaurelius.titan.graphdb.types.MixedIndexType)3 TitanSchemaVertex (com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex)3 Entry (com.thinkaurelius.titan.diskstorage.Entry)2 IndexEntry (com.thinkaurelius.titan.diskstorage.indexing.IndexEntry)2 IndexTransaction (com.thinkaurelius.titan.diskstorage.indexing.IndexTransaction)2 EdgeSerializer (com.thinkaurelius.titan.graphdb.database.EdgeSerializer)2 CompositeIndexType (com.thinkaurelius.titan.graphdb.types.CompositeIndexType)2 LongArrayList (com.carrotsearch.hppc.LongArrayList)1 LongHashSet (com.carrotsearch.hppc.LongHashSet)1 LongSet (com.carrotsearch.hppc.LongSet)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 EdgeLabel (com.thinkaurelius.titan.core.EdgeLabel)1 PropertyKey (com.thinkaurelius.titan.core.PropertyKey)1 RelationType (com.thinkaurelius.titan.core.RelationType)1 TitanElement (com.thinkaurelius.titan.core.TitanElement)1 TitanException (com.thinkaurelius.titan.core.TitanException)1