Search in sources :

Example 1 with RelationIdentifier

use of com.thinkaurelius.titan.graphdb.relations.RelationIdentifier in project titan by thinkaurelius.

the class TitanBlueprintsTransaction method edges.

@Override
public Iterator<Edge> edges(Object... eids) {
    if (eids == null || eids.length == 0)
        return (Iterator) getEdges().iterator();
    ElementUtils.verifyArgsMustBeEitherIdorElement(eids);
    RelationIdentifier[] ids = new RelationIdentifier[eids.length];
    int pos = 0;
    for (int i = 0; i < eids.length; i++) {
        RelationIdentifier id = ElementUtils.getEdgeId(eids[i]);
        if (id != null)
            ids[pos++] = id;
    }
    if (pos == 0)
        return Collections.emptyIterator();
    if (pos < ids.length)
        ids = Arrays.copyOf(ids, pos);
    return (Iterator) getEdges(ids).iterator();
}
Also used : Iterator(java.util.Iterator) RelationIdentifier(com.thinkaurelius.titan.graphdb.relations.RelationIdentifier)

Example 2 with RelationIdentifier

use of com.thinkaurelius.titan.graphdb.relations.RelationIdentifier in project titan by thinkaurelius.

the class StandardTransactionLogProcessor method fixSecondaryFailure.

private void fixSecondaryFailure(final StandardTransactionId txId, final TxEntry entry) {
    logRecoveryMsg("Attempting to repair partially failed transaction [%s]", txId);
    if (entry.entry == null) {
        logRecoveryMsg("Trying to repair expired or unpersisted transaction [%s] (Ignore in startup)", txId);
        return;
    }
    boolean userLogFailure = true;
    boolean secIndexFailure = true;
    final Predicate<String> isFailedIndex;
    final TransactionLogHeader.Entry commitEntry = entry.entry;
    final TransactionLogHeader.SecondaryFailures secFail = entry.failures;
    if (secFail != null) {
        userLogFailure = secFail.userLogFailure;
        secIndexFailure = !secFail.failedIndexes.isEmpty();
        isFailedIndex = new Predicate<String>() {

            @Override
            public boolean apply(@Nullable String s) {
                return secFail.failedIndexes.contains(s);
            }
        };
    } else {
        isFailedIndex = Predicates.alwaysTrue();
    }
    // I) Restore external indexes
    if (secIndexFailure) {
        //1) Collect all elements (vertices and relations) and the indexes for which they need to be restored
        final SetMultimap<String, IndexRestore> indexRestores = HashMultimap.create();
        BackendOperation.execute(new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                StandardTitanTx tx = (StandardTitanTx) graph.newTransaction();
                try {
                    for (TransactionLogHeader.Modification modification : commitEntry.getContentAsModifications(serializer)) {
                        InternalRelation rel = ModificationDeserializer.parseRelation(modification, tx);
                        //Collect affected vertex indexes
                        for (MixedIndexType index : getMixedIndexes(rel.getType())) {
                            if (index.getElement() == ElementCategory.VERTEX && isFailedIndex.apply(index.getBackingIndexName())) {
                                assert rel.isProperty();
                                indexRestores.put(index.getBackingIndexName(), new IndexRestore(rel.getVertex(0).longId(), ElementCategory.VERTEX, getIndexId(index)));
                            }
                        }
                        //See if relation itself is affected
                        for (RelationType relType : rel.getPropertyKeysDirect()) {
                            for (MixedIndexType index : getMixedIndexes(relType)) {
                                if (index.getElement().isInstance(rel) && isFailedIndex.apply(index.getBackingIndexName())) {
                                    assert rel.id() instanceof RelationIdentifier;
                                    indexRestores.put(index.getBackingIndexName(), new IndexRestore(rel.id(), ElementCategory.getByClazz(rel.getClass()), getIndexId(index)));
                                }
                            }
                        }
                    }
                } finally {
                    if (tx.isOpen())
                        tx.rollback();
                }
                return true;
            }
        }, readTime);
        //2) Restore elements per backing index
        for (final String indexName : indexRestores.keySet()) {
            final StandardTitanTx tx = (StandardTitanTx) graph.newTransaction();
            try {
                BackendTransaction btx = tx.getTxHandle();
                final IndexTransaction indexTx = btx.getIndexTransaction(indexName);
                BackendOperation.execute(new Callable<Boolean>() {

                    @Override
                    public Boolean call() throws Exception {
                        Map<String, Map<String, List<IndexEntry>>> restoredDocs = Maps.newHashMap();
                        for (IndexRestore restore : indexRestores.get(indexName)) {
                            TitanSchemaVertex indexV = (TitanSchemaVertex) tx.getVertex(restore.indexId);
                            MixedIndexType index = (MixedIndexType) indexV.asIndexType();
                            TitanElement element = restore.retrieve(tx);
                            if (element != null) {
                                graph.getIndexSerializer().reindexElement(element, index, restoredDocs);
                            } else {
                                //Element is deleted
                                graph.getIndexSerializer().removeElement(restore.elementId, index, restoredDocs);
                            }
                        }
                        indexTx.restore(restoredDocs);
                        indexTx.commit();
                        return true;
                    }

                    @Override
                    public String toString() {
                        return "IndexMutation";
                    }
                }, persistenceTime);
            } finally {
                if (tx.isOpen())
                    tx.rollback();
            }
        }
    }
    // II) Restore log messages
    final String logTxIdentifier = (String) commitEntry.getMetadata().get(LogTxMeta.LOG_ID);
    if (userLogFailure && logTxIdentifier != null) {
        TransactionLogHeader txHeader = new TransactionLogHeader(txCounter.incrementAndGet(), times.getTime(), times);
        final StaticBuffer userLogContent = txHeader.serializeUserLog(serializer, commitEntry, txId);
        BackendOperation.execute(new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                final Log userLog = graph.getBackend().getUserLog(logTxIdentifier);
                Future<Message> env = userLog.add(userLogContent);
                if (env.isDone()) {
                    env.get();
                }
                return true;
            }
        }, persistenceTime);
    }
}
Also used : IndexTransaction(com.thinkaurelius.titan.diskstorage.indexing.IndexTransaction) InternalRelation(com.thinkaurelius.titan.graphdb.internal.InternalRelation) InternalRelationType(com.thinkaurelius.titan.graphdb.internal.InternalRelationType) RelationType(com.thinkaurelius.titan.core.RelationType) TitanElement(com.thinkaurelius.titan.core.TitanElement) List(java.util.List) TransactionLogHeader(com.thinkaurelius.titan.graphdb.database.log.TransactionLogHeader) MixedIndexType(com.thinkaurelius.titan.graphdb.types.MixedIndexType) TitanSchemaVertex(com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex) RelationIdentifier(com.thinkaurelius.titan.graphdb.relations.RelationIdentifier) StandardTitanTx(com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx) TitanException(com.thinkaurelius.titan.core.TitanException) ExecutionException(java.util.concurrent.ExecutionException) Future(java.util.concurrent.Future) Map(java.util.Map)

