Search in sources :

Example 1 with IndexTypeWrapper

use of org.janusgraph.graphdb.types.indextype.IndexTypeWrapper in project janusgraph by JanusGraph.

the class ManagementSystem method setTypeModifier.

private void setTypeModifier(final JanusGraphSchemaElement element, final ModifierType modifierType, final Object value) {
    Preconditions.checkArgument(element != null, "null schema element");
    TypeDefinitionCategory cat = modifierType.getCategory();
    if (cat.hasDataType() && null != value) {
        Preconditions.checkArgument(cat.getDataType().equals(value.getClass()), "modifier value is not of expected type " + cat.getDataType());
    }
    JanusGraphSchemaVertex typeVertex;
    if (element instanceof JanusGraphSchemaVertex) {
        typeVertex = (JanusGraphSchemaVertex) element;
    } else if (element instanceof JanusGraphIndex) {
        IndexType index = ((JanusGraphIndexWrapper) element).getBaseIndex();
        assert index instanceof IndexTypeWrapper;
        SchemaSource base = ((IndexTypeWrapper) index).getSchemaBase();
        typeVertex = (JanusGraphSchemaVertex) base;
    } else
        throw new IllegalArgumentException("Invalid schema element: " + element);
    // remove any pre-existing value for the modifier, or return if an identical value has already been set
    for (JanusGraphEdge e : typeVertex.getEdges(TypeDefinitionCategory.TYPE_MODIFIER, Direction.OUT)) {
        JanusGraphSchemaVertex v = (JanusGraphSchemaVertex) e.vertex(Direction.IN);
        TypeDefinitionMap def = v.getDefinition();
        Object existingValue = def.getValue(modifierType.getCategory());
        if (null != existingValue) {
            if (existingValue.equals(value)) {
                // Already has the right value, don't need to do anything
                return;
            } else {
                e.remove();
                v.remove();
            }
        }
    }
    if (null != value) {
        TypeDefinitionMap def = new TypeDefinitionMap();
        def.setValue(cat, value);
        JanusGraphSchemaVertex cVertex = transaction.makeSchemaVertex(JanusGraphSchemaCategory.TYPE_MODIFIER, null, def);
        addSchemaEdge(typeVertex, cVertex, TypeDefinitionCategory.TYPE_MODIFIER, null);
    }
    updateSchemaVertex(typeVertex);
    updatedTypes.add(typeVertex);
}
Also used : TypeDefinitionCategory(org.janusgraph.graphdb.types.TypeDefinitionCategory) IndexTypeWrapper(org.janusgraph.graphdb.types.indextype.IndexTypeWrapper) JanusGraphEdge(org.janusgraph.core.JanusGraphEdge) JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) IndexType(org.janusgraph.graphdb.types.IndexType) CompositeIndexType(org.janusgraph.graphdb.types.CompositeIndexType) MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) SchemaSource(org.janusgraph.graphdb.types.SchemaSource) TypeDefinitionMap(org.janusgraph.graphdb.types.TypeDefinitionMap)

Example 2 with IndexTypeWrapper

use of org.janusgraph.graphdb.types.indextype.IndexTypeWrapper in project janusgraph by JanusGraph.

the class ManagementSystem method addIndexKey.

@Override
public void addIndexKey(final JanusGraphIndex index, final PropertyKey key, Parameter... parameters) {
    Preconditions.checkArgument(index != null && key != null && index instanceof JanusGraphIndexWrapper && !(key instanceof BaseKey), "Need to provide valid index and key");
    if (parameters == null)
        parameters = new Parameter[0];
    IndexType indexType = ((JanusGraphIndexWrapper) 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 JanusGraphSchemaVertex && ((IndexTypeWrapper) indexType).getSchemaBase() instanceof JanusGraphSchemaVertex);
    JanusGraphSchemaVertex indexVertex = (JanusGraphSchemaVertex) ((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 JanusGraphException("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 JanusGraphException("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(org.janusgraph.graphdb.types.MixedIndexType) JanusGraphException(org.janusgraph.core.JanusGraphException) BaseKey(org.janusgraph.graphdb.types.system.BaseKey) BackendException(org.janusgraph.diskstorage.BackendException) IndexTypeWrapper(org.janusgraph.graphdb.types.indextype.IndexTypeWrapper) Parameter(org.janusgraph.core.schema.Parameter) JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex) IndexType(org.janusgraph.graphdb.types.IndexType) CompositeIndexType(org.janusgraph.graphdb.types.CompositeIndexType) MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) IndexField(org.janusgraph.graphdb.types.IndexField) ParameterIndexField(org.janusgraph.graphdb.types.ParameterIndexField)

Aggregations

CompositeIndexType (org.janusgraph.graphdb.types.CompositeIndexType)2 IndexType (org.janusgraph.graphdb.types.IndexType)2 MixedIndexType (org.janusgraph.graphdb.types.MixedIndexType)2 IndexTypeWrapper (org.janusgraph.graphdb.types.indextype.IndexTypeWrapper)2 JanusGraphSchemaVertex (org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex)2 JanusGraphEdge (org.janusgraph.core.JanusGraphEdge)1 JanusGraphException (org.janusgraph.core.JanusGraphException)1 JanusGraphIndex (org.janusgraph.core.schema.JanusGraphIndex)1 Parameter (org.janusgraph.core.schema.Parameter)1 BackendException (org.janusgraph.diskstorage.BackendException)1 IndexField (org.janusgraph.graphdb.types.IndexField)1 ParameterIndexField (org.janusgraph.graphdb.types.ParameterIndexField)1 SchemaSource (org.janusgraph.graphdb.types.SchemaSource)1 TypeDefinitionCategory (org.janusgraph.graphdb.types.TypeDefinitionCategory)1 TypeDefinitionMap (org.janusgraph.graphdb.types.TypeDefinitionMap)1 BaseKey (org.janusgraph.graphdb.types.system.BaseKey)1