Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bfs optimize vertex-out-out query #1359

Merged
merged 18 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public boolean supportsQueryByLabel() {
return true;
}

@Override
public boolean supportsQueryWithInCondition() {
return true;
}

@Override
public boolean supportsQueryWithRangeCondition() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ protected Clause relation2Cql(Relation relation) {
case LTE:
return QueryBuilder.lte(key, value);
case IN:
return QueryBuilder.in(key, value);
return Clauses.in(key, (List<?>) value);
case CONTAINS_VALUE:
return QueryBuilder.contains(key, value);
case CONTAINS_KEY:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ public static boolean needAllowFiltering(Clause clause) {
ContainsClause.class.isInstance(clause);
}

public static AndClause and(Clause left, Clause right) {
public static Clause and(Clause left, Clause right) {
return new AndClause(left, right);
}

public static Clause in(String name, List<?> values) {
return new Clause.InClause(name, values);
}

static class BinClause extends Clause {

private Clause left;
Expand Down Expand Up @@ -83,6 +87,7 @@ void appendTo(StringBuilder sb, List<Object> variables,
}

static class AndClause extends BinClause {

public AndClause(Clause left, Clause right) {
super(left, "AND", right);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.baidu.hugegraph.backend.query;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;

import com.baidu.hugegraph.backend.query.Condition.RelationType;
import com.baidu.hugegraph.type.HugeType;
import com.baidu.hugegraph.type.define.HugeKeys;
import com.baidu.hugegraph.util.E;

public class BatchConditionQuery extends ConditionQuery {

private Condition.Relation in;
private final int batchSize;

public BatchConditionQuery(HugeType resultType, int batchSize) {
super(resultType);
this.in = null;
this.batchSize = batchSize;
}

public void mergeToIN(ConditionQuery query, HugeKeys key) {
Object value = query.condition(key);
if (this.in == null) {
assert !this.containsRelation(RelationType.IN);
this.resetConditions(new LinkedHashSet<>(query.conditions()));
this.unsetCondition(key);

List<Object> list = new ArrayList<>(this.batchSize);
list.add(value);
// TODO: ensure not flatten BatchQuery
this.in = (Condition.Relation) Condition.in(key, list);
this.query(this.in);
} else {
E.checkArgument(this.in.key().equals(key),
"Invalid key '%s'", key);
E.checkArgument(this.sameQueryExceptKeyIN(query),
"Can't merge query with different keys");

@SuppressWarnings("unchecked")
List<Object> values = ((List<Object>) this.in.value());
values.add(value);
}
}

protected boolean sameQueryExceptKeyIN(ConditionQuery query) {
List<Condition.Relation> relations = query.relations();
if (relations.size() != this.relations().size()) {
return false;
}

for (Condition.Relation r : this.relations()) {
if (r.relation() == RelationType.IN) {
continue;
}
Object key = r.key();
if (!this.condition(key).equals(query.condition(key))) {
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,9 @@ public int hashCode() {
public abstract boolean isSysprop();

public abstract Object key();

@Override
public abstract Relation copy();
}

public static class SyspropRelation extends Relation {
Expand All @@ -629,7 +632,7 @@ public SyspropRelation(HugeKeys key, RelationType op, Object value) {
}

@Override
public Object key() {
public HugeKeys key() {
return this.key;
}

Expand All @@ -646,7 +649,7 @@ public boolean test(HugeElement element) {
}

@Override
public Condition copy() {
public Relation copy() {
Relation clone = new SyspropRelation(this.key, this.relation(),
this.value);
clone.serialKey(this.serialKey);
Expand All @@ -655,6 +658,18 @@ public Condition copy() {
}
}

public static class FlattenSyspropRelation extends SyspropRelation {

public FlattenSyspropRelation(SyspropRelation relation) {
super(relation.key(), relation.relation(), relation.value());
}

@Override
public boolean isFlattened() {
return true;
}
}

public static class UserpropRelation extends Relation {

// Id of property key
Expand Down Expand Up @@ -699,7 +714,7 @@ public boolean test(HugeElement element) {
}

@Override
public Condition copy() {
public Relation copy() {
Relation clone = new UserpropRelation(this.key, this.relation(),
this.value);
clone.serialKey(this.serialKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;

public final class ConditionQuery extends IdQuery {
public class ConditionQuery extends IdQuery {

private static final Set<Condition> EMPTY_CONDITIONS = ImmutableSet.of();

Expand Down Expand Up @@ -197,7 +197,8 @@ public <T> T condition(Object key) {
for (Condition c : this.conditions) {
if (c.isRelation()) {
Condition.Relation r = (Condition.Relation) c;
if (r.key().equals(key) && r.relation() == RelationType.EQ) {
if (r.key().equals(key) && (r.relation() == RelationType.EQ ||
r.relation() == RelationType.IN)) {
values.add(r.value());
}
}
Expand All @@ -216,8 +217,7 @@ public void unsetCondition(Object key) {
for (Iterator<Condition> iter = this.conditions.iterator();
iter.hasNext();) {
Condition c = iter.next();
E.checkState(c.isRelation(), "Can't unset condition '%s'", c);
if (((Condition.Relation) c).key().equals(key)) {
if (c.isRelation() && ((Condition.Relation) c).key().equals(key)) {
iter.remove();
}
}
Expand All @@ -235,8 +235,7 @@ public boolean containsCondition(HugeKeys key) {
return false;
}

public boolean containsCondition(HugeKeys key,
Condition.RelationType type) {
public boolean containsRelation(HugeKeys key, Condition.RelationType type) {
for (Relation r : this.relations()) {
if (r.key().equals(key) && r.relation().equals(type)) {
return true;
Expand All @@ -245,7 +244,7 @@ public boolean containsCondition(HugeKeys key,
return false;
}

public boolean containsCondition(Condition.RelationType type) {
public boolean containsRelation(Condition.RelationType type) {
for (Relation r : this.relations()) {
if (r.relation().equals(type)) {
return true;
Expand All @@ -254,8 +253,8 @@ public boolean containsCondition(Condition.RelationType type) {
return false;
}

public boolean containsScanCondition() {
return this.containsCondition(Condition.RelationType.SCAN);
public boolean containsScanRelation() {
return this.containsRelation(Condition.RelationType.SCAN);
}

public boolean containsContainsCondition(Id key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@

import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.backend.query.Condition.Relation;
import com.baidu.hugegraph.backend.query.Condition.SyspropRelation;
import com.baidu.hugegraph.type.define.HugeKeys;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.InsertionOrderUtil;
import com.baidu.hugegraph.util.NumericUtil;
import com.google.common.collect.ImmutableList;
Expand All @@ -43,6 +45,11 @@ public final class ConditionQueryFlatten {
);

public static List<ConditionQuery> flatten(ConditionQuery query) {
return flatten(query, false);
}

public static List<ConditionQuery> flatten(ConditionQuery query,
boolean supportIn) {
if (query.isFlattened() && !query.mayHasDupKeys(SPECIAL_KEYS)) {
return Arrays.asList(query);
}
Expand All @@ -52,7 +59,7 @@ public static List<ConditionQuery> flatten(ConditionQuery query) {
// Flatten IN/NOT_IN if needed
Set<Condition> conditions = InsertionOrderUtil.newSet();
for (Condition condition : query.conditions()) {
Condition cond = flattenIn(condition);
Condition cond = flattenIn(condition, supportIn);
if (cond == null) {
// Process 'XX in []'
return ImmutableList.of();
Expand Down Expand Up @@ -92,14 +99,14 @@ public static List<ConditionQuery> flatten(ConditionQuery query) {
return queries;
}

private static Condition flattenIn(Condition condition) {
private static Condition flattenIn(Condition condition, boolean supportIn) {
switch (condition.type()) {
case RELATION:
Relation relation = (Relation) condition;
switch (relation.relation()) {
case IN:
// Flatten IN if needed
return convIn2Or(relation);
return convIn2Or(relation, supportIn);
case NOT_IN:
// Flatten NOT_IN if needed
return convNotin2And(relation);
Expand All @@ -111,30 +118,49 @@ private static Condition flattenIn(Condition condition) {
}
case AND:
Condition.And and = (Condition.And) condition;
return new Condition.And(flattenIn(and.left()),
flattenIn(and.right()));
return new Condition.And(flattenIn(and.left(), supportIn),
flattenIn(and.right(), supportIn));
case OR:
Condition.Or or = (Condition.Or) condition;
return new Condition.Or(flattenIn(or.left()),
flattenIn(or.right()));
return new Condition.Or(flattenIn(or.left(), supportIn),
flattenIn(or.right(), supportIn));
default:
throw new AssertionError(String.format(
"Wrong condition type: '%s'", condition.type()));
}
}

private static Condition convIn2Or(Relation relation) {
private static Condition convIn2Or(Relation relation, boolean supportIn) {
assert relation.relation() == Condition.RelationType.IN;
Object key = relation.key();
Object valueObject = relation.value();
E.checkArgument(valueObject instanceof List,
"Expect list value for IN condition: %s", relation);
@SuppressWarnings("unchecked")
List<Object> values = (List<Object>) relation.value();
Condition cond, conds = null;
List<Object> values = (List<Object>) valueObject;
E.checkArgument(values.size() <= Query.QUERY_BATCH,
"Too many conditions(%s) each query", values.size());

// Keep IN condition if IN query is supported and necessary
if (supportIn && relation.isSysprop() && values.size() > 1 &&
(key == HugeKeys.OWNER_VERTEX || key == HugeKeys.ID)) {
// TODO: Should not rely on HugeKeys here, improve key judgment
// Just mark flatten
return new Condition.FlattenSyspropRelation(
(SyspropRelation) relation);
}

// Do IN flatten, return null if values.size() == 0
Condition conds = null;
for (Object value : values) {
Condition cond;
// Convert IN to EQ
if (key instanceof HugeKeys) {
cond = Condition.eq((HugeKeys) key, value);
} else {
cond = Condition.eq((Id) key, value);
}
// Join EQ with OR
conds = conds == null ? cond : Condition.or(conds, cond);
}
return conds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public class Query implements Cloneable {

public static final long NO_LIMIT = Long.MAX_VALUE;

public static final long COMMIT_BATCH = 500;
public static final long COMMIT_BATCH = 500L;
public static final long QUERY_BATCH = 100L;

public static final long NO_CAPACITY = -1L;
public static final long DEFAULT_CAPACITY = 800000L; // HugeGraph-777
Expand Down
Loading