Java Code Examples for com.thinkaurelius.titan.core.PropertyKey

The following are top voted examples for showing how to use com.thinkaurelius.titan.core.PropertyKey. 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: TitanGraphTest.java   View source code 6 votes vote down vote up
@Test
public void testForceIndexUsage() {
    PropertyKey age = makeKey("age", Integer.class);
    PropertyKey time = makeKey("time", Long.class);
    mgmt.buildIndex("time", Vertex.class).addKey(time).buildCompositeIndex();
    finishSchema();

    for (int i = 1; i <= 10; i++) {
        TitanVertex v = tx.addVertex("time", i, "age", i);
    }

    //Graph query with and with-out index support
    assertCount(1, tx.query().has("time", 5).vertices());
    assertCount(1, tx.query().has("age", 6).vertices());

    clopen(option(FORCE_INDEX_USAGE), true);
    //Query with-out index support should now throw exception
    assertCount(1, tx.query().has("time", 5).vertices());
    try {
        assertCount(1, tx.query().has("age", 6).vertices());
        fail();
    } catch (Exception e) {
    }
}
 
Example 3
Project: titan0.5.4-hbase1.1.1-custom   File: IndexHelper.java   View source code 6 votes vote down vote up
public static GraphCentricQueryBuilder getQuery(CompositeIndexType index, Object[] values, StandardTitanTx tx) {
    Preconditions.checkArgument(index != null && values != null && values.length > 0 && tx != null);
    Preconditions.checkArgument(values.length==index.getFieldKeys().length);
    GraphCentricQueryBuilder gb = tx.query();
    IndexField[] fields = index.getFieldKeys();
    for (int i = 0; i <fields.length; i++) {
        IndexField f = fields[i];
        Object value = values[i];
        Preconditions.checkNotNull(value);
        PropertyKey key = f.getFieldKey();
        Preconditions.checkArgument(key.getDataType().equals(value.getClass()),"Incompatible data types for: " + value);
        gb.has(key, Cmp.EQUAL,value);
    }
    if (index.hasSchemaTypeConstraint()) {
        gb.has(ImplicitKey.LABEL,Cmp.EQUAL,index.getSchemaTypeConstraint().getName());
    }
    return gb;
}
 
Example 4
Project: incubator-atlas   File: Titan0Graph.java   View source code 6 votes vote down vote up
public Titan0Graph() {
    //determine multi-properties once at startup
    TitanManagement mgmt = null;
    try {
        mgmt = Titan0GraphDatabase.getGraphInstance().getManagementSystem();
        Iterable<PropertyKey> keys = mgmt.getRelationTypes(PropertyKey.class);
        multiProperties = Collections.synchronizedSet(new HashSet<String>());
        for(PropertyKey key : keys) {
            if (key.getCardinality() != Cardinality.SINGLE) {
                multiProperties.add(key.getName());
            }
        }
    } finally {
        if (mgmt != null) {
            mgmt.rollback();
        }
    }
}
 
Example 5
Project: titan1withtp3.1   File: TitanIndexTest.java   View source code 6 votes vote down vote up
@Test
// this tests a case when there as AND with a single CONTAINS condition inside AND(name:(was here))
// which (in case of Solr) spans multiple conditions such as AND(AND(name:was, name:here))
// so we need to make sure that we don't apply AND twice.
public void testContainsWithMultipleValues() throws Exception {
    PropertyKey name = makeKey("name", String.class);

    mgmt.buildIndex("store1", Vertex.class).addKey(name).buildMixedIndex(INDEX);
    mgmt.commit();

    TitanVertex v1 = tx.addVertex();
    v1.property("name", "hercules was here");

    tx.commit();

    Thread.sleep(2000);

    TitanVertex r = Iterables.<TitanVertex>get(graph.query().has("name", Text.CONTAINS, "hercules here").vertices(), 0);
    Assert.assertEquals(r.property("name").value(), "hercules was here");
}
 
Example 6
Project: titan1withtp3.1   File: TitanIndexTest.java   View source code 6 votes vote down vote up
/**
 * Tests indexing using _all virtual field
 */
