Search in sources :

Example 41 with JanusGraphIndex

use of org.janusgraph.core.schema.JanusGraphIndex in project janusgraph by JanusGraph.

the class IndexRepairJob method workerIterationEnd.

@Override
public void workerIterationEnd(final ScanMetrics metrics) {
    try {
        if (index instanceof JanusGraphIndex) {
            BackendTransaction mutator = writeTx.getTxHandle();
            IndexType indexType = managementSystem.getSchemaVertex(index).asIndexType();
            if (indexType.isMixedIndex() && documentsPerStore.size() > 0) {
                mutator.getIndexTransaction(indexType.getBackingIndexName()).restore(documentsPerStore);
                documentsPerStore = new HashMap<>();
            }
        }
    } catch (BackendException e) {
        throw new JanusGraphException(e.getMessage(), e);
    } finally {
        super.workerIterationEnd(metrics);
    }
}
Also used : JanusGraphException(org.janusgraph.core.JanusGraphException) 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) BackendException(org.janusgraph.diskstorage.BackendException)

Example 42 with JanusGraphIndex

use of org.janusgraph.core.schema.JanusGraphIndex in project janusgraph by JanusGraph.

the class JanusGraphAbstractAuthenticator method setup.

@Override
public void setup(final Map<String, Object> config) {
    logger.info("Initializing authentication with the {}", this.getClass().getName());
    Preconditions.checkArgument(config != null, "Could not configure a %s - provide a 'config' in the 'authentication' settings", this.getClass().getName());
    Preconditions.checkState(config.containsKey(CONFIG_CREDENTIALS_DB), "Credential configuration missing the %s key that points to a graph config file or graph name", CONFIG_CREDENTIALS_DB);
    Preconditions.checkState(config.containsKey(CONFIG_DEFAULT_USER), "Credential configuration missing the %s key for the default user", CONFIG_DEFAULT_USER);
    Preconditions.checkState(config.containsKey(CONFIG_DEFAULT_PASSWORD), "Credential configuration missing the %s key for the default password", CONFIG_DEFAULT_PASSWORD);
    final JanusGraph graph = openGraph(config.get(CONFIG_CREDENTIALS_DB).toString());
    credentials = createCredentials(graph);
    graph.tx().rollback();
    ManagementSystem mgmt = (ManagementSystem) graph.openManagement();
    if (!mgmt.containsGraphIndex(USERNAME_INDEX_NAME)) {
        final PropertyKey username = mgmt.makePropertyKey(PROPERTY_USERNAME).dataType(String.class).cardinality(Cardinality.SINGLE).make();
        mgmt.buildIndex(USERNAME_INDEX_NAME, Vertex.class).addKey(username).unique().buildCompositeIndex();
        mgmt.commit();
        mgmt = (ManagementSystem) graph.openManagement();
        final JanusGraphIndex index = mgmt.getGraphIndex(USERNAME_INDEX_NAME);
        if (!index.getIndexStatus(username).equals(SchemaStatus.ENABLED)) {
            try {
                mgmt = (ManagementSystem) graph.openManagement();
                mgmt.updateIndex(mgmt.getGraphIndex(USERNAME_INDEX_NAME), SchemaAction.REINDEX);
                ManagementSystem.awaitGraphIndexStatus(graph, USERNAME_INDEX_NAME).status(SchemaStatus.ENABLED).call();
                mgmt.commit();
            } catch (InterruptedException rude) {
                mgmt.rollback();
                throw new RuntimeException("Timed out waiting for byUsername index to be created on credential graph", rude);
            }
        }
    }
    final String defaultUser = config.get(CONFIG_DEFAULT_USER).toString();
    if (!userExists(defaultUser)) {
        createUser(defaultUser, config.get(CONFIG_DEFAULT_PASSWORD).toString());
    }
}
Also used : ManagementSystem(org.janusgraph.graphdb.database.management.ManagementSystem) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) JanusGraph(org.janusgraph.core.JanusGraph) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) PropertyKey(org.janusgraph.core.PropertyKey)

Example 43 with JanusGraphIndex

use of org.janusgraph.core.schema.JanusGraphIndex in project janusgraph by JanusGraph.

the class ManagementSystem method setConsistency.

/**
 * Sets the consistency level for those schema elements that support it (types and internal indexes)
 * <p>
 * Note, that it is possible to have a race condition here if two threads simultaneously try to change the
 * consistency level. However, this is resolved when the consistency level is being read by taking the
 * first one and deleting all existing attached consistency levels upon modification.
 *
 * @param element
 * @param consistency
 */