Example 3 with RelationIdentifier

use of com.thinkaurelius.titan.graphdb.relations.RelationIdentifier in project titan by thinkaurelius.

the class IndexSerializer method getIndexEntry.

private final Entry getIndexEntry(CompositeIndexType index, RecordEntry[] record, TitanElement element) {
    DataOutput out = serializer.getDataOutput(1 + 8 + 8 * record.length + 4 * 8);
    out.putByte(FIRST_INDEX_COLUMN_BYTE);
    if (index.getCardinality() != Cardinality.SINGLE) {
        VariableLong.writePositive(out, element.longId());
        if (index.getCardinality() != Cardinality.SET) {
            for (RecordEntry re : record) {
                VariableLong.writePositive(out, re.relationId);
            }
        }
    }
    int valuePosition = out.getPosition();
    if (element instanceof TitanVertex) {
        VariableLong.writePositive(out, element.longId());
    } else {
        assert element instanceof TitanRelation;
        RelationIdentifier rid = (RelationIdentifier) element.id();
        long[] longs = rid.getLongRepresentation();
        Preconditions.checkArgument(longs.length == 3 || longs.length == 4);
        for (int i = 0; i < longs.length; i++) VariableLong.writePositive(out, longs[i]);
    }
    return new StaticArrayEntry(out.getStaticBuffer(), valuePosition);
}
Also used : DataOutput(com.thinkaurelius.titan.graphdb.database.serialize.DataOutput) RelationIdentifier(com.thinkaurelius.titan.graphdb.relations.RelationIdentifier) StaticArrayEntry(com.thinkaurelius.titan.diskstorage.util.StaticArrayEntry)

Example 4 with RelationIdentifier

use of com.thinkaurelius.titan.graphdb.relations.RelationIdentifier in project titan by thinkaurelius.

the class StandardTitanTx method getEdges.

