Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.backend.query.Condition.Relation;
import com.baidu.hugegraph.type.define.HugeKeys;
import com.baidu.hugegraph.util.InsertionOrderUtil;
import com.baidu.hugegraph.util.NumericUtil;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
public final class ConditionQueryFlatten {
private static final Set SPECIAL_KEYS = ImmutableSet.of(
HugeKeys.LABEL
);
public static List flatten(ConditionQuery query) {
if (query.isFlattened() && !query.mayHasDupKeys(SPECIAL_KEYS)) {
return Arrays.asList(query);
}
List queries = new ArrayList<>();
// Flatten IN/NOT_IN if needed
Set conditions = InsertionOrderUtil.newSet();
for (Condition condition : query.conditions()) {
Condition cond = flattenIn(condition);
if (cond == null) {
// Process 'XX in []'
return ImmutableList.of();
}
conditions.add(cond);
}
query = query.copyAndResetUnshared();
query.resetConditions(conditions);
// Flatten OR if needed
Set results = null;
for (Condition condition : query.conditions()) {
if (results == null) {
results = flattenAndOr(condition);
} else {
results = and(results, flattenAndOr(condition));
}
}
// Optimize useless condition
assert results != null;
for (Relations relations : results) {
relations = optimizeRelations(relations);
/*
* Relations may be null after optimize, for example:
* age > 10 and age == 9
*/
if (relations == null) {
continue;
}
ConditionQuery cq = newQueryFromRelations(query, relations);
if (cq != null) {
queries.add(cq);
}
}
return queries;
}
private static Condition flattenIn(Condition condition) {
switch (condition.type()) {
case RELATION:
Relation relation = (Relation) condition;
switch (relation.relation()) {
case IN:
// Flatten IN if needed
return convIn2Or(relation);
case NOT_IN:
// Flatten NOT_IN if needed
return convNotin2And(relation);
case TEXT_CONTAINS_ANY:
// Flatten TEXT_CONTAINS_ANY if needed
return convTextContainsAny2Or(relation);
default:
return condition;
}
case AND:
Condition.And and = (Condition.And) condition;
return new Condition.And(flattenIn(and.left()),
flattenIn(and.right()));
case OR:
Condition.Or or = (Condition.Or) condition;
return new Condition.Or(flattenIn(or.left()),
flattenIn(or.right()));
default:
throw new AssertionError(String.format(
"Wrong condition type: '%s'", condition.type()));
}
}
private static Condition convIn2Or(Relation relation) {
assert relation.relation() == Condition.RelationType.IN;
Object key = relation.key();
@SuppressWarnings("unchecked")
List