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

The following are top voted examples for showing how to use com.thinkaurelius.titan.core.schema.RelationTypeIndex. 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: titan1withtp3.1   File: ManagementSystem.java   View source code 6 votes vote down vote up
@Override
public Iterable<RelationTypeIndex> getRelationIndexes(final RelationType type) {
    Preconditions.checkArgument(type != null && type instanceof InternalRelationType, "Invalid relation type provided: %s", type);
    return Iterables.transform(Iterables.filter(((InternalRelationType) type).getRelationIndexes(), new Predicate<InternalRelationType>() {
        @Override
        public boolean apply(@Nullable InternalRelationType internalRelationType) {
            return !type.equals(internalRelationType);
        }
    }), new Function<InternalRelationType, RelationTypeIndex>() {
        @Nullable
        @Override
        public RelationTypeIndex apply(@Nullable InternalRelationType internalType) {
            return new RelationTypeIndexWrapper(internalType);
        }
    });
}
 
Example 2
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 3
Project: titan1.0.1.kafka   File: ManagementSystem.java   View source code 6 votes vote down vote up
@Override
public Iterable<RelationTypeIndex> getRelationIndexes(final RelationType type) {
    Preconditions.checkArgument(type != null && type instanceof InternalRelationType, "Invalid relation type provided: %s", type);
    return Iterables.transform(Iterables.filter(((InternalRelationType) type).getRelationIndexes(), new Predicate<InternalRelationType>() {
        @Override
        public boolean apply(@Nullable InternalRelationType internalRelationType) {
            return !type.equals(internalRelationType);
        }
    }), new Function<InternalRelationType, RelationTypeIndex>() {
        @Nullable
        @Override
        public RelationTypeIndex apply(@Nullable InternalRelationType internalType) {
            return new RelationTypeIndexWrapper(internalType);
        }
    });
}
 
Example 4
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 5
Project: titan0.5.4-hbase1.1.1-custom   File: ManagementSystem.java   View source code 6 votes vote down vote up
@Override
public Iterable<RelationTypeIndex> getRelationIndexes(final RelationType type) {
    Preconditions.checkArgument(type!=null && type instanceof InternalRelationType,"Invalid relation type provided: %s",type);
    return Iterables.transform(Iterables.filter(((InternalRelationType) type).getRelationIndexes(), new Predicate<InternalRelationType>() {
        @Override
        public boolean apply(@Nullable InternalRelationType internalRelationType) {
            return !type.equals(internalRelationType);
        }
    }),new Function<InternalRelationType, RelationTypeIndex>() {
        @Nullable
        @Override
        public RelationTypeIndex apply(@Nullable InternalRelationType internalType) {
            return new RelationTypeIndexWrapper(internalType);
        }
    });
}
 
Example 6
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 7
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 8
Project: titan1withtp3.1   File: ManagementSystem.java   View source code 5 votes vote down vote up
@Override
public RelationTypeIndex getRelationIndex(RelationType type, String name) {
    Preconditions.checkArgument(type != null);
    Preconditions.checkArgument(StringUtils.isNotBlank(name));
    String composedName = composeRelationTypeIndexName(type, name);

    //Don't use SchemaCache to make code more compact and since we don't need the extra performance here
    TitanVertex v = Iterables.getOnlyElement(QueryUtil.getVertices(transaction, BaseKey.SchemaName, TitanSchemaCategory.getRelationTypeName(composedName)), null);
    if (v == null) return null;
    assert v instanceof InternalRelationType;
    return new RelationTypeIndexWrapper((InternalRelationType) v);
}
 
Example 9
Project: titan1withtp3.1   File: ManagementSystem.java   View source code 5 votes vote down vote up
private IndexIdentifier(TitanIndex index) {
    Preconditions.checkArgument(index != null);
    indexName = index.name();
    if (index instanceof RelationTypeIndex) relationTypeName = ((RelationTypeIndex) index).getType().name();
    else relationTypeName = null;
    Preconditions.checkArgument(StringUtils.isNotBlank(indexName));
    hashcode = new HashCodeBuilder().append(indexName).append(relationTypeName).hashCode();
}
 
