Java Code Examples for com.thinkaurelius.titan.core.schema.SchemaStatus

The following are top voted examples for showing how to use com.thinkaurelius.titan.core.schema.SchemaStatus. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: keti   File: GraphConfig.java   View source code 6 votes vote down vote up
public static void createUniqueCompositeIndex(final Graph newGraph, final String indexName,
        final String[] propertyKeyNames, final Class<?> propertyKeyType) throws InterruptedException {

    Assert.notEmpty(propertyKeyNames);
    newGraph.tx().rollback(); // Never create new indexes while a transaction is active
    TitanManagement mgmt = ((TitanGraph) newGraph).openManagement();
    IndexBuilder indexBuilder = mgmt.buildIndex(indexName, Vertex.class);
    if (!mgmt.containsGraphIndex(indexName)) {
        for (String propertyKeyName : propertyKeyNames) {
            PropertyKey indexPropertyKey = getOrCreatePropertyKey(propertyKeyName, propertyKeyType, mgmt);
            indexBuilder.addKey(indexPropertyKey);
        }
        indexBuilder.unique().buildCompositeIndex();
    }
    mgmt.commit();
    // Wait for the index to become available
    ManagementSystem.awaitGraphIndexStatus((TitanGraph) newGraph, indexName).status(SchemaStatus.ENABLED).call();
}
 
Example 2
Project: titan1withtp3.1   File: GraphCentricQueryBuilder.java   View source code 6 votes vote down vote up
private static final boolean coversAll(final MixedIndexType index, Condition<TitanElement> condition, IndexSerializer indexInfo) {
    if (condition.getType()==Condition.Type.LITERAL) {
        if (!(condition instanceof  PredicateCondition)) return false;
        PredicateCondition<RelationType, TitanElement> atom = (PredicateCondition) condition;
        if (atom.getValue()==null) return false;

        Preconditions.checkArgument(atom.getKey().isPropertyKey());
        PropertyKey key = (PropertyKey) atom.getKey();
        ParameterIndexField[] fields = index.getFieldKeys();
        ParameterIndexField match = null;
        for (int i = 0; i < fields.length; i++) {
            if (fields[i].getStatus()!= SchemaStatus.ENABLED) continue;
            if (fields[i].getFieldKey().equals(key)) match = fields[i];
        }
        if (match==null) return false;
        return indexInfo.supports(index,match,atom.getPredicate());
    } else {
        for (Condition<TitanElement> child : condition.getChildren()) {
            if (!coversAll(index,child,indexInfo)) return false;
        }
        return true;
    }
}
 
Example 3
Project: titan1withtp3.1   File: ManagementSystem.java   View source code 6 votes vote down vote up
private TitanGraphIndex createMixedIndex(String indexName, ElementCategory elementCategory,
                                         TitanSchemaType constraint, String backingIndex) {
    Preconditions.checkArgument(graph.getIndexSerializer().containsIndex(backingIndex), "Unknown external index backend: %s", backingIndex);
    checkIndexName(indexName);

    TypeDefinitionMap def = new TypeDefinitionMap();
    def.setValue(TypeDefinitionCategory.INTERNAL_INDEX, false);
    def.setValue(TypeDefinitionCategory.ELEMENT_CATEGORY, elementCategory);
    def.setValue(TypeDefinitionCategory.BACKING_INDEX, backingIndex);
    def.setValue(TypeDefinitionCategory.INDEXSTORE_NAME, indexName);
    def.setValue(TypeDefinitionCategory.INDEX_CARDINALITY, Cardinality.LIST);
    def.setValue(TypeDefinitionCategory.STATUS, SchemaStatus.ENABLED);
    TitanSchemaVertex indexVertex = transaction.makeSchemaVertex(TitanSchemaCategory.GRAPHINDEX, indexName, def);

    Preconditions.checkArgument(constraint == null || (elementCategory.isValidConstraint(constraint) && constraint instanceof TitanSchemaVertex));
    if (constraint != null) {
        addSchemaEdge(indexVertex, (TitanSchemaVertex) constraint, TypeDefinitionCategory.INDEX_SCHEMA_CONSTRAINT, null);
    }
    updateSchemaVertex(indexVertex);
    return new TitanGraphIndexWrapper(indexVertex.asIndexType());
}
 
Example 4
Project: titan1withtp3.1   File: ManagementSystem.java   View source code 6 votes vote down vote up
private void setStatusEdges(TitanSchemaVertex vertex, SchemaStatus status, Set<PropertyKeyVertex> keys) {
    Preconditions.checkArgument(vertex.asIndexType().isMixedIndex());

    for (TitanEdge edge : vertex.getEdges(TypeDefinitionCategory.INDEX_FIELD, Direction.OUT)) {
        if (!keys.contains(edge.vertex(Direction.IN))) continue; //Only address edges with matching keys
        TypeDefinitionDescription desc = edge.valueOrNull(BaseKey.SchemaDefinitionDesc);
        assert desc.getCategory() == TypeDefinitionCategory.INDEX_FIELD;
        Parameter[] parameters = (Parameter[]) desc.getModifier();
        assert parameters[parameters.length - 1].key().equals(ParameterType.STATUS.getName());
        if (parameters[parameters.length - 1].value().equals(status)) continue;

        Parameter[] paraCopy = Arrays.copyOf(parameters, parameters.length);
        paraCopy[parameters.length - 1] = ParameterType.STATUS.getParameter(status);
        edge.remove();
        addSchemaEdge(vertex, edge.vertex(Direction.IN), TypeDefinitionCategory.INDEX_FIELD, paraCopy);
    }

    for (PropertyKeyVertex prop : keys) prop.resetCache();
}
 
Example 5
Project: titan1withtp3.1   File: IndexSerializer.java   View source code 6 votes vote down vote up
public void reindexElement(TitanElement element, MixedIndexType index, Map<String,Map<String,List<IndexEntry>>> documentsPerStore) {
    if (!indexAppliesTo(index,element)) return;
    List<IndexEntry> entries = Lists.newArrayList();
    for (ParameterIndexField field: index.getFieldKeys()) {
        PropertyKey key = field.getFieldKey();
        if (field.getStatus()==SchemaStatus.DISABLED) continue;
        if (element.properties(key.name()).hasNext()) {
            element.values(key.name()).forEachRemaining(value->entries.add(new IndexEntry(key2Field(field), value)));
        }
    }
    Map<String,List<IndexEntry>> documents = documentsPerStore.get(index.getStoreName());
    if (documents==null) {
        documents = Maps.newHashMap();
        documentsPerStore.put(index.getStoreName(),documents);
    }
    getDocuments(documentsPerStore,index).put(element2String(element),entries);
}
 
Example 6
Project: titan1withtp3.1   File: IndexRemoveJob.java   View source code 6 votes vote down vote up
@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);
}
 