@Test
public void testWidcardQuery() {
    if (supportsWildcardQuery()) {
        PropertyKey p1 = makeKey("p1", String.class);
        PropertyKey p2 = makeKey("p2", String.class);
        mgmt.buildIndex("mixedIndex", Vertex.class).addKey(p1).addKey(p2).buildMixedIndex(INDEX);

        finishSchema();
        clopen();

        TitanVertex v1 = graph.addVertex();
        v1.property("p1", "test1");
        v1.property("p2", "test2");

        clopen();//Flush the index
        assertEquals(v1, graph.indexQuery("mixedIndex", "v.*:\"test1\"").vertices().iterator().next().getElement());
        assertEquals(v1, graph.indexQuery("mixedIndex", "v.*:\"test2\"").vertices().iterator().next().getElement());
    }

}
 
Example 7
Project: titan1.0.1.kafka   File: StandardEdge.java   View source code 6 votes vote down vote up
@Override
public void setPropertyDirect(PropertyKey key, Object value) {
    Preconditions.checkArgument(!(key instanceof ImplicitKey), "Cannot use implicit type [%s] when setting property", key.name());
    if (properties == EMPTY_PROPERTIES) {
        if (tx().getConfiguration().isSingleThreaded()) {
            properties = new HashMap<PropertyKey, Object>(5);
        } else {
            synchronized (this) {
                if (properties == EMPTY_PROPERTIES) {
                    properties = Collections.synchronizedMap(new HashMap<PropertyKey, Object>(5));
                }
            }
        }
    }
    properties.put(key, value);
}
 
Example 8
Project: incubator-atlas   File: Titan1Graph.java   View source code 6 votes vote down vote up
public Titan1Graph() {
    //determine multi-properties once at startup
    TitanManagement mgmt = null;
    try {
        mgmt = Titan1GraphDatabase.getGraphInstance().openManagement();
        Iterable<PropertyKey> keys = mgmt.getRelationTypes(PropertyKey.class);
        multiProperties = new HashSet<>();
        for (PropertyKey key : keys) {
            if (key.cardinality() != Cardinality.SINGLE) {
                multiProperties.add(key.name());
            }
        }
    } finally {
        if (mgmt != null) {
            mgmt.rollback();
        }
    }
}
 
Example 9
Project: titan1withtp3.1   File: IndexHelper.java   View source code 6 votes vote down vote up
public static GraphCentricQueryBuilder getQuery(CompositeIndexType index, Object[] values, StandardTitanTx tx) {
    Preconditions.checkArgument(index != null && values != null && values.length > 0 && tx != null);
    Preconditions.checkArgument(values.length==index.getFieldKeys().length);
    GraphCentricQueryBuilder gb = tx.query();
    IndexField[] fields = index.getFieldKeys();
    for (int i = 0; i <fields.length; i++) {
        IndexField f = fields[i];
        Object value = values[i];
        Preconditions.checkNotNull(value);
        PropertyKey key = f.getFieldKey();
        Preconditions.checkArgument(key.dataType().equals(value.getClass()),"Incompatible data types for: " + value);
        gb.has(key, Cmp.EQUAL,value);
    }
    if (index.hasSchemaTypeConstraint()) {
        gb.has(ImplicitKey.LABEL,Cmp.EQUAL,index.getSchemaTypeConstraint().name());
    }
    return gb;
}
 
Example 10
Project: titan1.0.1.kafka   File: TitanGraphTest.java   View source code 6 votes vote down vote up
@Test
public void testStaleVertex() {
    PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).make();
    PropertyKey age = mgmt.makePropertyKey("age").dataType(Integer.class).make();
    mgmt.buildIndex("byName", Vertex.class).addKey(name).unique().buildCompositeIndex();
    finishSchema();


    TitanVertex cartman = graph.addVertex("name", "cartman", "age", 10);
    TitanVertex stan = graph.addVertex("name", "stan", "age", 8);

    graph.tx().commit();

    cartman = getOnlyElement(graph.query().has("name", "cartman").vertices());

    graph.tx().commit();

    TitanVertexProperty p = (TitanVertexProperty) cartman.properties().next();
    assertTrue(((Long) p.longId()) > 0);
    graph.tx().commit();
}
 