Example 10
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 11
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 12
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 13
Project: titan1.0.1.kafka   File: ManagementSystem.java   View source code 5 votes vote down vote up
@Override
public RelationTypeIndex getRelationIndex(RelationType type, String name) {
    Preconditions.checkArgument(type != null);
    Preconditions.checkArgument(StringUtils.isNotBlank(name));
    String composedName = composeRelationTypeIndexName(type, name);

    //Don't use SchemaCache to make code more compact and since we don't need the extra performance here
    TitanVertex v = Iterables.getOnlyElement(QueryUtil.getVertices(transaction, BaseKey.SchemaName, TitanSchemaCategory.getRelationTypeName(composedName)), null);
    if (v == null) return null;
    assert v instanceof InternalRelationType;
    return new RelationTypeIndexWrapper((InternalRelationType) v);
}
 
Example 14
Project: titan1.0.1.kafka   File: ManagementSystem.java   View source code 5 votes vote down vote up
private IndexIdentifier(TitanIndex index) {
    Preconditions.checkArgument(index != null);
    indexName = index.name();
    if (index instanceof RelationTypeIndex) relationTypeName = ((RelationTypeIndex) index).getType().name();
    else relationTypeName = null;
    Preconditions.checkArgument(StringUtils.isNotBlank(indexName));
    hashcode = new HashCodeBuilder().append(indexName).append(relationTypeName).hashCode();
}
 
Example 15
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 16
Project: titan0.5.4-hbase1.1.1-custom   File: ManagementSystem.java   View source code 5 votes vote down vote up
@Override
public RelationTypeIndex getRelationIndex(RelationType type, String name) {
    Preconditions.checkArgument(type!=null);
    Preconditions.checkArgument(StringUtils.isNotBlank(name));
    String composedName = composeRelationTypeIndexName(type, name);

    //Don't use SchemaCache to make code more compact and since we don't need the extra performance here
    TitanVertex v = Iterables.getOnlyElement(transaction.getVertices(BaseKey.SchemaName,TitanSchemaCategory.getRelationTypeName(composedName)),null);
    if (v==null) return null;
    assert v instanceof InternalRelationType;
    return new RelationTypeIndexWrapper((InternalRelationType)v);
}
 
Example 17
Project: titan1withtp3.1   File: TitanGraphTest.java   View source code 4 votes vote down vote up
@Test
public void testSchemaNameChange() {
    PropertyKey time = mgmt.makePropertyKey("time").dataType(Integer.class).cardinality(Cardinality.SINGLE).make();
    EdgeLabel knows = mgmt.makeEdgeLabel("knows").multiplicity(Multiplicity.MULTI).make();
    mgmt.buildEdgeIndex(knows, "byTime", Direction.BOTH, time);
    mgmt.buildIndex("timeIndex", Vertex.class).addKey(time).buildCompositeIndex();
    mgmt.makeVertexLabel("people").make();
    finishSchema();

    //CREATE SMALL GRAPH
    TitanVertex v = tx.addVertex("people");
    v.property(VertexProperty.Cardinality.single, "time", 5);
    v.addEdge("knows", v, "time", 11);

    newTx();
    v = getOnlyElement(tx.query().has("time", 5).vertices());
    assertNotNull(v);
    assertEquals("people", v.label());
    assertEquals(5, v.<Integer>value("time").intValue());
    assertCount(1, v.query().direction(Direction.IN).labels("knows").edges());
    assertCount(1, v.query().direction(Direction.OUT).labels("knows").has("time", 11).edges());
    newTx();

    //UPDATE SCHEMA NAMES

    assertTrue(mgmt.containsRelationType("knows"));
    knows = mgmt.getEdgeLabel("knows");
    mgmt.changeName(knows, "know");
    assertEquals("know", knows.name());

    assertTrue(mgmt.containsRelationIndex(knows, "byTime"));
    RelationTypeIndex rindex = mgmt.getRelationIndex(knows, "byTime");
    assertEquals("byTime", rindex.name());
    mgmt.changeName(rindex, "overTime");
    assertEquals("overTime", rindex.name());

    assertTrue(mgmt.containsVertexLabel("people"));
    VertexLabel vl = mgmt.getVertexLabel("people");
    mgmt.changeName(vl, "person");
    assertEquals("person", vl.name());

    assertTrue(mgmt.containsGraphIndex("timeIndex"));
    TitanGraphIndex gindex = mgmt.getGraphIndex("timeIndex");
    mgmt.changeName(gindex, "byTime");
    assertEquals("byTime", gindex.name());

    finishSchema();

    //VERIFY UPDATES IN MGMT SYSTEM

    assertTrue(mgmt.containsRelationType("know"));
    assertFalse(mgmt.containsRelationType("knows"));
    knows = mgmt.getEdgeLabel("know");

    assertTrue(mgmt.containsRelationIndex(knows, "overTime"));
    assertFalse(mgmt.containsRelationIndex(knows, "byTime"));

    assertTrue(mgmt.containsVertexLabel("person"));
    assertFalse(mgmt.containsVertexLabel("people"));

    assertTrue(mgmt.containsGraphIndex("byTime"));
    assertFalse(mgmt.containsGraphIndex("timeIndex"));

    //VERIFY UPDATES IN TRANSACTION
    newTx();
    v = getOnlyElement(tx.query().has("time", 5).vertices());
    assertNotNull(v);
    assertEquals("person", v.label());
    assertEquals(5, v.<Integer>value("time").intValue());
    assertCount(1, v.query().direction(Direction.IN).labels("know").edges());
    assertCount(0, v.query().direction(Direction.IN).labels("knows").edges());
    assertCount(1, v.query().direction(Direction.OUT).labels("know").has("time", 11).edges());
}
 
