Search in sources :

Example 1 with TitanGraphIndex

use of com.thinkaurelius.titan.core.schema.TitanGraphIndex in project titan by thinkaurelius.

the class ManagementSystem method changeName.

@Override
public void changeName(TitanSchemaElement element, String newName) {
    Preconditions.checkArgument(StringUtils.isNotBlank(newName), "Invalid name: %s", newName);
    TitanSchemaVertex schemaVertex = getSchemaVertex(element);
    if (schemaVertex.name().equals(newName))
        return;
    TitanSchemaCategory schemaCategory = schemaVertex.valueOrNull(BaseKey.SchemaCategory);
    Preconditions.checkArgument(schemaCategory.hasName(), "Invalid schema element: %s", element);
    if (schemaVertex instanceof RelationType) {
        InternalRelationType relType = (InternalRelationType) schemaVertex;
        if (relType.getBaseType() != null) {
            newName = composeRelationTypeIndexName(relType.getBaseType(), newName);
        } else
            assert !(element instanceof RelationTypeIndex);
        TitanSchemaCategory cat = relType.isEdgeLabel() ? TitanSchemaCategory.EDGELABEL : TitanSchemaCategory.PROPERTYKEY;
        SystemTypeManager.isNotSystemName(cat, newName);
    } else if (element instanceof VertexLabel) {
        SystemTypeManager.isNotSystemName(TitanSchemaCategory.VERTEXLABEL, newName);
    } else if (element instanceof TitanGraphIndex) {
        checkIndexName(newName);
    }
    transaction.addProperty(schemaVertex, BaseKey.SchemaName, schemaCategory.getSchemaName(newName));
    updateSchemaVertex(schemaVertex);
    schemaVertex.resetCache();
    updatedTypes.add(schemaVertex);
}
Also used : TitanSchemaVertex(com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex) TitanSchemaCategory(com.thinkaurelius.titan.graphdb.internal.TitanSchemaCategory) VertexLabel(com.thinkaurelius.titan.core.VertexLabel) InternalRelationType(com.thinkaurelius.titan.graphdb.internal.InternalRelationType) RelationType(com.thinkaurelius.titan.core.RelationType) InternalRelationType(com.thinkaurelius.titan.graphdb.internal.InternalRelationType) RelationTypeIndex(com.thinkaurelius.titan.core.schema.RelationTypeIndex) TitanGraphIndex(com.thinkaurelius.titan.core.schema.TitanGraphIndex)

Example 2 with TitanGraphIndex

use of com.thinkaurelius.titan.core.schema.TitanGraphIndex in project titan by thinkaurelius.

the class ManagementUtil method awaitIndexUpdate.

private static void awaitIndexUpdate(TitanGraph g, String indexName, String relationTypeName, long time, TemporalUnit unit) {
    Preconditions.checkArgument(g != null && g.isOpen(), "Need to provide valid, open graph instance");
    Preconditions.checkArgument(time > 0 && unit != null, "Need to provide valid time interval");
    Preconditions.checkArgument(StringUtils.isNotBlank(indexName), "Need to provide an index name");
    StandardTitanGraph graph = (StandardTitanGraph) g;
    TimestampProvider times = graph.getConfiguration().getTimestampProvider();
    Instant end = times.getTime().plus(Duration.of(time, unit));
    boolean isStable = false;
    while (times.getTime().isBefore(end)) {
        TitanManagement mgmt = graph.openManagement();
        try {
            if (StringUtils.isNotBlank(relationTypeName)) {
                RelationTypeIndex idx = mgmt.getRelationIndex(mgmt.getRelationType(relationTypeName), indexName);
                Preconditions.checkArgument(idx != null, "Index could not be found: %s @ %s", indexName, relationTypeName);
                isStable = idx.getIndexStatus().isStable();
            } else {
                TitanGraphIndex idx = mgmt.getGraphIndex(indexName);
                Preconditions.checkArgument(idx != null, "Index could not be found: %s", indexName);
                isStable = true;
                for (PropertyKey key : idx.getFieldKeys()) {
                    if (!idx.getIndexStatus(key).isStable())
                        isStable = false;
                }
            }
        } finally {
            mgmt.rollback();
        }
        if (isStable)
            break;
        try {
            times.sleepFor(Duration.ofMillis(500));
        } catch (InterruptedException e) {
        }
    }
    if (!isStable)
        throw new TitanException("Index did not stabilize within the given amount of time. For sufficiently long " + "wait periods this is most likely caused by a failed/incorrectly shut down Titan instance or a lingering transaction.");
}
Also used : StandardTitanGraph(com.thinkaurelius.titan.graphdb.database.StandardTitanGraph) TimestampProvider(com.thinkaurelius.titan.diskstorage.util.time.TimestampProvider) Instant(java.time.Instant) TitanException(com.thinkaurelius.titan.core.TitanException) TitanManagement(com.thinkaurelius.titan.core.schema.TitanManagement) RelationTypeIndex(com.thinkaurelius.titan.core.schema.RelationTypeIndex) TitanGraphIndex(com.thinkaurelius.titan.core.schema.TitanGraphIndex) PropertyKey(com.thinkaurelius.titan.core.PropertyKey)