Example 7
Project: incubator-atlas   File: GraphCentricQueryBuilder.java   View source code 6 votes vote down vote up
private static final boolean coversAll(final MixedIndexType index, Condition<TitanElement> condition, IndexSerializer indexInfo) {
    if (condition.getType()==Condition.Type.LITERAL) {
        if (!(condition instanceof  PredicateCondition)) return false;
        PredicateCondition<RelationType, TitanElement> atom = (PredicateCondition) condition;
        if (atom.getValue()==null) return false;

        Preconditions.checkArgument(atom.getKey().isPropertyKey());
        PropertyKey key = (PropertyKey) atom.getKey();
        ParameterIndexField[] fields = index.getFieldKeys();
        ParameterIndexField match = null;
        for (ParameterIndexField field : fields) {
            if (field.getStatus() != SchemaStatus.ENABLED) continue;
            if (field.getFieldKey().equals(key)) match = field;
        }
        if (match==null) return false;
        return indexInfo.supports(index, match, atom.getPredicate());
    } else {
        for (Condition<TitanElement> child : condition.getChildren()) {
            if (!coversAll(index, child, indexInfo)) return false;
        }
        return true;
    }
}
 
Example 8
Project: titan1.0.1.kafka   File: GraphCentricQueryBuilder.java   View source code 6 votes vote down vote up
private static final boolean coversAll(final MixedIndexType index, Condition<TitanElement> condition, IndexSerializer indexInfo) {
    if (condition.getType()==Condition.Type.LITERAL) {
        if (!(condition instanceof  PredicateCondition)) return false;
        PredicateCondition<RelationType, TitanElement> atom = (PredicateCondition) condition;
        if (atom.getValue()==null) return false;

        Preconditions.checkArgument(atom.getKey().isPropertyKey());
        PropertyKey key = (PropertyKey) atom.getKey();
        ParameterIndexField[] fields = index.getFieldKeys();
        ParameterIndexField match = null;
        for (int i = 0; i < fields.length; i++) {
            if (fields[i].getStatus()!= SchemaStatus.ENABLED) continue;
            if (fields[i].getFieldKey().equals(key)) match = fields[i];
        }
        if (match==null) return false;
        return indexInfo.supports(index,match,atom.getPredicate());
    } else {
        for (Condition<TitanElement> child : condition.getChildren()) {
            if (!coversAll(index,child,indexInfo)) return false;
        }
        return true;
    }
}
 
Example 9
Project: titan1.0.1.kafka   File: ManagementSystem.java   View source code 6 votes vote down vote up
private TitanGraphIndex createMixedIndex(String indexName, ElementCategory elementCategory,
                                         TitanSchemaType constraint, String backingIndex) {
    Preconditions.checkArgument(graph.getIndexSerializer().containsIndex(backingIndex), "Unknown external index backend: %s", backingIndex);
    checkIndexName(indexName);

    TypeDefinitionMap def = new TypeDefinitionMap();
    def.setValue(TypeDefinitionCategory.INTERNAL_INDEX, false);
    def.setValue(TypeDefinitionCategory.ELEMENT_CATEGORY, elementCategory);
    def.setValue(TypeDefinitionCategory.BACKING_INDEX, backingIndex);
    def.setValue(TypeDefinitionCategory.INDEXSTORE_NAME, indexName);
    def.setValue(TypeDefinitionCategory.INDEX_CARDINALITY, Cardinality.LIST);
    def.setValue(TypeDefinitionCategory.STATUS, SchemaStatus.ENABLED);
    TitanSchemaVertex indexVertex = transaction.makeSchemaVertex(TitanSchemaCategory.GRAPHINDEX, indexName, def);

    Preconditions.checkArgument(constraint == null || (elementCategory.isValidConstraint(constraint) && constraint instanceof TitanSchemaVertex));
    if (constraint != null) {
        addSchemaEdge(indexVertex, (TitanSchemaVertex) constraint, TypeDefinitionCategory.INDEX_SCHEMA_CONSTRAINT, null);
    }
    updateSchemaVertex(indexVertex);
    return new TitanGraphIndexWrapper(indexVertex.asIndexType());
}
 
Example 10
Project: titan1.0.1.kafka   File: ManagementSystem.java   View source code 6 votes vote down vote up
private void setStatusEdges(TitanSchemaVertex vertex, SchemaStatus status, Set<PropertyKeyVertex> keys) {
    Preconditions.checkArgument(vertex.asIndexType().isMixedIndex());

    for (TitanEdge edge : vertex.getEdges(TypeDefinitionCategory.INDEX_FIELD, Direction.OUT)) {
        if (!keys.contains(edge.vertex(Direction.IN))) continue; //Only address edges with matching keys
        TypeDefinitionDescription desc = edge.valueOrNull(BaseKey.SchemaDefinitionDesc);
        assert desc.getCategory() == TypeDefinitionCategory.INDEX_FIELD;
        Parameter[] parameters = (Parameter[]) desc.getModifier();
        assert parameters[parameters.length - 1].key().equals(ParameterType.STATUS.getName());
        if (parameters[parameters.length - 1].value().equals(status)) continue;

        Parameter[] paraCopy = Arrays.copyOf(parameters, parameters.length);
        paraCopy[parameters.length - 1] = ParameterType.STATUS.getParameter(status);
        edge.remove();
        addSchemaEdge(vertex, edge.vertex(Direction.IN), TypeDefinitionCategory.INDEX_FIELD, paraCopy);
    }

    for (PropertyKeyVertex prop : keys) prop.resetCache();
}
 
Example 11
Project: titan1.0.1.kafka   File: IndexSerializer.java   View source code 6 votes vote down vote up
public void reindexElement(TitanElement element, MixedIndexType index, Map<String,Map<String,List<IndexEntry>>> documentsPerStore) {
    if (!indexAppliesTo(index,element)) return;
    List<IndexEntry> entries = Lists.newArrayList();
    for (ParameterIndexField field: index.getFieldKeys()) {
        PropertyKey key = field.getFieldKey();
        if (field.getStatus()==SchemaStatus.DISABLED) continue;
        if (element.properties(key.name()).hasNext()) {
            element.values(key.name()).forEachRemaining(value->entries.add(new IndexEntry(key2Field(field), value)));
        }
    }
    Map<String,List<IndexEntry>> documents = documentsPerStore.get(index.getStoreName());
    if (documents==null) {
        documents = Maps.newHashMap();
        documentsPerStore.put(index.getStoreName(),documents);
    }
    getDocuments(documentsPerStore,index).put(element2String(element),entries);
}
 
Example 12
Project: titan1.0.1.kafka   File: IndexRemoveJob.java   View source code 6 votes vote down vote up
@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);
}
 