Example 11
Project: titan1.0.1.kafka   File: IndexHelper.java   View source code 6 votes vote down vote up
public static GraphCentricQueryBuilder getQuery(CompositeIndexType index, Object[] values, StandardTitanTx tx) {
    Preconditions.checkArgument(index != null && values != null && values.length > 0 && tx != null);
    Preconditions.checkArgument(values.length==index.getFieldKeys().length);
    GraphCentricQueryBuilder gb = tx.query();
    IndexField[] fields = index.getFieldKeys();
    for (int i = 0; i <fields.length; i++) {
        IndexField f = fields[i];
        Object value = values[i];
        Preconditions.checkNotNull(value);
        PropertyKey key = f.getFieldKey();
        Preconditions.checkArgument(key.dataType().equals(value.getClass()),"Incompatible data types for: " + value);
        gb.has(key, Cmp.EQUAL,value);
    }
    if (index.hasSchemaTypeConstraint()) {
        gb.has(ImplicitKey.LABEL,Cmp.EQUAL,index.getSchemaTypeConstraint().name());
    }
    return gb;
}
 
Example 12
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 13
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 14
Project: keti   File: GraphConfig.java   View source code 5 votes vote down vote up
private static PropertyKey getOrCreatePropertyKey(final String keyName, final Class<?> keyType,
        final TitanManagement mgmt) {
    PropertyKey propertyKey = mgmt.getPropertyKey(keyName);
    if (null == propertyKey) {
        propertyKey = mgmt.makePropertyKey(keyName).dataType(keyType).make();
    }
    return propertyKey;
}
 
Example 15
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 16
Project: titan1.0.1.kafka   File: CacheEdge.java   View source code 5 votes vote down vote up
@Override
public Iterable<PropertyKey> getPropertyKeysDirect() {
    RelationCache map = getPropertyMap();
    List<PropertyKey> types = new ArrayList<>(map.numProperties());

    for (LongObjectCursor<Object> entry : map) {
        types.add(tx().getExistingPropertyKey(entry.key));
    }

    return types;
}
 
Example 17
Project: titan0.5.4-hbase1.1.1-custom   File: IndexTypeWrapper.java   View source code 5 votes vote down vote up
@Override
public IndexField getField(PropertyKey key) {
    Map<PropertyKey,IndexField> result = fieldMap;
    if (result==null) {
        ImmutableMap.Builder<PropertyKey,IndexField> b = ImmutableMap.builder();
        for (IndexField f : getFieldKeys()) b.put(f.getFieldKey(),f);
        result=b.build();
        fieldMap=result;
    }
    assert result!=null;
    return result.get(key);
}
 
Example 18
Project: titan1withtp3.1   File: TitanGraphTest.java   View source code 5 votes vote down vote up
@Test
public void testWithoutIndex() {
    PropertyKey kid = mgmt.makePropertyKey("kid").dataType(Long.class).make();
    mgmt.makePropertyKey("name").dataType(String.class).make();
    mgmt.makeEdgeLabel("knows").signature(kid).make();
    finishSchema();

    Random random = new Random();
    int numV = 1000;
    TitanVertex previous = null;
    for (int i = 0; i < numV; i++) {
        TitanVertex v = graph.addVertex(
                "kid", random.nextInt(numV), "name", "v" + i);
        if (previous != null) {
            Edge e = v.addEdge("knows", previous, "kid", random.nextInt(numV / 2));
        }
        previous = v;
    }

    verifyElementOrder(graph.query().orderBy("kid", incr).limit(500).vertices(), "kid", Order.ASC, 500);
    verifyElementOrder(graph.query().orderBy("kid", incr).limit(300).edges(), "kid", Order.ASC, 300);
    verifyElementOrder(graph.query().orderBy("kid", decr).limit(400).vertices(), "kid", Order.DESC, 400);
    verifyElementOrder(graph.query().orderBy("kid", decr).limit(200).edges(), "kid", Order.DESC, 200);

    clopen();

    //Copied from above
    verifyElementOrder(graph.query().orderBy("kid", incr).limit(500).vertices(), "kid", Order.ASC, 500);
    verifyElementOrder(graph.query().orderBy("kid", incr).limit(300).edges(), "kid", Order.ASC, 300);
    verifyElementOrder(graph.query().orderBy("kid", decr).limit(400).vertices(), "kid", Order.DESC, 400);
    verifyElementOrder(graph.query().orderBy("kid", decr).limit(200).edges(), "kid", Order.DESC, 200);
}
 
