Search in sources :

Example 1 with TitanSchemaVertex

use of com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex in project titan by thinkaurelius.

the class ManagementSystem method addIndexKey.

@Override
public void addIndexKey(final TitanGraphIndex index, final PropertyKey key, Parameter... parameters) {
    Preconditions.checkArgument(index != null && key != null && index instanceof TitanGraphIndexWrapper && !(key instanceof BaseKey), "Need to provide valid index and key");
    if (parameters == null)
        parameters = new Parameter[0];
    IndexType indexType = ((TitanGraphIndexWrapper) index).getBaseIndex();
    Preconditions.checkArgument(indexType instanceof MixedIndexType, "Can only add keys to an external index, not %s", index.name());
    Preconditions.checkArgument(indexType instanceof IndexTypeWrapper && key instanceof TitanSchemaVertex && ((IndexTypeWrapper) indexType).getSchemaBase() instanceof TitanSchemaVertex);
    TitanSchemaVertex indexVertex = (TitanSchemaVertex) ((IndexTypeWrapper) indexType).getSchemaBase();
    for (IndexField field : indexType.getFieldKeys()) Preconditions.checkArgument(!field.getFieldKey().equals(key), "Key [%s] has already been added to index %s", key.name(), index.name());
    //Assemble parameters
    boolean addMappingParameter = !ParameterType.MAPPED_NAME.hasParameter(parameters);
    Parameter[] extendedParas = new Parameter[parameters.length + 1 + (addMappingParameter ? 1 : 0)];
    System.arraycopy(parameters, 0, extendedParas, 0, parameters.length);
    int arrPosition = parameters.length;
    if (addMappingParameter)
        extendedParas[arrPosition++] = ParameterType.MAPPED_NAME.getParameter(graph.getIndexSerializer().getDefaultFieldName(key, parameters, indexType.getBackingIndexName()));
    extendedParas[arrPosition++] = ParameterType.STATUS.getParameter(key.isNew() ? SchemaStatus.ENABLED : SchemaStatus.INSTALLED);
    addSchemaEdge(indexVertex, key, TypeDefinitionCategory.INDEX_FIELD, extendedParas);
    updateSchemaVertex(indexVertex);
    indexType.resetCache();
    //Check to see if the index supports this
    if (!graph.getIndexSerializer().supports((MixedIndexType) indexType, ParameterIndexField.of(key, parameters))) {
        throw new TitanException("Could not register new index field '" + key.name() + "' with index backend as the data type, cardinality or parameter combination is not supported.");
    }
    try {
        IndexSerializer.register((MixedIndexType) indexType, key, transaction.getTxHandle());
    } catch (BackendException e) {
        throw new TitanException("Could not register new index field with index backend", e);
    }
    if (!indexVertex.isNew())
        updatedTypes.add(indexVertex);
    if (!key.isNew())
        updateIndex(index, SchemaAction.REGISTER_INDEX);
}
Also used : MixedIndexType(com.thinkaurelius.titan.graphdb.types.MixedIndexType) TitanSchemaVertex(com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex) BaseKey(com.thinkaurelius.titan.graphdb.types.system.BaseKey) BackendException(com.thinkaurelius.titan.diskstorage.BackendException) IndexTypeWrapper(com.thinkaurelius.titan.graphdb.types.indextype.IndexTypeWrapper) TitanException(com.thinkaurelius.titan.core.TitanException) Parameter(com.thinkaurelius.titan.core.schema.Parameter) IndexType(com.thinkaurelius.titan.graphdb.types.IndexType) MixedIndexType(com.thinkaurelius.titan.graphdb.types.MixedIndexType) CompositeIndexType(com.thinkaurelius.titan.graphdb.types.CompositeIndexType) IndexField(com.thinkaurelius.titan.graphdb.types.IndexField) ParameterIndexField(com.thinkaurelius.titan.graphdb.types.ParameterIndexField)

Example 2 with TitanSchemaVertex

use of com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex 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 3 with TitanSchemaVertex

use of com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex in project titan by thinkaurelius.

the class ManagementSystem method createMixedIndex.