Example 18
Project: titan1withtp3.1   File: ManagementSystem.java   View source code 4 votes vote down vote up
@Override
public RelationTypeIndex buildEdgeIndex(EdgeLabel label, String name, Direction direction, PropertyKey... sortKeys) {
    return buildRelationTypeIndex(label, name, direction, Order.ASC, sortKeys);
}
 
Example 19
Project: titan1withtp3.1   File: ManagementSystem.java   View source code 4 votes vote down vote up
@Override
public RelationTypeIndex buildEdgeIndex(EdgeLabel label, String name, Direction direction, org.apache.tinkerpop.gremlin.process.traversal.Order sortOrder, PropertyKey... sortKeys) {
    return buildRelationTypeIndex(label, name, direction, Order.convert(sortOrder), sortKeys);
}
 
Example 20
Project: titan1withtp3.1   File: ManagementSystem.java   View source code 4 votes vote down vote up
@Override
public RelationTypeIndex buildPropertyIndex(PropertyKey key, String name, PropertyKey... sortKeys) {
    return buildRelationTypeIndex(key, name, Direction.OUT, Order.ASC, sortKeys);
}
 
Example 21
Project: titan1withtp3.1   File: ManagementSystem.java   View source code 4 votes vote down vote up
@Override
public RelationTypeIndex buildPropertyIndex(PropertyKey key, String name, org.apache.tinkerpop.gremlin.process.traversal.Order sortOrder, PropertyKey... sortKeys) {
    return buildRelationTypeIndex(key, name, Direction.OUT, Order.convert(sortOrder), sortKeys);
}
 