Example 3 with TitanGraphIndex

use of com.thinkaurelius.titan.core.schema.TitanGraphIndex in project titan by thinkaurelius.

the class MapReduceIndexManagement method updateIndex.

/**
     * Updates the provided index according to the given {@link SchemaAction}.
     * Only {@link SchemaAction#REINDEX} and {@link SchemaAction#REMOVE_INDEX} are supported.
     *
     * @param index the index to process
     * @param updateAction either {@code REINDEX} or {@code REMOVE_INDEX}
     * @return a future that returns immediately;
     *         this method blocks until the Hadoop MapReduce job completes
     */
// TODO make this future actually async and update javadoc @return accordingly
public TitanManagement.IndexJobFuture updateIndex(TitanIndex index, SchemaAction updateAction) throws BackendException {
    Preconditions.checkNotNull(index, "Index parameter must not be null", index);
    Preconditions.checkNotNull(updateAction, "%s parameter must not be null", SchemaAction.class.getSimpleName());
    Preconditions.checkArgument(SUPPORTED_ACTIONS.contains(updateAction), "Only these %s parameters are supported: %s (was given %s)", SchemaAction.class.getSimpleName(), SUPPORTED_ACTIONS_STRING, updateAction);
    Preconditions.checkArgument(RelationTypeIndex.class.isAssignableFrom(index.getClass()) || TitanGraphIndex.class.isAssignableFrom(index.getClass()), "Index %s has class %s: must be a %s or %s (or subtype)", index.getClass(), RelationTypeIndex.class.getSimpleName(), TitanGraphIndex.class.getSimpleName());
    org.apache.hadoop.conf.Configuration hadoopConf = new org.apache.hadoop.conf.Configuration();
    ModifiableHadoopConfiguration titanmrConf = ModifiableHadoopConfiguration.of(TitanHadoopConfiguration.MAPRED_NS, hadoopConf);
    // The job we'll execute to either REINDEX or REMOVE_INDEX
    final Class<? extends IndexUpdateJob> indexJobClass;
    final Class<? extends Mapper> mapperClass;
    // The class of the IndexUpdateJob and the Mapper that will be used to run it (VertexScanJob vs ScanJob)
    if (updateAction.equals(SchemaAction.REINDEX)) {
        indexJobClass = IndexRepairJob.class;
        mapperClass = HadoopVertexScanMapper.class;
    } else if (updateAction.equals(SchemaAction.REMOVE_INDEX)) {
        indexJobClass = IndexRemoveJob.class;
        mapperClass = HadoopScanMapper.class;
    } else {
        // Shouldn't get here -- if this exception is ever thrown, update SUPPORTED_ACTIONS
        throw new IllegalStateException("Unrecognized " + SchemaAction.class.getSimpleName() + ": " + updateAction);
    }
    // The column family that serves as input to the IndexUpdateJob
    final String readCF;
    if (RelationTypeIndex.class.isAssignableFrom(index.getClass())) {
        readCF = Backend.EDGESTORE_NAME;
    } else {
        TitanGraphIndex gindex = (TitanGraphIndex) index;
        if (gindex.isMixedIndex() && !updateAction.equals(SchemaAction.REINDEX))
            throw new UnsupportedOperationException("External mixed indexes must be removed in the indexing system directly.");
        Preconditions.checkState(TitanGraphIndex.class.isAssignableFrom(index.getClass()));
        if (updateAction.equals(SchemaAction.REMOVE_INDEX))
            readCF = Backend.INDEXSTORE_NAME;
        else
            readCF = Backend.EDGESTORE_NAME;
    }
    titanmrConf.set(TitanHadoopConfiguration.COLUMN_FAMILY_NAME, readCF);
    // The MapReduce InputFormat class based on the open graph's store manager
    final Class<? extends InputFormat> inputFormat;
    final Class<? extends KeyColumnValueStoreManager> storeManagerClass = graph.getBackend().getStoreManagerClass();
    if (CASSANDRA_STORE_MANAGER_CLASSES.contains(storeManagerClass)) {
        inputFormat = CassandraBinaryInputFormat.class;
        // Set the partitioner
        IPartitioner part = ((AbstractCassandraStoreManager) graph.getBackend().getStoreManager()).getCassandraPartitioner();
        hadoopConf.set("cassandra.input.partitioner.class", part.getClass().getName());
    } else if (HBASE_STORE_MANAGER_CLASSES.contains(storeManagerClass)) {
        inputFormat = HBaseBinaryInputFormat.class;
    } else {
        throw new IllegalArgumentException("Store manager class " + storeManagerClass + "is not supported");
    }
    // The index name and relation type name (if the latter is applicable)
    final String indexName = index.name();
    final String relationTypeName = RelationTypeIndex.class.isAssignableFrom(index.getClass()) ? ((RelationTypeIndex) index).getType().name() : "";
    Preconditions.checkNotNull(indexName);
    // Set the class of the IndexUpdateJob
    titanmrConf.set(TitanHadoopConfiguration.SCAN_JOB_CLASS, indexJobClass.getName());
    // Set the configuration of the IndexUpdateJob
    copyIndexJobKeys(hadoopConf, indexName, relationTypeName);
    titanmrConf.set(TitanHadoopConfiguration.SCAN_JOB_CONFIG_ROOT, GraphDatabaseConfiguration.class.getName() + "#JOB_NS");
    // Copy the StandardTitanGraph configuration under TitanHadoopConfiguration.GRAPH_CONFIG_KEYS
    org.apache.commons.configuration.Configuration localbc = graph.getConfiguration().getLocalConfiguration();
    localbc.clearProperty(Graph.GRAPH);
    copyInputKeys(hadoopConf, localbc);
    String jobName = HadoopScanMapper.class.getSimpleName() + "[" + indexJobClass.getSimpleName() + "]";
    try {
        return new CompletedJobFuture(HadoopScanRunner.runJob(hadoopConf, inputFormat, jobName, mapperClass));
    } catch (Exception e) {
        return new FailedJobFuture(e);
    }
}
Also used : GraphDatabaseConfiguration(com.thinkaurelius.titan.graphdb.configuration.GraphDatabaseConfiguration) TitanHadoopConfiguration(com.thinkaurelius.titan.hadoop.config.TitanHadoopConfiguration) ModifiableHadoopConfiguration(com.thinkaurelius.titan.hadoop.config.ModifiableHadoopConfiguration) SchemaAction(com.thinkaurelius.titan.core.schema.SchemaAction) HBaseBinaryInputFormat(com.thinkaurelius.titan.hadoop.formats.hbase.HBaseBinaryInputFormat) ModifiableHadoopConfiguration(com.thinkaurelius.titan.hadoop.config.ModifiableHadoopConfiguration) RelationTypeIndex(com.thinkaurelius.titan.core.schema.RelationTypeIndex) TitanGraphIndex(com.thinkaurelius.titan.core.schema.TitanGraphIndex) AbstractCassandraStoreManager(com.thinkaurelius.titan.diskstorage.cassandra.AbstractCassandraStoreManager) IPartitioner(org.apache.cassandra.dht.IPartitioner) HadoopScanMapper(com.thinkaurelius.titan.hadoop.scan.HadoopScanMapper) TimeoutException(java.util.concurrent.TimeoutException) BackendException(com.thinkaurelius.titan.diskstorage.BackendException) ExecutionException(java.util.concurrent.ExecutionException) IndexRemoveJob(com.thinkaurelius.titan.graphdb.olap.job.IndexRemoveJob)

