Search in sources :

Example 1 with PropertyKeyVertex

use of org.janusgraph.graphdb.types.vertices.PropertyKeyVertex in project janusgraph by JanusGraph.

the class StandardJanusGraphTx method makeSchemaVertex.

/*
     * ------------------------------------ Schema Handling ------------------------------------
     */
public final JanusGraphSchemaVertex makeSchemaVertex(JanusGraphSchemaCategory schemaCategory, String name, TypeDefinitionMap definition) {
    verifyOpen();
    Preconditions.checkArgument(!schemaCategory.hasName() || StringUtils.isNotBlank(name), "Need to provide a valid name for type [%s]", schemaCategory);
    schemaCategory.verifyValidDefinition(definition);
    JanusGraphSchemaVertex schemaVertex;
    if (schemaCategory.isRelationType()) {
        if (schemaCategory == JanusGraphSchemaCategory.PROPERTYKEY) {
            schemaVertex = new PropertyKeyVertex(this, IDManager.getTemporaryVertexID(IDManager.VertexIDType.UserPropertyKey, temporaryIds.nextID()), ElementLifeCycle.New);
        } else {
            assert schemaCategory == JanusGraphSchemaCategory.EDGELABEL;
            schemaVertex = new EdgeLabelVertex(this, IDManager.getTemporaryVertexID(IDManager.VertexIDType.UserEdgeLabel, temporaryIds.nextID()), ElementLifeCycle.New);
        }
    } else if (schemaCategory == JanusGraphSchemaCategory.VERTEXLABEL) {
        schemaVertex = new VertexLabelVertex(this, IDManager.getTemporaryVertexID(IDManager.VertexIDType.GenericSchemaType, temporaryIds.nextID()), ElementLifeCycle.New);
    } else {
        schemaVertex = new JanusGraphSchemaVertex(this, IDManager.getTemporaryVertexID(IDManager.VertexIDType.GenericSchemaType, temporaryIds.nextID()), ElementLifeCycle.New);
    }
    graph.assignID(schemaVertex, BaseVertexLabel.DEFAULT_VERTEXLABEL);
    Preconditions.checkArgument(schemaVertex.longId() > 0);
    if (schemaCategory.hasName())
        addProperty(schemaVertex, BaseKey.SchemaName, schemaCategory.getSchemaName(name));
    addProperty(schemaVertex, BaseKey.VertexExists, Boolean.TRUE);
    addProperty(schemaVertex, BaseKey.SchemaCategory, schemaCategory);
    updateSchemaVertex(schemaVertex);
    addProperty(schemaVertex, BaseKey.SchemaUpdateTime, times.getTime(times.getTime()));
    for (Map.Entry<TypeDefinitionCategory, Object> def : definition.entrySet()) {
        JanusGraphVertexProperty p = addProperty(schemaVertex, BaseKey.SchemaDefinitionProperty, def.getValue());
        p.property(BaseKey.SchemaDefinitionDesc.name(), TypeDefinitionDescription.of(def.getKey()));
    }
    vertexCache.add(schemaVertex, schemaVertex.longId());
    if (schemaCategory.hasName())
        newTypeCache.put(schemaCategory.getSchemaName(name), schemaVertex.longId());
    return schemaVertex;
}
Also used : EdgeLabelVertex(org.janusgraph.graphdb.types.vertices.EdgeLabelVertex) JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex) PropertyKeyVertex(org.janusgraph.graphdb.types.vertices.PropertyKeyVertex) NonBlockingHashMap(org.cliffc.high_scale_lib.NonBlockingHashMap)

Example 2 with PropertyKeyVertex

use of org.janusgraph.graphdb.types.vertices.PropertyKeyVertex in project janusgraph by JanusGraph.

the class ManagementSystem method updateIndex.

/* --------------
    Schema Update
     --------------- */