Example 22
Project: titan1withtp3.1   File: ManagementSystem.java   View source code 4 votes vote down vote up
private RelationTypeIndex buildRelationTypeIndex(RelationType type, String name, Direction direction, Order sortOrder, PropertyKey... sortKeys) {
    Preconditions.checkArgument(type != null && direction != null && sortOrder != null && sortKeys != null);
    Preconditions.checkArgument(StringUtils.isNotBlank(name), "Name cannot be blank: %s", name);
    Token.verifyName(name);
    Preconditions.checkArgument(sortKeys.length > 0, "Need to specify sort keys");
    for (RelationType key : sortKeys) Preconditions.checkArgument(key != null, "Keys cannot be null");
    Preconditions.checkArgument(!(type instanceof EdgeLabel) || !((EdgeLabel) type).isUnidirected() || direction == Direction.OUT,
            "Can only index uni-directed labels in the out-direction: %s", type);
    Preconditions.checkArgument(!((InternalRelationType) type).multiplicity().isConstrained(direction),
            "The relation type [%s] has a multiplicity or cardinality constraint in direction [%s] and can therefore not be indexed", type, direction);

    String composedName = composeRelationTypeIndexName(type, name);
    StandardRelationTypeMaker maker;
    if (type.isEdgeLabel()) {
        StandardEdgeLabelMaker lm = (StandardEdgeLabelMaker) transaction.makeEdgeLabel(composedName);
        lm.unidirected(direction);
        maker = lm;
    } else {
        assert type.isPropertyKey();
        assert direction == Direction.OUT;
        StandardPropertyKeyMaker lm = (StandardPropertyKeyMaker) transaction.makePropertyKey(composedName);
        lm.dataType(((PropertyKey) type).dataType());
        maker = lm;
    }
    maker.status(type.isNew() ? SchemaStatus.ENABLED : SchemaStatus.INSTALLED);
    maker.invisible();
    maker.multiplicity(Multiplicity.MULTI);
    maker.sortKey(sortKeys);
    maker.sortOrder(sortOrder);

    //Compose signature
    long[] typeSig = ((InternalRelationType) type).getSignature();
    Set<PropertyKey> signature = Sets.newHashSet();
    for (long typeId : typeSig) signature.add(transaction.getExistingPropertyKey(typeId));
    for (RelationType sortType : sortKeys) signature.remove(sortType);
    if (!signature.isEmpty()) {
        PropertyKey[] sig = signature.toArray(new PropertyKey[signature.size()]);
        maker.signature(sig);
    }
    RelationType typeIndex = maker.make();
    addSchemaEdge(type, typeIndex, TypeDefinitionCategory.RELATIONTYPE_INDEX, null);
    RelationTypeIndexWrapper index = new RelationTypeIndexWrapper((InternalRelationType) typeIndex);
    if (!type.isNew()) updateIndex(index, SchemaAction.REGISTER_INDEX);
    return index;
}
 
Example 23
Project: titan1.0.1.kafka   File: TitanGraphTest.java   View source code 4 votes vote down vote up
@Test
public void testSchemaNameChange() {
    PropertyKey time = mgmt.makePropertyKey("time").dataType(Integer.class).cardinality(Cardinality.SINGLE).make();
    EdgeLabel knows = mgmt.makeEdgeLabel("knows").multiplicity(Multiplicity.MULTI).make();
    mgmt.buildEdgeIndex(knows, "byTime", Direction.BOTH, time);
    mgmt.buildIndex("timeIndex", Vertex.class).addKey(time).buildCompositeIndex();
    mgmt.makeVertexLabel("people").make();
    finishSchema();

    //CREATE SMALL GRAPH
    TitanVertex v = tx.addVertex("people");
    v.property(VertexProperty.Cardinality.single, "time", 5);
    v.addEdge("knows", v, "time", 11);

    newTx();
    v = getOnlyElement(tx.query().has("time", 5).vertices());
    assertNotNull(v);
    assertEquals("people", v.label());
    assertEquals(5, v.<Integer>value("time").intValue());
    assertCount(1, v.query().direction(Direction.IN).labels("knows").edges());
    assertCount(1, v.query().direction(Direction.OUT).labels("knows").has("time", 11).edges());
    newTx();

    //UPDATE SCHEMA NAMES

    assertTrue(mgmt.containsRelationType("knows"));
    knows = mgmt.getEdgeLabel("knows");
    mgmt.changeName(knows, "know");
    assertEquals("know", knows.name());

    assertTrue(mgmt.containsRelationIndex(knows, "byTime"));
    RelationTypeIndex rindex = mgmt.getRelationIndex(knows, "byTime");
    assertEquals("byTime", rindex.name());
    mgmt.changeName(rindex, "overTime");
    assertEquals("overTime", rindex.name());

    assertTrue(mgmt.containsVertexLabel("people"));
    VertexLabel vl = mgmt.getVertexLabel("people");
    mgmt.changeName(vl, "person");
    assertEquals("person", vl.name());

    assertTrue(mgmt.containsGraphIndex("timeIndex"));
    TitanGraphIndex gindex = mgmt.getGraphIndex("timeIndex");
    mgmt.changeName(gindex, "byTime");
    assertEquals("byTime", gindex.name());

    finishSchema();

    //VERIFY UPDATES IN MGMT SYSTEM

    assertTrue(mgmt.containsRelationType("know"));
    assertFalse(mgmt.containsRelationType("knows"));
    knows = mgmt.getEdgeLabel("know");

    assertTrue(mgmt.containsRelationIndex(knows, "overTime"));
    assertFalse(mgmt.containsRelationIndex(knows, "byTime"));

    assertTrue(mgmt.containsVertexLabel("person"));
    assertFalse(mgmt.containsVertexLabel("people"));

    assertTrue(mgmt.containsGraphIndex("byTime"));
    assertFalse(mgmt.containsGraphIndex("timeIndex"));

    //VERIFY UPDATES IN TRANSACTION
    newTx();
    v = getOnlyElement(tx.query().has("time", 5).vertices());
    assertNotNull(v);
    assertEquals("person", v.label());
    assertEquals(5, v.<Integer>value("time").intValue());
    assertCount(1, v.query().direction(Direction.IN).labels("know").edges());
    assertCount(0, v.query().direction(Direction.IN).labels("knows").edges());
    assertCount(1, v.query().direction(Direction.OUT).labels("know").has("time", 11).edges());
}
 