Example 4 with TitanGraphIndex

use of com.thinkaurelius.titan.core.schema.TitanGraphIndex in project titan by thinkaurelius.

the class AbstractIndexManagementIT method testRemoveGraphIndex.

@Test
public void testRemoveGraphIndex() throws InterruptedException, BackendException, ExecutionException {
    tx.commit();
    mgmt.commit();
    // Load the "Graph of the Gods" sample data
    GraphOfTheGodsFactory.loadWithoutMixedIndex(graph, true);
    // Disable the "name" composite index
    TitanManagement m = graph.openManagement();
    TitanGraphIndex nameIndex = m.getGraphIndex("name");
    m.updateIndex(nameIndex, SchemaAction.DISABLE_INDEX);
    m.commit();
    graph.tx().commit();
    // Block until the SchemaStatus transitions to DISABLED
    assertTrue(ManagementSystem.awaitGraphIndexStatus(graph, "name").status(SchemaStatus.DISABLED).call().getSucceeded());
    // Remove index
    MapReduceIndexManagement mri = new MapReduceIndexManagement(graph);
    m = graph.openManagement();
    TitanGraphIndex index = m.getGraphIndex("name");
    ScanMetrics metrics = mri.updateIndex(index, SchemaAction.REMOVE_INDEX).get();
    assertEquals(12, metrics.getCustom(IndexRemoveJob.DELETED_RECORDS_COUNT));
}
Also used : ScanMetrics(com.thinkaurelius.titan.diskstorage.keycolumnvalue.scan.ScanMetrics) TitanManagement(com.thinkaurelius.titan.core.schema.TitanManagement) TitanGraphIndex(com.thinkaurelius.titan.core.schema.TitanGraphIndex) Test(org.junit.Test) TitanGraphBaseTest(com.thinkaurelius.titan.graphdb.TitanGraphBaseTest)

