use of org.janusgraph.core.schema.JanusGraphIndex in project janusgraph by JanusGraph.
the class SaslAndHMACAuthenticatorTest method testSetupEmptyCredGraphNoUserIndex.
@Test
public void testSetupEmptyCredGraphNoUserIndex() {
final SaslAndHMACAuthenticator authenticator = createMockBuilder(SaslAndHMACAuthenticator.class).addMockedMethod("openGraph").addMockedMethod("createCredentialGraph").addMockedMethod("createSimpleAuthenticator").addMockedMethod("createHMACAuthenticator").createMock();
final Map<String, Object> configMap = new HashMap<String, Object>();
configMap.put(CONFIG_CREDENTIALS_DB, "configCredDb");
configMap.put(SaslAndHMACAuthenticator.CONFIG_DEFAULT_PASSWORD, "pass");
configMap.put(SaslAndHMACAuthenticator.CONFIG_DEFAULT_USER, "user");
final JanusGraph graph = createMock(JanusGraph.class);
final CredentialGraph credentialGraph = createMock(CredentialGraph.class);
final ManagementSystem mgmt = createMock(ManagementSystem.class);
final JanusGraphSimpleAuthenticator janusSimpleAuthenticator = createMock(JanusGraphSimpleAuthenticator.class);
final HMACAuthenticator hmacAuthenticator = createMock(HMACAuthenticator.class);
final SimpleAuthenticator simpleAuthenticator = createMock(SimpleAuthenticator.class);
final Transaction tx = createMock(Transaction.class);
final PropertyKey pk = createMock(PropertyKey.class);
final PropertyKeyMaker pkm = createMock(PropertyKeyMaker.class);
final JanusGraphManagement.IndexBuilder indexBuilder = createMock(JanusGraphManagement.IndexBuilder.class);
final JanusGraphIndex index = createMock(JanusGraphIndex.class);
final PropertyKey[] pks = { pk };
expect(authenticator.openGraph(isA(String.class))).andReturn(graph);
expect(authenticator.createCredentialGraph(isA(JanusGraph.class))).andReturn(credentialGraph);
expect(authenticator.createSimpleAuthenticator()).andReturn(janusSimpleAuthenticator);
expect(authenticator.createHMACAuthenticator()).andReturn(hmacAuthenticator);
hmacAuthenticator.setup(configMap);
expectLastCall();
expect(janusSimpleAuthenticator.createSimpleAuthenticator()).andReturn(simpleAuthenticator);
simpleAuthenticator.setup(configMap);
expectLastCall();
expect(credentialGraph.findUser("user")).andReturn(null);
expect(credentialGraph.createUser(eq("user"), eq("pass"))).andReturn(null);
expect(graph.openManagement()).andReturn(mgmt).times(2);
expect(graph.tx()).andReturn(tx);
expect(index.getFieldKeys()).andReturn(pks);
expect(index.getIndexStatus(eq(pk))).andReturn(SchemaStatus.ENABLED);
tx.rollback();
expectLastCall();
expect(mgmt.containsGraphIndex(eq("byUsername"))).andReturn(false);
expect(mgmt.makePropertyKey(PROPERTY_USERNAME)).andReturn(pkm);
expect(pkm.dataType(eq(String.class))).andReturn(pkm);
expect(pkm.cardinality(Cardinality.SINGLE)).andReturn(pkm);
expect(pkm.make()).andReturn(pk);
expect(mgmt.buildIndex(eq("byUsername"), eq(Vertex.class))).andReturn(indexBuilder);
expect(mgmt.getGraphIndex(eq("byUsername"))).andReturn(index);
expect(indexBuilder.addKey(eq(pk))).andReturn(indexBuilder);
expect(indexBuilder.unique()).andReturn(indexBuilder);
expect(indexBuilder.buildCompositeIndex()).andReturn(index);
mgmt.commit();
expectLastCall();
mgmt.rollback();
expectLastCall();
replayAll();
authenticator.setup(configMap);
}
use of org.janusgraph.core.schema.JanusGraphIndex in project janusgraph by JanusGraph.
the class JanusGraphSimpleAuthenticatorTest method testSetupEmptyCredGraphNoUserIndex.
@Test
public void testSetupEmptyCredGraphNoUserIndex() {
final JanusGraphSimpleAuthenticator authenticator = createMockBuilder(JanusGraphSimpleAuthenticator.class).addMockedMethod("openGraph").addMockedMethod("createCredentialGraph").addMockedMethod("createSimpleAuthenticator").createMock();
final Map<String, Object> configMap = new HashMap<String, Object>();
configMap.put(CONFIG_CREDENTIALS_DB, "configCredDb");
configMap.put(JanusGraphSimpleAuthenticator.CONFIG_DEFAULT_PASSWORD, "pass");
configMap.put(JanusGraphSimpleAuthenticator.CONFIG_DEFAULT_USER, "user");
final JanusGraph graph = createMock(JanusGraph.class);
final CredentialGraph credentialGraph = createMock(CredentialGraph.class);
final ManagementSystem mgmt = createMock(ManagementSystem.class);
final Transaction tx = createMock(Transaction.class);
final PropertyKey pk = createMock(PropertyKey.class);
final PropertyKeyMaker pkm = createMock(PropertyKeyMaker.class);
final JanusGraphManagement.IndexBuilder indexBuilder = createMock(JanusGraphManagement.IndexBuilder.class);
final JanusGraphIndex index = createMock(JanusGraphIndex.class);
final PropertyKey[] pks = { pk };
final SimpleAuthenticator sa = createMock(SimpleAuthenticator.class);
expect(authenticator.openGraph(isA(String.class))).andReturn(graph);
expect(authenticator.createCredentialGraph(isA(JanusGraph.class))).andReturn(credentialGraph);
expect(authenticator.createSimpleAuthenticator()).andReturn(sa);
expect(credentialGraph.findUser("user")).andReturn(null);
expect(credentialGraph.createUser(eq("user"), eq("pass"))).andReturn(null);
expect(graph.openManagement()).andReturn(mgmt);
expect(graph.tx()).andReturn(tx);
sa.setup(configMap);
EasyMock.expectLastCall();
graph.close();
EasyMock.expectLastCall();
tx.rollback();
EasyMock.expectLastCall();
expect(mgmt.containsGraphIndex(eq("byUsername"))).andReturn(false);
expect(mgmt.makePropertyKey(PROPERTY_USERNAME)).andReturn(pkm);
expect(pkm.dataType(eq(String.class))).andReturn(pkm);
expect(pkm.cardinality(Cardinality.SINGLE)).andReturn(pkm);
expect(pkm.make()).andReturn(pk);
expect(mgmt.buildIndex(eq("byUsername"), eq(Vertex.class))).andReturn(indexBuilder);
expect(mgmt.getGraphIndex(eq("byUsername"))).andReturn(index);
expect(indexBuilder.addKey(eq(pk))).andReturn(indexBuilder);
expect(indexBuilder.unique()).andReturn(indexBuilder);
expect(indexBuilder.buildCompositeIndex()).andReturn(index);
expect(index.getFieldKeys()).andReturn(pks);
expect(index.getIndexStatus(eq(pk))).andReturn(SchemaStatus.ENABLED);
expect(graph.tx()).andReturn(tx);
expect(graph.openManagement()).andReturn(mgmt);
tx.rollback();
EasyMock.expectLastCall().atLeastOnce();
mgmt.commit();
EasyMock.expectLastCall();
mgmt.rollback();
EasyMock.expectLastCall();
replayAll();
authenticator.setup(configMap);
}
use of org.janusgraph.core.schema.JanusGraphIndex in project atlas by apache.
the class AtlasJanusGraph method getIndexKeys.
private Set<String> getIndexKeys(Class<? extends Element> janusGraphElementClass) {
JanusGraphManagement mgmt = getGraph().openManagement();
Iterable<JanusGraphIndex> indices = mgmt.getGraphIndexes(janusGraphElementClass);
Set<String> result = new HashSet<String>();
for (JanusGraphIndex index : indices) {
result.add(index.name());
}
mgmt.commit();
return result;
}
use of org.janusgraph.core.schema.JanusGraphIndex 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> outAdditions = new ArrayList<>();
Map<StaticBuffer, List<Entry>> inAdditionsMap = new HashMap<>();
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);
if (pos == 0) {
outAdditions.add(entry);
} else {
assert pos == 1;
InternalVertex otherVertex = janusgraphRelation.getVertex(1);
StaticBuffer otherVertexKey = writeTx.getIdInspector().getKey(otherVertex.longId());
inAdditionsMap.computeIfAbsent(otherVertexKey, k -> new ArrayList<>()).add(entry);
}
}
}
// Mutating all OUT relationships for the current vertex
StaticBuffer vertexKey = writeTx.getIdInspector().getKey(vertex.longId());
mutator.mutateEdges(vertexKey, outAdditions, KCVSCache.NO_DELETIONS);
// Mutating all IN relationships for the current vertex
int totalInAdditions = 0;
for (Map.Entry<StaticBuffer, List<Entry>> entry : inAdditionsMap.entrySet()) {
StaticBuffer otherVertexKey = entry.getKey();
List<Entry> inAdditions = entry.getValue();
totalInAdditions += inAdditions.size();
mutator.mutateEdges(otherVertexKey, inAdditions, KCVSCache.NO_DELETIONS);
}
metrics.incrementCustom(ADDED_RECORDS_COUNT, outAdditions.size() + totalInAdditions);
} 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 = Collections.singletonList(vertex);
break;
case PROPERTY:
elements = new ArrayList<>();
addIndexSchemaConstraint(vertex.query(), indexType).properties().forEach(elements::add);
break;
case EDGE:
elements = new ArrayList<>();
addIndexSchemaConstraint(vertex.query().direction(Direction.OUT), indexType).edges().forEach(elements::add);
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(), new ArrayList<Entry>(1) {
{
add(update.getEntry());
}
}, KCVSCache.NO_DELETIONS);
metrics.incrementCustom(ADDED_RECORDS_COUNT);
}
}
} else {
assert indexType.isMixedIndex();
for (JanusGraphElement element : elements) {
if (indexSerializer.reindexElement(element, (MixedIndexType) indexType, documentsPerStore)) {
metrics.incrementCustom(DOCUMENT_UPDATES_COUNT);
}
}
}
} 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.core.schema.JanusGraphIndex in project janusgraph by JanusGraph.
the class IndexRemoveJob method validateIndexStatus.
@Override
protected void validateIndexStatus() {
if (!(index instanceof RelationTypeIndex || index instanceof JanusGraphIndex)) {
throw new UnsupportedOperationException("Unsupported index found: " + index);
}
if (index instanceof JanusGraphIndex) {
JanusGraphIndex graphIndex = (JanusGraphIndex) index;
if (graphIndex.isMixedIndex())
throw new UnsupportedOperationException("Cannot remove mixed indexes through JanusGraph. This can " + "only be accomplished in the indexing system directly.");
CompositeIndexType indexType = (CompositeIndexType) managementSystem.getSchemaVertex(index).asIndexType();
graphIndexId = indexType.getID();
}
// Must be a relation type index or a composite graph index
JanusGraphSchemaVertex schemaVertex = managementSystem.getSchemaVertex(index);
SchemaStatus actualStatus = schemaVertex.getStatus();
Preconditions.checkArgument(actualStatus == SchemaStatus.DISABLED, "The index [%s] must be disabled before it can be removed", indexName);
}
Aggregations