Search in sources :

Example 1 with IndexSerializer

use of org.janusgraph.graphdb.database.IndexSerializer in project janusgraph by JanusGraph.

the class IndexRepairJob method process.

@Override
public void process(JanusGraphVertex 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 (Object relation : vertex.query().types(indexRelationTypeName).direction(Direction.OUT).relations()) {
                InternalRelation janusgraphRelation = (InternalRelation) relation;
                for (int pos = 0; pos < janusgraphRelation.getArity(); pos++) {
                    if (!wrappedType.isUnidirected(Direction.BOTH) && !wrappedType.isUnidirected(EdgeDirection.fromPosition(pos)))
                        // Directionality is not covered
                        continue;
                    Entry entry = edgeSerializer.writeRelation(janusgraphRelation, 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 JanusGraphIndex) {
            IndexType indexType = managementSystem.getSchemaVertex(index).asIndexType();
            assert indexType != null;
            IndexSerializer indexSerializer = graph.getIndexSerializer();
            // Gather elements to index
            List<JanusGraphElement> elements;
            switch(indexType.getElement()) {
                case VERTEX:
                    elements = ImmutableList.of(vertex);
                    break;
                case PROPERTY:
                    elements = Lists.newArrayList();
                    for (JanusGraphVertexProperty p : addIndexSchemaConstraint(vertex.query(), indexType).properties()) {
                        elements.add(p);
                    }
                    break;
                case EDGE:
                    elements = Lists.newArrayList();
                    for (Object e : addIndexSchemaConstraint(vertex.query().direction(Direction.OUT), indexType).edges()) {
                        elements.add((JanusGraphEdge) e);
                    }
                    break;
                default:
                    throw new AssertionError("Unexpected category: " + indexType.getElement());
            }
            if (indexType.isCompositeIndex()) {
                for (JanusGraphElement 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 (JanusGraphElement 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) {
        managementSystem.rollback();
        writeTx.rollback();
        metrics.incrementCustom(FAILED_TX);
        throw new JanusGraphException(e.getMessage(), e);
    }
}
Also used : InternalRelation(org.janusgraph.graphdb.internal.InternalRelation) IndexEntry(org.janusgraph.diskstorage.indexing.IndexEntry) Entry(org.janusgraph.diskstorage.Entry) EdgeSerializer(org.janusgraph.graphdb.database.EdgeSerializer) RelationTypeIndexWrapper(org.janusgraph.graphdb.database.management.RelationTypeIndexWrapper) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) ImmutableList(com.google.common.collect.ImmutableList) MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) IndexType(org.janusgraph.graphdb.types.IndexType) CompositeIndexType(org.janusgraph.graphdb.types.CompositeIndexType) BackendTransaction(org.janusgraph.diskstorage.BackendTransaction) MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) IndexSerializer(org.janusgraph.graphdb.database.IndexSerializer) CompositeIndexType(org.janusgraph.graphdb.types.CompositeIndexType) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType)

Example 2 with IndexSerializer

use of org.janusgraph.graphdb.database.IndexSerializer in project janusgraph by JanusGraph.

the class StandardJanusGraphTxTest method createTxWithMockedInternals.

private StandardJanusGraphTx createTxWithMockedInternals() {
    StandardJanusGraph mockGraph = createMock(StandardJanusGraph.class);
    TransactionConfiguration txConfig = createMock(TransactionConfiguration.class);
    GraphDatabaseConfiguration gdbConfig = createMock(GraphDatabaseConfiguration.class);
    TimestampProvider tsProvider = createMock(TimestampProvider.class);
    Serializer mockSerializer = createMock(Serializer.class);
    EdgeSerializer mockEdgeSerializer = createMock(EdgeSerializer.class);
    IndexSerializer mockIndexSerializer = createMock(IndexSerializer.class);
    RelationType relationType = createMock(RelationType.class);
    IDManager idManager = createMock(IDManager.class);
    PropertyKey propertyKey = createMock(PropertyKey.class);
    DefaultSchemaMaker defaultSchemaMaker = createMock(DefaultSchemaMaker.class);
    expect(mockGraph.getConfiguration()).andReturn(gdbConfig);
    expect(mockGraph.isOpen()).andReturn(true).anyTimes();
    expect(mockGraph.getDataSerializer()).andReturn(mockSerializer);
    expect(mockGraph.getEdgeSerializer()).andReturn(mockEdgeSerializer);
    expect(mockGraph.getIndexSerializer()).andReturn(mockIndexSerializer);
    expect(mockGraph.getIDManager()).andReturn(idManager);
    expect(gdbConfig.getTimestampProvider()).andReturn(tsProvider);
    expect(txConfig.isSingleThreaded()).andReturn(true);
    expect(txConfig.hasPreloadedData()).andReturn(false);
    expect(txConfig.hasVerifyExternalVertexExistence()).andReturn(false);
    expect(txConfig.hasVerifyInternalVertexExistence()).andReturn(false);
    expect(txConfig.getVertexCacheSize()).andReturn(6);
    expect(txConfig.isReadOnly()).andReturn(true);
    expect(txConfig.getDirtyVertexSize()).andReturn(2);
    expect(txConfig.getIndexCacheWeight()).andReturn(2L);
    expect(txConfig.getGroupName()).andReturn(null);
    expect(txConfig.getAutoSchemaMaker()).andReturn(defaultSchemaMaker);
    expect(defaultSchemaMaker.makePropertyKey(isA(PropertyKeyMaker.class), notNull())).andReturn(propertyKey);
    expect(relationType.isPropertyKey()).andReturn(false);
    expect(propertyKey.isPropertyKey()).andReturn(true);
    replayAll();
    StandardJanusGraphTx partialMock = createMockBuilder(StandardJanusGraphTx.class).withConstructor(mockGraph, txConfig).addMockedMethod("getRelationType").createMock();
    expect(partialMock.getRelationType("Foo")).andReturn(null);
    expect(partialMock.getRelationType("Qux")).andReturn(propertyKey);
    expect(partialMock.getRelationType("Baz")).andReturn(relationType);
    replay(partialMock);
    return partialMock;
}
Also used : IndexSerializer(org.janusgraph.graphdb.database.IndexSerializer) IDManager(org.janusgraph.graphdb.idmanagement.IDManager) GraphDatabaseConfiguration(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration) PropertyKeyMaker(org.janusgraph.core.schema.PropertyKeyMaker) TimestampProvider(org.janusgraph.diskstorage.util.time.TimestampProvider) DefaultSchemaMaker(org.janusgraph.core.schema.DefaultSchemaMaker) RelationType(org.janusgraph.core.RelationType) EdgeSerializer(org.janusgraph.graphdb.database.EdgeSerializer) PropertyKey(org.janusgraph.core.PropertyKey) StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph) EdgeSerializer(org.janusgraph.graphdb.database.EdgeSerializer) IndexSerializer(org.janusgraph.graphdb.database.IndexSerializer) Serializer(org.janusgraph.graphdb.database.serialize.Serializer)