@Override
public IndexJobFuture updateIndex(Index index, SchemaAction updateAction) {
    Preconditions.checkArgument(index != null, "Need to provide an index");
    Preconditions.checkArgument(updateAction != null, "Need to provide update action");
    JanusGraphSchemaVertex schemaVertex = getSchemaVertex(index);
    Set<JanusGraphSchemaVertex> dependentTypes;
    Set<PropertyKeyVertex> keySubset = ImmutableSet.of();
    if (index instanceof RelationTypeIndex) {
        dependentTypes = ImmutableSet.of((JanusGraphSchemaVertex) ((InternalRelationType) schemaVertex).getBaseType());
        if (!updateAction.isApplicableStatus(schemaVertex.getStatus()))
            return null;
    } else if (index instanceof JanusGraphIndex) {
        IndexType indexType = schemaVertex.asIndexType();
        dependentTypes = Sets.newHashSet();
        if (indexType.isCompositeIndex()) {
            if (!updateAction.isApplicableStatus(schemaVertex.getStatus()))
                return null;
            for (PropertyKey key : ((JanusGraphIndex) index).getFieldKeys()) {
                dependentTypes.add((PropertyKeyVertex) key);
            }
        } else {
            keySubset = Sets.newHashSet();
            MixedIndexType mixedIndexType = (MixedIndexType) indexType;
            Set<SchemaStatus> applicableStatus = updateAction.getApplicableStatus();
            for (ParameterIndexField field : mixedIndexType.getFieldKeys()) {
                if (applicableStatus.contains(field.getStatus()))
                    keySubset.add((PropertyKeyVertex) field.getFieldKey());
            }
            if (keySubset.isEmpty())
                return null;
            dependentTypes.addAll(keySubset);
        }
    } else
        throw new UnsupportedOperationException("Updates not supported for index: " + index);
    IndexIdentifier indexId = new IndexIdentifier(index);
    StandardScanner.Builder builder;
    IndexJobFuture future;
    switch(updateAction) {
        case REGISTER_INDEX:
            setStatus(schemaVertex, SchemaStatus.INSTALLED, keySubset);
            updatedTypes.add(schemaVertex);
            updatedTypes.addAll(dependentTypes);
            setUpdateTrigger(new UpdateStatusTrigger(graph, schemaVertex, SchemaStatus.REGISTERED, keySubset));
            future = new EmptyIndexJobFuture();
            break;
        case REINDEX:
            builder = graph.getBackend().buildEdgeScanJob();
            builder.setFinishJob(indexId.getIndexJobFinisher(graph, SchemaAction.ENABLE_INDEX));
            builder.setJobId(indexId);
            builder.setJob(VertexJobConverter.convert(graph, new IndexRepairJob(indexId.indexName, indexId.relationTypeName)));
            try {
                future = builder.execute();
            } catch (BackendException e) {
                throw new JanusGraphException(e);
            }
            break;
        case ENABLE_INDEX:
            setStatus(schemaVertex, SchemaStatus.ENABLED, keySubset);
            updatedTypes.add(schemaVertex);
            if (!keySubset.isEmpty())
                updatedTypes.addAll(dependentTypes);
            future = new EmptyIndexJobFuture();
            break;
        case DISABLE_INDEX:
            setStatus(schemaVertex, SchemaStatus.INSTALLED, keySubset);
            updatedTypes.add(schemaVertex);
            if (!keySubset.isEmpty())
                updatedTypes.addAll(dependentTypes);
            setUpdateTrigger(new UpdateStatusTrigger(graph, schemaVertex, SchemaStatus.DISABLED, keySubset));
            future = new EmptyIndexJobFuture();
            break;
        case REMOVE_INDEX:
            if (index instanceof RelationTypeIndex) {
                builder = graph.getBackend().buildEdgeScanJob();
            } else {
                JanusGraphIndex graphIndex = (JanusGraphIndex) index;
                if (graphIndex.isMixedIndex())
                    throw new UnsupportedOperationException("External mixed indexes must be removed in the indexing system directly.");
                builder = graph.getBackend().buildGraphIndexScanJob();
            }
            builder.setFinishJob(indexId.getIndexJobFinisher());
            builder.setJobId(indexId);
            builder.setJob(new IndexRemoveJob(graph, indexId.indexName, indexId.relationTypeName));
            try {
                future = builder.execute();
            } catch (BackendException e) {
                throw new JanusGraphException(e);
            }
            break;
        default:
            throw new UnsupportedOperationException("Update action not supported: " + updateAction);
    }
    return future;
}
Also used : Set(java.util.Set) ImmutableSet(com.google.common.collect.ImmutableSet) HashSet(java.util.HashSet) MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) JanusGraphException(org.janusgraph.core.JanusGraphException) IndexRepairJob(org.janusgraph.graphdb.olap.job.IndexRepairJob) ParameterIndexField(org.janusgraph.graphdb.types.ParameterIndexField) RelationTypeIndex(org.janusgraph.core.schema.RelationTypeIndex) BackendException(org.janusgraph.diskstorage.BackendException) StandardScanner(org.janusgraph.diskstorage.keycolumnvalue.scan.StandardScanner) JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex) PropertyKeyVertex(org.janusgraph.graphdb.types.vertices.PropertyKeyVertex) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) IndexType(org.janusgraph.graphdb.types.IndexType) CompositeIndexType(org.janusgraph.graphdb.types.CompositeIndexType) MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) PropertyKey(org.janusgraph.core.PropertyKey) IndexRemoveJob(org.janusgraph.graphdb.olap.job.IndexRemoveJob)