@Override
public Iterable<TitanEdge> getEdges(RelationIdentifier... ids) {
    verifyOpen();
    if (ids == null || ids.length == 0)
        return new VertexCentricEdgeIterable(getInternalVertices(), RelationCategory.EDGE);
    if (null != config.getGroupName()) {
        MetricManager.INSTANCE.getCounter(config.getGroupName(), "db", "getEdgesByID").inc();
    }
    List<TitanEdge> result = new ArrayList<>(ids.length);
    for (RelationIdentifier id : ids) {
        if (id == null)
            continue;
        TitanEdge edge = id.findEdge(this);
        if (edge != null && !edge.isRemoved())
            result.add(edge);
    }
    return result;
}
Also used : VertexCentricEdgeIterable(com.thinkaurelius.titan.graphdb.util.VertexCentricEdgeIterable) LongArrayList(com.carrotsearch.hppc.LongArrayList) RelationIdentifier(com.thinkaurelius.titan.graphdb.relations.RelationIdentifier)

Example 5 with RelationIdentifier

use of com.thinkaurelius.titan.graphdb.relations.RelationIdentifier in project titan by thinkaurelius.

the class TitanGraphTest method testImplicitKey.

/**
     * Test the correct application of {@link com.thinkaurelius.titan.graphdb.types.system.ImplicitKey}
     * to vertices, edges, and properties.
     * <p/>
     * Additionally tests RelationIdentifier since this is closely related to ADJACENT and TITANID implicit keys.
     */
@Test
public void testImplicitKey() {
    TitanVertex v = graph.addVertex("name", "Dan"), u = graph.addVertex();
    Edge e = v.addEdge("knows", u);
    graph.tx().commit();
    RelationIdentifier eid = (RelationIdentifier) e.id();
    assertEquals(v.id(), v.value(ID_NAME));
    assertEquals(eid, e.value(ID_NAME));
    assertEquals("knows", e.value(LABEL_NAME));
    assertEquals(BaseVertexLabel.DEFAULT_VERTEXLABEL.name(), v.value(LABEL_NAME));
    assertCount(1, v.query().direction(Direction.BOTH).labels("knows").has(ID_NAME, eid).edges());
    assertCount(0, v.query().direction(Direction.BOTH).labels("knows").has(ID_NAME, RelationIdentifier.get(new long[] { 4, 5, 6, 7 })).edges());
    assertCount(1, v.query().direction(Direction.BOTH).labels("knows").has("~nid", eid.getRelationId()).edges());
    assertCount(0, v.query().direction(Direction.BOTH).labels("knows").has("~nid", 110111).edges());
    //Test edge retrieval
    assertNotNull(getE(graph, eid));
    assertEquals(eid, getE(graph, eid).id());
    //Test adjacent constraint
    assertEquals(1, v.query().direction(BOTH).has("~adjacent", u.id()).edgeCount());
    assertCount(1, v.query().direction(BOTH).has("~adjacent", (int) getId(u)).edges());
    try {
        //Not a valid vertex
        assertCount(0, v.query().direction(BOTH).has("~adjacent", 110111).edges());
        fail();
    } catch (IllegalArgumentException ex) {
    }
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) RelationIdentifier(com.thinkaurelius.titan.graphdb.relations.RelationIdentifier) TitanEdge(com.thinkaurelius.titan.core.TitanEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Test(org.junit.Test)

Aggregations

RelationIdentifier (com.thinkaurelius.titan.graphdb.relations.RelationIdentifier)5 LongArrayList (com.carrotsearch.hppc.LongArrayList)1 RelationType (com.thinkaurelius.titan.core.RelationType)1 TitanEdge (com.thinkaurelius.titan.core.TitanEdge)1 TitanElement (com.thinkaurelius.titan.core.TitanElement)1 TitanException (com.thinkaurelius.titan.core.TitanException)1 TitanVertex (com.thinkaurelius.titan.core.TitanVertex)1 IndexTransaction (com.thinkaurelius.titan.diskstorage.indexing.IndexTransaction)1 StaticArrayEntry (com.thinkaurelius.titan.diskstorage.util.StaticArrayEntry)1 TransactionLogHeader (com.thinkaurelius.titan.graphdb.database.log.TransactionLogHeader)1 DataOutput (com.thinkaurelius.titan.graphdb.database.serialize.DataOutput)1 InternalRelation (com.thinkaurelius.titan.graphdb.internal.InternalRelation)1 InternalRelationType (com.thinkaurelius.titan.graphdb.internal.InternalRelationType)1 StandardTitanTx (com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx)1 MixedIndexType (com.thinkaurelius.titan.graphdb.types.MixedIndexType)1 TitanSchemaVertex (com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex)1 VertexCentricEdgeIterable (com.thinkaurelius.titan.graphdb.util.VertexCentricEdgeIterable)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1