Aggregations

EdgeSerializer (org.janusgraph.graphdb.database.EdgeSerializer)2 IndexSerializer (org.janusgraph.graphdb.database.IndexSerializer)2 ImmutableList (com.google.common.collect.ImmutableList)1 PropertyKey (org.janusgraph.core.PropertyKey)1 RelationType (org.janusgraph.core.RelationType)1 DefaultSchemaMaker (org.janusgraph.core.schema.DefaultSchemaMaker)1 PropertyKeyMaker (org.janusgraph.core.schema.PropertyKeyMaker)1 BackendTransaction (org.janusgraph.diskstorage.BackendTransaction)1 Entry (org.janusgraph.diskstorage.Entry)1 StaticBuffer (org.janusgraph.diskstorage.StaticBuffer)1 IndexEntry (org.janusgraph.diskstorage.indexing.IndexEntry)1 TimestampProvider (org.janusgraph.diskstorage.util.time.TimestampProvider)1 GraphDatabaseConfiguration (org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration)1 StandardJanusGraph (org.janusgraph.graphdb.database.StandardJanusGraph)1 RelationTypeIndexWrapper (org.janusgraph.graphdb.database.management.RelationTypeIndexWrapper)1 Serializer (org.janusgraph.graphdb.database.serialize.Serializer)1 IDManager (org.janusgraph.graphdb.idmanagement.IDManager)1 InternalRelation (org.janusgraph.graphdb.internal.InternalRelation)1 InternalRelationType (org.janusgraph.graphdb.internal.InternalRelationType)1 CompositeIndexType (org.janusgraph.graphdb.types.CompositeIndexType)1