Example 13
Project: titan0.5.4-hbase1.1.1-custom   File: GraphCentricQueryBuilder.java   View source code 6 votes vote down vote up
private static final boolean coversAll(final MixedIndexType index, Condition<TitanElement> condition, IndexSerializer indexInfo) {
    if (condition.getType()==Condition.Type.LITERAL) {
        if (!(condition instanceof  PredicateCondition)) return false;
        PredicateCondition<RelationType, TitanElement> atom = (PredicateCondition) condition;
        if (atom.getValue()==null) return false;

        Preconditions.checkArgument(atom.getKey().isPropertyKey());
        PropertyKey key = (PropertyKey) atom.getKey();
        ParameterIndexField[] fields = index.getFieldKeys();
        ParameterIndexField match = null;
        for (int i = 0; i < fields.length; i++) {
            if (fields[i].getStatus()!= SchemaStatus.ENABLED) continue;
            if (fields[i].getFieldKey().equals(key)) match = fields[i];
        }
        if (match==null) return false;
        return indexInfo.supports(index,match,atom.getPredicate());
    } else {
        for (Condition<TitanElement> child : condition.getChildren()) {
            if (!coversAll(index,child,indexInfo)) return false;
        }
        return true;
    }
}
 
Example 14
Project: titan0.5.4-hbase1.1.1-custom   File: ManagementSystem.java   View source code 6 votes vote down vote up
private TitanGraphIndex createMixedIndex(String indexName, ElementCategory elementCategory,
                                         TitanSchemaType constraint, String backingIndex) {
    Preconditions.checkArgument(graph.getIndexSerializer().containsIndex(backingIndex),"Unknown external index backend: %s",backingIndex);
    checkIndexName(indexName);

    TypeDefinitionMap def = new TypeDefinitionMap();
    def.setValue(TypeDefinitionCategory.INTERNAL_INDEX,false);
    def.setValue(TypeDefinitionCategory.ELEMENT_CATEGORY,elementCategory);
    def.setValue(TypeDefinitionCategory.BACKING_INDEX,backingIndex);
    def.setValue(TypeDefinitionCategory.INDEXSTORE_NAME,indexName);
    def.setValue(TypeDefinitionCategory.INDEX_CARDINALITY, Cardinality.LIST);
    def.setValue(TypeDefinitionCategory.STATUS,SchemaStatus.ENABLED);
    TitanSchemaVertex indexVertex = transaction.makeSchemaVertex(TitanSchemaCategory.GRAPHINDEX,indexName,def);

    Preconditions.checkArgument(constraint==null || (elementCategory.isValidConstraint(constraint) && constraint instanceof TitanSchemaVertex));
    if (constraint!=null) {
        addSchemaEdge(indexVertex,(TitanSchemaVertex)constraint,TypeDefinitionCategory.INDEX_SCHEMA_CONSTRAINT,null);
    }
    updateSchemaVertex(indexVertex);
    return new TitanGraphIndexWrapper(indexVertex.asIndexType());
}
 
Example 15
Project: titan0.5.4-hbase1.1.1-custom   File: ManagementSystem.java   View source code 6 votes vote down vote up
private void setStatusEdges(TitanSchemaVertex vertex, SchemaStatus status, Set<PropertyKeyVertex> keys) {
    Preconditions.checkArgument(vertex.asIndexType().isMixedIndex());

    for (TitanEdge edge : vertex.getEdges(TypeDefinitionCategory.INDEX_FIELD,Direction.OUT)) {
        if (!keys.contains(edge.getVertex(Direction.IN))) continue; //Only address edges with matching keys
        TypeDefinitionDescription desc = edge.getProperty(BaseKey.SchemaDefinitionDesc);
        assert desc.getCategory()==TypeDefinitionCategory.INDEX_FIELD;
        Parameter[] parameters = (Parameter[])desc.getModifier();
        assert parameters[parameters.length-1].getKey().equals(ParameterType.STATUS.getName());
        if (parameters[parameters.length-1].getValue().equals(status)) continue;

        Parameter[] paraCopy = Arrays.copyOf(parameters,parameters.length);
        paraCopy[parameters.length-1]=ParameterType.STATUS.getParameter(status);
        edge.remove();
        addSchemaEdge(vertex,edge.getVertex(Direction.IN),TypeDefinitionCategory.INDEX_FIELD,paraCopy);
    }

    for (PropertyKeyVertex prop : keys) prop.resetCache();
}
 
Example 16
Project: titan0.5.4-hbase1.1.1-custom   File: IndexSerializer.java   View source code 6 votes vote down vote up
public void reindexElement(TitanElement element, MixedIndexType index, Map<String,Map<String,List<IndexEntry>>> documentsPerStore) {
    if (!indexAppliesTo(index,element)) return;
    List<IndexEntry> entries = Lists.newArrayList();
    for (ParameterIndexField field: index.getFieldKeys()) {
        PropertyKey key = field.getFieldKey();
        if (field.getStatus()==SchemaStatus.DISABLED) continue;
        Object value = element.getProperty(key.getName());
        if (value!=null) {
            entries.add(new IndexEntry(key2Field(field), value));
        }
    }
    Map<String,List<IndexEntry>> documents = documentsPerStore.get(index.getStoreName());
    if (documents==null) {
        documents = Maps.newHashMap();
        documentsPerStore.put(index.getStoreName(),documents);
    }
    getDocuments(documentsPerStore,index).put(element2String(element),entries);
}
 
Example 17
Project: keti   File: GraphConfig.java   View source code 5 votes vote down vote up
public static void createIndex(final Graph newGraph, final String indexName, final String indexKey)
        throws InterruptedException {
    newGraph.tx().rollback(); // Never create new indexes while a transaction is active
    TitanManagement mgmt = ((TitanGraph) newGraph).openManagement();
    if (!mgmt.containsGraphIndex(indexName)) {
        PropertyKey indexPropertyKey = mgmt.makePropertyKey(indexKey).dataType(String.class).make();
        mgmt.buildIndex(indexName, Vertex.class).addKey(indexPropertyKey).buildCompositeIndex();
    }
    mgmt.commit();
    // Wait for the index to become available
    ManagementSystem.awaitGraphIndexStatus((TitanGraph) newGraph, indexName).status(SchemaStatus.ENABLED).call();
}
 
Example 18
Project: keti   File: GraphConfig.java   View source code 5 votes vote down vote up
public static void createUniqueIndexForLabel(final Graph newGraph, final String indexName, final String indexKey,
        final String label) throws InterruptedException {
    newGraph.tx().rollback(); // Never create new indexes while a transaction is active
    TitanManagement mgmt = ((TitanGraph) newGraph).openManagement();
    if (!mgmt.containsGraphIndex(indexName)) {
        PropertyKey indexPropertyKey = mgmt.makePropertyKey(indexKey).dataType(Integer.class).make();
        VertexLabel versionLabel = mgmt.makeVertexLabel(label).make();
        // Create a unique composite index for the property key that indexes only vertices with a given label
        mgmt.buildIndex(indexName, Vertex.class).addKey(indexPropertyKey).indexOnly(versionLabel).unique()
                .buildCompositeIndex();
    }
    mgmt.commit();
    // Wait for the index to become available
    ManagementSystem.awaitGraphIndexStatus((TitanGraph) newGraph, indexName).status(SchemaStatus.ENABLED).call();
}
 