Example 19
Project: incubator-atlas   File: Titan0GraphManagement.java   View source code 5 votes vote down vote up
@Override
public void createExactMatchIndex(String propertyName, boolean enforceUniqueness,
        List<AtlasPropertyKey> propertyKeys) {

    TitanManagement.IndexBuilder indexBuilder = management.buildIndex(propertyName, Vertex.class);
    for(AtlasPropertyKey key : propertyKeys) {
        PropertyKey titanKey = TitanObjectFactory.createPropertyKey(key);
        indexBuilder.addKey(titanKey);
    }
    if (enforceUniqueness) {
        indexBuilder.unique();
    }
    indexBuilder.buildCompositeIndex();
}
 
Example 20
Project: titan0.5.4-hbase1.1.1-custom   File: EdgeSerializer.java   View source code 5 votes vote down vote up
private void writePropertyValue(DataOutput out, PropertyKey key, Object value, InlineType inlineType) {
    if (AttributeUtil.hasGenericDataType(key)) {
        assert !inlineType.writeByteOrdered();
        out.writeClassAndObject(value);
    } else {
        assert value==null || value.getClass().equals(key.getDataType());
        if (inlineType.writeByteOrdered()) out.writeObjectByteOrder(value, key.getDataType());
        else out.writeObject(value, key.getDataType());
    }
}
 
Example 21
Project: titan1withtp3.1   File: TitanIndexTest.java   View source code 5 votes vote down vote up
/**
 * Tests indexing boolean
 */
@Test
public void testBooleanIndexing() {
    PropertyKey name = makeKey("visible", Boolean.class);
    mgmt.buildIndex("booleanIndex", Vertex.class).
            addKey(name).buildMixedIndex(INDEX);
    finishSchema();
    clopen();

    TitanVertex v1 = graph.addVertex();
    v1.property("visible", true);

    TitanVertex v2 = graph.addVertex();
    v2.property("visible", false);

    assertCount(2, graph.vertices());
    assertEquals(v1, getOnlyVertex(graph.query().has("visible", true)));
    assertEquals(v2, getOnlyVertex(graph.query().has("visible", false)));
    assertEquals(v2, getOnlyVertex(graph.query().has("visible", Cmp.NOT_EQUAL, true)));
    assertEquals(v1, getOnlyVertex(graph.query().has("visible", Cmp.NOT_EQUAL, false)));

    clopen();//Flush the index
    assertCount(2, graph.vertices());
    assertEquals(v1, getOnlyVertex(graph.query().has("visible", true)));
    assertEquals(v2, getOnlyVertex(graph.query().has("visible", false)));
    assertEquals(v2, getOnlyVertex(graph.query().has("visible", Cmp.NOT_EQUAL, true)));
    assertEquals(v1, getOnlyVertex(graph.query().has("visible", Cmp.NOT_EQUAL, false)));
}
 
Example 22
Project: titan1withtp3.1   File: TitanIndexTest.java   View source code 5 votes vote down vote up
/**
 * Tests indexing dates
 */
@Test
public void testDateIndexing() {
    PropertyKey name = makeKey("date", Date.class);
    mgmt.buildIndex("dateIndex", Vertex.class).
            addKey(name).buildMixedIndex(INDEX);
    finishSchema();
    clopen();

    TitanVertex v1 = graph.addVertex();
    v1.property("date", new Date(1));

    TitanVertex v2 = graph.addVertex();
    v2.property("date", new Date(2000));


    assertEquals(v1, getOnlyVertex(graph.query().has("date", Cmp.EQUAL, new Date(1))));
    assertEquals(v2, getOnlyVertex(graph.query().has("date", Cmp.GREATER_THAN, new Date(1))));
    assertEquals(Sets.newHashSet(v1, v2), Sets.newHashSet(graph.query().has("date", Cmp.GREATER_THAN_EQUAL, new Date(1)).vertices()));
    assertEquals(v1, getOnlyVertex(graph.query().has("date", Cmp.LESS_THAN, new Date(2000))));
    assertEquals(Sets.newHashSet(v1, v2), Sets.newHashSet(graph.query().has("date", Cmp.LESS_THAN_EQUAL, new Date(2000)).vertices()));
    assertEquals(v2, getOnlyVertex(graph.query().has("date", Cmp.NOT_EQUAL, new Date(1))));

    clopen();//Flush the index
    assertEquals(v1, getOnlyVertex(graph.query().has("date", Cmp.EQUAL, new Date(1))));
    assertEquals(v2, getOnlyVertex(graph.query().has("date", Cmp.GREATER_THAN, new Date(1))));
    assertEquals(Sets.newHashSet(v1, v2), Sets.newHashSet(graph.query().has("date", Cmp.GREATER_THAN_EQUAL, new Date(1)).vertices()));
    assertEquals(v1, getOnlyVertex(graph.query().has("date", Cmp.LESS_THAN, new Date(2000))));
    assertEquals(Sets.newHashSet(v1, v2), Sets.newHashSet(graph.query().has("date", Cmp.LESS_THAN_EQUAL, new Date(2000)).vertices()));
    assertEquals(v2, getOnlyVertex(graph.query().has("date", Cmp.NOT_EQUAL, new Date(1))));


}
 
