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();
}
}
}
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) {
}
}
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);
}
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();
}
}
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);
}
Aggregations