Example 24
Project: titan1.0.1.kafka   File: ManagementSystem.java   View source code 4 votes vote down vote up
@Override
public RelationTypeIndex buildEdgeIndex(EdgeLabel label, String name, Direction direction, PropertyKey... sortKeys) {
    return buildRelationTypeIndex(label, name, direction, Order.ASC, sortKeys);
}
 
Example 25
Project: titan1.0.1.kafka   File: ManagementSystem.java   View source code 4 votes vote down vote up
@Override
public RelationTypeIndex buildEdgeIndex(EdgeLabel label, String name, Direction direction, org.apache.tinkerpop.gremlin.process.traversal.Order sortOrder, PropertyKey... sortKeys) {
    return buildRelationTypeIndex(label, name, direction, Order.convert(sortOrder), sortKeys);
}
 
Example 26
Project: titan1.0.1.kafka   File: ManagementSystem.java   View source code 4 votes vote down vote up
@Override
public RelationTypeIndex buildPropertyIndex(PropertyKey key, String name, PropertyKey... sortKeys) {
    return buildRelationTypeIndex(key, name, Direction.OUT, Order.ASC, sortKeys);
}
 
Example 27
Project: titan1.0.1.kafka   File: ManagementSystem.java   View source code 4 votes vote down vote up
@Override
public RelationTypeIndex buildPropertyIndex(PropertyKey key, String name, org.apache.tinkerpop.gremlin.process.traversal.Order sortOrder, PropertyKey... sortKeys) {
    return buildRelationTypeIndex(key, name, Direction.OUT, Order.convert(sortOrder), sortKeys);
}
 
