Java Code Examples for com.thinkaurelius.titan.core.TitanVertex#property()

The following examples show how to use com.thinkaurelius.titan.core.TitanVertex#property() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TitanIndexTest.java    From titan1withtp3.1 with Apache License 2.0 6 votes vote down vote up
@Test
public void testIndexQueryWithScore() throws InterruptedException {
    PropertyKey textKey = mgmt.makePropertyKey("text").dataType(String.class).make();
    mgmt.buildIndex("store1", Vertex.class).addKey(textKey).buildMixedIndex(INDEX);
    mgmt.commit();

    TitanVertex v1 = tx.addVertex();
    TitanVertex v2 = tx.addVertex();
    TitanVertex v3 = tx.addVertex();

    v1.property("text", "Hello Hello Hello Hello Hello Hello Hello Hello");
    v2.property("text", "Hello abab abab fsdfsd sfdfsd sdffs fsdsdf fdf fsdfsd aera fsad abab abab fsdfsd sfdf");
    v3.property("text", "Hello");

    tx.commit();

    Thread.sleep(5000);

    Set<Double> scores = new HashSet<Double>();
    for (TitanIndexQuery.Result<TitanVertex> r : graph.indexQuery("store1", "v.text:(Hello)").vertices()) {
        scores.add(r.getScore());
    }

    Assert.assertEquals(3, scores.size());
}
 
Example 2
Source File: TitanIndexTest.java    From titan1withtp3.1 with Apache License 2.0 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 3
Source File: TitanIndexTest.java    From titan1withtp3.1 with Apache License 2.0 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 4
Source File: TitanIndexTest.java    From titan1withtp3.1 with Apache License 2.0 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 5
Source File: TitanIndexTest.java    From titan1withtp3.1 with Apache License 2.0 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 6
Source File: TitanIndexTest.java    From titan1withtp3.1 with Apache License 2.0 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 7
Source File: TitanIndexTest.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
private void setupChainGraph(int numV, String[] strs, boolean sameNameMapping) {
    clopen(option(INDEX_NAME_MAPPING, INDEX), sameNameMapping);
    TitanGraphIndex vindex = getExternalIndex(Vertex.class, INDEX);
    TitanGraphIndex eindex = getExternalIndex(Edge.class, INDEX);
    TitanGraphIndex pindex = getExternalIndex(TitanVertexProperty.class, INDEX);
    PropertyKey name = makeKey("name", String.class);

    mgmt.addIndexKey(vindex, name, getStringMapping());
    mgmt.addIndexKey(eindex, name, getStringMapping());
    mgmt.addIndexKey(pindex, name, getStringMapping(), Parameter.of("mapped-name", "xstr"));
    PropertyKey text = makeKey("text", String.class);
    mgmt.addIndexKey(vindex, text, getTextMapping(), Parameter.of("mapped-name", "xtext"));
    mgmt.addIndexKey(eindex, text, getTextMapping());
    mgmt.addIndexKey(pindex, text, getTextMapping());
    mgmt.makeEdgeLabel("knows").signature(name).make();
    mgmt.makePropertyKey("uid").dataType(String.class).signature(text).make();
    finishSchema();
    TitanVertex previous = null;
    for (int i = 0; i < numV; i++) {
        TitanVertex v = graph.addVertex("name", strs[i % strs.length], "text", strs[i % strs.length]);
        Edge e = v.addEdge("knows", previous == null ? v : previous,
                "name", strs[i % strs.length], "text", strs[i % strs.length]);
        VertexProperty p = v.property("uid", "v" + i,
                "name", strs[i % strs.length], "text", strs[i % strs.length]);
        previous = v;
    }
}
 
Example 8
Source File: TitanIndexTest.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
private void addVertex(int time, String text, double height, String[] phones) {
    newTx();
    TitanVertex v = tx.addVertex("text", text, "time", time, "height", height);
    for (String phone : phones) {
        v.property("phone", phone);
    }

    newTx();
}
 