Example 19
Project: keti   File: GraphConfig.java   View source code 5 votes vote down vote up
public static void createEdgeIndex(final Graph newGraph, final String indexName, final String label,
        final String indexKey) throws InterruptedException {
    newGraph.tx().rollback(); // Never create new indexes while a transaction is active
    TitanManagement mgmt = ((TitanGraph) newGraph).openManagement();
    EdgeLabel edgeLabel = mgmt.getOrCreateEdgeLabel(label);
    if (!mgmt.containsRelationIndex(edgeLabel, indexName)) {
        PropertyKey indexPropertyKey = getOrCreatePropertyKey(indexKey, String.class, mgmt);
        mgmt.buildEdgeIndex(edgeLabel, indexName, Direction.OUT, indexPropertyKey);
    }
    mgmt.commit();
    // Wait for the index to become available
    ManagementSystem.awaitRelationIndexStatus((TitanGraph) newGraph, indexName, label).status(SchemaStatus.ENABLED)
            .call();
}
 
Example 20
Project: titan1withtp3.1   File: AbstractIndexManagementIT.java   View source code 5 votes vote down vote up
@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));
}
 
Example 21
Project: titan1withtp3.1   File: AbstractIndexManagementIT.java   View source code 5 votes vote down vote up
@Test
public void testRemoveRelationIndex() throws InterruptedException, BackendException, ExecutionException {
    tx.commit();
    mgmt.commit();

    // Load the "Graph of the Gods" sample data
    GraphOfTheGodsFactory.loadWithoutMixedIndex(graph, true);

    // Disable the "battlesByTime" index
    TitanManagement m = graph.openManagement();
    RelationType battled = m.getRelationType("battled");
    RelationTypeIndex battlesByTime = m.getRelationIndex(battled, "battlesByTime");
    m.updateIndex(battlesByTime, SchemaAction.DISABLE_INDEX);
    m.commit();
    graph.tx().commit();

    // Block until the SchemaStatus transitions to DISABLED
    assertTrue(ManagementSystem.awaitRelationIndexStatus(graph, "battlesByTime", "battled")
            .status(SchemaStatus.DISABLED).call().getSucceeded());

    // Remove index
    MapReduceIndexManagement mri = new MapReduceIndexManagement(graph);
    m = graph.openManagement();
    battled = m.getRelationType("battled");
    battlesByTime = m.getRelationIndex(battled, "battlesByTime");
    ScanMetrics metrics = mri.updateIndex(battlesByTime, SchemaAction.REMOVE_INDEX).get();

    assertEquals(6, metrics.getCustom(IndexRemoveJob.DELETED_RECORDS_COUNT));
}
 
Example 22
Project: titan1withtp3.1   File: AbstractIndexManagementIT.java   View source code 5 votes vote down vote up
@Test
public void testRepairRelationIndex() throws InterruptedException, BackendException, ExecutionException {
    tx.commit();
    mgmt.commit();

    // Load the "Graph of the Gods" sample data (WITHOUT mixed index coverage)
    GraphOfTheGodsFactory.loadWithoutMixedIndex(graph, true);

    // Create and enable a relation index on lives edges by reason
    TitanManagement m = graph.openManagement();
    PropertyKey reason = m.getPropertyKey("reason");
    EdgeLabel lives = m.getEdgeLabel("lives");
    m.buildEdgeIndex(lives, "livesByReason", Direction.BOTH, Order.decr, reason);
    m.commit();
    graph.tx().commit();

    // Block until the SchemaStatus transitions to REGISTERED
    assertTrue(ManagementSystem.awaitRelationIndexStatus(graph, "livesByReason", "lives")
            .status(SchemaStatus.REGISTERED).call().getSucceeded());

    m = graph.openManagement();
    RelationTypeIndex index = m.getRelationIndex(m.getRelationType("lives"), "livesByReason");
    m.updateIndex(index, SchemaAction.ENABLE_INDEX);
    m.commit();
    graph.tx().commit();

    // Block until the SchemaStatus transitions to ENABLED
    assertTrue(ManagementSystem.awaitRelationIndexStatus(graph, "livesByReason", "lives")
            .status(SchemaStatus.ENABLED).call().getSucceeded());

    // Run a query that hits the index but erroneously returns nothing because we haven't repaired yet
    //assertFalse(graph.query().has("reason", "no fear of death").edges().iterator().hasNext());

    // Repair
    MapReduceIndexManagement mri = new MapReduceIndexManagement(graph);
    m = graph.openManagement();
    index = m.getRelationIndex(m.getRelationType("lives"), "livesByReason");
    ScanMetrics metrics = mri.updateIndex(index, SchemaAction.REINDEX).get();
    assertEquals(8, metrics.getCustom(IndexRepairJob.ADDED_RECORDS_COUNT));
}
 
Example 23
Project: titan1withtp3.1   File: ManagementSystem.java   View source code 5 votes vote down vote up
@Override
public void addIndexKey(final TitanGraphIndex index, final PropertyKey key, Parameter... parameters) {
    Preconditions.checkArgument(index != null && key != null && index instanceof TitanGraphIndexWrapper
            && !(key instanceof BaseKey), "Need to provide valid index and key");
    if (parameters == null) parameters = new Parameter[0];
    IndexType indexType = ((TitanGraphIndexWrapper) index).getBaseIndex();
    Preconditions.checkArgument(indexType instanceof MixedIndexType, "Can only add keys to an external index, not %s", index.name());
    Preconditions.checkArgument(indexType instanceof IndexTypeWrapper && key instanceof TitanSchemaVertex
            && ((IndexTypeWrapper) indexType).getSchemaBase() instanceof TitanSchemaVertex);

    TitanSchemaVertex indexVertex = (TitanSchemaVertex) ((IndexTypeWrapper) indexType).getSchemaBase();

    for (IndexField field : indexType.getFieldKeys())
        Preconditions.checkArgument(!field.getFieldKey().equals(key), "Key [%s] has already been added to index %s", key.name(), index.name());

    //Assemble parameters
    boolean addMappingParameter = !ParameterType.MAPPED_NAME.hasParameter(parameters);
    Parameter[] extendedParas = new Parameter[parameters.length + 1 + (addMappingParameter ? 1 : 0)];
    System.arraycopy(parameters, 0, extendedParas, 0, parameters.length);
    int arrPosition = parameters.length;
    if (addMappingParameter) extendedParas[arrPosition++] = ParameterType.MAPPED_NAME.getParameter(
            graph.getIndexSerializer().getDefaultFieldName(key, parameters, indexType.getBackingIndexName()));
    extendedParas[arrPosition++] = ParameterType.STATUS.getParameter(key.isNew() ? SchemaStatus.ENABLED : SchemaStatus.INSTALLED);

    addSchemaEdge(indexVertex, key, TypeDefinitionCategory.INDEX_FIELD, extendedParas);
    updateSchemaVertex(indexVertex);
    indexType.resetCache();
    //Check to see if the index supports this
    if (!graph.getIndexSerializer().supports((MixedIndexType) indexType, ParameterIndexField.of(key, parameters))) {
        throw new TitanException("Could not register new index field '" + key.name() + "' with index backend as the data type, cardinality or parameter combination is not supported.");
    }

    try {
        IndexSerializer.register((MixedIndexType) indexType, key, transaction.getTxHandle());
    } catch (BackendException e) {
        throw new TitanException("Could not register new index field with index backend", e);
    }
    if (!indexVertex.isNew()) updatedTypes.add(indexVertex);
    if (!key.isNew()) updateIndex(index, SchemaAction.REGISTER_INDEX);
}
 
