/** * Sets or updates a property value. The property key will be set to a constant. This is a convenience method * which allows treating the multi-valued nature of properties as only containing a single value. Care must be * taken when using this method because properties are not only uniquely identified by just key and name but also * visibility so adding properties with the same name and different visibility strings is still permitted. * <p> * The added property will also be indexed in the configured search provider. The type of the value * will determine how it gets indexed. * * @param name The name of the property. * @param value The value of the property. * @param visibility The visibility to give this property. */ public ElementBuilder<T> setProperty(String name, Object value, Visibility visibility) { return setProperty(name, value, Metadata.create(FetchHints.ALL), visibility); }
/** * Sets or updates a property value. The property key will be set to a constant. This is a convenience method * which allows treating the multi-valued nature of properties as only containing a single value. Care must be * taken when using this method because properties are not only uniquely identified by just key and name but also * visibility so adding properties with the same name and different visibility strings is still permitted. * <p> * The added property will also be indexed in the configured search provider. The type of the value * will determine how it gets indexed. * * @param name The name of the property. * @param value The value of the property. * @param visibility The visibility to give this property. */ public ElementBuilder<T> setProperty(String name, Object value, Visibility visibility) { return setProperty(name, value, Metadata.create(FetchHints.ALL), visibility); }
@Test public void historicalPropertyValueAddProp() { graph.prepareVertex("v1", VISIBILITY_A) .setProperty("prop1_A", "value1", VISIBILITY_A) .setProperty("prop2_B", "value2", VISIBILITY_B) .save(AUTHORIZATIONS_A_AND_B); graph.flush(); // Add property Vertex v1 = graph.getVertex("v1", AUTHORIZATIONS_A_AND_B); v1.prepareMutation() .setProperty("prop3_A", "value3", VISIBILITY_A) .save(AUTHORIZATIONS_A_AND_B); graph.flush(); v1 = graph.getVertex("v1", AUTHORIZATIONS_A_AND_B); List<HistoricalPropertyValue> values = toList(v1.getHistoricalPropertyValues(AUTHORIZATIONS_A_AND_B)); Collections.reverse(values); assertEquals(3, values.size()); assertEquals("prop1_A", values.get(0).getPropertyName()); assertEquals("prop2_B", values.get(1).getPropertyName()); assertEquals("prop3_A", values.get(2).getPropertyName()); }
@Test public void testConcurrentModificationOfProperties() { Vertex v = graph.prepareVertex("v1", VISIBILITY_EMPTY) .setProperty("prop1", "value1", VISIBILITY_A) .setProperty("prop2", "value2", VISIBILITY_A) .save(AUTHORIZATIONS_A_AND_B); int i = 0; for (Property p : v.getProperties()) { assertNotNull(p.toString()); if (i == 0) { v.setProperty("prop3", "value3", VISIBILITY_A, AUTHORIZATIONS_A_AND_B); } i++; } }
@Test public void testConcurrentModificationOfProperties() { Vertex v = graph.prepareVertex("v1", VISIBILITY_EMPTY) .setProperty("prop1", "value1", VISIBILITY_A) .setProperty("prop2", "value2", VISIBILITY_A) .save(AUTHORIZATIONS_A_AND_B); int i = 0; for (Property p : v.getProperties()) { assertNotNull(p.toString()); if (i == 0) { v.setProperty("prop3", "value3", VISIBILITY_A, AUTHORIZATIONS_A_AND_B); } i++; } }
@Test public void testGraphQueryWithANDOperatorAndWithExactMatchFields() { graph.defineProperty("firstName").dataType(String.class).textIndexHint(TextIndexHint.EXACT_MATCH).define(); graph.prepareVertex("v1", VISIBILITY_A) .setProperty("firstName", "Joe", VISIBILITY_A) .setProperty("lastName", "Ferner", VISIBILITY_A) .save(AUTHORIZATIONS_A_AND_B); graph.prepareVertex("v2", VISIBILITY_A) .setProperty("firstName", "Joe", VISIBILITY_A) .setProperty("lastName", "Smith", VISIBILITY_A) .save(AUTHORIZATIONS_A_AND_B); graph.flush(); assumeTrue("lucene and queries not supported", isLuceneQueriesSupported() && isLuceneAndQueriesSupported()); Iterable<Vertex> vertices = graph.query("Joe AND ferner", AUTHORIZATIONS_A) .vertices(); Assert.assertEquals(1, count(vertices)); }
@Test public void testGraphQueryWithANDOperatorAndWithExactMatchFields() { graph.defineProperty("firstName").dataType(String.class).textIndexHint(TextIndexHint.EXACT_MATCH).define(); graph.prepareVertex("v1", VISIBILITY_A) .setProperty("firstName", "Joe", VISIBILITY_A) .setProperty("lastName", "Ferner", VISIBILITY_A) .save(AUTHORIZATIONS_A_AND_B); graph.prepareVertex("v2", VISIBILITY_A) .setProperty("firstName", "Joe", VISIBILITY_A) .setProperty("lastName", "Smith", VISIBILITY_A) .save(AUTHORIZATIONS_A_AND_B); graph.flush(); assumeTrue("lucene and queries not supported", isLuceneQueriesSupported() && isLuceneAndQueriesSupported()); Iterable<Vertex> vertices = graph.query("Joe AND ferner", AUTHORIZATIONS_A) .vertices(); Assert.assertEquals(1, count(vertices)); }
@Test public void testChangeVisibilityOnBadPropertyName() { graph.prepareVertex("v1", VISIBILITY_A) .setProperty("prop1", "value1", VISIBILITY_EMPTY) .setProperty("prop2", "value2", VISIBILITY_B) .save(AUTHORIZATIONS_A_AND_B); graph.flush(); try { graph.getVertex("v1", AUTHORIZATIONS_A) .prepareMutation() .alterPropertyVisibility("propBad", VISIBILITY_B) .save(AUTHORIZATIONS_A_AND_B); fail("show throw"); } catch (VertexiumException ex) { assertNotNull(ex); } }
@Test public void testModifyVertexWithLowerAuthorizationThenOtherProperties() { graph.prepareVertex("v1", VISIBILITY_A) .setProperty("prop1", "value1", VISIBILITY_A) .setProperty("prop2", "value2", VISIBILITY_B) .save(AUTHORIZATIONS_A_AND_B); graph.flush(); Vertex v1 = graph.getVertex("v1", AUTHORIZATIONS_A); v1.setProperty("prop1", "value1New", VISIBILITY_A, AUTHORIZATIONS_A_AND_B); graph.flush(); Iterable<Vertex> vertices = graph.query(AUTHORIZATIONS_A_AND_B) .has("prop2", "value2") .vertices(); assertVertexIds(vertices, "v1"); }
@Test public void testModifyVertexWithLowerAuthorizationThenOtherProperties() { graph.prepareVertex("v1", VISIBILITY_A) .setProperty("prop1", "value1", VISIBILITY_A) .setProperty("prop2", "value2", VISIBILITY_B) .save(AUTHORIZATIONS_A_AND_B); graph.flush(); Vertex v1 = graph.getVertex("v1", AUTHORIZATIONS_A); v1.setProperty("prop1", "value1New", VISIBILITY_A, AUTHORIZATIONS_A_AND_B); graph.flush(); Iterable<Vertex> vertices = graph.query(AUTHORIZATIONS_A_AND_B) .has("prop2", "value2") .vertices(); assertVertexIds(vertices, "v1"); }
@Test public void testChangeVisibilityOnBadPropertyName() { graph.prepareVertex("v1", VISIBILITY_A) .setProperty("prop1", "value1", VISIBILITY_EMPTY) .setProperty("prop2", "value2", VISIBILITY_B) .save(AUTHORIZATIONS_A_AND_B); graph.flush(); try { graph.getVertex("v1", AUTHORIZATIONS_A) .prepareMutation() .alterPropertyVisibility("propBad", VISIBILITY_B) .save(AUTHORIZATIONS_A_AND_B); fail("show throw"); } catch (VertexiumException ex) { assertNotNull(ex); } }
@Test public void testPartialUpdateOfVertex() { graph.prepareVertex("v1", VISIBILITY_A) .setProperty("prop1", "value1", VISIBILITY_A) .setProperty("prop2", "value2", VISIBILITY_A) .save(AUTHORIZATIONS_A_AND_B); graph.flush(); graph.prepareVertex("v1", VISIBILITY_A) .setProperty("prop1", "value1New", VISIBILITY_A) .save(AUTHORIZATIONS_A_AND_B); graph.flush(); Iterable<Vertex> vertices = graph.query(AUTHORIZATIONS_A_AND_B) .has("prop2", "value2") .vertices(); assertVertexIds(vertices, "v1"); }
@Test public void testPartialUpdateOfVertex() { graph.prepareVertex("v1", VISIBILITY_A) .setProperty("prop1", "value1", VISIBILITY_A) .setProperty("prop2", "value2", VISIBILITY_A) .save(AUTHORIZATIONS_A_AND_B); graph.flush(); graph.prepareVertex("v1", VISIBILITY_A) .setProperty("prop1", "value1New", VISIBILITY_A) .save(AUTHORIZATIONS_A_AND_B); graph.flush(); Iterable<Vertex> vertices = graph.query(AUTHORIZATIONS_A_AND_B) .has("prop2", "value2") .vertices(); assertVertexIds(vertices, "v1"); }
@Test public void testGraphQueryVertexNoVisibility() { graph.prepareVertex("v1", VISIBILITY_EMPTY) .setProperty("text", "hello", VISIBILITY_EMPTY) .setProperty("age", 25, VISIBILITY_EMPTY) .save(AUTHORIZATIONS_A_AND_B); graph.flush(); Iterable<Vertex> vertices = graph.query("hello", AUTHORIZATIONS_A_AND_B) .has("age", Compare.EQUAL, 25) .vertices(); Assert.assertEquals(1, count(vertices)); vertices = graph.query("hello", AUTHORIZATIONS_A_AND_B) .vertices(); Assert.assertEquals(1, count(vertices)); }
@Test public void testGraphQueryVertexNoVisibility() { graph.prepareVertex("v1", VISIBILITY_EMPTY) .setProperty("text", "hello", VISIBILITY_EMPTY) .setProperty("age", 25, VISIBILITY_EMPTY) .save(AUTHORIZATIONS_A_AND_B); graph.flush(); Iterable<Vertex> vertices = graph.query("hello", AUTHORIZATIONS_A_AND_B) .has("age", Compare.EQUAL, 25) .vertices(); Assert.assertEquals(1, count(vertices)); vertices = graph.query("hello", AUTHORIZATIONS_A_AND_B) .vertices(); Assert.assertEquals(1, count(vertices)); }
@Test public void testTextIndexStreamingPropertyValue() throws Exception { graph.defineProperty("none").dataType(String.class).textIndexHint(TextIndexHint.NONE).define(); graph.defineProperty("both").dataType(String.class).textIndexHint(TextIndexHint.ALL).define(); graph.defineProperty("fullText").dataType(String.class).textIndexHint(TextIndexHint.FULL_TEXT).define(); graph.prepareVertex("v1", VISIBILITY_A) .setProperty("none", StreamingPropertyValue.create("Test Value"), VISIBILITY_A) .setProperty("both", StreamingPropertyValue.create("Test Value"), VISIBILITY_A) .setProperty("fullText", StreamingPropertyValue.create("Test Value"), VISIBILITY_A) .save(AUTHORIZATIONS_A_AND_B); graph.flush(); Assert.assertEquals(1, count(graph.query(AUTHORIZATIONS_A).has("both", TextPredicate.CONTAINS, "Test").vertices())); Assert.assertEquals(1, count(graph.query(AUTHORIZATIONS_A).has("fullText", TextPredicate.CONTAINS, "Test").vertices())); Assert.assertEquals("un-indexed property shouldn't match partials", 0, count(graph.query(AUTHORIZATIONS_A).has("none", "Test").vertices())); try { graph.query(AUTHORIZATIONS_A).has("none", TextPredicate.CONTAINS, "Test"); fail("Full text queries should not be allowed for properties that are not indexed with FULL_TEXT."); } catch (VertexiumException ve) { assertEquals("Check your TextIndexHint settings. Property none is not full text indexed.", ve.getMessage()); } }
@Test public void testGraphQueryHasWithSpacesAndFieldedQueryString() { assumeTrue("fielded query not supported", isFieldNamesInQuerySupported()); graph.defineProperty("http://vertexium.org#name").dataType(String.class).textIndexHint(TextIndexHint.ALL).define(); graph.prepareVertex("v1", VISIBILITY_A) .setProperty("http://vertexium.org#name", "Joe Ferner", VISIBILITY_A) .setProperty("propWithHyphen", "hyphen-word", VISIBILITY_A) .save(AUTHORIZATIONS_A_AND_B); graph.prepareVertex("v2", VISIBILITY_A) .setProperty("http://vertexium.org#name", "Joe Smith", VISIBILITY_A) .save(AUTHORIZATIONS_A_AND_B); assumeTrue("lucene queries", isLuceneQueriesSupported()); Iterable<Vertex> vertices = graph.query("http\\:\\/\\/vertexium.org#name:Joe", AUTHORIZATIONS_A) .vertices(); Assert.assertEquals(2, count(vertices)); vertices = graph.query("http\\:\\/\\/vertexium.org#name:\"Joe Ferner\"", AUTHORIZATIONS_A) .vertices(); Assert.assertEquals(1, count(vertices)); }
@Test public void testAddVertexWithPropertiesWithTwoDifferentVisibilities() { Vertex v = graph.prepareVertex("v1", VISIBILITY_EMPTY) .setProperty("prop1", "value1a", VISIBILITY_A) .setProperty("prop1", "value1b", VISIBILITY_B) .save(AUTHORIZATIONS_A_AND_B); Assert.assertEquals(2, count(v.getProperties("prop1"))); graph.flush(); v = graph.getVertex("v1", AUTHORIZATIONS_A_AND_B); Assert.assertEquals(2, count(v.getProperties("prop1"))); v = graph.getVertex("v1", AUTHORIZATIONS_A); Assert.assertEquals(1, count(v.getProperties("prop1"))); assertEquals("value1a", v.getPropertyValue("prop1")); v = graph.getVertex("v1", AUTHORIZATIONS_B); Assert.assertEquals(1, count(v.getProperties("prop1"))); assertEquals("value1b", v.getPropertyValue("prop1")); }
@Test public void testAddVertexWithPropertiesWithTwoDifferentVisibilities() { Vertex v = graph.prepareVertex("v1", VISIBILITY_EMPTY) .setProperty("prop1", "value1a", VISIBILITY_A) .setProperty("prop1", "value1b", VISIBILITY_B) .save(AUTHORIZATIONS_A_AND_B); Assert.assertEquals(2, count(v.getProperties("prop1"))); graph.flush(); v = graph.getVertex("v1", AUTHORIZATIONS_A_AND_B); Assert.assertEquals(2, count(v.getProperties("prop1"))); v = graph.getVertex("v1", AUTHORIZATIONS_A); Assert.assertEquals(1, count(v.getProperties("prop1"))); assertEquals("value1a", v.getPropertyValue("prop1")); v = graph.getVertex("v1", AUTHORIZATIONS_B); Assert.assertEquals(1, count(v.getProperties("prop1"))); assertEquals("value1b", v.getPropertyValue("prop1")); }
@Test public void testGraphQueryHasWithSpacesAndFieldedQueryString() { assumeTrue("fielded query not supported", isFieldNamesInQuerySupported()); graph.defineProperty("http://vertexium.org#name").dataType(String.class).textIndexHint(TextIndexHint.ALL).define(); graph.prepareVertex("v1", VISIBILITY_A) .setProperty("http://vertexium.org#name", "Joe Ferner", VISIBILITY_A) .setProperty("propWithHyphen", "hyphen-word", VISIBILITY_A) .save(AUTHORIZATIONS_A_AND_B); graph.prepareVertex("v2", VISIBILITY_A) .setProperty("http://vertexium.org#name", "Joe Smith", VISIBILITY_A) .save(AUTHORIZATIONS_A_AND_B); assumeTrue("lucene queries", isLuceneQueriesSupported()); Iterable<Vertex> vertices = graph.query("http\\:\\/\\/vertexium.org#name:Joe", AUTHORIZATIONS_A) .vertices(); Assert.assertEquals(2, count(vertices)); vertices = graph.query("http\\:\\/\\/vertexium.org#name:\"Joe Ferner\"", AUTHORIZATIONS_A) .vertices(); Assert.assertEquals(1, count(vertices)); }