@Override
public void setConsistency(JanusGraphSchemaElement element, ConsistencyModifier consistency) {
    if (element instanceof RelationType) {
        RelationTypeVertex rv = (RelationTypeVertex) element;
        Preconditions.checkArgument(consistency != ConsistencyModifier.FORK || !rv.multiplicity().isConstrained(), "Cannot apply FORK consistency mode to constraint relation type: %s", rv.name());
    } else if (element instanceof JanusGraphIndex) {
        IndexType index = ((JanusGraphIndexWrapper) element).getBaseIndex();
        if (index.isMixedIndex())
            throw new IllegalArgumentException("Cannot change consistency on mixed index: " + element);
    } else
        throw new IllegalArgumentException("Cannot change consistency of schema element: " + element);
    setTypeModifier(element, ModifierType.CONSISTENCY, consistency);
}
Also used : RelationType(org.janusgraph.core.RelationType) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) IndexType(org.janusgraph.graphdb.types.IndexType) CompositeIndexType(org.janusgraph.graphdb.types.CompositeIndexType) MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) RelationTypeVertex(org.janusgraph.graphdb.types.vertices.RelationTypeVertex)

Example 44 with JanusGraphIndex

use of org.janusgraph.core.schema.JanusGraphIndex in project janusgraph by JanusGraph.

the class ManagementSystem method updateIndex.

@Override
public ScanJobFuture updateIndex(Index index, SchemaAction updateAction, int numOfThreads) {
    Preconditions.checkArgument(index != null, "Need to provide an index");
    Preconditions.checkArgument(updateAction != null, "Need to provide update action");
    JanusGraphSchemaVertex schemaVertex = getSchemaVertex(index);
    Set<JanusGraphSchemaVertex> dependentTypes;
    Set<PropertyKeyVertex> keySubset = Collections.emptySet();
    if (index instanceof RelationTypeIndex) {
        dependentTypes = Collections.singleton((JanusGraphSchemaVertex) ((InternalRelationType) schemaVertex).getBaseType());
        if (!updateAction.isApplicableStatus(schemaVertex.getStatus()))
            return null;
    } else if (index instanceof JanusGraphIndex) {
        IndexType indexType = schemaVertex.asIndexType();
        dependentTypes = new HashSet<>();
        if (indexType.isCompositeIndex()) {
            if (!updateAction.isApplicableStatus(schemaVertex.getStatus()))
                return null;
            for (PropertyKey key : ((JanusGraphIndex) index).getFieldKeys()) {
                dependentTypes.add((PropertyKeyVertex) key);
            }
        } else {
            keySubset = new HashSet<>();
            MixedIndexType mixedIndexType = (MixedIndexType) indexType;
            Set<SchemaStatus> applicableStatus = updateAction.getApplicableStatus();
            for (ParameterIndexField field : mixedIndexType.getFieldKeys()) {
                if (applicableStatus.contains(field.getStatus()))
                    keySubset.add((PropertyKeyVertex) field.getFieldKey());
            }
            if (keySubset.isEmpty())
                return null;
            dependentTypes.addAll(keySubset);
        }
    } else
        throw new UnsupportedOperationException("Updates not supported for index: " + index);
    IndexIdentifier indexId = new IndexIdentifier(index);
    StandardScanner.Builder builder;
    ScanJobFuture future;
    switch(updateAction) {
        case REGISTER_INDEX:
            setStatus(schemaVertex, SchemaStatus.INSTALLED, keySubset);
            updatedTypes.add(schemaVertex);
            updatedTypes.addAll(dependentTypes);
            setUpdateTrigger(new UpdateStatusTrigger(graph, schemaVertex, SchemaStatus.REGISTERED, keySubset));
            future = new EmptyScanJobFuture();
            break;
        case REINDEX:
            builder = graph.getBackend().buildEdgeScanJob();
            builder.setFinishJob(indexId.getIndexJobFinisher(graph, SchemaAction.ENABLE_INDEX));
            builder.setJobId(indexId);
            builder.setNumProcessingThreads(numOfThreads);
            builder.setJob(VertexJobConverter.convert(graph, new IndexRepairJob(indexId.indexName, indexId.relationTypeName)));
            try {
                future = builder.execute();
            } catch (BackendException e) {
                throw new JanusGraphException(e);
            }
            break;
        case ENABLE_INDEX:
            setStatus(schemaVertex, SchemaStatus.ENABLED, keySubset);
            updatedTypes.add(schemaVertex);
            if (!keySubset.isEmpty())
                updatedTypes.addAll(dependentTypes);
            future = new EmptyScanJobFuture();
            break;
        case DISABLE_INDEX:
            setStatus(schemaVertex, SchemaStatus.INSTALLED, keySubset);
            updatedTypes.add(schemaVertex);
            if (!keySubset.isEmpty())
                updatedTypes.addAll(dependentTypes);
            setUpdateTrigger(new UpdateStatusTrigger(graph, schemaVertex, SchemaStatus.DISABLED, keySubset));
            future = new EmptyScanJobFuture();
            break;
        case REMOVE_INDEX:
            if (index instanceof RelationTypeIndex) {
                builder = graph.getBackend().buildEdgeScanJob();
            } else {
                JanusGraphIndex graphIndex = (JanusGraphIndex) index;
                if (graphIndex.isMixedIndex())
                    throw new UnsupportedOperationException("External mixed indexes must be removed in the indexing system directly.");
                builder = graph.getBackend().buildGraphIndexScanJob();
            }
            builder.setFinishJob(indexId.getIndexJobFinisher());
            builder.setJobId(indexId);
            builder.setNumProcessingThreads(numOfThreads);
            builder.setJob(new IndexRemoveJob(graph, indexId.indexName, indexId.relationTypeName));
            try {
                future = builder.execute();
            } catch (BackendException e) {
                throw new JanusGraphException(e);
            }
            break;
        default:
            throw new UnsupportedOperationException("Update action not supported: " + updateAction);
    }
    return future;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) JanusGraphException(org.janusgraph.core.JanusGraphException) IndexRepairJob(org.janusgraph.graphdb.olap.job.IndexRepairJob) ParameterIndexField(org.janusgraph.graphdb.types.ParameterIndexField) RelationTypeIndex(org.janusgraph.core.schema.RelationTypeIndex) EmptyScanJobFuture(org.janusgraph.diskstorage.keycolumnvalue.scan.EmptyScanJobFuture) ScanJobFuture(org.janusgraph.diskstorage.keycolumnvalue.scan.ScanJobFuture) BackendException(org.janusgraph.diskstorage.BackendException) StandardScanner(org.janusgraph.diskstorage.keycolumnvalue.scan.StandardScanner) EmptyScanJobFuture(org.janusgraph.diskstorage.keycolumnvalue.scan.EmptyScanJobFuture) JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex) PropertyKeyVertex(org.janusgraph.graphdb.types.vertices.PropertyKeyVertex) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) IndexType(org.janusgraph.graphdb.types.IndexType) CompositeIndexType(org.janusgraph.graphdb.types.CompositeIndexType) MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) PropertyKey(org.janusgraph.core.PropertyKey) HashSet(java.util.HashSet) IndexRemoveJob(org.janusgraph.graphdb.olap.job.IndexRemoveJob)