Example 24
Project: titan1withtp3.1   File: ManagementSystem.java   View source code 5 votes vote down vote up
private TitanGraphIndex createCompositeIndex(String indexName, ElementCategory elementCategory, boolean unique, TitanSchemaType constraint, PropertyKey... keys) {
    checkIndexName(indexName);
    Preconditions.checkArgument(keys != null && keys.length > 0, "Need to provide keys to index [%s]", indexName);
    Preconditions.checkArgument(!unique || elementCategory == ElementCategory.VERTEX, "Unique indexes can only be created on vertices [%s]", indexName);
    boolean allSingleKeys = true;
    boolean oneNewKey = false;
    for (PropertyKey key : keys) {
        Preconditions.checkArgument(key != null && key instanceof PropertyKeyVertex, "Need to provide valid keys: %s", key);
        if (key.cardinality() != Cardinality.SINGLE) allSingleKeys = false;
        if (key.isNew()) oneNewKey = true;
        else updatedTypes.add((PropertyKeyVertex) key);
    }

    Cardinality indexCardinality;
    if (unique) indexCardinality = Cardinality.SINGLE;
    else indexCardinality = (allSingleKeys ? Cardinality.SET : Cardinality.LIST);

    TypeDefinitionMap def = new TypeDefinitionMap();
    def.setValue(TypeDefinitionCategory.INTERNAL_INDEX, true);
    def.setValue(TypeDefinitionCategory.ELEMENT_CATEGORY, elementCategory);
    def.setValue(TypeDefinitionCategory.BACKING_INDEX, Token.INTERNAL_INDEX_NAME);
    def.setValue(TypeDefinitionCategory.INDEXSTORE_NAME, indexName);
    def.setValue(TypeDefinitionCategory.INDEX_CARDINALITY, indexCardinality);
    def.setValue(TypeDefinitionCategory.STATUS, oneNewKey ? SchemaStatus.ENABLED : SchemaStatus.INSTALLED);
    TitanSchemaVertex indexVertex = transaction.makeSchemaVertex(TitanSchemaCategory.GRAPHINDEX, indexName, def);
    for (int i = 0; i < keys.length; i++) {
        Parameter[] paras = {ParameterType.INDEX_POSITION.getParameter(i)};
        addSchemaEdge(indexVertex, keys[i], TypeDefinitionCategory.INDEX_FIELD, paras);
    }

    Preconditions.checkArgument(constraint == null || (elementCategory.isValidConstraint(constraint) && constraint instanceof TitanSchemaVertex));
    if (constraint != null) {
        addSchemaEdge(indexVertex, (TitanSchemaVertex) constraint, TypeDefinitionCategory.INDEX_SCHEMA_CONSTRAINT, null);
    }
    updateSchemaVertex(indexVertex);
    TitanGraphIndexWrapper index = new TitanGraphIndexWrapper(indexVertex.asIndexType());
    if (!oneNewKey) updateIndex(index, SchemaAction.REGISTER_INDEX);
    return index;
}
 
Example 25
Project: titan1withtp3.1   File: ManagementSystem.java   View source code 5 votes vote down vote up
private UpdateStatusTrigger(StandardTitanGraph graph, TitanSchemaVertex vertex, SchemaStatus newStatus, Iterable<PropertyKeyVertex> keys) {
    this.graph = graph;
    this.schemaVertexId = vertex.longId();
    this.newStatus = newStatus;
    this.propertyKeys = Sets.newHashSet(Iterables.transform(keys, new Function<PropertyKey, Long>() {
        @Nullable
        @Override
        public Long apply(@Nullable PropertyKey propertyKey) {
            return propertyKey.longId();
        }
    }));
}
 
Example 26
Project: titan1withtp3.1   File: RelationIndexStatusWatcher.java   View source code 5 votes vote down vote up
/**
 * Poll a relation index until it has a certain {@link SchemaStatus},
 * or until a configurable timeout is exceeded.
 *
 * @return a report with information about schema state, execution duration, and the index
 */
@Override
public RelationIndexStatusReport call() throws InterruptedException {
    Preconditions.checkNotNull(g, "Graph instance must not be null");
    Preconditions.checkNotNull(relationIndexName, "Index name must not be null");
    Preconditions.checkNotNull(status, "Target status must not be null");

    RelationTypeIndex idx;

    Timer t = new Timer(TimestampProviders.MILLI).start();
    boolean timedOut;
    while (true) {
        SchemaStatus actualStatus = null;
        TitanManagement mgmt = null;
        try {
            mgmt = g.openManagement();
            idx = mgmt.getRelationIndex(mgmt.getRelationType(relationTypeName), relationIndexName);
            actualStatus = idx.getIndexStatus();
            LOGGER.info("Index {} (relation type {}) has status {}", relationIndexName, relationTypeName, actualStatus);
            if (status.equals(actualStatus)) {
                return new RelationIndexStatusReport(true, relationIndexName, relationTypeName, actualStatus, status, t.elapsed());
            }
        } finally {
            if (null != mgmt)
                mgmt.rollback(); // Let an exception here propagate up the stack
        }

        timedOut = null != timeout && 0 < t.elapsed().compareTo(timeout);

        if (timedOut) {
            LOGGER.info("Timed out ({}) while waiting for index {} (relation type {}) to reach status {}",
                    timeout, relationIndexName, relationTypeName, status);
            return new RelationIndexStatusReport(false, relationIndexName, relationTypeName, actualStatus, status, t.elapsed());
        }

        Thread.sleep(poll.toMillis());
    }
}
 
Example 27
Project: titan1withtp3.1   File: RelationIndexStatusReport.java   View source code 5 votes vote down vote up
public RelationIndexStatusReport(boolean succeeded, String indexName, String relationTypeName, SchemaStatus actualStatus,
                               SchemaStatus targetStatus, Duration elapsed) {
    this.succeeded = succeeded;
    this.indexName = indexName;
    this.relationTypeName = relationTypeName;
    this.actualStatus = actualStatus;
    this.targetStatus = targetStatus;
    this.elapsed = elapsed;
}
 