Example 28
Project: titan1.0.1.kafka   File: ManagementSystem.java   View source code 4 votes vote down vote up
private RelationTypeIndex buildRelationTypeIndex(RelationType type, String name, Direction direction, Order sortOrder, PropertyKey... sortKeys) {
    Preconditions.checkArgument(type != null && direction != null && sortOrder != null && sortKeys != null);
    Preconditions.checkArgument(StringUtils.isNotBlank(name), "Name cannot be blank: %s", name);
    Token.verifyName(name);
    Preconditions.checkArgument(sortKeys.length > 0, "Need to specify sort keys");
    for (RelationType key : sortKeys) Preconditions.checkArgument(key != null, "Keys cannot be null");
    Preconditions.checkArgument(!(type instanceof EdgeLabel) || !((EdgeLabel) type).isUnidirected() || direction == Direction.OUT,
            "Can only index uni-directed labels in the out-direction: %s", type);
    Preconditions.checkArgument(!((InternalRelationType) type).multiplicity().isConstrained(direction),
            "The relation type [%s] has a multiplicity or cardinality constraint in direction [%s] and can therefore not be indexed", type, direction);

    String composedName = composeRelationTypeIndexName(type, name);
    StandardRelationTypeMaker maker;
    if (type.isEdgeLabel()) {
        StandardEdgeLabelMaker lm = (StandardEdgeLabelMaker) transaction.makeEdgeLabel(composedName);
        lm.unidirected(direction);
        maker = lm;
    } else {
        assert type.isPropertyKey();
        assert direction == Direction.OUT;
        StandardPropertyKeyMaker lm = (StandardPropertyKeyMaker) transaction.makePropertyKey(composedName);
        lm.dataType(((PropertyKey) type).dataType());
        maker = lm;
    }
    maker.status(type.isNew() ? SchemaStatus.ENABLED : SchemaStatus.INSTALLED);
    maker.invisible();
    maker.multiplicity(Multiplicity.MULTI);
    maker.sortKey(sortKeys);
    maker.sortOrder(sortOrder);

    //Compose signature
    long[] typeSig = ((InternalRelationType) type).getSignature();
    Set<PropertyKey> signature = Sets.newHashSet();
    for (long typeId : typeSig) signature.add(transaction.getExistingPropertyKey(typeId));
    for (RelationType sortType : sortKeys) signature.remove(sortType);
    if (!signature.isEmpty()) {
        PropertyKey[] sig = signature.toArray(new PropertyKey[signature.size()]);
        maker.signature(sig);
    }
    RelationType typeIndex = maker.make();
    addSchemaEdge(type, typeIndex, TypeDefinitionCategory.RELATIONTYPE_INDEX, null);
    RelationTypeIndexWrapper index = new RelationTypeIndexWrapper((InternalRelationType) typeIndex);
    if (!type.isNew()) updateIndex(index, SchemaAction.REGISTER_INDEX);
    return index;
}
 
Example 29
Project: titan0.5.4-hbase1.1.1-custom   File: TitanIndexRepairMapper.java   View source code 4 votes vote down vote up
/**
 * Check that our target index is in either the ENABLED or REGISTERED state.
 */
private void validateIndexStatus() {
    if (indexType==null || StringUtils.isBlank(indexType)) {
        index = mgmt.getGraphIndex(indexName);
    } else {
        indexRelationType = mgmt.getRelationType(indexType);
        Preconditions.checkArgument(indexRelationType!=null,"Could not find relation type: %s",indexType);
        index = mgmt.getRelationIndex(indexRelationType,indexName);
    }
    Preconditions.checkArgument(index!=null,"Could not find index: %s",indexName);
    log.info("Found index {}", indexName);
    TitanSchemaVertex schemaVertex = mgmt.getSchemaVertex(index);
    Set<SchemaStatus> acceptableStatuses = SchemaAction.REINDEX.getApplicableStatus();
    boolean isValidIndex = true;
    String invalidIndexHint;
    if (index instanceof RelationTypeIndex ||
            (index instanceof TitanGraphIndex && ((TitanGraphIndex)index).isCompositeIndex()) ) {
        SchemaStatus actualStatus = schemaVertex.getStatus();
        isValidIndex = acceptableStatuses.contains(actualStatus);
        invalidIndexHint = String.format(
                "The index has status %s, but one of %s is required",
                actualStatus, acceptableStatuses);
    } else {
        Preconditions.checkArgument(index instanceof TitanGraphIndex,"Unexpected index: %s",index);
        TitanGraphIndex gindex = (TitanGraphIndex)index;
        Preconditions.checkArgument(gindex.isMixedIndex());
        Map<String, SchemaStatus> invalidKeyStatuses = Maps.newHashMap();
        for (PropertyKey key : gindex.getFieldKeys()) {
            SchemaStatus status = gindex.getIndexStatus(key);
            if (status!=SchemaStatus.DISABLED && !acceptableStatuses.contains(status)) {
                isValidIndex=false;
                invalidKeyStatuses.put(key.getName(), status);
                log.warn("Index {} has key {} in an invalid status {}",index,key,status);
            }
        }
        invalidIndexHint = String.format(
                "The following index keys have invalid status: %s (status must be one of %s)",
                Joiner.on(",").withKeyValueSeparator(" has status ").join(invalidKeyStatuses), acceptableStatuses);
    }
    Preconditions.checkArgument(isValidIndex, "The index %s is in an invalid state and cannot be indexed. %s", indexName, invalidIndexHint);
    // TODO consider retrieving the current Job object and calling killJob() if !isValidIndex -- would be more efficient than throwing an exception on the first pair processed by each mapper
    log.debug("Index {} is valid for re-indexing");
}
 
