public JanusGraphIndex getExternalIndex(Class<? extends Element> clazz, String backingIndex) { String prefix; if (Vertex.class.isAssignableFrom(clazz)) prefix = "v"; else if (Edge.class.isAssignableFrom(clazz)) prefix = "e"; else if (JanusGraphVertexProperty.class.isAssignableFrom(clazz)) prefix = "p"; else throw new AssertionError(clazz.toString()); final String indexName = prefix+backingIndex; JanusGraphIndex index = mgmt.getGraphIndex(indexName); if (index==null) { index = mgmt.buildIndex(indexName,clazz).buildMixedIndex(backingIndex); } return index; }
public PropertyKey makeVertexIndexedKey(String name, Class dataType) { final PropertyKey key = mgmt.makePropertyKey(name).dataType(dataType).cardinality(Cardinality.SINGLE).make(); mgmt.buildIndex(name,Vertex.class).addKey(key).buildCompositeIndex(); return key; }
public PropertyKey makeVertexIndexedUniqueKey(String name, Class dataType) { final PropertyKey key = mgmt.makePropertyKey(name).dataType(dataType).cardinality(Cardinality.SINGLE).make(); mgmt.buildIndex(name,Vertex.class).addKey(key).unique().buildCompositeIndex(); return key; }
@Test public void testIndexQueryWithScore() throws InterruptedException { final PropertyKey textKey = mgmt.makePropertyKey("text").dataType(String.class).make(); mgmt.buildIndex("store1", Vertex.class).addKey(textKey).buildMixedIndex(INDEX); mgmt.commit(); final JanusGraphVertex v1 = tx.addVertex(); final JanusGraphVertex v2 = tx.addVertex(); final JanusGraphVertex v3 = tx.addVertex(); v1.property("text", "Hello Hello Hello Hello Hello Hello Hello Hello world"); v2.property("text", "Hello abab abab fsdfsd sfdfsd sdffs fsdsdf fdf fsdfsd aera fsad abab abab fsdfsd sfdf"); v3.property("text", "Hello Hello world world"); tx.commit(); final Set<Double> scores = graph.indexQuery("store1", "v.text:(Hello)").vertexStream() .map(JanusGraphIndexQuery.Result::getScore) .collect(Collectors.toSet()); Assert.assertEquals(3, scores.size()); }
/** * Tests indexing using _all virtual field */ @Test public void testWidcardQuery() { if (supportsWildcardQuery()) { final PropertyKey p1 = makeKey("p1", String.class); final PropertyKey p2 = makeKey("p2", String.class); mgmt.buildIndex("mixedIndex", Vertex.class).addKey(p1).addKey(p2).buildMixedIndex(INDEX); finishSchema(); clopen(); final JanusGraphVertex v1 = graph.addVertex(); v1.property("p1", "test1"); v1.property("p2", "test2"); clopen();//Flush the index assertEquals(v1, graph.indexQuery("mixedIndex", "v.*:\"test1\"").vertexStream().findFirst().orElseThrow(IllegalStateException::new).getElement()); assertEquals(v1, graph.indexQuery("mixedIndex", "v.*:\"test2\"").vertexStream().findFirst().orElseThrow(IllegalStateException::new).getElement()); } }
@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 { final PropertyKey name = makeKey("name", String.class); mgmt.buildIndex("store1", Vertex.class).addKey(name).buildMixedIndex(INDEX); mgmt.commit(); final JanusGraphVertex v1 = tx.addVertex(); v1.property("name", "hercules was here"); tx.commit(); final JanusGraphVertex r = Iterables.get(graph.query().has("name", Text.CONTAINS, "hercules here").vertices(), 0); Assert.assertEquals(r.property("name").value(), "hercules was here"); }
@Test public void testOrForceIndexMixedAndCompositeIndex() throws Exception { JanusGraph customGraph = null; try { customGraph = this.getForceIndexGraph(); final JanusGraphManagement management = customGraph.openManagement(); final PropertyKey nameProperty = management.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.SINGLE).make(); final PropertyKey ageProperty = management.makePropertyKey("age").dataType(Integer.class).cardinality(Cardinality.SINGLE).make(); final PropertyKey lengthProperty = management.makePropertyKey("length").dataType(Integer.class).cardinality(Cardinality.SINGLE).make(); management.buildIndex("nameidx", Vertex.class).addKey(nameProperty, getStringMapping()).buildMixedIndex(INDEX); management.buildIndex("ageridx", Vertex.class).addKey(ageProperty).buildCompositeIndex(); management.buildIndex("lengthidx", Vertex.class).addKey(lengthProperty).buildMixedIndex(INDEX); management.commit(); customGraph.tx().commit(); testOr(customGraph); } finally { if (customGraph != null) { JanusGraphFactory.close(customGraph); } } }
@Test public void testIndexShouldNotBeEnabledForExistingPropertyKeyWithoutLabelConstraint() { mgmt.makePropertyKey("alreadyExistingProperty").dataType(String.class).cardinality(Cardinality.SINGLE).make(); finishSchema(); PropertyKey existingPropertyKey = mgmt.getPropertyKey("alreadyExistingProperty"); mgmt.buildIndex("newIndex", Vertex.class).addKey(existingPropertyKey).buildCompositeIndex(); finishSchema(); assertNotEquals(SchemaStatus.ENABLED, mgmt.getGraphIndex("newIndex").getIndexStatus(existingPropertyKey)); }
public void testBatchLoadingLocking(boolean batchLoading) { PropertyKey uid = makeKey("uid",Long.class); JanusGraphIndex uidIndex = mgmt.buildIndex("uid",Vertex.class).unique().addKey(uid).buildCompositeIndex(); mgmt.setConsistency(uid, ConsistencyModifier.LOCK); mgmt.setConsistency(uidIndex,ConsistencyModifier.LOCK); EdgeLabel knows = mgmt.makeEdgeLabel("knows").multiplicity(Multiplicity.ONE2ONE).make(); mgmt.setConsistency(knows,ConsistencyModifier.LOCK); finishSchema(); TestLockerManager.ERROR_ON_LOCKING=true; clopen(option(GraphDatabaseConfiguration.STORAGE_BATCH),batchLoading, option(GraphDatabaseConfiguration.LOCK_BACKEND),"test"); int numV = 10000; for (int i=0;i<numV;i++) { JanusGraphVertex v = tx.addVertex("uid",i+1); v.addEdge("knows",v); } clopen(); for (int i=0;i<Math.min(numV,300);i++) { assertEquals(1, Iterables.size(graph.query().has("uid", i + 1).vertices())); JanusGraphVertex v = Iterables.getOnlyElement(graph.query().has("uid", i + 1).vertices()); assertEquals(1, Iterables.size(v.query().direction(OUT).labels("knows").edges())); } }
@Test public void testStaleVertex() { PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).make(); mgmt.makePropertyKey("age").dataType(Integer.class).make(); mgmt.buildIndex("byName", Vertex.class).addKey(name).unique().buildCompositeIndex(); finishSchema(); JanusGraphVertex cartman = graph.addVertex("name", "cartman", "age", 10); graph.addVertex("name", "stan", "age", 8); graph.tx().commit(); cartman = Iterables.getOnlyElement(graph.query().has("name", "cartman").vertices()); graph.tx().commit(); JanusGraphVertexProperty p = (JanusGraphVertexProperty) cartman.properties().next(); assertTrue((p.longId()) > 0); graph.tx().commit(); }
@Test public void testIndexShouldBeEnabledForExistingPropertyKeyAndConstrainedToNewVertexLabel() { mgmt.makePropertyKey("alreadyExistingProperty").dataType(String.class).cardinality(Cardinality.SINGLE).make(); finishSchema(); PropertyKey existingPropertyKey = mgmt.getPropertyKey("alreadyExistingProperty"); VertexLabel newLabel = mgmt.makeVertexLabel("newLabel").make(); mgmt.buildIndex("newIndex", Vertex.class).addKey(existingPropertyKey).indexOnly(newLabel).buildCompositeIndex(); finishSchema(); assertEquals(SchemaStatus.ENABLED, mgmt.getGraphIndex("newIndex").getIndexStatus(existingPropertyKey)); }
@Test public void testIndexShouldBeEnabledForExistingPropertyKeyAndConstrainedToNewEdgeLabel() { mgmt.makePropertyKey("alreadyExistingProperty").dataType(String.class).cardinality(Cardinality.SINGLE).make(); finishSchema(); PropertyKey existingPropertyKey = mgmt.getPropertyKey("alreadyExistingProperty"); EdgeLabel newLabel = mgmt.makeEdgeLabel("newLabel").make(); mgmt.buildIndex("newIndex", Edge.class).addKey(existingPropertyKey).indexOnly(newLabel).buildCompositeIndex(); finishSchema(); assertEquals(SchemaStatus.ENABLED, mgmt.getGraphIndex("newIndex").getIndexStatus(existingPropertyKey)); }
@Test public void testIndexShouldNotBeEnabledForExistingPropertyKeyAndConstrainedToExistingEdgeLabel() { mgmt.makePropertyKey("alreadyExistingProperty").dataType(String.class).cardinality(Cardinality.SINGLE).make(); mgmt.makeEdgeLabel("alreadyExistingLabel").make(); finishSchema(); PropertyKey existingPropertyKey = mgmt.getPropertyKey("alreadyExistingProperty"); EdgeLabel existingLabel = mgmt.getEdgeLabel("alreadyExistingLabel"); mgmt.buildIndex("newIndex", Edge.class).addKey(existingPropertyKey).indexOnly(existingLabel).buildCompositeIndex(); finishSchema(); assertNotEquals(SchemaStatus.ENABLED, mgmt.getGraphIndex("newIndex").getIndexStatus(existingPropertyKey)); }
@Test public void testIndexShouldNotBeEnabledForExistingPropertyKeyAndConstrainedToExistingVertexLabel() { mgmt.makePropertyKey("alreadyExistingProperty").dataType(String.class).cardinality(Cardinality.SINGLE).make(); mgmt.makeVertexLabel("alreadyExistingLabel").make(); finishSchema(); PropertyKey existingPropertyKey = mgmt.getPropertyKey("alreadyExistingProperty"); VertexLabel existingLabel = mgmt.getVertexLabel("alreadyExistingLabel"); mgmt.buildIndex("newIndex", Vertex.class).addKey(existingPropertyKey).indexOnly(existingLabel).buildCompositeIndex(); finishSchema(); assertNotEquals(SchemaStatus.ENABLED, mgmt.getGraphIndex("newIndex").getIndexStatus(existingPropertyKey)); }
@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(); JanusGraphIndex uidCompositeIndex = mgmt.buildIndex("uidIndex", Vertex.class) .indexOnly(label).addKey(uid).unique().buildCompositeIndex(); mgmt.setConsistency(uidCompositeIndex, ConsistencyModifier.LOCK); finishSchema(); JanusGraphVertex foo = graph.addVertex(labelName); JanusGraphVertex bar = graph.addVertex(labelName); foo.property("uid", "foo"); bar.property("uid", "bar"); graph.tx().commit(); Iterable<JanusGraphVertex> vertexes = graph.query() .has("uid", Contain.IN, ImmutableList.of("foo", "bar")) .has(LABEL_NAME, labelName) .vertices(); assertEquals(2, Iterables.size(vertexes)); for (JanusGraphVertex v : vertexes) { assertEquals(labelName, v.vertexLabel().name()); } }
@Test public void testOrPartialIndex() { final PropertyKey nameProperty = mgmt.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.SINGLE).make(); mgmt.makePropertyKey("age").dataType(Integer.class).cardinality(Cardinality.SINGLE).make(); final PropertyKey lengthProperty = mgmt.makePropertyKey("length").dataType(Integer.class).cardinality(Cardinality.SINGLE).make(); mgmt.buildIndex("otheridx", Vertex.class).addKey(nameProperty, getStringMapping()).addKey(lengthProperty).buildMixedIndex(INDEX); finishSchema(); clopen(); testOr(graph); }
@Test public void shouldAwaitMultipleStatuses() throws InterruptedException, ExecutionException { final PropertyKey key1 = makeKey("key1", String.class); final JanusGraphIndex index = mgmt.buildIndex("randomMixedIndex", Vertex.class).addKey(key1).buildMixedIndex(INDEX); if (index.getIndexStatus(key1) == SchemaStatus.INSTALLED) { mgmt.updateIndex(mgmt.getGraphIndex("randomMixedIndex"), SchemaAction.REGISTER_INDEX).get(); mgmt.updateIndex(mgmt.getGraphIndex("randomMixedIndex"), SchemaAction.ENABLE_INDEX).get(); } else if (index.getIndexStatus(key1) == SchemaStatus.REGISTERED) { mgmt.updateIndex(mgmt.getGraphIndex("randomMixedIndex"), SchemaAction.ENABLE_INDEX).get(); } final PropertyKey key2 = makeKey("key2", String.class); mgmt.addIndexKey(index, key2); mgmt.commit(); //key1 now has status ENABLED, let's ensure we can watch for REGISTERED and ENABLED try { ManagementSystem.awaitGraphIndexStatus(graph, "randomMixedIndex").status(SchemaStatus.REGISTERED, SchemaStatus.ENABLED).call(); } catch (final Exception e) { Assert.fail("Failed to awaitGraphIndexStatus on multiple statuses."); } }
@Test public void testOrForceIndexUniqueMixedIndex() throws Exception { JanusGraph customGraph = null; try { customGraph = this.getForceIndexGraph(); final JanusGraphManagement management = customGraph.openManagement(); final PropertyKey nameProperty = management.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.SINGLE).make(); final PropertyKey ageProperty = management.makePropertyKey("age").dataType(Integer.class).cardinality(Cardinality.SINGLE).make(); final PropertyKey lengthProperty = management.makePropertyKey("length").dataType(Integer.class).cardinality(Cardinality.SINGLE).make(); management.buildIndex("oridx", Vertex.class).addKey(nameProperty, getStringMapping()).addKey(ageProperty).addKey(lengthProperty).buildMixedIndex(INDEX); management.commit(); customGraph.tx().commit(); testOr(customGraph); } finally { if (customGraph != null) { JanusGraphFactory.close(customGraph); } } }
@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++) { JanusGraphVertex 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 ignored) { } }
@Test public void testTinkerPopCardinality() { PropertyKey id = mgmt.makePropertyKey("id").cardinality(Cardinality.SINGLE).dataType(Integer.class).make(); PropertyKey name = mgmt.makePropertyKey("name").cardinality(Cardinality.SINGLE).dataType(String.class).make(); PropertyKey names = mgmt.makePropertyKey("names").cardinality(Cardinality.LIST).dataType(String.class).make(); mgmt.buildIndex("byId", Vertex.class).addKey(id).buildCompositeIndex(); finishSchema(); GraphTraversalSource gts; Vertex v; v = graph.addVertex("id", 1); v.property(single, "name", "t1"); graph.addVertex("id", 2, "names", "n1", "names", "n2"); graph.tx().commit(); gts = graph.traversal(); v = gts.V().has("id", 1).next(); v.property(single, "name", "t2"); v = gts.V().has("id", 1).next(); v.property(single, "name", "t3"); assertCount(1, gts.V(v).properties("name")); assertCount(2, gts.V().has("id", 2).properties("names")); assertCount(2, gts.V().hasLabel("vertex")); }