@GET @Timed @Path("scan") @Compress @Produces(APPLICATION_JSON_WITH_CHARSET) public String scan(@Context GraphManager manager, @PathParam("graph") String graph, @QueryParam("start") String start, @QueryParam("end") String end) { LOG.debug("Graph [{}] query edges by shard(start: {}, end: {}) ", graph, start, end); HugeGraph g = graph(manager, graph); ConditionQuery query = new ConditionQuery(HugeType.EDGE_OUT); query.scan(start, end); Iterator<Edge> edges = g.edges(query); return manager.serializer(g).writeEdges(edges, false); } }
@GET @Timed @Path("scan") @Compress @Produces(APPLICATION_JSON_WITH_CHARSET) public String scan(@Context GraphManager manager, @PathParam("graph") String graph, @QueryParam("start") String start, @QueryParam("end") String end) { LOG.debug("Graph [{}] query vertices by shard(start: {}, end: {}) ", graph, start, end); HugeGraph g = graph(manager, graph); ConditionQuery query = new ConditionQuery(HugeType.VERTEX); query.scan(start, end); Iterator<Vertex> vertices = g.vertices(query); return manager.serializer(g).writeVertices(vertices, false); } }
public Iterator<Edge> queryVertexEdge(Object id, Directions direction) { ConditionQuery q = new ConditionQuery(HugeType.EDGE); q.eq(HugeKeys.OWNER_VERTEX, id); q.eq(HugeKeys.DIRECTION, direction); return this.hugegraph.edges(q); } }
/** * Currently doesn't allow to exist schema with the same name * @param type the query schema type * @param name the query schema name * @param <T> SubClass of SchemaElement * @return the queried schema object */ protected <T extends SchemaElement> T getSchema(HugeType type, String name) { LOG.debug("SchemaTransaction get {} by name '{}'", type, name); this.beforeRead(); ConditionQuery query = new ConditionQuery(type); query.eq(HugeKeys.NAME, name); Iterator<BackendEntry> itor = this.indexTx.query(query); this.afterRead(); if (itor.hasNext()) { T schema = this.deserialize(itor.next(), type); E.checkState(!itor.hasNext(), "Should not exist schema with same name '%s'", name); return schema; } return null; }
@Test public void testFlattenWithOr() { Condition c1 = Condition.eq(IdGenerator.of("c1"), "1"); Condition c2 = Condition.eq(IdGenerator.of("c2"), "2"); ConditionQuery query = new ConditionQuery(HugeType.VERTEX); query.query(c1.or(c2)); Assert.assertEquals(1, query.conditions().size()); List<ConditionQuery> queries = ConditionQueryFlatten.flatten(query); Assert.assertEquals(2, queries.size()); Set<Set<Condition>> expect; expect = ImmutableSet.of(ImmutableSet.of(c1), ImmutableSet.of(c2)); Set<Set<Condition>> actual = new HashSet<>(); for (ConditionQuery q : queries) { actual.add(q.conditions()); } Assert.assertEquals(expect, actual); }
@Test public void testFlattenWithAnd() { Condition c1 = Condition.eq(IdGenerator.of("c1"), "1"); Condition c2 = Condition.eq(IdGenerator.of("c2"), "2"); ConditionQuery query = new ConditionQuery(HugeType.VERTEX); query.query(c1.and(c2)); Assert.assertEquals(1, query.conditions().size()); List<ConditionQuery> queries = ConditionQueryFlatten.flatten(query); Assert.assertEquals(1, queries.size()); Set<Set<Condition>> expect; expect = ImmutableSet.of(ImmutableSet.of(c1, c2)); Set<Set<Condition>> actual = new HashSet<>(); for (ConditionQuery q : queries) { actual.add(q.conditions()); } Assert.assertEquals(expect, actual); }
private Iterator<E> edges() { LOG.debug("HugeGraphStep.edges(): {}", this); HugeGraph graph = (HugeGraph) this.getTraversal().getGraph().get(); if (this.ids != null && this.ids.length > 0) { return TraversalUtil.filterResult(this.hasContainers, graph.edges(this.ids)); } Query query = null; if (this.hasContainers.isEmpty()) { /* Query all */ query = new Query(HugeType.EDGE); } else { ConditionQuery q = new ConditionQuery(HugeType.EDGE); query = TraversalUtil.fillConditionQuery(this.hasContainers, q, graph); } query = this.injectQueryInfo(query); @SuppressWarnings("unchecked") Iterator<E> result = (Iterator<E>) graph.edges(query); return result; }
private Iterator<E> vertices() { LOG.debug("HugeGraphStep.vertices(): {}", this); HugeGraph graph = (HugeGraph) this.getTraversal().getGraph().get(); if (this.ids != null && this.ids.length > 0) { return TraversalUtil.filterResult(this.hasContainers, graph.vertices(this.ids)); } Query query = null; if (this.hasContainers.isEmpty()) { // Query all query = new Query(HugeType.VERTEX); } else { ConditionQuery q = new ConditionQuery(HugeType.VERTEX); query = TraversalUtil.fillConditionQuery(this.hasContainers, q, graph); } query = this.injectQueryInfo(query); @SuppressWarnings("unchecked") Iterator<E> result = (Iterator<E>) graph.vertices(query); return result; }
@Test public void testFlattenWithOrTree() { Condition c1 = Condition.eq(IdGenerator.of("c1"), "1"); Condition c2 = Condition.eq(IdGenerator.of("c2"), "2"); Condition c3 = Condition.eq(IdGenerator.of("c3"), "3"); Condition c4 = Condition.eq(IdGenerator.of("c4"), "4"); ConditionQuery query = new ConditionQuery(HugeType.VERTEX); query.query(c1.or(c2).or(c3.or(c4))); Assert.assertEquals(1, query.conditions().size()); List<ConditionQuery> queries = ConditionQueryFlatten.flatten(query); Assert.assertEquals(4, queries.size()); Set<Set<Condition>> expect; expect = ImmutableSet.of(ImmutableSet.of(c1), ImmutableSet.of(c2), ImmutableSet.of(c3), ImmutableSet.of(c4)); Set<Set<Condition>> actual = new HashSet<>(); for (ConditionQuery q : queries) { actual.add(q.conditions()); } Assert.assertEquals(expect, actual); }
private Iterator<Vertex> queryAllVariableVertices() { ConditionQuery query = new ConditionQuery(HugeType.VERTEX); VertexLabel vl = this.graph.vertexLabel(Hidden.hide(VARIABLES)); query.eq(HugeKeys.LABEL, vl.id()); query.showHidden(true); return this.graph.vertices(query); }
@Test public void testFlattenWithAndOrTree() { Condition c1 = Condition.eq(IdGenerator.of("c1"), "1"); Condition c2 = Condition.eq(IdGenerator.of("c2"), "2"); Condition c3 = Condition.eq(IdGenerator.of("c3"), "3"); Condition c4 = Condition.eq(IdGenerator.of("c4"), "4"); ConditionQuery query = new ConditionQuery(HugeType.VERTEX); query.query(c1.or(c2).and(c3.or(c4))); Assert.assertEquals(1, query.conditions().size()); List<ConditionQuery> queries = ConditionQueryFlatten.flatten(query); Assert.assertEquals(4, queries.size()); Set<Set<Condition>> expect; expect = ImmutableSet.of(ImmutableSet.of(c1, c3), ImmutableSet.of(c1, c4), ImmutableSet.of(c2, c3), ImmutableSet.of(c2, c4)); Set<Set<Condition>> actual = new HashSet<>(); for (ConditionQuery q : queries) { actual.add(q.conditions()); } Assert.assertEquals(expect, actual); }
@Test public void testQueryEdgesByDirection() { HugeGraph graph = graph(); init18Edges(); // Query vertex by condition (filter by Direction) ConditionQuery q = new ConditionQuery(HugeType.EDGE); q.eq(HugeKeys.DIRECTION, Direction.OUT); Assert.assertThrows(BackendException.class, () -> { graph.edges(q); }); }
private HugeVertex queryVariableVertex(String key) { ConditionQuery query = new ConditionQuery(HugeType.VERTEX); VertexLabel vl = this.graph.vertexLabel(Hidden.hide(VARIABLES)); query.eq(HugeKeys.LABEL, vl.id()); PropertyKey pkey = this.graph.propertyKey(Hidden.hide(VARIABLE_KEY)); query.query(Condition.eq(pkey.id(), key)); query.showHidden(true); Iterator<Vertex> vertices = this.graph.vertices(query); if (!vertices.hasNext()) { return null; } return (HugeVertex) vertices.next(); }
@Test public void testFlattenWithOrAndTree() { Condition c1 = Condition.eq(IdGenerator.of("c1"), "1"); Condition c2 = Condition.eq(IdGenerator.of("c2"), "2"); Condition c3 = Condition.eq(IdGenerator.of("c3"), "3"); Condition c4 = Condition.eq(IdGenerator.of("c4"), "4"); ConditionQuery query = new ConditionQuery(HugeType.VERTEX); query.query(c1.and(c2).or(c3.and(c4))); Assert.assertEquals(1, query.conditions().size()); List<ConditionQuery> queries = ConditionQueryFlatten.flatten(query); Assert.assertEquals(2, queries.size()); Set<Set<Condition>> expect; expect = ImmutableSet.of(ImmutableSet.of(c1, c2), ImmutableSet.of(c3, c4)); Set<Set<Condition>> actual = new HashSet<>(); for (ConditionQuery q : queries) { actual.add(q.conditions()); } Assert.assertEquals(expect, actual); } }
@Test public void testFlattenWithAndTree() { Condition c1 = Condition.eq(IdGenerator.of("c1"), "1"); Condition c2 = Condition.eq(IdGenerator.of("c2"), "2"); Condition c3 = Condition.eq(IdGenerator.of("c3"), "3"); Condition c4 = Condition.eq(IdGenerator.of("c4"), "4"); ConditionQuery query = new ConditionQuery(HugeType.VERTEX); query.query(c1.and(c2).and(c3.and(c4))); Assert.assertEquals(1, query.conditions().size()); List<ConditionQuery> queries = ConditionQueryFlatten.flatten(query); Assert.assertEquals(1, queries.size()); Set<Set<Condition>> expect; expect = ImmutableSet.of(ImmutableSet.of(c1, c2, c3, c4)); Set<Set<Condition>> actual = new HashSet<>(); for (ConditionQuery q : queries) { actual.add(q.conditions()); } Assert.assertEquals(expect, actual); }
private <V> Iterator<HugeTask<V>> queryTask(Map<String, Object> conditions, long limit) { return this.call(() -> { ConditionQuery query = new ConditionQuery(HugeType.VERTEX); VertexLabel vl = this.graph.vertexLabel(TaskTransaction.TASK); query.eq(HugeKeys.LABEL, vl.id()); for (Map.Entry<String, Object> entry : conditions.entrySet()) { PropertyKey pk = this.graph.propertyKey(entry.getKey()); query.query(Condition.eq(pk.id(), entry.getValue())); } query.showHidden(true); if (limit != NO_LIMIT) { query.limit(limit); } Iterator<Vertex> vertices = this.tx().queryVertices(query); Iterator<HugeTask<V>> tasks = new MapperIterator<>(vertices, HugeTask::fromVertex); // Convert iterator to list to avoid across thread tx accessed return IteratorUtils.list(tasks); }).iterator(); }
@SuppressWarnings("unchecked") @Test public void testScanVertex() { HugeGraph graph = graph(); // TODO: also support test scan by range Assume.assumeTrue("Not support scan", storeFeatures().supportsScanToken() || storeFeatures().supportsScanKeyRange()); init10Vertices(); List<Vertex> vertexes = new LinkedList<>(); long splitSize = 1 * 1024 * 1024; Object splits = graph.graphTransaction() .metadata(HugeType.VERTEX, "splits", splitSize); for (Shard split : (List<Shard>) splits) { ConditionQuery q = new ConditionQuery(HugeType.VERTEX); q.scan(split.start(), split.end()); vertexes.addAll(ImmutableList.copyOf(graph.vertices(q))); } Assert.assertEquals(10, vertexes.size()); }
@SuppressWarnings("unchecked") @Test public void testScanEdge() { HugeGraph graph = graph(); Assume.assumeTrue("Not support scan", storeFeatures().supportsScanToken() || storeFeatures().supportsScanKeyRange()); init18Edges(); Set<Edge> edges = new HashSet<>(); long splitSize = 1 * 1024 * 1024; Object splits = graph.graphTransaction() .metadata(HugeType.EDGE_OUT, "splits", splitSize); for (Shard split : (List<Shard>) splits) { ConditionQuery q = new ConditionQuery(HugeType.EDGE); q.scan(split.start(), split.end()); edges.addAll(ImmutableList.copyOf(graph.edges(q))); } Assert.assertEquals(18, edges.size()); }
@Test public void testFlattenWithMultiAnd() { Condition c1 = Condition.eq(IdGenerator.of("c1"), "1"); Condition c2 = Condition.eq(IdGenerator.of("c2"), "2"); Condition c3 = Condition.eq(IdGenerator.of("c3"), "3"); Condition c4 = Condition.eq(IdGenerator.of("c4"), "4"); ConditionQuery query = new ConditionQuery(HugeType.VERTEX); query.query(c1); query.query(c2); query.query(c3); query.query(c4); Assert.assertEquals(4, query.conditions().size()); List<ConditionQuery> queries = ConditionQueryFlatten.flatten(query); Assert.assertEquals(1, queries.size()); Set<Set<Condition>> expect; expect = ImmutableSet.of(ImmutableSet.of(c1, c2, c3, c4)); Set<Set<Condition>> actual = new HashSet<>(); for (ConditionQuery q : queries) { actual.add(q.conditions()); } Assert.assertEquals(expect, actual); }
@Test public void testQueryFilterByPropName() { HugeGraph graph = graph(); BackendFeatures features = graph.graphTransaction().store().features(); Assume.assumeTrue("Not support CONTAINS_KEY query", features.supportsQueryWithContainsKey()); init10Vertices(); VertexLabel language = graph.vertexLabel("language"); PropertyKey dynamic = graph.propertyKey("dynamic"); // Query vertex by condition (does contain the property name?) ConditionQuery q = new ConditionQuery(HugeType.VERTEX); q.eq(HugeKeys.LABEL, language.id()); q.key(HugeKeys.PROPERTIES, dynamic.id()); List<Vertex> vertexes = ImmutableList.copyOf(graph.vertices(q)); Assert.assertEquals(1, vertexes.size()); assertContains(vertexes, T.label, "language", "name", "python", "dynamic", true); }