Example 28
Project: titan1withtp3.1   File: GraphIndexStatusReport.java   View source code 5 votes vote down vote up
public GraphIndexStatusReport(boolean success, String indexName, SchemaStatus targetStatus,
               Map<String, SchemaStatus> notConverged,
               Map<String, SchemaStatus> converged, Duration elapsed) {
    this.success = success;
    this.indexName = indexName;
    this.targetStatus = targetStatus;
    this.notConverged = notConverged;
    this.converged = converged;
    this.elapsed = elapsed;
}
 
Example 29
Project: antioch   File: TitanService.java   View source code 5 votes vote down vote up
private static void waitForCompletion(TitanManagement mgmt, VertexCompositeIndex compositeIndex) {
  String name = compositeIndex.name;
  TitanGraphIndex graphIndex = mgmt.getGraphIndex(name);
  PropertyKey propertyKey = graphIndex.getFieldKeys()[0];
  // For composite indexes, the propertyKey is ignored and the status of the index as a whole is returned
  if (!SchemaStatus.ENABLED.equals(graphIndex.getIndexStatus(propertyKey))) {
    try {
      GraphIndexStatusReport report = ManagementSystem.awaitGraphIndexStatus(titanGraph, name).call();
      LOG.info("report={}", report);
    } catch (InterruptedException e) {
      e.printStackTrace();
      throw new RuntimeException(e);
    }
  }
}
 
Example 30
Project: titan1.0.1.kafka   File: AbstractIndexManagementIT.java   View source code 5 votes vote down vote up
@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));
}
 
Example 31
Project: titan1.0.1.kafka   File: AbstractIndexManagementIT.java   View source code 5 votes vote down vote up
@Test
public void testRemoveRelationIndex() throws InterruptedException, BackendException, ExecutionException {
    tx.commit();
    mgmt.commit();

    // Load the "Graph of the Gods" sample data
    GraphOfTheGodsFactory.loadWithoutMixedIndex(graph, true);

    // Disable the "battlesByTime" index
    TitanManagement m = graph.openManagement();
    RelationType battled = m.getRelationType("battled");
    RelationTypeIndex battlesByTime = m.getRelationIndex(battled, "battlesByTime");
    m.updateIndex(battlesByTime, SchemaAction.DISABLE_INDEX);
    m.commit();
    graph.tx().commit();

    // Block until the SchemaStatus transitions to DISABLED
    assertTrue(ManagementSystem.awaitRelationIndexStatus(graph, "battlesByTime", "battled")
            .status(SchemaStatus.DISABLED).call().getSucceeded());

    // Remove index
    MapReduceIndexManagement mri = new MapReduceIndexManagement(graph);
    m = graph.openManagement();
    battled = m.getRelationType("battled");
    battlesByTime = m.getRelationIndex(battled, "battlesByTime");
    ScanMetrics metrics = mri.updateIndex(battlesByTime, SchemaAction.REMOVE_INDEX).get();

    assertEquals(6, metrics.getCustom(IndexRemoveJob.DELETED_RECORDS_COUNT));
}
 
Example 32
Project: titan1.0.1.kafka   File: AbstractIndexManagementIT.java   View source code 5 votes vote down vote up
@Test
public void testRepairRelationIndex() throws InterruptedException, BackendException, ExecutionException {
    tx.commit();
    mgmt.commit();

    // Load the "Graph of the Gods" sample data (WITHOUT mixed index coverage)
    GraphOfTheGodsFactory.loadWithoutMixedIndex(graph, true);

    // Create and enable a relation index on lives edges by reason
    TitanManagement m = graph.openManagement();
    PropertyKey reason = m.getPropertyKey("reason");
    EdgeLabel lives = m.getEdgeLabel("lives");
    m.buildEdgeIndex(lives, "livesByReason", Direction.BOTH, Order.decr, reason);
    m.commit();
    graph.tx().commit();

    // Block until the SchemaStatus transitions to REGISTERED
    assertTrue(ManagementSystem.awaitRelationIndexStatus(graph, "livesByReason", "lives")
            .status(SchemaStatus.REGISTERED).call().getSucceeded());

    m = graph.openManagement();
    RelationTypeIndex index = m.getRelationIndex(m.getRelationType("lives"), "livesByReason");
    m.updateIndex(index, SchemaAction.ENABLE_INDEX);
    m.commit();
    graph.tx().commit();

    // Block until the SchemaStatus transitions to ENABLED
    assertTrue(ManagementSystem.awaitRelationIndexStatus(graph, "livesByReason", "lives")
            .status(SchemaStatus.ENABLED).call().getSucceeded());

    // Run a query that hits the index but erroneously returns nothing because we haven't repaired yet
    //assertFalse(graph.query().has("reason", "no fear of death").edges().iterator().hasNext());

    // Repair
    MapReduceIndexManagement mri = new MapReduceIndexManagement(graph);
    m = graph.openManagement();
    index = m.getRelationIndex(m.getRelationType("lives"), "livesByReason");
    ScanMetrics metrics = mri.updateIndex(index, SchemaAction.REINDEX).get();
    assertEquals(8, metrics.getCustom(IndexRepairJob.ADDED_RECORDS_COUNT));
}
 
Example 33
Project: titan1.0.1.kafka   File: ManagementSystem.java   View source code 5 votes vote down vote up
@Override
public void addIndexKey(final TitanGraphIndex index, final PropertyKey key, Parameter... parameters) {
    Preconditions.checkArgument(index != null && key != null && index instanceof TitanGraphIndexWrapper
            && !(key instanceof BaseKey), "Need to provide valid index and key");
    if (parameters == null) parameters = new Parameter[0];
    IndexType indexType = ((TitanGraphIndexWrapper) index).getBaseIndex();
    Preconditions.checkArgument(indexType instanceof MixedIndexType, "Can only add keys to an external index, not %s", index.name());
    Preconditions.checkArgument(indexType instanceof IndexTypeWrapper && key instanceof TitanSchemaVertex
            && ((IndexTypeWrapper) indexType).getSchemaBase() instanceof TitanSchemaVertex);

    TitanSchemaVertex indexVertex = (TitanSchemaVertex) ((IndexTypeWrapper) indexType).getSchemaBase();

    for (IndexField field : indexType.getFieldKeys())
        Preconditions.checkArgument(!field.getFieldKey().equals(key), "Key [%s] has already been added to index %s", key.name(), index.name());

    //Assemble parameters
    boolean addMappingParameter = !ParameterType.MAPPED_NAME.hasParameter(parameters);
    Parameter[] extendedParas = new Parameter[parameters.length + 1 + (addMappingParameter ? 1 : 0)];
    System.arraycopy(parameters, 0, extendedParas, 0, parameters.length);
    int arrPosition = parameters.length;
    if (addMappingParameter) extendedParas[arrPosition++] = ParameterType.MAPPED_NAME.getParameter(
            graph.getIndexSerializer().getDefaultFieldName(key, parameters, indexType.getBackingIndexName()));
    extendedParas[arrPosition++] = ParameterType.STATUS.getParameter(key.isNew() ? SchemaStatus.ENABLED : SchemaStatus.INSTALLED);

    addSchemaEdge(indexVertex, key, TypeDefinitionCategory.INDEX_FIELD, extendedParas);
    updateSchemaVertex(indexVertex);
    indexType.resetCache();
    //Check to see if the index supports this
    if (!graph.getIndexSerializer().supports((MixedIndexType) indexType, ParameterIndexField.of(key, parameters))) {
        throw new TitanException("Could not register new index field '" + key.name() + "' with index backend as the data type, cardinality or parameter combination is not supported.");
    }

    try {
        IndexSerializer.register((MixedIndexType) indexType, key, transaction.getTxHandle());
    } catch (BackendException e) {
        throw new TitanException("Could not register new index field with index backend", e);
    }
    if (!indexVertex.isNew()) updatedTypes.add(indexVertex);
    if (!key.isNew()) updateIndex(index, SchemaAction.REGISTER_INDEX);
}
 