Example 23
Project: titan1withtp3.1   File: TitanIndexTest.java   View source code 5 votes vote down vote up
/**
 * Tests indexing boolean
 */
@Test
public void testUUIDIndexing() {
    PropertyKey name = makeKey("uid", UUID.class);
    mgmt.buildIndex("uuidIndex", Vertex.class).
            addKey(name).buildMixedIndex(INDEX);
    finishSchema();
    clopen();

    UUID uid1 = UUID.randomUUID();
    UUID uid2 = UUID.randomUUID();

    TitanVertex v1 = graph.addVertex();
    v1.property("uid", uid1);

    TitanVertex v2 = graph.addVertex();
    v2.property("uid", uid2);

    assertCount(2, graph.query().vertices());
    assertEquals(v1, getOnlyVertex(graph.query().has("uid", uid1)));
    assertEquals(v2, getOnlyVertex(graph.query().has("uid", uid2)));

    assertEquals(v2, getOnlyVertex(graph.query().has("uid", Cmp.NOT_EQUAL, uid1)));
    assertEquals(v1, getOnlyVertex(graph.query().has("uid", Cmp.NOT_EQUAL, uid2)));

    clopen();//Flush the index
    assertCount(2, graph.query().vertices());
    assertEquals(v1, getOnlyVertex(graph.query().has("uid", uid1)));
    assertEquals(v2, getOnlyVertex(graph.query().has("uid", uid2)));

    assertEquals(v2, getOnlyVertex(graph.query().has("uid", Cmp.NOT_EQUAL, uid1)));
    assertEquals(v1, getOnlyVertex(graph.query().has("uid", Cmp.NOT_EQUAL, uid2)));

}
 
Example 24
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 25
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 26
Project: titan1.0.1.kafka   File: TitanGraphTest.java   View source code 5 votes vote down vote up
@Test
public void testIndexQueryWithLabelsAndContainsIN() {
    // This test is based on the steps to reproduce #882

    String labelName = "labelName";

    VertexLabel label = mgmt.makeVertexLabel(labelName).make();
    PropertyKey uid = mgmt.makePropertyKey("uid").dataType(String.class).make();
    TitanGraphIndex uidCompositeIndex = mgmt.buildIndex("uidIndex", Vertex.class).indexOnly(label).addKey(uid).unique().buildCompositeIndex();
    mgmt.setConsistency(uidCompositeIndex, ConsistencyModifier.LOCK);
    finishSchema();

    TitanVertex foo = graph.addVertex(labelName);
    TitanVertex bar = graph.addVertex(labelName);
    foo.property("uid", "foo");
    bar.property("uid", "bar");
    graph.tx().commit();

    Iterable<TitanVertex> vertexes = graph.query()
            .has("uid", Contain.IN, ImmutableList.of("foo", "bar"))
            .has(LABEL_NAME, labelName)
            .vertices();
    assertEquals(2, Iterables.size(vertexes));
    for (TitanVertex v : vertexes) {
        assertEquals(labelName, v.vertexLabel().name());
    }
}
 
Example 27
Project: titan1.0.1.kafka   File: ManagementSystem.java   View source code 5 votes vote down vote up
@Override
public TitanGraphIndex buildCompositeIndex() {
    Preconditions.checkArgument(!keys.isEmpty(), "Need to specify at least one key for the composite index");
    PropertyKey[] keyArr = new PropertyKey[keys.size()];
    int pos = 0;
    for (Map.Entry<PropertyKey, Parameter[]> entry : keys.entrySet()) {
        Preconditions.checkArgument(entry.getValue() == null, "Cannot specify parameters for composite index: %s", entry.getKey());
        keyArr[pos++] = entry.getKey();
    }
    return createCompositeIndex(indexName, elementCategory, unique, constraint, keyArr);
}
 