Example 3 with PropertyKeyVertex

use of org.janusgraph.graphdb.types.vertices.PropertyKeyVertex in project janusgraph by JanusGraph.

the class ManagementSystem method setStatusEdges.

private void setStatusEdges(JanusGraphSchemaVertex vertex, SchemaStatus status, Set<PropertyKeyVertex> keys) {
    Preconditions.checkArgument(vertex.asIndexType().isMixedIndex());
    for (JanusGraphEdge edge : vertex.getEdges(TypeDefinitionCategory.INDEX_FIELD, Direction.OUT)) {
        // Only address edges with matching keys
        if (!keys.contains(edge.vertex(Direction.IN)))
            continue;
        TypeDefinitionDescription desc = edge.valueOrNull(BaseKey.SchemaDefinitionDesc);
        assert desc.getCategory() == TypeDefinitionCategory.INDEX_FIELD;
        Parameter[] parameters = (Parameter[]) desc.getModifier();
        assert parameters[parameters.length - 1].key().equals(ParameterType.STATUS.getName());
        if (parameters[parameters.length - 1].value().equals(status))
            continue;
        Parameter[] paraCopy = Arrays.copyOf(parameters, parameters.length);
        paraCopy[parameters.length - 1] = ParameterType.STATUS.getParameter(status);
        edge.remove();
        addSchemaEdge(vertex, edge.vertex(Direction.IN), TypeDefinitionCategory.INDEX_FIELD, paraCopy);
    }
    for (PropertyKeyVertex prop : keys) prop.resetCache();
}
Also used : JanusGraphEdge(org.janusgraph.core.JanusGraphEdge) TypeDefinitionDescription(org.janusgraph.graphdb.types.TypeDefinitionDescription) Parameter(org.janusgraph.core.schema.Parameter) PropertyKeyVertex(org.janusgraph.graphdb.types.vertices.PropertyKeyVertex)

Example 4 with PropertyKeyVertex

use of org.janusgraph.graphdb.types.vertices.PropertyKeyVertex in project janusgraph by JanusGraph.

the class ManagementSystem method createCompositeIndex.

