Search in sources :

Example 1 with RelationType

use of com.thinkaurelius.titan.core.RelationType in project titan by thinkaurelius.

the class ManagementSystem method changeName.

@Override
public void changeName(TitanSchemaElement element, String newName) {
    Preconditions.checkArgument(StringUtils.isNotBlank(newName), "Invalid name: %s", newName);
    TitanSchemaVertex schemaVertex = getSchemaVertex(element);
    if (schemaVertex.name().equals(newName))
        return;
    TitanSchemaCategory schemaCategory = schemaVertex.valueOrNull(BaseKey.SchemaCategory);
    Preconditions.checkArgument(schemaCategory.hasName(), "Invalid schema element: %s", element);
    if (schemaVertex instanceof RelationType) {
        InternalRelationType relType = (InternalRelationType) schemaVertex;
        if (relType.getBaseType() != null) {
            newName = composeRelationTypeIndexName(relType.getBaseType(), newName);
        } else
            assert !(element instanceof RelationTypeIndex);
        TitanSchemaCategory cat = relType.isEdgeLabel() ? TitanSchemaCategory.EDGELABEL : TitanSchemaCategory.PROPERTYKEY;
        SystemTypeManager.isNotSystemName(cat, newName);
    } else if (element instanceof VertexLabel) {
        SystemTypeManager.isNotSystemName(TitanSchemaCategory.VERTEXLABEL, newName);
    } else if (element instanceof TitanGraphIndex) {
        checkIndexName(newName);
    }
    transaction.addProperty(schemaVertex, BaseKey.SchemaName, schemaCategory.getSchemaName(newName));
    updateSchemaVertex(schemaVertex);
    schemaVertex.resetCache();
    updatedTypes.add(schemaVertex);
}
Also used : TitanSchemaVertex(com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex) TitanSchemaCategory(com.thinkaurelius.titan.graphdb.internal.TitanSchemaCategory) VertexLabel(com.thinkaurelius.titan.core.VertexLabel) InternalRelationType(com.thinkaurelius.titan.graphdb.internal.InternalRelationType) RelationType(com.thinkaurelius.titan.core.RelationType) InternalRelationType(com.thinkaurelius.titan.graphdb.internal.InternalRelationType) RelationTypeIndex(com.thinkaurelius.titan.core.schema.RelationTypeIndex) TitanGraphIndex(com.thinkaurelius.titan.core.schema.TitanGraphIndex)

Example 2 with RelationType

use of com.thinkaurelius.titan.core.RelationType in project titan by thinkaurelius.

the class AbstractIndexManagementIT method testRemoveRelationIndex.

@Test
public void testRemoveRelationIndex() throws InterruptedException, BackendException, ExecutionException {
    tx.commit();
    mgmt.commit();
    // Load the "Graph of the Gods" sample data
    GraphOfTheGodsFactory.loadWithoutMixedIndex(graph, true);
    // Disable the "battlesByTime" index
    TitanManagement m = graph.openManagement();
    RelationType battled = m.getRelationType("battled");
    RelationTypeIndex battlesByTime = m.getRelationIndex(battled, "battlesByTime");
    m.updateIndex(battlesByTime, SchemaAction.DISABLE_INDEX);
    m.commit();
    graph.tx().commit();
    // Block until the SchemaStatus transitions to DISABLED
    assertTrue(ManagementSystem.awaitRelationIndexStatus(graph, "battlesByTime", "battled").status(SchemaStatus.DISABLED).call().getSucceeded());
    // Remove index
    MapReduceIndexManagement mri = new MapReduceIndexManagement(graph);
    m = graph.openManagement();
    battled = m.getRelationType("battled");
    battlesByTime = m.getRelationIndex(battled, "battlesByTime");
    ScanMetrics metrics = mri.updateIndex(battlesByTime, SchemaAction.REMOVE_INDEX).get();
    assertEquals(6, metrics.getCustom(IndexRemoveJob.DELETED_RECORDS_COUNT));
}
Also used : RelationType(com.thinkaurelius.titan.core.RelationType) ScanMetrics(com.thinkaurelius.titan.diskstorage.keycolumnvalue.scan.ScanMetrics) TitanManagement(com.thinkaurelius.titan.core.schema.TitanManagement) RelationTypeIndex(com.thinkaurelius.titan.core.schema.RelationTypeIndex) Test(org.junit.Test) TitanGraphBaseTest(com.thinkaurelius.titan.graphdb.TitanGraphBaseTest)

