Search in sources :

Example 1 with IndexField

use of com.thinkaurelius.titan.graphdb.types.IndexField in project titan by thinkaurelius.

the class IndexTypeWrapper method getField.

@Override
public IndexField getField(PropertyKey key) {
    Map<PropertyKey, IndexField> result = fieldMap;
    if (result == null) {
        ImmutableMap.Builder<PropertyKey, IndexField> b = ImmutableMap.builder();
        for (IndexField f : getFieldKeys()) b.put(f.getFieldKey(), f);
        result = b.build();
        fieldMap = result;
    }
    assert result != null;
    return result.get(key);
}
Also used : IndexField(com.thinkaurelius.titan.graphdb.types.IndexField) PropertyKey(com.thinkaurelius.titan.core.PropertyKey) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 2 with IndexField

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

use of com.thinkaurelius.titan.graphdb.types.IndexField in project titan by thinkaurelius.

the class IndexHelper method getQuery.

public static GraphCentricQueryBuilder getQuery(CompositeIndexType index, Object[] values, StandardTitanTx tx) {
    Preconditions.checkArgument(index != null && values != null && values.length > 0 && tx != null);
    Preconditions.checkArgument(values.length == index.getFieldKeys().length);
    GraphCentricQueryBuilder gb = tx.query();
    IndexField[] fields = index.getFieldKeys();
    for (int i = 0; i < fields.length; i++) {
        IndexField f = fields[i];
        Object value = values[i];
        Preconditions.checkNotNull(value);
        PropertyKey key = f.getFieldKey();
        Preconditions.checkArgument(key.dataType().equals(value.getClass()), "Incompatible data types for: " + value);
        gb.has(key, Cmp.EQUAL, value);
    }
    if (index.hasSchemaTypeConstraint()) {
        gb.has(ImplicitKey.LABEL, Cmp.EQUAL, index.getSchemaTypeConstraint().name());
    }
    return gb;
}
Also used : GraphCentricQueryBuilder(com.thinkaurelius.titan.graphdb.query.graph.GraphCentricQueryBuilder) IndexField(com.thinkaurelius.titan.graphdb.types.IndexField) PropertyKey(com.thinkaurelius.titan.core.PropertyKey)

Aggregations

IndexField (com.thinkaurelius.titan.graphdb.types.IndexField)3 PropertyKey (com.thinkaurelius.titan.core.PropertyKey)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 TitanException (com.thinkaurelius.titan.core.TitanException)1 Parameter (com.thinkaurelius.titan.core.schema.Parameter)1 BackendException (com.thinkaurelius.titan.diskstorage.BackendException)1 GraphCentricQueryBuilder (com.thinkaurelius.titan.graphdb.query.graph.GraphCentricQueryBuilder)1 CompositeIndexType (com.thinkaurelius.titan.graphdb.types.CompositeIndexType)1 IndexType (com.thinkaurelius.titan.graphdb.types.IndexType)1 MixedIndexType (com.thinkaurelius.titan.graphdb.types.MixedIndexType)1 ParameterIndexField (com.thinkaurelius.titan.graphdb.types.ParameterIndexField)1 IndexTypeWrapper (com.thinkaurelius.titan.graphdb.types.indextype.IndexTypeWrapper)1 BaseKey (com.thinkaurelius.titan.graphdb.types.system.BaseKey)1 TitanSchemaVertex (com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex)1