Example 34
Project: titan1.0.1.kafka   File: ManagementSystem.java   View source code 5 votes vote down vote up
private TitanGraphIndex createCompositeIndex(String indexName, ElementCategory elementCategory, boolean unique, TitanSchemaType constraint, PropertyKey... keys) {
    checkIndexName(indexName);
    Preconditions.checkArgument(keys != null && keys.length > 0, "Need to provide keys to index [%s]", indexName);
    Preconditions.checkArgument(!unique || elementCategory == ElementCategory.VERTEX, "Unique indexes can only be created on vertices [%s]", indexName);
    boolean allSingleKeys = true;
    boolean oneNewKey = false;
    for (PropertyKey key : keys) {
        Preconditions.checkArgument(key != null && key instanceof PropertyKeyVertex, "Need to provide valid keys: %s", key);
        if (key.cardinality() != Cardinality.SINGLE) allSingleKeys = false;
        if (key.isNew()) oneNewKey = true;
        else updatedTypes.add((PropertyKeyVertex) key);
    }

    Cardinality indexCardinality;
    if (unique) indexCardinality = Cardinality.SINGLE;
    else indexCardinality = (allSingleKeys ? Cardinality.SET : Cardinality.LIST);

    TypeDefinitionMap def = new TypeDefinitionMap();
    def.setValue(TypeDefinitionCategory.INTERNAL_INDEX, true);
    def.setValue(TypeDefinitionCategory.ELEMENT_CATEGORY, elementCategory);
    def.setValue(TypeDefinitionCategory.BACKING_INDEX, Token.INTERNAL_INDEX_NAME);
    def.setValue(TypeDefinitionCategory.INDEXSTORE_NAME, indexName);
    def.setValue(TypeDefinitionCategory.INDEX_CARDINALITY, indexCardinality);
    def.setValue(TypeDefinitionCategory.STATUS, oneNewKey ? SchemaStatus.ENABLED : SchemaStatus.INSTALLED);
    TitanSchemaVertex indexVertex = transaction.makeSchemaVertex(TitanSchemaCategory.GRAPHINDEX, indexName, def);
    for (int i = 0; i < keys.length; i++) {
        Parameter[] paras = {ParameterType.INDEX_POSITION.getParameter(i)};
        addSchemaEdge(indexVertex, keys[i], TypeDefinitionCategory.INDEX_FIELD, paras);
    }

    Preconditions.checkArgument(constraint == null || (elementCategory.isValidConstraint(constraint) && constraint instanceof TitanSchemaVertex));
    if (constraint != null) {
        addSchemaEdge(indexVertex, (TitanSchemaVertex) constraint, TypeDefinitionCategory.INDEX_SCHEMA_CONSTRAINT, null);
    }
    updateSchemaVertex(indexVertex);
    TitanGraphIndexWrapper index = new TitanGraphIndexWrapper(indexVertex.asIndexType());
    if (!oneNewKey) updateIndex(index, SchemaAction.REGISTER_INDEX);
    return index;
}
 
Example 35
Project: titan1.0.1.kafka   File: ManagementSystem.java   View source code 5 votes vote down vote up
private UpdateStatusTrigger(StandardTitanGraph graph, TitanSchemaVertex vertex, SchemaStatus newStatus, Iterable<PropertyKeyVertex> keys) {
    this.graph = graph;
    this.schemaVertexId = vertex.longId();
    this.newStatus = newStatus;
    this.propertyKeys = Sets.newHashSet(Iterables.transform(keys, new Function<PropertyKey, Long>() {
        @Nullable
        @Override
        public Long apply(@Nullable PropertyKey propertyKey) {
            return propertyKey.longId();
        }
    }));
}
 
Example 36
Project: titan1.0.1.kafka   File: RelationIndexStatusWatcher.java   View source code 5 votes vote down vote up
/**
 * Poll a relation index until it has a certain {@link SchemaStatus},
 * or until a configurable timeout is exceeded.
 *
 * @return a report with information about schema state, execution duration, and the index
 */
@Override
public RelationIndexStatusReport call() throws InterruptedException {
    Preconditions.checkNotNull(g, "Graph instance must not be null");
    Preconditions.checkNotNull(relationIndexName, "Index name must not be null");
    Preconditions.checkNotNull(status, "Target status must not be null");

    RelationTypeIndex idx;

    Timer t = new Timer(TimestampProviders.MILLI).start();
    boolean timedOut;
    while (true) {
        SchemaStatus actualStatus = null;
        TitanManagement mgmt = null;
        try {
            mgmt = g.openManagement();
            idx = mgmt.getRelationIndex(mgmt.getRelationType(relationTypeName), relationIndexName);
            actualStatus = idx.getIndexStatus();
            LOGGER.info("Index {} (relation type {}) has status {}", relationIndexName, relationTypeName, actualStatus);
            if (status.equals(actualStatus)) {
                return new RelationIndexStatusReport(true, relationIndexName, relationTypeName, actualStatus, status, t.elapsed());
            }
        } finally {
            if (null != mgmt)
                mgmt.rollback(); // Let an exception here propagate up the stack
        }

        timedOut = null != timeout && 0 < t.elapsed().compareTo(timeout);

        if (timedOut) {
            LOGGER.info("Timed out ({}) while waiting for index {} (relation type {}) to reach status {}",
                    timeout, relationIndexName, relationTypeName, status);
            return new RelationIndexStatusReport(false, relationIndexName, relationTypeName, actualStatus, status, t.elapsed());
        }

        Thread.sleep(poll.toMillis());
    }
}
 
