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);
}
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);
}
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());
}
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");
}
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);
}
}
}
Aggregations