Example 3 with RelationType

use of com.thinkaurelius.titan.core.RelationType 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 4 with RelationType

use of com.thinkaurelius.titan.core.RelationType in project titan by thinkaurelius.

the class ManagementSystem method buildRelationTypeIndex.

private RelationTypeIndex buildRelationTypeIndex(RelationType type, String name, Direction direction, Order sortOrder, PropertyKey... sortKeys) {
    Preconditions.checkArgument(type != null && direction != null && sortOrder != null && sortKeys != null);
    Preconditions.checkArgument(StringUtils.isNotBlank(name), "Name cannot be blank: %s", name);
    Token.verifyName(name);
    Preconditions.checkArgument(sortKeys.length > 0, "Need to specify sort keys");
    for (RelationType key : sortKeys) Preconditions.checkArgument(key != null, "Keys cannot be null");
    Preconditions.checkArgument(!(type instanceof EdgeLabel) || !((EdgeLabel) type).isUnidirected() || direction == Direction.OUT, "Can only index uni-directed labels in the out-direction: %s", type);
    Preconditions.checkArgument(!((InternalRelationType) type).multiplicity().isConstrained(direction), "The relation type [%s] has a multiplicity or cardinality constraint in direction [%s] and can therefore not be indexed", type, direction);
    String composedName = composeRelationTypeIndexName(type, name);
    StandardRelationTypeMaker maker;
    if (type.isEdgeLabel()) {
        StandardEdgeLabelMaker lm = (StandardEdgeLabelMaker) transaction.makeEdgeLabel(composedName);
        lm.unidirected(direction);
        maker = lm;
    } else {
        assert type.isPropertyKey();
        assert direction == Direction.OUT;
        StandardPropertyKeyMaker lm = (StandardPropertyKeyMaker) transaction.makePropertyKey(composedName);
        lm.dataType(((PropertyKey) type).dataType());
        maker = lm;
    }
    maker.status(type.isNew() ? SchemaStatus.ENABLED : SchemaStatus.INSTALLED);
    maker.invisible();
    maker.multiplicity(Multiplicity.MULTI);
    maker.sortKey(sortKeys);
    maker.sortOrder(sortOrder);
    //Compose signature
    long[] typeSig = ((InternalRelationType) type).getSignature();
    Set<PropertyKey> signature = Sets.newHashSet();
    for (long typeId : typeSig) signature.add(transaction.getExistingPropertyKey(typeId));
    for (RelationType sortType : sortKeys) signature.remove(sortType);
    if (!signature.isEmpty()) {
        PropertyKey[] sig = signature.toArray(new PropertyKey[signature.size()]);
        maker.signature(sig);
    }
    RelationType typeIndex = maker.make();
    addSchemaEdge(type, typeIndex, TypeDefinitionCategory.RELATIONTYPE_INDEX, null);
    RelationTypeIndexWrapper index = new RelationTypeIndexWrapper((InternalRelationType) typeIndex);
    if (!type.isNew())
        updateIndex(index, SchemaAction.REGISTER_INDEX);
    return index;
}
Also used : StandardRelationTypeMaker(com.thinkaurelius.titan.graphdb.types.StandardRelationTypeMaker) EdgeLabel(com.thinkaurelius.titan.core.EdgeLabel) StandardEdgeLabelMaker(com.thinkaurelius.titan.graphdb.types.StandardEdgeLabelMaker) InternalRelationType(com.thinkaurelius.titan.graphdb.internal.InternalRelationType) RelationType(com.thinkaurelius.titan.core.RelationType) StandardPropertyKeyMaker(com.thinkaurelius.titan.graphdb.types.StandardPropertyKeyMaker) InternalRelationType(com.thinkaurelius.titan.graphdb.internal.InternalRelationType) PropertyKey(com.thinkaurelius.titan.core.PropertyKey)