Example 28
Project: titan1.0.1.kafka   File: TitanGraphTest.java   View source code 5 votes vote down vote up
@Test
public void testWithoutIndex() {
    PropertyKey kid = mgmt.makePropertyKey("kid").dataType(Long.class).make();
    mgmt.makePropertyKey("name").dataType(String.class).make();
    mgmt.makeEdgeLabel("knows").signature(kid).make();
    finishSchema();

    Random random = new Random();
    int numV = 1000;
    TitanVertex previous = null;
    for (int i = 0; i < numV; i++) {
        TitanVertex v = graph.addVertex(
                "kid", random.nextInt(numV), "name", "v" + i);
        if (previous != null) {
            Edge e = v.addEdge("knows", previous, "kid", random.nextInt(numV / 2));
        }
        previous = v;
    }

    verifyElementOrder(graph.query().orderBy("kid", incr).limit(500).vertices(), "kid", Order.ASC, 500);
    verifyElementOrder(graph.query().orderBy("kid", incr).limit(300).edges(), "kid", Order.ASC, 300);
    verifyElementOrder(graph.query().orderBy("kid", decr).limit(400).vertices(), "kid", Order.DESC, 400);
    verifyElementOrder(graph.query().orderBy("kid", decr).limit(200).edges(), "kid", Order.DESC, 200);

    clopen();

    //Copied from above
    verifyElementOrder(graph.query().orderBy("kid", incr).limit(500).vertices(), "kid", Order.ASC, 500);
    verifyElementOrder(graph.query().orderBy("kid", incr).limit(300).edges(), "kid", Order.ASC, 300);
    verifyElementOrder(graph.query().orderBy("kid", decr).limit(400).vertices(), "kid", Order.DESC, 400);
    verifyElementOrder(graph.query().orderBy("kid", decr).limit(200).edges(), "kid", Order.DESC, 200);
}
 
Example 29
Project: titan1.0.1.kafka   File: TitanGraphTest.java   View source code 5 votes vote down vote up
/**
 * This test exercises different types of updates against cardinality restricted properties
 * to ensure that the resulting behavior is fully consistent.
 */
@Test
public void testPropertyCardinality() {
    PropertyKey uid = mgmt.makePropertyKey("uid").dataType(Long.class).cardinality(Cardinality.SINGLE).make();
    PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.SINGLE).make();
    mgmt.buildIndex("byUid", Vertex.class).addKey(uid).unique().buildCompositeIndex();
    mgmt.buildIndex("byName", Vertex.class).addKey(name).buildCompositeIndex();

    finishSchema();

    TitanVertex v1 = tx.addVertex();
    v1.property("name", "name1");
    TitanVertex v2 = tx.addVertex();
    v2.property("uid", 512);

    newTx();

    v1 = tx.getVertex(v1.longId());
    v1.property("name", "name2"); //Ensure that the old index record gets removed
    v2 = tx.getVertex(v2.longId());
    v2.property("uid", 512); //Ensure that replacement is allowed

    newTx();

    assertCount(0, tx.query().has("name", "name1").vertices());
    assertCount(1, tx.query().has("name", "name2").vertices());
    assertCount(1, tx.query().has("uid", 512).vertices());
}
 
Example 30
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 31
Project: incubator-atlas   File: Titan0GraphManagement.java   View source code 5 votes vote down vote up
@Override
public void createFullTextIndex(String indexName, AtlasPropertyKey propertyKey, String backingIndex) {

    PropertyKey fullText = TitanObjectFactory.createPropertyKey(propertyKey);

    management.buildIndex(indexName, Vertex.class)
            .addKey(fullText, com.thinkaurelius.titan.core.schema.Parameter.of("mapping", Mapping.TEXT))
            .buildMixedIndex(backingIndex);
}
 