Example 5 with TitanGraphIndex

use of com.thinkaurelius.titan.core.schema.TitanGraphIndex in project titan by thinkaurelius.

the class IndexRemoveJob method validateIndexStatus.

@Override
protected void validateIndexStatus() {
    if (index instanceof RelationTypeIndex) {
    //Nothing specific to be done
    } else if (index instanceof TitanGraphIndex) {
        TitanGraphIndex gindex = (TitanGraphIndex) index;
        if (gindex.isMixedIndex())
            throw new UnsupportedOperationException("Cannot remove mixed indexes through Titan. This can " + "only be accomplished in the indexing system directly.");
        CompositeIndexType indexType = (CompositeIndexType) mgmt.getSchemaVertex(index).asIndexType();
        graphIndexId = indexType.getID();
    } else
        throw new UnsupportedOperationException("Unsupported index found: " + index);
    //Must be a relation type index or a composite graph index
    TitanSchemaVertex schemaVertex = mgmt.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 : TitanSchemaVertex(com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex) CompositeIndexType(com.thinkaurelius.titan.graphdb.types.CompositeIndexType) RelationTypeIndex(com.thinkaurelius.titan.core.schema.RelationTypeIndex) TitanGraphIndex(com.thinkaurelius.titan.core.schema.TitanGraphIndex) SchemaStatus(com.thinkaurelius.titan.core.schema.SchemaStatus)

Aggregations

TitanGraphIndex (com.thinkaurelius.titan.core.schema.TitanGraphIndex)31 PropertyKey (com.thinkaurelius.titan.core.PropertyKey)20 Test (org.junit.Test)16 TitanVertex (com.thinkaurelius.titan.core.TitanVertex)13 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)10 VertexLabel (com.thinkaurelius.titan.core.VertexLabel)8 RelationTypeIndex (com.thinkaurelius.titan.core.schema.RelationTypeIndex)7 TitanManagement (com.thinkaurelius.titan.core.schema.TitanManagement)7 BaseVertexLabel (com.thinkaurelius.titan.graphdb.types.system.BaseVertexLabel)6 EdgeLabel (com.thinkaurelius.titan.core.EdgeLabel)5 CompositeIndexType (com.thinkaurelius.titan.graphdb.types.CompositeIndexType)4 TitanSchemaVertex (com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex)4 TitanTransaction (com.thinkaurelius.titan.core.TitanTransaction)3 ScanMetrics (com.thinkaurelius.titan.diskstorage.keycolumnvalue.scan.ScanMetrics)3 IndexType (com.thinkaurelius.titan.graphdb.types.IndexType)3 MixedIndexType (com.thinkaurelius.titan.graphdb.types.MixedIndexType)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 RelationType (com.thinkaurelius.titan.core.RelationType)2 TitanEdge (com.thinkaurelius.titan.core.TitanEdge)2 TitanException (com.thinkaurelius.titan.core.TitanException)2