Search in sources :

Example 1 with JanusGraphIndex

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);
}
Also used : JanusGraphManagement(org.janusgraph.core.schema.JanusGraphManagement) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) CredentialGraph(org.apache.tinkerpop.gremlin.groovy.jsr223.dsl.credential.CredentialGraph) HashMap(java.util.HashMap) JanusGraph(org.janusgraph.core.JanusGraph) ManagementSystem(org.janusgraph.graphdb.database.management.ManagementSystem) PropertyKeyMaker(org.janusgraph.core.schema.PropertyKeyMaker) Transaction(org.apache.tinkerpop.gremlin.structure.Transaction) SimpleAuthenticator(org.apache.tinkerpop.gremlin.server.auth.SimpleAuthenticator) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 2 with JanusGraphIndex

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);
}
Also used : JanusGraphManagement(org.janusgraph.core.schema.JanusGraphManagement) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) CredentialGraph(org.apache.tinkerpop.gremlin.groovy.jsr223.dsl.credential.CredentialGraph) HashMap(java.util.HashMap) JanusGraph(org.janusgraph.core.JanusGraph) ManagementSystem(org.janusgraph.graphdb.database.management.ManagementSystem) PropertyKeyMaker(org.janusgraph.core.schema.PropertyKeyMaker) Transaction(org.apache.tinkerpop.gremlin.structure.Transaction) SimpleAuthenticator(org.apache.tinkerpop.gremlin.server.auth.SimpleAuthenticator) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 3 with JanusGraphIndex

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;
}
Also used : JanusGraphManagement(org.janusgraph.core.schema.JanusGraphManagement) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) HashSet(java.util.HashSet)

Example 4 with JanusGraphIndex

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);
    }
}
Also used : InternalVertex(org.janusgraph.graphdb.internal.InternalVertex) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) StringUtils(org.janusgraph.util.StringUtils) BaseVertexQuery(org.janusgraph.core.BaseVertexQuery) BackendTransaction(org.janusgraph.diskstorage.BackendTransaction) HashMap(java.util.HashMap) SchemaAction(org.janusgraph.core.schema.SchemaAction) IndexSerializer(org.janusgraph.graphdb.database.IndexSerializer) ArrayList(java.util.ArrayList) ScanMetrics(org.janusgraph.diskstorage.keycolumnvalue.scan.ScanMetrics) IndexEntry(org.janusgraph.diskstorage.indexing.IndexEntry) SchemaStatus(org.janusgraph.core.schema.SchemaStatus) QueryContainer(org.janusgraph.graphdb.olap.QueryContainer) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) InternalRelation(org.janusgraph.graphdb.internal.InternalRelation) Map(java.util.Map) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) JanusGraphException(org.janusgraph.core.JanusGraphException) IndexType(org.janusgraph.graphdb.types.IndexType) JanusGraphElement(org.janusgraph.core.JanusGraphElement) JanusGraphSchemaType(org.janusgraph.core.schema.JanusGraphSchemaType) BackendException(org.janusgraph.diskstorage.BackendException) RelationType(org.janusgraph.core.RelationType) VertexScanJob(org.janusgraph.graphdb.olap.VertexScanJob) CompositeIndexType(org.janusgraph.graphdb.types.CompositeIndexType) PropertyKey(org.janusgraph.core.PropertyKey) KCVSCache(org.janusgraph.diskstorage.keycolumnvalue.cache.KCVSCache) BaseLabel(org.janusgraph.graphdb.types.system.BaseLabel) Set(java.util.Set) JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex) EdgeSerializer(org.janusgraph.graphdb.database.EdgeSerializer) EdgeDirection(org.janusgraph.graphdb.relations.EdgeDirection) Direction(org.apache.tinkerpop.gremlin.structure.Direction) List(java.util.List) Entry(org.janusgraph.diskstorage.Entry) Preconditions(com.google.common.base.Preconditions) RelationTypeIndex(org.janusgraph.core.schema.RelationTypeIndex) RelationTypeIndexWrapper(org.janusgraph.graphdb.database.management.RelationTypeIndexWrapper) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) Collections(java.util.Collections) Set(java.util.Set) HashMap(java.util.HashMap) JanusGraphException(org.janusgraph.core.JanusGraphException) ArrayList(java.util.ArrayList) InternalRelation(org.janusgraph.graphdb.internal.InternalRelation) RelationTypeIndex(org.janusgraph.core.schema.RelationTypeIndex) IndexEntry(org.janusgraph.diskstorage.indexing.IndexEntry) Entry(org.janusgraph.diskstorage.Entry) JanusGraphElement(org.janusgraph.core.JanusGraphElement) EdgeSerializer(org.janusgraph.graphdb.database.EdgeSerializer) RelationTypeIndexWrapper(org.janusgraph.graphdb.database.management.RelationTypeIndexWrapper) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) ArrayList(java.util.ArrayList) List(java.util.List) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) 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) JanusGraphException(org.janusgraph.core.JanusGraphException) BackendException(org.janusgraph.diskstorage.BackendException) InternalVertex(org.janusgraph.graphdb.internal.InternalVertex) CompositeIndexType(org.janusgraph.graphdb.types.CompositeIndexType) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with JanusGraphIndex

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);
}
Also used : CompositeIndexType(org.janusgraph.graphdb.types.CompositeIndexType) JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) RelationTypeIndex(org.janusgraph.core.schema.RelationTypeIndex) SchemaStatus(org.janusgraph.core.schema.SchemaStatus)

Aggregations

JanusGraphIndex (org.janusgraph.core.schema.JanusGraphIndex)55 PropertyKey (org.janusgraph.core.PropertyKey)42 Test (org.junit.jupiter.api.Test)25 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)20 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)19 JanusGraphManagement (org.janusgraph.core.schema.JanusGraphManagement)17 RelationTypeIndex (org.janusgraph.core.schema.RelationTypeIndex)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 HashMap (java.util.HashMap)10 VertexLabel (org.janusgraph.core.VertexLabel)10 JanusGraph (org.janusgraph.core.JanusGraph)9 JanusGraphException (org.janusgraph.core.JanusGraphException)9 RepeatedIfExceptionsTest (io.github.artsok.RepeatedIfExceptionsTest)8 EdgeLabel (org.janusgraph.core.EdgeLabel)8 BaseVertexLabel (org.janusgraph.graphdb.types.system.BaseVertexLabel)8 BackendException (org.janusgraph.diskstorage.BackendException)7 ScanMetrics (org.janusgraph.diskstorage.keycolumnvalue.scan.ScanMetrics)7 ManagementSystem (org.janusgraph.graphdb.database.management.ManagementSystem)7 CompositeIndexType (org.janusgraph.graphdb.types.CompositeIndexType)7 JanusGraphSchemaVertex (org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex)7