Example 32
Project: titan1withtp3.1   File: TitanGraphIndexWrapper.java   View source code 5 votes vote down vote up
@Override
public PropertyKey[] getFieldKeys() {
    IndexField[] fields = index.getFieldKeys();
    PropertyKey[] keys = new PropertyKey[fields.length];
    for (int i = 0; i < fields.length; i++) {
        keys[i]=fields[i].getFieldKey();
    }
    return keys;
}
 
Example 33
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 34
Project: titan1withtp3.1   File: HasStepFolder.java   View source code 5 votes vote down vote up
public static boolean validTitanOrder(OrderGlobalStep ostep, Traversal rootTraversal,
                                      boolean isVertexOrder) {
    for (Comparator comp : (List<Comparator>) ostep.getComparators()) {
        if (!(comp instanceof ElementValueComparator)) return false;
        ElementValueComparator evc = (ElementValueComparator) comp;
        if (!(evc.getValueComparator() instanceof Order)) return false;

        TitanTransaction tx = TitanTraversalUtil.getTx(rootTraversal.asAdmin());
        String key = evc.getPropertyKey();
        PropertyKey pkey = tx.getPropertyKey(key);
        if (pkey == null || !(Comparable.class.isAssignableFrom(pkey.dataType()))) return false;
        if (isVertexOrder && pkey.cardinality() != Cardinality.SINGLE) return false;
    }
    return true;
}
 
Example 35
Project: titan1.0.1.kafka   File: TitanGraphTest.java   View source code 5 votes vote down vote up
/**
 * Very simple graph operation to ensure minimal functionality and cleanup
 */
@Test
public void testBasic() {

    PropertyKey uid = makeVertexIndexedUniqueKey("name", String.class);
    finishSchema();

    TitanVertex n1 = tx.addVertex();
    uid = tx.getPropertyKey("name");
    n1.property(uid.name(), "abcd");
    clopen();
    long nid = n1.longId();
    uid = tx.getPropertyKey("name");
    assertTrue(getV(tx, nid) != null);
    assertTrue(getV(tx, uid.longId()) != null);
    assertMissing(tx, nid + 64);
    uid = tx.getPropertyKey(uid.name());
    n1 = getV(tx, nid);
    assertEquals(n1, getOnlyVertex(tx.query().has(uid.name(), "abcd")));
    assertEquals(1, Iterables.size(n1.query().relations())); //TODO: how to expose relations?
    assertEquals("abcd", n1.value(uid.name()));
    assertCount(1, tx.query().vertices());
    close();
    TitanCleanup.clear(graph);
    open(config);
    assertEmpty(tx.query().vertices());
}
 
Example 36
Project: titan1.0.1.kafka   File: RelationIdentifier.java   View source code 5 votes vote down vote up
TitanRelation findRelation(TitanTransaction tx) {
    TitanVertex v = ((StandardTitanTx)tx).getInternalVertex(outVertexId);
    if (v == null || v.isRemoved()) return null;
    TitanVertex typeVertex = tx.getVertex(typeId);
    if (typeVertex == null) return null;
    if (!(typeVertex instanceof RelationType))
        throw new IllegalArgumentException("Invalid RelationIdentifier: typeID does not reference a type");

    RelationType type = (RelationType) typeVertex;
    Iterable<? extends TitanRelation> rels;
    if (((RelationType) typeVertex).isEdgeLabel()) {
        Direction dir = Direction.OUT;
        TitanVertex other = ((StandardTitanTx)tx).getInternalVertex(inVertexId);
        if (other==null || other.isRemoved()) return null;
        if (((StandardTitanTx) tx).isPartitionedVertex(v) && !((StandardTitanTx) tx).isPartitionedVertex(other)) { //Swap for likely better performance
            TitanVertex tmp = other;
            other = v;
            v = tmp;
            dir = Direction.IN;
        }
        rels = ((VertexCentricQueryBuilder) v.query()).noPartitionRestriction().types((EdgeLabel) type).direction(dir).adjacent(other).edges();
    } else {
        rels = ((VertexCentricQueryBuilder) v.query()).noPartitionRestriction().types((PropertyKey) type).properties();
    }

    for (TitanRelation r : rels) {
        //Find current or previous relation
        if (r.longId() == relationId ||
                ((r instanceof StandardRelation) && ((StandardRelation) r).getPreviousID() == relationId)) return r;
    }
    return null;
}
 