Example 45 with JanusGraphIndex

use of org.janusgraph.core.schema.JanusGraphIndex in project janusgraph by JanusGraph.

the class ManagementSystem method printIndexes.

private String printIndexes(boolean calledDirectly) {
    StringBuilder sb = new StringBuilder();
    String pattern = "%-30s | %-11s | %-9s | %-14s | %-10s %10s |%n";
    String relationPattern = "%-30s | %-11s | %-9s | %-14s | %-8s | %10s |%n";
    Iterable<JanusGraphIndex> vertexIndexes = getGraphIndexes(Vertex.class);
    Iterable<JanusGraphIndex> edgeIndexes = getGraphIndexes(Edge.class);
    Iterable<RelationType> relationTypes = getRelationTypes(RelationType.class);
    LinkedList<RelationTypeIndex> relationIndexes = new LinkedList<>();
    for (RelationType rt : relationTypes) {
        Iterable<RelationTypeIndex> rti = getRelationIndexes(rt);
        rti.forEach(relationIndexes::add);
    }
    if (calledDirectly) {
        sb.append(FIRSTDASH);
    } else {
        sb.append(DASHBREAK);
    }
    sb.append(String.format(pattern, "Graph Index (Vertex)", "Type", "Unique", "Backing", "Key:", "Status"));
    sb.append(DASHBREAK);
    sb.append(iterateIndexes(pattern, vertexIndexes));
    sb.append(DASHBREAK);
    sb.append(String.format(pattern, "Graph Index (Edge)", "Type", "Unique", "Backing", "Key:", "Status"));
    sb.append(DASHBREAK);
    sb.append(iterateIndexes(pattern, edgeIndexes));
    sb.append(DASHBREAK);
    sb.append(String.format(relationPattern, "Relation Index (VCI)", "Type", "Direction", "Sort Key", "Order", "Status"));
    sb.append(DASHBREAK);
    for (RelationTypeIndex ri : relationIndexes) {
        sb.append(String.format(relationPattern, ri.name(), ri.getType(), ri.getDirection(), ri.getSortKey()[0], ri.getSortOrder(), ri.getIndexStatus().name()));
    }
    if (!relationIndexes.isEmpty()) {
        sb.append(DASHBREAK);
    }
    return sb.toString();
}
Also used : RelationType(org.janusgraph.core.RelationType) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) RelationTypeIndex(org.janusgraph.core.schema.RelationTypeIndex) LinkedList(java.util.LinkedList)

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