private TitanGraphIndex createMixedIndex(String indexName, ElementCategory elementCategory, TitanSchemaType constraint, String backingIndex) {
    Preconditions.checkArgument(graph.getIndexSerializer().containsIndex(backingIndex), "Unknown external index backend: %s", backingIndex);
    checkIndexName(indexName);
    TypeDefinitionMap def = new TypeDefinitionMap();
    def.setValue(TypeDefinitionCategory.INTERNAL_INDEX, false);
    def.setValue(TypeDefinitionCategory.ELEMENT_CATEGORY, elementCategory);
    def.setValue(TypeDefinitionCategory.BACKING_INDEX, backingIndex);
    def.setValue(TypeDefinitionCategory.INDEXSTORE_NAME, indexName);
    def.setValue(TypeDefinitionCategory.INDEX_CARDINALITY, Cardinality.LIST);
    def.setValue(TypeDefinitionCategory.STATUS, SchemaStatus.ENABLED);
    TitanSchemaVertex indexVertex = transaction.makeSchemaVertex(TitanSchemaCategory.GRAPHINDEX, indexName, def);
    Preconditions.checkArgument(constraint == null || (elementCategory.isValidConstraint(constraint) && constraint instanceof TitanSchemaVertex));
    if (constraint != null) {
        addSchemaEdge(indexVertex, (TitanSchemaVertex) constraint, TypeDefinitionCategory.INDEX_SCHEMA_CONSTRAINT, null);
    }
    updateSchemaVertex(indexVertex);
    return new TitanGraphIndexWrapper(indexVertex.asIndexType());
}
Also used : TitanSchemaVertex(com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex) TypeDefinitionMap(com.thinkaurelius.titan.graphdb.types.TypeDefinitionMap)

Example 4 with TitanSchemaVertex

use of com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex in project titan by thinkaurelius.

the class VertexIDAssigner method assignID.

private void assignID(InternalElement element, IDManager.VertexIDType vertexIDType) {
    for (int attempt = 0; attempt < MAX_PARTITION_RENEW_ATTEMPTS; attempt++) {
        long partitionID = -1;
        if (element instanceof TitanSchemaVertex) {
            partitionID = IDManager.SCHEMA_PARTITION;
        } else if (element instanceof TitanVertex) {
            if (vertexIDType == IDManager.VertexIDType.PartitionedVertex)
                partitionID = IDManager.PARTITIONED_VERTEX_PARTITION;
            else
                partitionID = placementStrategy.getPartition(element);
        } else if (element instanceof InternalRelation) {
            InternalRelation relation = (InternalRelation) element;
            if (attempt < relation.getLen()) {
                //On the first attempts, try to use partition of incident vertices
                InternalVertex incident = relation.getVertex(attempt);
                Preconditions.checkArgument(incident.hasId());
                if (!IDManager.VertexIDType.PartitionedVertex.is(incident.longId()) || relation.isProperty()) {
                    partitionID = getPartitionID(incident);
                } else {
                    continue;
                }
            } else {
                partitionID = placementStrategy.getPartition(element);
            }
        }
        try {
            assignID(element, partitionID, vertexIDType);
        } catch (IDPoolExhaustedException e) {
            //try again on a different partition
            continue;
        }
        assert element.hasId();
        //Check if we should assign a different representative of a potential partitioned vertex
        if (element instanceof InternalRelation) {
            InternalRelation relation = (InternalRelation) element;
            if (relation.isProperty() && isPartitionedAt(relation, 0)) {
                //Always assign properties to the canonical representative of a partitioned vertex
                InternalVertex vertex = relation.getVertex(0);
                ((ReassignableRelation) relation).setVertexAt(0, vertex.tx().getInternalVertex(idManager.getCanonicalVertexId(vertex.longId())));
            } else if (relation.isEdge()) {
                for (int pos = 0; pos < relation.getArity(); pos++) {
                    if (isPartitionedAt(relation, pos)) {
                        InternalVertex incident = relation.getVertex(pos);
                        long newPartition;
                        int otherpos = (pos + 1) % 2;
                        if (((InternalRelationType) relation.getType()).multiplicity().isUnique(EdgeDirection.fromPosition(pos))) {
                            //If the relation is unique in the direction, we assign it to the canonical vertex...
                            newPartition = idManager.getPartitionId(idManager.getCanonicalVertexId(incident.longId()));
                        } else if (!isPartitionedAt(relation, otherpos)) {
                            //...else, we assign it to the partition of the non-partitioned vertex...
                            newPartition = getPartitionID(relation.getVertex(otherpos));
                        } else {
                            //...and if such does not exists (i.e. both end vertices are partitioned) we use the hash of the relation id
                            newPartition = idManager.getPartitionHashForId(relation.longId());
                        }
                        if (idManager.getPartitionId(incident.longId()) != newPartition) {
                            ((ReassignableRelation) relation).setVertexAt(pos, incident.tx().getOtherPartitionVertex(incident, newPartition));
                        }
                    }
                }
            }
        }
        return;
    }
    throw new IDPoolExhaustedException("Could not find non-exhausted partition ID Pool after " + MAX_PARTITION_RENEW_ATTEMPTS + " attempts");
}
Also used : ReassignableRelation(com.thinkaurelius.titan.graphdb.relations.ReassignableRelation) TitanSchemaVertex(com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex) InternalVertex(com.thinkaurelius.titan.graphdb.internal.InternalVertex) InternalRelationType(com.thinkaurelius.titan.graphdb.internal.InternalRelationType) InternalRelation(com.thinkaurelius.titan.graphdb.internal.InternalRelation)

Example 5 with TitanSchemaVertex

use of com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex in project titan by thinkaurelius.

the class VertexIDAssigner method assignIDs.

