Search in sources :

Example 6 with IndexType

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

the class IndexRepairJob method process.

@Override
public void process(TitanVertex vertex, ScanMetrics metrics) {
    try {
        BackendTransaction mutator = writeTx.getTxHandle();
        if (index instanceof RelationTypeIndex) {
            RelationTypeIndexWrapper wrapper = (RelationTypeIndexWrapper) index;
            InternalRelationType wrappedType = wrapper.getWrappedType();
            EdgeSerializer edgeSerializer = writeTx.getEdgeSerializer();
            List<Entry> additions = new ArrayList<>();
            for (TitanRelation relation : vertex.query().types(indexRelationTypeName).direction(Direction.OUT).relations()) {
                InternalRelation titanRelation = (InternalRelation) relation;
                for (int pos = 0; pos < titanRelation.getArity(); pos++) {
                    if (!wrappedType.isUnidirected(Direction.BOTH) && !wrappedType.isUnidirected(EdgeDirection.fromPosition(pos)))
                        //Directionality is not covered
                        continue;
                    Entry entry = edgeSerializer.writeRelation(titanRelation, wrappedType, pos, writeTx);
                    additions.add(entry);
                }
            }
            StaticBuffer vertexKey = writeTx.getIdInspector().getKey(vertex.longId());
            mutator.mutateEdges(vertexKey, additions, KCVSCache.NO_DELETIONS);
            metrics.incrementCustom(ADDED_RECORDS_COUNT, additions.size());
        } else if (index instanceof TitanGraphIndex) {
            IndexType indexType = mgmt.getSchemaVertex(index).asIndexType();
            assert indexType != null;
            IndexSerializer indexSerializer = graph.getIndexSerializer();
            //Gather elements to index
            List<TitanElement> elements;
            switch(indexType.getElement()) {
                case VERTEX:
                    elements = ImmutableList.of(vertex);
                    break;
                case PROPERTY:
                    elements = Lists.newArrayList();
                    for (TitanVertexProperty p : addIndexSchemaConstraint(vertex.query(), indexType).properties()) {
                        elements.add(p);
                    }
                    break;
                case EDGE:
                    elements = Lists.newArrayList();
                    for (TitanEdge e : addIndexSchemaConstraint(vertex.query().direction(Direction.OUT), indexType).edges()) {
                        elements.add(e);
                    }
                    break;
                default:
                    throw new AssertionError("Unexpected category: " + indexType.getElement());
            }
            if (indexType.isCompositeIndex()) {
                for (TitanElement element : elements) {
                    Set<IndexSerializer.IndexUpdate<StaticBuffer, Entry>> updates = indexSerializer.reindexElement(element, (CompositeIndexType) indexType);
                    for (IndexSerializer.IndexUpdate<StaticBuffer, Entry> update : updates) {
                        log.debug("Mutating index {}: {}", indexType, update.getEntry());
                        mutator.mutateIndex(update.getKey(), Lists.newArrayList(update.getEntry()), KCVSCache.NO_DELETIONS);
                        metrics.incrementCustom(ADDED_RECORDS_COUNT);
                    }
                }
            } else {
                assert indexType.isMixedIndex();
                Map<String, Map<String, List<IndexEntry>>> documentsPerStore = new HashMap<>();
                for (TitanElement element : elements) {
                    indexSerializer.reindexElement(element, (MixedIndexType) indexType, documentsPerStore);
                    metrics.incrementCustom(DOCUMENT_UPDATES_COUNT);
                }
                mutator.getIndexTransaction(indexType.getBackingIndexName()).restore(documentsPerStore);
            }
        } else
            throw new UnsupportedOperationException("Unsupported index found: " + index);
    } catch (final Exception e) {
        mgmt.rollback();
        writeTx.rollback();
        metrics.incrementCustom(FAILED_TX);
        throw new TitanException(e.getMessage(), e);
    }
}
Also used : InternalRelation(com.thinkaurelius.titan.graphdb.internal.InternalRelation) Entry(com.thinkaurelius.titan.diskstorage.Entry) IndexEntry(com.thinkaurelius.titan.diskstorage.indexing.IndexEntry) EdgeSerializer(com.thinkaurelius.titan.graphdb.database.EdgeSerializer) RelationTypeIndexWrapper(com.thinkaurelius.titan.graphdb.database.management.RelationTypeIndexWrapper) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) ImmutableList(com.google.common.collect.ImmutableList) MixedIndexType(com.thinkaurelius.titan.graphdb.types.MixedIndexType) CompositeIndexType(com.thinkaurelius.titan.graphdb.types.CompositeIndexType) IndexType(com.thinkaurelius.titan.graphdb.types.IndexType) BackendTransaction(com.thinkaurelius.titan.diskstorage.BackendTransaction) MixedIndexType(com.thinkaurelius.titan.graphdb.types.MixedIndexType) IndexSerializer(com.thinkaurelius.titan.graphdb.database.IndexSerializer) CompositeIndexType(com.thinkaurelius.titan.graphdb.types.CompositeIndexType) InternalRelationType(com.thinkaurelius.titan.graphdb.internal.InternalRelationType)

Example 7 with IndexType

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

the class RelationTypeVertex method getKeyIndexes.

public Iterable<IndexType> getKeyIndexes() {
    List<IndexType> result = indexes;
    if (result == null) {
        ImmutableList.Builder<IndexType> b = ImmutableList.builder();
        for (Entry entry : getRelated(TypeDefinitionCategory.INDEX_FIELD, Direction.IN)) {
            SchemaSource index = entry.getSchemaType();
            b.add(index.asIndexType());
        }
        result = b.build();
        indexes = result;
    }
    assert result != null;
    return result;
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) IndexType(com.thinkaurelius.titan.graphdb.types.IndexType) SchemaSource(com.thinkaurelius.titan.graphdb.types.SchemaSource)

Aggregations

IndexType (com.thinkaurelius.titan.graphdb.types.IndexType)7 CompositeIndexType (com.thinkaurelius.titan.graphdb.types.CompositeIndexType)6 MixedIndexType (com.thinkaurelius.titan.graphdb.types.MixedIndexType)5 TitanGraphIndex (com.thinkaurelius.titan.core.schema.TitanGraphIndex)3 InternalRelationType (com.thinkaurelius.titan.graphdb.internal.InternalRelationType)3 TitanSchemaVertex (com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex)3 ImmutableList (com.google.common.collect.ImmutableList)2 TitanException (com.thinkaurelius.titan.core.TitanException)2 BackendException (com.thinkaurelius.titan.diskstorage.BackendException)2 ParameterIndexField (com.thinkaurelius.titan.graphdb.types.ParameterIndexField)2 SchemaSource (com.thinkaurelius.titan.graphdb.types.SchemaSource)2 IndexTypeWrapper (com.thinkaurelius.titan.graphdb.types.indextype.IndexTypeWrapper)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 PropertyKey (com.thinkaurelius.titan.core.PropertyKey)1 RelationType (com.thinkaurelius.titan.core.RelationType)1 TitanEdge (com.thinkaurelius.titan.core.TitanEdge)1 Parameter (com.thinkaurelius.titan.core.schema.Parameter)1 RelationTypeIndex (com.thinkaurelius.titan.core.schema.RelationTypeIndex)1 BackendTransaction (com.thinkaurelius.titan.diskstorage.BackendTransaction)1 Entry (com.thinkaurelius.titan.diskstorage.Entry)1