private JanusGraphIndex createCompositeIndex(String indexName, ElementCategory elementCategory, boolean unique, JanusGraphSchemaType constraint, PropertyKey... keys) {
    checkIndexName(indexName);
    Preconditions.checkArgument(keys != null && keys.length > 0, "Need to provide keys to index [%s]", indexName);
    Preconditions.checkArgument(!unique || elementCategory == ElementCategory.VERTEX, "Unique indexes can only be created on vertices [%s]", indexName);
    boolean allSingleKeys = true;
    boolean oneNewKey = false;
    for (PropertyKey key : keys) {
        Preconditions.checkArgument(key != null && key instanceof PropertyKeyVertex, "Need to provide valid keys: %s", key);
        if (key.cardinality() != Cardinality.SINGLE)
            allSingleKeys = false;
        if (key.isNew())
            oneNewKey = true;
        else
            updatedTypes.add((PropertyKeyVertex) key);
    }
    Cardinality indexCardinality;
    if (unique)
        indexCardinality = Cardinality.SINGLE;
    else
        indexCardinality = (allSingleKeys ? Cardinality.SET : Cardinality.LIST);
    TypeDefinitionMap def = new TypeDefinitionMap();
    def.setValue(TypeDefinitionCategory.INTERNAL_INDEX, true);
    def.setValue(TypeDefinitionCategory.ELEMENT_CATEGORY, elementCategory);
    def.setValue(TypeDefinitionCategory.BACKING_INDEX, Token.INTERNAL_INDEX_NAME);
    def.setValue(TypeDefinitionCategory.INDEXSTORE_NAME, indexName);
    def.setValue(TypeDefinitionCategory.INDEX_CARDINALITY, indexCardinality);
    def.setValue(TypeDefinitionCategory.STATUS, oneNewKey ? SchemaStatus.ENABLED : SchemaStatus.INSTALLED);
    JanusGraphSchemaVertex indexVertex = transaction.makeSchemaVertex(JanusGraphSchemaCategory.GRAPHINDEX, indexName, def);
    for (int i = 0; i < keys.length; i++) {
        Parameter[] paras = { ParameterType.INDEX_POSITION.getParameter(i) };
        addSchemaEdge(indexVertex, keys[i], TypeDefinitionCategory.INDEX_FIELD, paras);
    }
    Preconditions.checkArgument(constraint == null || (elementCategory.isValidConstraint(constraint) && constraint instanceof JanusGraphSchemaVertex));
    if (constraint != null) {
        addSchemaEdge(indexVertex, (JanusGraphSchemaVertex) constraint, TypeDefinitionCategory.INDEX_SCHEMA_CONSTRAINT, null);
    }
    updateSchemaVertex(indexVertex);
    JanusGraphIndexWrapper index = new JanusGraphIndexWrapper(indexVertex.asIndexType());
    if (!oneNewKey)
        updateIndex(index, SchemaAction.REGISTER_INDEX);
    return index;
}
Also used : Cardinality(org.janusgraph.core.Cardinality) JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex) Parameter(org.janusgraph.core.schema.Parameter) PropertyKeyVertex(org.janusgraph.graphdb.types.vertices.PropertyKeyVertex) TypeDefinitionMap(org.janusgraph.graphdb.types.TypeDefinitionMap) PropertyKey(org.janusgraph.core.PropertyKey)

Aggregations

PropertyKeyVertex (org.janusgraph.graphdb.types.vertices.PropertyKeyVertex)4 JanusGraphSchemaVertex (org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex)3 PropertyKey (org.janusgraph.core.PropertyKey)2 Parameter (org.janusgraph.core.schema.Parameter)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 NonBlockingHashMap (org.cliffc.high_scale_lib.NonBlockingHashMap)1 Cardinality (org.janusgraph.core.Cardinality)1 JanusGraphEdge (org.janusgraph.core.JanusGraphEdge)1 JanusGraphException (org.janusgraph.core.JanusGraphException)1 JanusGraphIndex (org.janusgraph.core.schema.JanusGraphIndex)1 RelationTypeIndex (org.janusgraph.core.schema.RelationTypeIndex)1 BackendException (org.janusgraph.diskstorage.BackendException)1 StandardScanner (org.janusgraph.diskstorage.keycolumnvalue.scan.StandardScanner)1 IndexRemoveJob (org.janusgraph.graphdb.olap.job.IndexRemoveJob)1 IndexRepairJob (org.janusgraph.graphdb.olap.job.IndexRepairJob)1 CompositeIndexType (org.janusgraph.graphdb.types.CompositeIndexType)1 IndexType (org.janusgraph.graphdb.types.IndexType)1 MixedIndexType (org.janusgraph.graphdb.types.MixedIndexType)1