Example 37
Project: titan1.0.1.kafka   File: RelationIndexStatusReport.java   View source code 5 votes vote down vote up
public RelationIndexStatusReport(boolean succeeded, String indexName, String relationTypeName, SchemaStatus actualStatus,
                               SchemaStatus targetStatus, Duration elapsed) {
    this.succeeded = succeeded;
    this.indexName = indexName;
    this.relationTypeName = relationTypeName;
    this.actualStatus = actualStatus;
    this.targetStatus = targetStatus;
    this.elapsed = elapsed;
}
 
Example 38
Project: titan1.0.1.kafka   File: GraphIndexStatusReport.java   View source code 5 votes vote down vote up
public GraphIndexStatusReport(boolean success, String indexName, SchemaStatus targetStatus,
               Map<String, SchemaStatus> notConverged,
               Map<String, SchemaStatus> converged, Duration elapsed) {
    this.success = success;
    this.indexName = indexName;
    this.targetStatus = targetStatus;
    this.notConverged = notConverged;
    this.converged = converged;
    this.elapsed = elapsed;
}
 
Example 39
Project: titan0.5.4-hbase1.1.1-custom   File: ManagementSystem.java   View source code 5 votes vote down vote up
@Override
public void addIndexKey(final TitanGraphIndex index, final PropertyKey key, Parameter... parameters) {
    Preconditions.checkArgument(index!=null && key!=null && index instanceof TitanGraphIndexWrapper
            && !(key instanceof BaseKey),"Need to provide valid index and key");
    if (parameters==null) parameters=new Parameter[0];
    IndexType indexType = ((TitanGraphIndexWrapper)index).getBaseIndex();
    Preconditions.checkArgument(indexType instanceof MixedIndexType,"Can only add keys to an external index, not %s",index.getName());
    Preconditions.checkArgument(indexType instanceof IndexTypeWrapper && key instanceof TitanSchemaVertex
        && ((IndexTypeWrapper)indexType).getSchemaBase() instanceof TitanSchemaVertex);
    Preconditions.checkArgument(key.getCardinality()==Cardinality.SINGLE || indexType.getElement()!=ElementCategory.VERTEX,
            "Can only index single-valued property keys on vertices [%s]",key);
    TitanSchemaVertex indexVertex = (TitanSchemaVertex)((IndexTypeWrapper)indexType).getSchemaBase();

    for (IndexField field : indexType.getFieldKeys())
        Preconditions.checkArgument(!field.getFieldKey().equals(key),"Key [%s] has already been added to index %s",key.getName(),index.getName());

    //Assemble parameters
    boolean addMappingParameter = !ParameterType.MAPPED_NAME.hasParameter(parameters);
    Parameter[] extendedParas = new Parameter[parameters.length+1+(addMappingParameter?1:0)];
    System.arraycopy(parameters,0,extendedParas,0,parameters.length);
    int arrPosition = parameters.length;
    if (addMappingParameter) extendedParas[arrPosition++] = ParameterType.MAPPED_NAME.getParameter(
            graph.getIndexSerializer().getDefaultFieldName(key,parameters,indexType.getBackingIndexName()));
    extendedParas[arrPosition++] = ParameterType.STATUS.getParameter(key.isNew()?SchemaStatus.ENABLED:SchemaStatus.INSTALLED);

    addSchemaEdge(indexVertex, key, TypeDefinitionCategory.INDEX_FIELD, extendedParas);
    updateSchemaVertex(indexVertex);
    indexType.resetCache();
    try {
        IndexSerializer.register((MixedIndexType) indexType,key,transaction.getTxHandle());
    } catch (BackendException e) {
        throw new TitanException("Could not register new index field with index backend",e);
    }
    if (!indexVertex.isNew()) updatedTypes.add(indexVertex);
    if (!key.isNew()) updateIndex(index, SchemaAction.REGISTER_INDEX);
}
 
Example 40
Project: titan0.5.4-hbase1.1.1-custom   File: ManagementSystem.java   View source code 5 votes vote down vote up
private TitanGraphIndex createCompositeIndex(String indexName, ElementCategory elementCategory, boolean unique, TitanSchemaType constraint, PropertyKey... keys) {
    checkIndexName(indexName);
    Preconditions.checkArgument(keys!=null && keys.length>0,"Need to provide keys to index [%s]",indexName);
    Preconditions.checkArgument(!unique || elementCategory==ElementCategory.VERTEX,"Unique indexes can only be created on vertices [%s]",indexName);
    boolean allSingleKeys = true;
    boolean oneNewKey = false;
    for (PropertyKey key : keys) {
        Preconditions.checkArgument(key!=null && key instanceof PropertyKeyVertex,"Need to provide valid keys: %s",key);
        if (key.getCardinality()!=Cardinality.SINGLE) allSingleKeys=false;
        if (key.isNew()) oneNewKey = true;
        else updatedTypes.add((PropertyKeyVertex)key);
    }

    Cardinality indexCardinality;
    if (unique) indexCardinality=Cardinality.SINGLE;
    else indexCardinality=(allSingleKeys?Cardinality.SET:Cardinality.LIST);

    TypeDefinitionMap def = new TypeDefinitionMap();
    def.setValue(TypeDefinitionCategory.INTERNAL_INDEX,true);
    def.setValue(TypeDefinitionCategory.ELEMENT_CATEGORY,elementCategory);
    def.setValue(TypeDefinitionCategory.BACKING_INDEX,Token.INTERNAL_INDEX_NAME);
    def.setValue(TypeDefinitionCategory.INDEXSTORE_NAME,indexName);
    def.setValue(TypeDefinitionCategory.INDEX_CARDINALITY,indexCardinality);
    def.setValue(TypeDefinitionCategory.STATUS,oneNewKey?SchemaStatus.ENABLED:SchemaStatus.INSTALLED);
    TitanSchemaVertex indexVertex = transaction.makeSchemaVertex(TitanSchemaCategory.GRAPHINDEX,indexName,def);
    for (int i = 0; i <keys.length; i++) {
        Parameter[] paras = {ParameterType.INDEX_POSITION.getParameter(i)};
        addSchemaEdge(indexVertex,keys[i],TypeDefinitionCategory.INDEX_FIELD,paras);
    }

    Preconditions.checkArgument(constraint==null || (elementCategory.isValidConstraint(constraint) && constraint instanceof TitanSchemaVertex));
    if (constraint!=null) {
        addSchemaEdge(indexVertex,(TitanSchemaVertex)constraint,TypeDefinitionCategory.INDEX_SCHEMA_CONSTRAINT,null);
    }
    updateSchemaVertex(indexVertex);
    TitanGraphIndexWrapper index = new TitanGraphIndexWrapper(indexVertex.asIndexType());
    if (!oneNewKey) updateIndex(index,SchemaAction.REGISTER_INDEX);
    return index;
}