Example 9
Source File: TitanIndexTest.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
@Test
public void testCompositeAndMixedIndexing() {
    PropertyKey name = makeKey("name", String.class);
    PropertyKey weight = makeKey("weight", Double.class);
    PropertyKey text = makeKey("text", String.class);
    PropertyKey flag = makeKey("flag", Boolean.class);

    TitanGraphIndex composite = mgmt.buildIndex("composite", Vertex.class).addKey(name).addKey(weight).buildCompositeIndex();
    TitanGraphIndex mixed = mgmt.buildIndex("mixed", Vertex.class).addKey(weight)
            .addKey(text, getTextMapping()).buildMixedIndex(INDEX);
    mixed.name();
    composite.name();
    finishSchema();

    final int numV = 100;
    String[] strs = {"houseboat", "humanoid", "differential", "extraordinary"};
    String[] strs2 = new String[strs.length];
    for (int i = 0; i < strs.length; i++) strs2[i] = strs[i] + " " + strs[i];
    final int modulo = 5;
    final int divisor = modulo * strs.length;

    for (int i = 0; i < numV; i++) {
        TitanVertex v = tx.addVertex();
        v.property("name", strs[i % strs.length]);
        v.property("text", strs[i % strs.length]);
        v.property("weight", (i % modulo) + 0.5);
        v.property("flag", true);
    }

    evaluateQuery(tx.query().has("name", Cmp.EQUAL, strs[0]), ElementCategory.VERTEX,
            numV / strs.length, new boolean[]{false, true});
    evaluateQuery(tx.query().has("text", Text.CONTAINS, strs[0]), ElementCategory.VERTEX,
            numV / strs.length, new boolean[]{true, true}, mixed.name());
    evaluateQuery(tx.query().has("text", Text.CONTAINS, strs[0]).has("flag"), ElementCategory.VERTEX,
            numV / strs.length, new boolean[]{false, true}, mixed.name());
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, strs[0]).has("weight", Cmp.EQUAL, 1.5), ElementCategory.VERTEX,
            numV / divisor, new boolean[]{true, true}, composite.name());
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, strs[0]).has("weight", Cmp.EQUAL, 1.5).has("flag"), ElementCategory.VERTEX,
            numV / divisor, new boolean[]{false, true}, composite.name());
    evaluateQuery(tx.query().has("text", Text.CONTAINS, strs[2]).has("weight", Cmp.EQUAL, 2.5), ElementCategory.VERTEX,
            numV / divisor, new boolean[]{true, true}, mixed.name());
    evaluateQuery(tx.query().has("text", Text.CONTAINS, strs[2]).has("weight", Cmp.EQUAL, 2.5).has("flag"), ElementCategory.VERTEX,
            numV / divisor, new boolean[]{false, true}, mixed.name());
    evaluateQuery(tx.query().has("text", Text.CONTAINS, strs[3]).has("name", Cmp.EQUAL, strs[3]).has("weight", Cmp.EQUAL, 3.5), ElementCategory.VERTEX,
            numV / divisor, new boolean[]{true, true}, mixed.name(), composite.name());
    evaluateQuery(tx.query().has("text", Text.CONTAINS, strs[3]).has("name", Cmp.EQUAL, strs[3]).has("weight", Cmp.EQUAL, 3.5).has("flag"), ElementCategory.VERTEX,
            numV / divisor, new boolean[]{false, true}, mixed.name(), composite.name());

    clopen();

    //Same queries as above
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, strs[0]), ElementCategory.VERTEX,
            numV / strs.length, new boolean[]{false, true});
    evaluateQuery(tx.query().has("text", Text.CONTAINS, strs[0]), ElementCategory.VERTEX,
            numV / strs.length, new boolean[]{true, true}, mixed.name());
    evaluateQuery(tx.query().has("text", Text.CONTAINS, strs[0]).has("flag"), ElementCategory.VERTEX,
            numV / strs.length, new boolean[]{false, true}, mixed.name());
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, strs[0]).has("weight", Cmp.EQUAL, 1.5), ElementCategory.VERTEX,
            numV / divisor, new boolean[]{true, true}, composite.name());
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, strs[0]).has("weight", Cmp.EQUAL, 1.5).has("flag"), ElementCategory.VERTEX,
            numV / divisor, new boolean[]{false, true}, composite.name());
    evaluateQuery(tx.query().has("text", Text.CONTAINS, strs[2]).has("weight", Cmp.EQUAL, 2.5), ElementCategory.VERTEX,
            numV / divisor, new boolean[]{true, true}, mixed.name());
    evaluateQuery(tx.query().has("text", Text.CONTAINS, strs[2]).has("weight", Cmp.EQUAL, 2.5).has("flag"), ElementCategory.VERTEX,
            numV / divisor, new boolean[]{false, true}, mixed.name());
    evaluateQuery(tx.query().has("text", Text.CONTAINS, strs[3]).has("name", Cmp.EQUAL, strs[3]).has("weight", Cmp.EQUAL, 3.5), ElementCategory.VERTEX,
            numV / divisor, new boolean[]{true, true}, mixed.name(), composite.name());
    evaluateQuery(tx.query().has("text", Text.CONTAINS, strs[3]).has("name", Cmp.EQUAL, strs[3]).has("weight", Cmp.EQUAL, 3.5).has("flag"), ElementCategory.VERTEX,
            numV / divisor, new boolean[]{false, true}, mixed.name(), composite.name());

}
 
