Search in sources :

Example 1 with RelationIdentifier

use of org.janusgraph.graphdb.relations.RelationIdentifier in project janusgraph by JanusGraph.

the class StandardTransactionLogProcessor method restoreExternalIndexes.

private void restoreExternalIndexes(Predicate<String> isFailedIndex, TransactionLogHeader.Entry entry) {
    // 1) Collect all elements (vertices and relations) and the indexes for which they need to be restored
    SetMultimap<String, IndexRestore> indexRestores = HashMultimap.create();
    BackendOperation.execute(() -> {
        final StandardJanusGraphTx tx = (StandardJanusGraphTx) graph.newTransaction();
        try {
            entry.getContentAsModifications(serializer).stream().map(m -> ModificationDeserializer.parseRelation(m, tx)).forEach(rel -> {
                // Collect affected vertex indexes
                for (final MixedIndexType index : getMixedIndexes(rel.getType())) {
                    if (index.getElement() == ElementCategory.VERTEX && isFailedIndex.apply(index.getBackingIndexName())) {
                        assert rel.isProperty();
                        indexRestores.put(index.getBackingIndexName(), new IndexRestore(rel.getVertex(0).longId(), ElementCategory.VERTEX, getIndexId(index)));
                    }
                }
                // See if relation itself is affected
                for (final RelationType relType : rel.getPropertyKeysDirect()) {
                    for (final MixedIndexType index : getMixedIndexes(relType)) {
                        if (index.getElement().isInstance(rel) && isFailedIndex.apply(index.getBackingIndexName())) {
                            assert rel.id() instanceof RelationIdentifier;
                            indexRestores.put(index.getBackingIndexName(), new IndexRestore(rel.id(), ElementCategory.getByClazz(rel.getClass()), getIndexId(index)));
                        }
                    }
                }
            });
        } finally {
            if (tx.isOpen())
                tx.rollback();
        }
        return true;
    }, readTime);
    // 2) Restore elements per backing index
    for (final String indexName : indexRestores.keySet()) {
        final StandardJanusGraphTx tx = (StandardJanusGraphTx) graph.newTransaction();
        try {
            BackendTransaction btx = tx.getTxHandle();
            final IndexTransaction indexTx = btx.getIndexTransaction(indexName);
            BackendOperation.execute(new Callable<Boolean>() {

                @Override
                public Boolean call() throws Exception {
                    Map<String, Map<String, List<IndexEntry>>> restoredDocs = Maps.newHashMap();
                    indexRestores.get(indexName).forEach(restore -> {
                        JanusGraphSchemaVertex indexV = (JanusGraphSchemaVertex) tx.getVertex(restore.indexId);
                        MixedIndexType index = (MixedIndexType) indexV.asIndexType();
                        JanusGraphElement element = restore.retrieve(tx);
                        if (element != null) {
                            graph.getIndexSerializer().reindexElement(element, index, restoredDocs);
                        } else {
                            // Element is deleted
                            graph.getIndexSerializer().removeElement(restore.elementId, index, restoredDocs);
                        }
                    });
                    indexTx.restore(restoredDocs);
                    indexTx.commit();
                    return true;
                }

                @Override
                public String toString() {
                    return "IndexMutation";
                }
            }, persistenceTime);
        } finally {
            if (tx.isOpen())
                tx.rollback();
        }
    }
}
Also used : ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) Log(org.janusgraph.diskstorage.log.Log) StandardJanusGraphTx(org.janusgraph.graphdb.transaction.StandardJanusGraphTx) MessageReader(org.janusgraph.diskstorage.log.MessageReader) BackendOperation(org.janusgraph.diskstorage.util.BackendOperation) LoggerFactory(org.slf4j.LoggerFactory) JanusGraphTransaction(org.janusgraph.core.JanusGraphTransaction) Future(java.util.concurrent.Future) HashMultimap(com.google.common.collect.HashMultimap) Serializer(org.janusgraph.graphdb.database.serialize.Serializer) IndexEntry(org.janusgraph.diskstorage.indexing.IndexEntry) Duration(java.time.Duration) Map(java.util.Map) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) IndexType(org.janusgraph.graphdb.types.IndexType) LogTxStatus(org.janusgraph.graphdb.database.log.LogTxStatus) TransactionRecovery(org.janusgraph.core.log.TransactionRecovery) RelationType(org.janusgraph.core.RelationType) TimestampProvider(org.janusgraph.diskstorage.util.time.TimestampProvider) StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph) JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex) LogTxMeta(org.janusgraph.graphdb.database.log.LogTxMeta) Instant(java.time.Instant) Objects(java.util.Objects) List(java.util.List) RelationIdentifier(org.janusgraph.graphdb.relations.RelationIdentifier) Predicate(com.google.common.base.Predicate) CacheBuilder(com.google.common.cache.CacheBuilder) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) Iterables(com.google.common.collect.Iterables) SchemaSource(org.janusgraph.graphdb.types.SchemaSource) BackgroundThread(org.janusgraph.util.system.BackgroundThread) IndexTransaction(org.janusgraph.diskstorage.indexing.IndexTransaction) BackendTransaction(org.janusgraph.diskstorage.BackendTransaction) Callable(java.util.concurrent.Callable) IndexTypeWrapper(org.janusgraph.graphdb.types.indextype.IndexTypeWrapper) Predicates(com.google.common.base.Predicates) JanusGraphException(org.janusgraph.core.JanusGraphException) Message(org.janusgraph.diskstorage.log.Message) JanusGraphElement(org.janusgraph.core.JanusGraphElement) TransactionLogHeader(org.janusgraph.graphdb.database.log.TransactionLogHeader) Logger(org.slf4j.Logger) Maps(com.google.common.collect.Maps) SetMultimap(com.google.common.collect.SetMultimap) ElementCategory(org.janusgraph.graphdb.internal.ElementCategory) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) RemovalCause(com.google.common.cache.RemovalCause) GraphDatabaseConfiguration(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration) Preconditions(com.google.common.base.Preconditions) RemovalListener(com.google.common.cache.RemovalListener) Cache(com.google.common.cache.Cache) ReadMarker(org.janusgraph.diskstorage.log.ReadMarker) Collections(java.util.Collections) MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) IndexTransaction(org.janusgraph.diskstorage.indexing.IndexTransaction) RelationIdentifier(org.janusgraph.graphdb.relations.RelationIdentifier) JanusGraphException(org.janusgraph.core.JanusGraphException) ExecutionException(java.util.concurrent.ExecutionException) JanusGraphElement(org.janusgraph.core.JanusGraphElement) StandardJanusGraphTx(org.janusgraph.graphdb.transaction.StandardJanusGraphTx) RelationType(org.janusgraph.core.RelationType) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex) List(java.util.List) Map(java.util.Map) BackendTransaction(org.janusgraph.diskstorage.BackendTransaction)