Example 37
Project: titan1withtp3.1   File: CacheEdge.java   View source code 5 votes vote down vote up
private void copyProperties(InternalRelation to) {
    for (LongObjectCursor<Object> entry : getPropertyMap()) {
        PropertyKey type = tx().getExistingPropertyKey(entry.key);
        if (!(type instanceof ImplicitKey))
            to.setPropertyDirect(type, entry.value);
    }
}
 
Example 38
Project: titan1.0.1.kafka   File: IndexTypeWrapper.java   View source code 5 votes vote down vote up
@Override
public IndexField getField(PropertyKey key) {
    Map<PropertyKey,IndexField> result = fieldMap;
    if (result==null) {
        ImmutableMap.Builder<PropertyKey,IndexField> b = ImmutableMap.builder();
        for (IndexField f : getFieldKeys()) b.put(f.getFieldKey(),f);
        result=b.build();
        fieldMap=result;
    }
    assert result!=null;
    return result.get(key);
}
 
Example 39
Project: titan-cassandra-elasticsearch-index   File: TitanStart.java   View source code 4 votes vote down vote up
public static void main(String[] args) throws Exception {

		Configuration conf = new PropertiesConfiguration( getRelativeResourcePath( "titan-cassandra-es.properties" ) );
		// start elastic search on startup
		Node node = new NodeBuilder().node();
		
		TitanGraph graph = TitanFactory.open(conf);
		/* Comment if you do not want to reload the graph every time */
		graph.close();
		TitanCleanup.clear(graph);
		graph = TitanFactory.open(conf);
		GraphOfTheGodsFactory.load(graph);
		/* graph loaded  */
		
		// create own indexes
		TitanManagement mgmt = graph.openManagement();
		PropertyKey name = mgmt.getPropertyKey("name");
		PropertyKey age = mgmt.getPropertyKey("age");
		mgmt.buildIndex( "byNameComposite", Vertex.class ).addKey(name).buildCompositeIndex();
		// index consisting of multiple properties
		mgmt.buildIndex( "byNameAndAgeComposite", Vertex.class ).addKey(name).addKey(age).buildCompositeIndex();
		mgmt.commit();
		
		// wait for the index to become available
		ManagementSystem.awaitGraphIndexStatus(graph, "byNameComposite").call();
		ManagementSystem.awaitGraphIndexStatus(graph, "byNameAndAgeComposite").call();
		
		// create new vertex
		Vertex me = graph.addVertex("theOneAndOnly");
		me.property( "name", "me" );
		me.property( "age", 1 );
		graph.tx().commit();
		System.out.println("Created the one and only!");
		
		// re index the existing data (not required, just for demo purposes)
		mgmt = graph.openManagement();
		mgmt.updateIndex( mgmt.getGraphIndex("byNameComposite"), SchemaAction.REINDEX ).get();
		mgmt.updateIndex( mgmt.getGraphIndex("byNameAndAgeComposite"), SchemaAction.REINDEX ).get();
		mgmt.commit();
		
		GraphTraversalSource g = graph.traversal();
		GremlinPipeline<GraphTraversal<?, ?>, ?> pipe = new GremlinPipeline();
		
		// read our new vertex
		pipe.start( g.V().has( "name", "me" ) );	
		Vertex v = (Vertex)pipe.next();
		System.out.println();
		System.out.println( "Label: " + v.label() );
		System.out.println( "Name: " + v.property("name").value() );
		System.out.println( "Age: " + v.property("age").value() );
		System.out.println();
		
		// read different vertex
		pipe.start( g.V().has( "name", "hercules" ) );	
		Vertex herclues = (Vertex)pipe.next();
		System.out.println( "Label: " + herclues.label() );
		System.out.println( "Name: " + herclues.property("name").value() );
		System.out.println( "Age: " + herclues.property("age").value() );
		
		// print some edges
		Iterator<Edge> it = herclues.edges( Direction.OUT );
		while( it.hasNext() ) {
			Edge e = it.next();
			System.out.println( "Out: " + e.label()  + " --> " + e.inVertex().property("name").value() );
		}
		System.out.println();
		
		// close graph
		graph.close();
		// close elastic search on shutdown
		node.close();
		
		System.exit(0);
	}
 
Example 40
Project: titan1withtp3.1   File: PropertyKeyMaker.java   View source code 4 votes vote down vote up
@Override
public PropertyKeyMaker signature(PropertyKey... types);