Example 5 with RelationType

use of com.thinkaurelius.titan.core.RelationType in project titan by thinkaurelius.

the class ManagementSystem method setConsistency.

/**
     * Sets the consistency level for those schema elements that support it (types and internal indexes)
     * </p>
     * Note, that it is possible to have a race condition here if two threads simultaneously try to change the
     * consistency level. However, this is resolved when the consistency level is being read by taking the
     * first one and deleting all existing attached consistency levels upon modification.
     *
     * @param element
     * @param consistency
     */
@Override
public void setConsistency(TitanSchemaElement element, ConsistencyModifier consistency) {
    if (element instanceof RelationType) {
        RelationTypeVertex rv = (RelationTypeVertex) element;
        Preconditions.checkArgument(consistency != ConsistencyModifier.FORK || !rv.multiplicity().isConstrained(), "Cannot apply FORK consistency mode to constraint relation type: %s", rv.name());
    } else if (element instanceof TitanGraphIndex) {
        IndexType index = ((TitanGraphIndexWrapper) element).getBaseIndex();
        if (index.isMixedIndex())
            throw new IllegalArgumentException("Cannot change consistency on mixed index: " + element);
    } else
        throw new IllegalArgumentException("Cannot change consistency of schema element: " + element);
    setTypeModifier(element, ModifierType.CONSISTENCY, consistency);
}
Also used : InternalRelationType(com.thinkaurelius.titan.graphdb.internal.InternalRelationType) RelationType(com.thinkaurelius.titan.core.RelationType) IndexType(com.thinkaurelius.titan.graphdb.types.IndexType) MixedIndexType(com.thinkaurelius.titan.graphdb.types.MixedIndexType) CompositeIndexType(com.thinkaurelius.titan.graphdb.types.CompositeIndexType) TitanGraphIndex(com.thinkaurelius.titan.core.schema.TitanGraphIndex) RelationTypeVertex(com.thinkaurelius.titan.graphdb.types.vertices.RelationTypeVertex)

Aggregations

RelationType (com.thinkaurelius.titan.core.RelationType)7 InternalRelationType (com.thinkaurelius.titan.graphdb.internal.InternalRelationType)5 StandardTitanTx (com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx)3 RelationTypeIndex (com.thinkaurelius.titan.core.schema.RelationTypeIndex)2 TitanGraphIndex (com.thinkaurelius.titan.core.schema.TitanGraphIndex)2 MixedIndexType (com.thinkaurelius.titan.graphdb.types.MixedIndexType)2 TitanSchemaVertex (com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex)2 EdgeLabel (com.thinkaurelius.titan.core.EdgeLabel)1 PropertyKey (com.thinkaurelius.titan.core.PropertyKey)1 TitanElement (com.thinkaurelius.titan.core.TitanElement)1 TitanException (com.thinkaurelius.titan.core.TitanException)1 TitanRelation (com.thinkaurelius.titan.core.TitanRelation)1 TitanVertex (com.thinkaurelius.titan.core.TitanVertex)1 VertexLabel (com.thinkaurelius.titan.core.VertexLabel)1 TitanManagement (com.thinkaurelius.titan.core.schema.TitanManagement)1 IndexTransaction (com.thinkaurelius.titan.diskstorage.indexing.IndexTransaction)1 ScanMetrics (com.thinkaurelius.titan.diskstorage.keycolumnvalue.scan.ScanMetrics)1 TitanGraphBaseTest (com.thinkaurelius.titan.graphdb.TitanGraphBaseTest)1 TransactionLogHeader (com.thinkaurelius.titan.graphdb.database.log.TransactionLogHeader)1 InternalRelation (com.thinkaurelius.titan.graphdb.internal.InternalRelation)1