Example 2 with RelationIdentifier

use of org.janusgraph.graphdb.relations.RelationIdentifier in project janusgraph by JanusGraph.

the class JanusGraphTest method testImplicitKey.

/**
 * Test the correct application of {@link org.janusgraph.graphdb.types.system.ImplicitKey}
 * to vertices, edges, and properties.
 * <p>
 * Additionally tests RelationIdentifier since this is closely related to ADJACENT and JANUSGRAPHID implicit keys.
 */
@Test
public void testImplicitKey() {
    JanusGraphVertex v = graph.addVertex("name", "Dan"), u = graph.addVertex();
    Edge e = v.addEdge("knows", u);
    graph.tx().commit();
    RelationIdentifier eid = (RelationIdentifier) e.id();
    assertEquals(v.id(), v.value(ID_NAME));
    assertEquals(eid, e.value(ID_NAME));
    assertEquals("knows", e.value(LABEL_NAME));
    assertEquals(BaseVertexLabel.DEFAULT_VERTEXLABEL.name(), v.value(LABEL_NAME));
    assertCount(1, v.query().direction(Direction.BOTH).labels("knows").has(ID_NAME, eid).edges());
    assertCount(0, v.query().direction(Direction.BOTH).labels("knows").has(ID_NAME, RelationIdentifier.get(new long[] { 4, 5, 6, 7 })).edges());
    assertCount(1, v.query().direction(Direction.BOTH).labels("knows").has("~nid", eid.getRelationId()).edges());
    assertCount(0, v.query().direction(Direction.BOTH).labels("knows").has("~nid", 110111).edges());
    // Test edge retrieval
    assertNotNull(getE(graph, eid));
    assertEquals(eid, getE(graph, eid).id());
    // Test adjacent constraint
    assertEquals(1, v.query().direction(BOTH).has("~adjacent", u.id()).edgeCount());
    assertCount(1, v.query().direction(BOTH).has("~adjacent", (int) getId(u)).edges());
    try {
        // Not a valid vertex
        assertCount(0, v.query().direction(BOTH).has("~adjacent", 110111).edges());
        fail();
    } catch (IllegalArgumentException ignored) {
    }
}
Also used : JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) RelationIdentifier(org.janusgraph.graphdb.relations.RelationIdentifier) AbstractEdge(org.janusgraph.graphdb.relations.AbstractEdge) JanusGraphEdge(org.janusgraph.core.JanusGraphEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 3 with RelationIdentifier

use of org.janusgraph.graphdb.relations.RelationIdentifier in project janusgraph by JanusGraph.

the class IndexSerializer method getIndexEntry.

private Entry getIndexEntry(CompositeIndexType index, RecordEntry[] record, JanusGraphElement element) {
    final DataOutput out = serializer.getDataOutput(1 + 8 + 8 * record.length + 4 * 8);
    out.putByte(FIRST_INDEX_COLUMN_BYTE);
    if (index.getCardinality() != Cardinality.SINGLE) {
        VariableLong.writePositive(out, element.longId());
        if (index.getCardinality() != Cardinality.SET) {
            for (final RecordEntry re : record) {
                VariableLong.writePositive(out, re.relationId);
            }
        }
    }
    final int valuePosition = out.getPosition();
    if (element instanceof JanusGraphVertex) {
        VariableLong.writePositive(out, element.longId());
    } else {
        assert element instanceof JanusGraphRelation;
        final RelationIdentifier rid = (RelationIdentifier) element.id();
        final long[] longs = rid.getLongRepresentation();
        Preconditions.checkArgument(longs.length == 3 || longs.length == 4);
        for (final long aLong : longs) VariableLong.writePositive(out, aLong);
    }
    return new StaticArrayEntry(out.getStaticBuffer(), valuePosition);
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) JanusGraphRelation(org.janusgraph.core.JanusGraphRelation) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) RelationIdentifier(org.janusgraph.graphdb.relations.RelationIdentifier) StaticArrayEntry(org.janusgraph.diskstorage.util.StaticArrayEntry)