Example 10
Source File: TitanIndexTest.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
@Test
public void testVertexTTLWithMixedIndices() throws Exception {
    if (!features.hasCellTTL() || !indexFeatures.supportsDocumentTTL()) {
        return;
    }

    PropertyKey name = makeKey("name", String.class);
    PropertyKey time = makeKey("time", Long.class);
    PropertyKey text = makeKey("text", String.class);

    VertexLabel event = mgmt.makeVertexLabel("event").setStatic().make();
    final int eventTTLSeconds = (int) TestGraphConfigs.getTTL(TimeUnit.SECONDS);
    mgmt.setTTL(event, Duration.ofSeconds(eventTTLSeconds));

    mgmt.buildIndex("index1", Vertex.class).
            addKey(name, getStringMapping()).addKey(time).buildMixedIndex(INDEX);
    mgmt.buildIndex("index2", Vertex.class).indexOnly(event).
            addKey(text, getTextMapping()).buildMixedIndex(INDEX);

    assertEquals(Duration.ZERO, mgmt.getTTL(name));
    assertEquals(Duration.ZERO, mgmt.getTTL(time));
    assertEquals(Duration.ofSeconds(eventTTLSeconds), mgmt.getTTL(event));
    finishSchema();

    TitanVertex v1 = tx.addVertex("event");
    v1.property(VertexProperty.Cardinality.single, "name", "first event");
    v1.property(VertexProperty.Cardinality.single, "text", "this text will help to identify the first event");
    long time1 = System.currentTimeMillis();
    v1.property(VertexProperty.Cardinality.single, "time", time1);
    TitanVertex v2 = tx.addVertex("event");
    v2.property(VertexProperty.Cardinality.single, "name", "second event");
    v2.property(VertexProperty.Cardinality.single, "text", "this text won't match");
    long time2 = time1 + 1;
    v2.property(VertexProperty.Cardinality.single, "time", time2);

    evaluateQuery(tx.query().has("name", "first event").orderBy("time", decr),
            ElementCategory.VERTEX, 1, new boolean[]{true, true}, tx.getPropertyKey("time"), Order.DESC, "index1");
    evaluateQuery(tx.query().has("text", Text.CONTAINS, "help").has(LABEL_NAME, "event"),
            ElementCategory.VERTEX, 1, new boolean[]{true, true}, "index2");

    clopen();

    Object v1Id = v1.id();
    Object v2Id = v2.id();

    evaluateQuery(tx.query().has("name", "first event").orderBy("time", decr),
            ElementCategory.VERTEX, 1, new boolean[]{true, true}, tx.getPropertyKey("time"), Order.DESC, "index1");
    evaluateQuery(tx.query().has("text", Text.CONTAINS, "help").has(LABEL_NAME, "event"),
            ElementCategory.VERTEX, 1, new boolean[]{true, true}, "index2");

    v1 = getV(tx, v1Id);
    v2 = getV(tx, v1Id);
    assertNotNull(v1);
    assertNotNull(v2);

    Thread.sleep(TimeUnit.MILLISECONDS.convert((long) Math.ceil(eventTTLSeconds * 1.25), TimeUnit.SECONDS));

    clopen();

    Thread.sleep(TimeUnit.MILLISECONDS.convert((long) Math.ceil(eventTTLSeconds * 1.25), TimeUnit.SECONDS));

    evaluateQuery(tx.query().has("text", Text.CONTAINS, "help").has(LABEL_NAME, "event"),
            ElementCategory.VERTEX, 0, new boolean[]{true, true}, "index2");
    evaluateQuery(tx.query().has("name", "first event").orderBy("time", decr),
            ElementCategory.VERTEX, 0, new boolean[]{true, true}, tx.getPropertyKey("time"), Order.DESC, "index1");


    v1 = getV(tx, v1Id);
    v2 = getV(tx, v2Id);
    assertNull(v1);
    assertNull(v2);
}
 
Example 11
Source File: TitanIndexTest.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
private void testNestedWrites(String initialValue, String updatedValue) throws BackendException {
    // This method touches a single vertex with multiple transactions,
    // leading to deadlock under BDB and cascading test failures. Check for
    // the hasTxIsolation() store feature, which is currently true for BDB
    // but false for HBase/Cassandra. This is kind of a hack; a more robust
    // approach might implement different methods/assertions depending on
    // whether the store is capable of deadlocking or detecting conflicting
    // writes and aborting a transaction.
    Backend b = null;
    try {
        b = graph.getConfiguration().getBackend();
        if (b.getStoreFeatures().hasTxIsolation()) {
            log.info("Skipping " + getClass().getSimpleName() + "." + methodName.getMethodName());
            return;
        }
    } finally {
        if (null != b)
            b.close();
    }

    final String propName = "foo";

    // Write schema and one vertex
    PropertyKey prop = makeKey(propName, String.class);
    createExternalVertexIndex(prop, INDEX);
    finishSchema();

    TitanVertex v = graph.addVertex();
    if (null != initialValue)
        v.property(VertexProperty.Cardinality.single, propName, initialValue);
    graph.tx().commit();

    Object id = v.id();

    // Open two transactions and modify the same vertex
    TitanTransaction vertexDeleter = graph.newTransaction();
    TitanTransaction propDeleter = graph.newTransaction();

    getV(vertexDeleter, id).remove();
    if (null == updatedValue)
        getV(propDeleter, id).property(propName).remove();
    else
        getV(propDeleter, id).property(VertexProperty.Cardinality.single, propName, updatedValue);

    vertexDeleter.commit();
    propDeleter.commit();

    // The vertex must not exist after deletion
    graph.tx().rollback();
    assertEquals(null, getV(graph, id));
    assertEmpty(graph.query().has(propName).vertices());
    if (null != updatedValue)
        assertEmpty(graph.query().has(propName, updatedValue).vertices());
    graph.tx().rollback();
}