public void assignIDs(Iterable<InternalRelation> addedRelations) {
    if (!placementStrategy.supportsBulkPlacement()) {
        for (InternalRelation relation : addedRelations) {
            for (int i = 0; i < relation.getArity(); i++) {
                InternalVertex vertex = relation.getVertex(i);
                if (!vertex.hasId()) {
                    assignID(vertex, getVertexIDType(vertex));
                }
            }
            assignID(relation);
        }
    } else {
        //2) only assign ids to (user) vertices
        Map<InternalVertex, PartitionAssignment> assignments = new HashMap<InternalVertex, PartitionAssignment>();
        for (InternalRelation relation : addedRelations) {
            for (int i = 0; i < relation.getArity(); i++) {
                InternalVertex vertex = relation.getVertex(i);
                if (!vertex.hasId()) {
                    //Those are assigned ids immediately in the transaction
                    assert !(vertex instanceof TitanSchemaVertex);
                    if (vertex.vertexLabel().isPartitioned())
                        //Assign partitioned vertex ids immediately
                        assignID(vertex, getVertexIDType(vertex));
                    else
                        assignments.put(vertex, PartitionAssignment.EMPTY);
                }
            }
        }
        log.trace("Bulk id assignment for {} vertices", assignments.size());
        for (int attempt = 0; attempt < MAX_PARTITION_RENEW_ATTEMPTS && (assignments != null && !assignments.isEmpty()); attempt++) {
            placementStrategy.getPartitions(assignments);
            Map<InternalVertex, PartitionAssignment> leftOvers = null;
            Iterator<Map.Entry<InternalVertex, PartitionAssignment>> iter = assignments.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry<InternalVertex, PartitionAssignment> entry = iter.next();
                try {
                    assignID(entry.getKey(), entry.getValue().getPartitionID(), getVertexIDType(entry.getKey()));
                    Preconditions.checkArgument(entry.getKey().hasId());
                } catch (IDPoolExhaustedException e) {
                    if (leftOvers == null)
                        leftOvers = new HashMap<InternalVertex, PartitionAssignment>();
                    leftOvers.put(entry.getKey(), PartitionAssignment.EMPTY);
                    break;
                }
            }
            if (leftOvers != null) {
                while (iter.hasNext()) leftOvers.put(iter.next().getKey(), PartitionAssignment.EMPTY);
                log.debug("Exhausted ID Pool in bulk assignment. Left-over vertices {}", leftOvers.size());
            }
            assignments = leftOvers;
        }
        if (assignments != null && !assignments.isEmpty())
            throw new IDPoolExhaustedException("Could not find non-exhausted partition ID Pool after " + MAX_PARTITION_RENEW_ATTEMPTS + " attempts");
        //3) assign ids to relations
        for (InternalRelation relation : addedRelations) {
            assignID(relation);
        }
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TitanSchemaVertex(com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex) InternalRelation(com.thinkaurelius.titan.graphdb.internal.InternalRelation) InternalVertex(com.thinkaurelius.titan.graphdb.internal.InternalVertex) ConcurrentMap(java.util.concurrent.ConcurrentMap) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

TitanSchemaVertex (com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex)15 TitanGraphIndex (com.thinkaurelius.titan.core.schema.TitanGraphIndex)4 CompositeIndexType (com.thinkaurelius.titan.graphdb.types.CompositeIndexType)4 MixedIndexType (com.thinkaurelius.titan.graphdb.types.MixedIndexType)4 TypeDefinitionMap (com.thinkaurelius.titan.graphdb.types.TypeDefinitionMap)4 TitanException (com.thinkaurelius.titan.core.TitanException)3 RelationTypeIndex (com.thinkaurelius.titan.core.schema.RelationTypeIndex)3 InternalRelation (com.thinkaurelius.titan.graphdb.internal.InternalRelation)3 InternalRelationType (com.thinkaurelius.titan.graphdb.internal.InternalRelationType)3 IndexType (com.thinkaurelius.titan.graphdb.types.IndexType)3 PropertyKeyVertex (com.thinkaurelius.titan.graphdb.types.vertices.PropertyKeyVertex)3 PropertyKey (com.thinkaurelius.titan.core.PropertyKey)2 RelationType (com.thinkaurelius.titan.core.RelationType)2 Parameter (com.thinkaurelius.titan.core.schema.Parameter)2 BackendException (com.thinkaurelius.titan.diskstorage.BackendException)2 DataOutput (com.thinkaurelius.titan.graphdb.database.serialize.DataOutput)2 InternalVertex (com.thinkaurelius.titan.graphdb.internal.InternalVertex)2 TitanSchemaCategory (com.thinkaurelius.titan.graphdb.internal.TitanSchemaCategory)2 ParameterIndexField (com.thinkaurelius.titan.graphdb.types.ParameterIndexField)2 IndexTypeWrapper (com.thinkaurelius.titan.graphdb.types.indextype.IndexTypeWrapper)2