Example 4 with RelationIdentifier

use of org.janusgraph.graphdb.relations.RelationIdentifier in project janusgraph by JanusGraph.

the class RelationIdentifierGraphBinarySerializerTest method readValueAndWriteValueShouldBeSymmetric.

@ParameterizedTest
@MethodSource("relationIdentifierProvider")
public void readValueAndWriteValueShouldBeSymmetric(final RelationIdentifier relationIdentifier) throws IOException {
    final TypeSerializerRegistry registry = TypeSerializerRegistry.build().addCustomType(RelationIdentifier.class, new RelationIdentifierGraphBinarySerializer()).create();
    final GraphBinaryReader reader = new GraphBinaryReader(registry);
    final GraphBinaryWriter writer = new GraphBinaryWriter(registry);
    for (boolean nullable : new boolean[] { true, false }) {
        final Buffer buffer = bufferFactory.create(allocator.buffer());
        writer.writeValue(relationIdentifier, buffer, nullable);
        final RelationIdentifier actual = reader.readValue(buffer, RelationIdentifier.class, nullable);
        assertEquals(actual.toString(), relationIdentifier.toString());
        buffer.release();
    }
}
Also used : Buffer(org.apache.tinkerpop.gremlin.structure.io.Buffer) GraphBinaryWriter(org.apache.tinkerpop.gremlin.structure.io.binary.GraphBinaryWriter) TypeSerializerRegistry(org.apache.tinkerpop.gremlin.structure.io.binary.TypeSerializerRegistry) GraphBinaryReader(org.apache.tinkerpop.gremlin.structure.io.binary.GraphBinaryReader) RelationIdentifier(org.janusgraph.graphdb.relations.RelationIdentifier) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 5 with RelationIdentifier

use of org.janusgraph.graphdb.relations.RelationIdentifier in project janusgraph by JanusGraph.

the class JanusGraphSerializerBaseIT method testRelationIdentifier.

@Test
public void testRelationIdentifier(TestInfo testInfo) {
    GraphTraversalSource g = traversal();
    RelationIdentifier inputId = (RelationIdentifier) g.addV(testInfo.getDisplayName()).as("from").addV(testInfo.getDisplayName()).addE(testInfo.getDisplayName()).from("from").id().next();
    RelationIdentifier outputId = (RelationIdentifier) g.E(inputId).id().next();
    assertEquals(inputId, outputId);
}
Also used : GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) RelationIdentifier(org.janusgraph.graphdb.relations.RelationIdentifier) Test(org.junit.jupiter.api.Test)

Aggregations

RelationIdentifier (org.janusgraph.graphdb.relations.RelationIdentifier)9 Test (org.junit.jupiter.api.Test)3 GraphTraversalSource (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource)2 Edge (org.apache.tinkerpop.gremlin.structure.Edge)2 JanusGraphEdge (org.janusgraph.core.JanusGraphEdge)2 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 LongArrayList (com.carrotsearch.hppc.LongArrayList)1 Preconditions (com.google.common.base.Preconditions)1 Predicate (com.google.common.base.Predicate)1 Predicates (com.google.common.base.Predicates)1 Cache (com.google.common.cache.Cache)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 RemovalCause (com.google.common.cache.RemovalCause)1 RemovalListener (com.google.common.cache.RemovalListener)1 HashMultimap (com.google.common.collect.HashMultimap)1 Iterables (com.google.common.collect.Iterables)1 Maps (com.google.common.collect.Maps)1 SetMultimap (com.google.common.collect.SetMultimap)1 ByteBuf (io.netty.buffer.ByteBuf)1