Example 30
Project: titan0.5.4-hbase1.1.1-custom   File: ManagementSystem.java   View source code 4 votes vote down vote up
@Override
public RelationTypeIndex buildEdgeIndex(EdgeLabel label, String name, Direction direction, RelationType... sortKeys) {
    return buildEdgeIndex(label, name, direction, Order.ASC, sortKeys);
}
 
Example 31
Project: titan0.5.4-hbase1.1.1-custom   File: ManagementSystem.java   View source code 4 votes vote down vote up
@Override
public RelationTypeIndex buildEdgeIndex(EdgeLabel label, String name, Direction direction, Order sortOrder, RelationType... sortKeys) {
    return buildRelationTypeIndex(label, name, direction, sortOrder, sortKeys);
}
 
Example 32
Project: titan0.5.4-hbase1.1.1-custom   File: ManagementSystem.java   View source code 4 votes vote down vote up
@Override
public RelationTypeIndex buildPropertyIndex(PropertyKey key, String name, RelationType... sortKeys) {
    return buildPropertyIndex(key, name, Order.ASC, sortKeys);
}
 
Example 33
Project: titan0.5.4-hbase1.1.1-custom   File: ManagementSystem.java   View source code 4 votes vote down vote up
@Override
public RelationTypeIndex buildPropertyIndex(PropertyKey key, String name, Order sortOrder, RelationType... sortKeys) {
    return buildRelationTypeIndex(key, name, Direction.OUT, sortOrder, sortKeys);
}
 
Example 34
Project: titan0.5.4-hbase1.1.1-custom   File: ManagementSystem.java   View source code 4 votes vote down vote up
private RelationTypeIndex buildRelationTypeIndex(RelationType type, String name, Direction direction, Order sortOrder, RelationType... sortKeys) {
    Preconditions.checkArgument(type!=null && direction!=null && sortOrder!=null && sortKeys!=null);
    Preconditions.checkArgument(StringUtils.isNotBlank(name),"Name cannot be blank: %s",name);
    Token.verifyName(name);
    Preconditions.checkArgument(sortKeys.length>0,"Need to specify sort keys");
    for (RelationType key : sortKeys) Preconditions.checkArgument(key!=null,"Keys cannot be null");
    Preconditions.checkArgument(!(type instanceof EdgeLabel) || !((EdgeLabel)type).isUnidirected() || direction==Direction.OUT,
            "Can only index uni-directed labels in the out-direction: %s",type);
    Preconditions.checkArgument(!((InternalRelationType)type).getMultiplicity().isConstrained(direction),
            "The relation type [%s] has a multiplicity or cardinality constraint in direction [%s] and can therefore not be indexed",type,direction);

    String composedName = composeRelationTypeIndexName(type,name);
    StandardRelationTypeMaker maker;
    if (type.isEdgeLabel()) {
        StandardEdgeLabelMaker lm = (StandardEdgeLabelMaker)transaction.makeEdgeLabel(composedName);
        lm.unidirected(direction);
        maker = lm;
    } else {
        assert type.isPropertyKey();
        assert direction==Direction.OUT;
        StandardPropertyKeyMaker lm = (StandardPropertyKeyMaker)transaction.makePropertyKey(composedName);
        lm.dataType(((PropertyKey)type).getDataType());
        maker = lm;
    }
    maker.status(type.isNew()?SchemaStatus.ENABLED:SchemaStatus.INSTALLED);
    maker.hidden();
    maker.multiplicity(Multiplicity.MULTI);
    maker.sortKey(sortKeys);
    maker.sortOrder(sortOrder);

    //Compose signature
    long[] typeSig = ((InternalRelationType)type).getSignature();
    Set<RelationType> signature = Sets.newHashSet();
    for (long typeId : typeSig) signature.add(transaction.getExistingRelationType(typeId));
    for (RelationType sortType : sortKeys) signature.remove(sortType);
    if (!signature.isEmpty()) {
        RelationType[] sig = signature.toArray(new RelationType[signature.size()]);
        maker.signature(sig);
    }
    RelationType typeIndex = maker.make();
    addSchemaEdge(type, typeIndex, TypeDefinitionCategory.RELATIONTYPE_INDEX, null);
    RelationTypeIndexWrapper index = new RelationTypeIndexWrapper((InternalRelationType)typeIndex);
    if (!type.isNew()) updateIndex(index,SchemaAction.REGISTER_INDEX);
    return index;
}
 
