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