Example 35
Project: titan0.5.4-hbase1.1.1-custom   File: ManagementSystem.java   View source code 4 votes vote down vote up
@Override
public void updateIndex(TitanIndex index, SchemaAction updateAction) {
    Preconditions.checkArgument(updateAction!=null,"Need to provide update action");
    Preconditions.checkArgument(updateAction!=SchemaAction.REMOVE_INDEX);

    TitanSchemaVertex schemaVertex = getSchemaVertex(index);
    Set<TitanSchemaVertex> dependentTypes;
    Set<PropertyKeyVertex> keySubset = ImmutableSet.of();
    if (index instanceof RelationTypeIndex) {
        dependentTypes = ImmutableSet.of((TitanSchemaVertex)((InternalRelationType)schemaVertex).getBaseType());
        Preconditions.checkArgument(updateAction.getApplicableStatus().contains(schemaVertex.getStatus()),
                "Update action [%s] does not apply for index with status [%s]",updateAction,schemaVertex.getStatus());
    } else if (index instanceof TitanGraphIndex) {
        IndexType indexType = schemaVertex.asIndexType();
        dependentTypes = Sets.newHashSet();
        if (indexType.isCompositeIndex()) {
            Preconditions.checkArgument(updateAction.getApplicableStatus().contains(schemaVertex.getStatus()),
                    "Update action [%s] does not apply for index with status [%s]",updateAction,schemaVertex.getStatus());
            for (PropertyKey key : ((TitanGraphIndex)index).getFieldKeys()) {
                dependentTypes.add((PropertyKeyVertex)key);
            }
        } else {
            keySubset = Sets.newHashSet();
            MixedIndexType cindexType = (MixedIndexType)indexType;
            Set<SchemaStatus> applicableStatus = updateAction.getApplicableStatus();
            for (ParameterIndexField field : cindexType.getFieldKeys()) {
                if (applicableStatus.contains(field.getStatus())) keySubset.add((PropertyKeyVertex)field.getFieldKey());
            }
            Preconditions.checkArgument(!keySubset.isEmpty(),"Update action [%s] does not apply to any fields for index [%s]",updateAction,index);
            dependentTypes.addAll(keySubset);
        }
    } else throw new UnsupportedOperationException("Updates not supported for index: " + index);

    switch(updateAction) {
        case REGISTER_INDEX:
            setStatus(schemaVertex,SchemaStatus.INSTALLED,keySubset);
            updatedTypes.add(schemaVertex);
            updatedTypes.addAll(dependentTypes);
            setUpdateTrigger(new UpdateStatusTrigger(graph, schemaVertex, SchemaStatus.REGISTERED, keySubset));
            break;
        case REINDEX:
            throw new UnsupportedOperationException(updateAction + " requires a manual step: run a MapReduce reindex on index name \"" + index.getName() + "\"");
        case ENABLE_INDEX:
            setStatus(schemaVertex,SchemaStatus.ENABLED,keySubset);
            updatedTypes.add(schemaVertex);
            if (!keySubset.isEmpty()) updatedTypes.addAll(dependentTypes);
            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));
            break;
        case REMOVE_INDEX:
            throw new UnsupportedOperationException("Removing indexes is not yet supported");
        default: throw new UnsupportedOperationException("Update action not supported: " + updateAction);
    }
}