Commit fae00825 by apoorvnaik Committed by Madhan Neethiraj

ATLAS-2044: In-memory filtering for correctness after index query

parent fa11d13a
......@@ -87,6 +87,8 @@ public class ClassificationSearchProcessor extends SearchProcessor {
indexQueryString = STRAY_ELIPSIS_PATTERN.matcher(indexQueryString).replaceAll("");
this.indexQuery = graph.indexQuery(Constants.VERTEX_INDEX, indexQueryString);
constructInMemoryPredicate(classificationType, filterCriteria, indexAttributes);
} else {
indexQuery = null;
}
......@@ -184,6 +186,9 @@ public class ClassificationSearchProcessor extends SearchProcessor {
getVertices(queryResult, classificationVertices);
}
// Do in-memory filtering before the graph query
CollectionUtils.filter(classificationVertices, inMemoryPredicate);
for (AtlasVertex classificationVertex : classificationVertices) {
Iterable<AtlasEdge> edges = classificationVertex.getEdges(AtlasEdgeDirection.IN);
......@@ -208,19 +213,7 @@ public class ClassificationSearchProcessor extends SearchProcessor {
super.filter(entityVertices);
for (AtlasVertex entityVertex : entityVertices) {
resultIdx++;
if (resultIdx <= startIdx) {
continue;
}
ret.add(entityVertex);
if (ret.size() == limit) {
break;
}
}
resultIdx = collectResultVertices(ret, startIdx, limit, resultIdx, entityVertices);
}
} finally {
AtlasPerfTracer.log(perf);
......
......@@ -71,6 +71,8 @@ public class EntitySearchProcessor extends SearchProcessor {
if (attrSearchByIndex) {
constructFilterQuery(indexQuery, entityType, filterCriteria, indexAttributes);
constructInMemoryPredicate(entityType, filterCriteria, indexAttributes);
} else {
graphAttributes.addAll(indexAttributes);
}
......@@ -165,11 +167,10 @@ public class EntitySearchProcessor extends SearchProcessor {
break;
}
while (idxQueryResult.hasNext()) {
AtlasVertex vertex = idxQueryResult.next().getVertex();
getVerticesFromIndexQueryResult(idxQueryResult, entityVertices);
entityVertices.add(vertex);
}
// Do in-memory filtering before the graph query
CollectionUtils.filter(entityVertices, inMemoryPredicate);
if (graphQuery != null) {
AtlasGraphQuery guidQuery = context.getGraph().query().in(Constants.GUID_PROPERTY_KEY, getGuids(entityVertices));
......@@ -191,19 +192,7 @@ public class EntitySearchProcessor extends SearchProcessor {
super.filter(entityVertices);
for (AtlasVertex entityVertex : entityVertices) {
resultIdx++;
if (resultIdx <= startIdx) {
continue;
}
ret.add(entityVertex);
if (ret.size() == limit) {
break;
}
}
resultIdx = collectResultVertices(ret, startIdx, limit, resultIdx, entityVertices);
}
} finally {
AtlasPerfTracer.log(perf);
......
......@@ -141,19 +141,7 @@ public class FullTextSearchProcessor extends SearchProcessor {
super.filter(entityVertices);
for (AtlasVertex entityVertex : entityVertices) {
resultIdx++;
if (resultIdx <= startIdx) {
continue;
}
ret.add(entityVertex);
if (ret.size() == limit) {
break;
}
}
resultIdx = collectResultVertices(ret, startIdx, limit, resultIdx, entityVertices);
}
} finally {
AtlasPerfTracer.log(perf);
......
......@@ -23,22 +23,38 @@ import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.discovery.SearchParameters;
import org.apache.atlas.model.discovery.SearchParameters.FilterCriteria;
import org.apache.atlas.model.discovery.SearchParameters.FilterCriteria.Condition;
import org.apache.atlas.model.typedef.AtlasBaseTypeDef;
import org.apache.atlas.repository.Constants;
import org.apache.atlas.repository.graphdb.*;
import org.apache.atlas.repository.graphdb.AtlasGraphQuery;
import org.apache.atlas.repository.graphdb.AtlasIndexQuery;
import org.apache.atlas.repository.graphdb.AtlasVertex;
import org.apache.atlas.repository.store.graph.v1.AtlasGraphUtilsV1;
import org.apache.atlas.type.AtlasEntityType;
import org.apache.atlas.type.AtlasStructType;
import org.apache.atlas.type.AtlasStructType.AtlasAttribute;
import org.apache.atlas.util.AtlasGremlinQueryProvider;
import org.apache.atlas.util.SearchPredicateUtil.VertexAttributePredicateGenerator;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.PredicateUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import static org.apache.atlas.util.SearchPredicateUtil.*;
public abstract class SearchProcessor {
private static final Logger LOG = LoggerFactory.getLogger(SearchProcessor.class);
......@@ -55,24 +71,47 @@ public abstract class SearchProcessor {
public static final String BRACE_CLOSE_STR = ")";
private static final Map<SearchParameters.Operator, String> OPERATOR_MAP = new HashMap<>();
private static final Map<SearchParameters.Operator, VertexAttributePredicateGenerator> OPERATOR_PREDICATE_MAP = new HashMap<>();
static
{
OPERATOR_MAP.put(SearchParameters.Operator.LT,"v.\"%s\": [* TO %s}");
OPERATOR_PREDICATE_MAP.put(SearchParameters.Operator.LT, getLTPredicateGenerator());
OPERATOR_MAP.put(SearchParameters.Operator.GT,"v.\"%s\": {%s TO *]");
OPERATOR_PREDICATE_MAP.put(SearchParameters.Operator.GT, getGTPredicateGenerator());
OPERATOR_MAP.put(SearchParameters.Operator.LTE,"v.\"%s\": [* TO %s]");
OPERATOR_PREDICATE_MAP.put(SearchParameters.Operator.LTE, getLTEPredicateGenerator());
OPERATOR_MAP.put(SearchParameters.Operator.GTE,"v.\"%s\": [%s TO *]");
OPERATOR_PREDICATE_MAP.put(SearchParameters.Operator.GTE, getGTEPredicateGenerator());
OPERATOR_MAP.put(SearchParameters.Operator.EQ,"v.\"%s\": %s");
OPERATOR_PREDICATE_MAP.put(SearchParameters.Operator.EQ, getEQPredicateGenerator());
OPERATOR_MAP.put(SearchParameters.Operator.NEQ,"-" + "v.\"%s\": %s");
OPERATOR_PREDICATE_MAP.put(SearchParameters.Operator.NEQ, getNEQPredicateGenerator());
OPERATOR_MAP.put(SearchParameters.Operator.IN, "v.\"%s\": (%s)"); // this should be a list of quoted strings
OPERATOR_PREDICATE_MAP.put(SearchParameters.Operator.IN, getINPredicateGenerator()); // this should be a list of quoted strings
OPERATOR_MAP.put(SearchParameters.Operator.LIKE, "v.\"%s\": (%s)"); // this should be regex pattern
OPERATOR_PREDICATE_MAP.put(SearchParameters.Operator.LIKE, getLIKEPredicateGenerator()); // this should be regex pattern
OPERATOR_MAP.put(SearchParameters.Operator.STARTS_WITH, "v.\"%s\": (%s*)");
OPERATOR_PREDICATE_MAP.put(SearchParameters.Operator.STARTS_WITH, getStartsWithPredicateGenerator());
OPERATOR_MAP.put(SearchParameters.Operator.ENDS_WITH, "v.\"%s\": (*%s)");
OPERATOR_PREDICATE_MAP.put(SearchParameters.Operator.ENDS_WITH, getEndsWithPredicateGenerator());
OPERATOR_MAP.put(SearchParameters.Operator.CONTAINS, "v.\"%s\": (*%s*)");
OPERATOR_PREDICATE_MAP.put(SearchParameters.Operator.CONTAINS, getContainsPredicateGenerator());
}
protected final SearchContext context;
protected SearchProcessor nextProcessor;
protected Predicate inMemoryPredicate;
protected SearchProcessor(SearchContext context) {
......@@ -89,6 +128,24 @@ public abstract class SearchProcessor {
public abstract List<AtlasVertex> execute();
protected int collectResultVertices(final List<AtlasVertex> ret, final int startIdx, final int limit, int resultIdx, final List<AtlasVertex> entityVertices) {
for (AtlasVertex entityVertex : entityVertices) {
resultIdx++;
if (resultIdx <= startIdx) {
continue;
}
ret.add(entityVertex);
if (ret.size() == limit) {
break;
}
}
return resultIdx;
}
public void filter(List<AtlasVertex> entityVertices) {
if (nextProcessor != null && CollectionUtils.isNotEmpty(entityVertices)) {
nextProcessor.filter(entityVertices);
......@@ -194,7 +251,9 @@ public abstract class SearchProcessor {
protected void constructFilterQuery(StringBuilder indexQuery, AtlasStructType type, FilterCriteria filterCriteria, Set<String> indexAttributes) {
if (filterCriteria != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Processing Filters");
}
String filterQuery = toIndexQuery(type, filterCriteria, indexAttributes, 0);
......@@ -208,6 +267,16 @@ public abstract class SearchProcessor {
}
}
protected void constructInMemoryPredicate(AtlasStructType type, FilterCriteria filterCriteria, Set<String> indexAttributes) {
if (filterCriteria != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Processing Filters");
}
inMemoryPredicate = toInMemoryPredicate(type, filterCriteria, indexAttributes);
}
}
protected void constructGremlinFilterQuery(StringBuilder gremlinQuery, Map<String, Object> queryBindings, AtlasStructType structType, FilterCriteria filterCriteria) {
if (filterCriteria != null) {
FilterCriteria.Condition condition = filterCriteria.getCondition();
......@@ -291,6 +360,32 @@ public abstract class SearchProcessor {
}
}
private Predicate toInMemoryPredicate(AtlasStructType type, FilterCriteria criteria, Set<String> indexAttributes) {
if (criteria.getCondition() != null && CollectionUtils.isNotEmpty(criteria.getCriterion())) {
List<Predicate> predicates = new ArrayList<>();
for (FilterCriteria filterCriteria : criteria.getCriterion()) {
Predicate predicate = toInMemoryPredicate(type, filterCriteria, indexAttributes);
if (predicate != null) {
predicates.add(predicate);
}
}
if (CollectionUtils.isNotEmpty(predicates)) {
if (criteria.getCondition() == Condition.AND) {
return PredicateUtils.allPredicate(predicates);
} else {
return PredicateUtils.anyPredicate(predicates);
}
}
} else if (indexAttributes.contains(criteria.getAttributeName())){
return toInMemoryPredicate(type, criteria.getAttributeName(), criteria.getOperator(), criteria.getAttributeValue());
}
return null;
}
private String toIndexExpression(AtlasStructType type, String attrName, SearchParameters.Operator op, String attrVal) {
String ret = EMPTY_STRING;
......@@ -307,6 +402,71 @@ public abstract class SearchProcessor {
return ret;
}
private Predicate toInMemoryPredicate(AtlasStructType type, String attrName, SearchParameters.Operator op, String attrVal) {
Predicate ret = null;
AtlasAttribute attribute = type.getAttribute(attrName);
VertexAttributePredicateGenerator predicate = OPERATOR_PREDICATE_MAP.get(op);
if (attribute != null && predicate != null) {
final String attributeType = attribute.getAttributeType().getTypeName().toLowerCase();
final Class attrClass;
final Object attrValue;
switch (attributeType) {
case "string":
attrClass = String.class;
attrValue = attrVal;
break;
case "short":
attrClass = Short.class;
attrValue = Short.parseShort(attrVal);
break;
case "int":
attrClass = Integer.class;
attrValue = Integer.parseInt(attrVal);
break;
case "biginteger":
attrClass = BigInteger.class;
attrValue = new BigInteger(attrVal);
break;
case "boolean":
attrClass = Boolean.class;
attrValue = Boolean.parseBoolean(attrVal);
break;
case "byte":
attrClass = Byte.class;
attrValue = Byte.parseByte(attrVal);
break;
case "long":
case "date":
attrClass = Long.class;
attrValue = Long.parseLong(attrVal);
break;
case "float":
attrClass = Float.class;
attrValue = Float.parseFloat(attrVal);
break;
case "double":
attrClass = Double.class;
attrValue = Double.parseDouble(attrVal);
break;
case "bigdecimal":
attrClass = BigDecimal.class;
attrValue = new BigDecimal(attrVal);
break;
default:
attrClass = Object.class;
attrValue = attrVal;
break;
}
ret = predicate.generatePredicate(attribute.getQualifiedName(), attrValue, attrClass);
}
return ret;
}
protected AtlasGraphQuery toGraphFilterQuery(AtlasStructType type, FilterCriteria criteria, Set<String> graphAttributes, AtlasGraphQuery query) {
if (criteria != null) {
if (criteria.getCondition() != null) {
......@@ -493,4 +653,5 @@ public abstract class SearchProcessor {
return defaultValue;
}
}
/**
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.apache.atlas.util;
import org.apache.atlas.repository.graphdb.AtlasVertex;
import org.apache.atlas.repository.store.graph.v1.AtlasGraphUtilsV1;
import org.apache.commons.collections.Predicate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
public class SearchPredicateUtil {
private static final Logger LOG = LoggerFactory.getLogger(SearchPredicateUtil.class);
private static Predicate ALWAYS_FALSE = new Predicate() {
@Override
public boolean evaluate(final Object object) {
return false;
}
};
public static VertexAttributePredicateGenerator getLTPredicateGenerator() {
if (LOG.isDebugEnabled()) {
LOG.debug("==> getLTPredicateGenerator");
}
VertexAttributePredicateGenerator ret = new VertexAttributePredicateGenerator() {
@Override
public Predicate generatePredicate(final String attrName, final Object attrVal, final Class attrClass) {
final Predicate ret;
if (attrName == null || attrClass == null || attrVal == null) {
ret = ALWAYS_FALSE;
} else if (Short.class.isAssignableFrom(attrClass)) {
ret = ShortPredicate.getLTPredicate(attrName, attrClass, (Short)attrVal);
} else if (Integer.class.isAssignableFrom(attrClass)) {
ret = IntegerPredicate.getLTPredicate(attrName, attrClass, (Integer)attrVal);
} else if (Long.class.isAssignableFrom(attrClass)) {
ret = LongPredicate.getLTPredicate(attrName, attrClass, (Long)attrVal);
} else if (Float.class.isAssignableFrom(attrClass)) {
ret = FloatPredicate.getLTPredicate(attrName, attrClass, (Float)attrVal);
} else if (Double.class.isAssignableFrom(attrClass)) {
ret = DoublePredicate.getLTPredicate(attrName, attrClass, (Double)attrVal);
} else if (Byte.class.isAssignableFrom(attrClass)) {
ret = BytePredicate.getLTPredicate(attrName, attrClass, (Byte)attrVal);
} else if (BigInteger.class.isAssignableFrom(attrClass)) {
ret = BigIntegerPredicate.getLTPredicate(attrName, attrClass, (BigInteger)attrVal);
} else if (BigDecimal.class.isAssignableFrom(attrClass)) {
ret = BigDecimalPredicate.getLTPredicate(attrName, attrClass, (BigDecimal)attrVal);
} else if (String.class.isAssignableFrom(attrClass)) {
ret = StringPredicate.getLTPredicate(attrName, attrClass, (String)attrVal);
} else {
ret = ALWAYS_FALSE;
}
return ret;
}
};
if (LOG.isDebugEnabled()) {
LOG.debug("<== getLTPredicateGenerator");
}
return ret;
}
public static VertexAttributePredicateGenerator getGTPredicateGenerator() {
if (LOG.isDebugEnabled()) {
LOG.debug("==> getGTPredicateGenerator");
}
VertexAttributePredicateGenerator ret = new VertexAttributePredicateGenerator() {
@Override
public Predicate generatePredicate(final String attrName, final Object attrVal, final Class attrClass) {
final Predicate ret;
if (attrName == null || attrClass == null || attrVal == null) {
ret = ALWAYS_FALSE;
} else if (Short.class.isAssignableFrom(attrClass)) {
ret = ShortPredicate.getGTPredicate(attrName, attrClass, (Short)attrVal);
} else if (Integer.class.isAssignableFrom(attrClass)) {
ret = IntegerPredicate.getGTPredicate(attrName, attrClass, (Integer)attrVal);
} else if (Long.class.isAssignableFrom(attrClass)) {
ret = LongPredicate.getGTPredicate(attrName, attrClass, (Long)attrVal);
} else if (Float.class.isAssignableFrom(attrClass)) {
ret = FloatPredicate.getGTPredicate(attrName, attrClass, (Float)attrVal);
} else if (Double.class.isAssignableFrom(attrClass)) {
ret = DoublePredicate.getGTPredicate(attrName, attrClass, (Double)attrVal);
} else if (Byte.class.isAssignableFrom(attrClass)) {
ret = BytePredicate.getGTPredicate(attrName, attrClass, (Byte)attrVal);
} else if (BigInteger.class.isAssignableFrom(attrClass)) {
ret = BigIntegerPredicate.getGTPredicate(attrName, attrClass, (BigInteger)attrVal);
} else if (BigDecimal.class.isAssignableFrom(attrClass)) {
ret = BigDecimalPredicate.getGTPredicate(attrName, attrClass, (BigDecimal)attrVal);
} else if (String.class.isAssignableFrom(attrClass)) {
ret = StringPredicate.getGTPredicate(attrName, attrClass, (String)attrVal);
} else {
ret = ALWAYS_FALSE;
}
return ret;
}
};
if (LOG.isDebugEnabled()) {
LOG.debug("<== getGTPredicateGenerator");
}
return ret;
}
public static VertexAttributePredicateGenerator getLTEPredicateGenerator() {
if (LOG.isDebugEnabled()) {
LOG.debug("==> getLTEPredicateGenerator");
}
VertexAttributePredicateGenerator ret = new VertexAttributePredicateGenerator() {
@Override
public Predicate generatePredicate(final String attrName, final Object attrVal, final Class attrClass) {
final Predicate ret;
if (attrName == null || attrClass == null || attrVal == null) {
ret = ALWAYS_FALSE;
} else if (Short.class.isAssignableFrom(attrClass)) {
ret = ShortPredicate.getLTEPredicate(attrName, attrClass, (Short)attrVal);
} else if (Integer.class.isAssignableFrom(attrClass)) {
ret = IntegerPredicate.getLTEPredicate(attrName, attrClass, (Integer)attrVal);
} else if (Long.class.isAssignableFrom(attrClass)) {
ret = LongPredicate.getLTEPredicate(attrName, attrClass, (Long)attrVal);
} else if (Float.class.isAssignableFrom(attrClass)) {
ret = FloatPredicate.getLTEPredicate(attrName, attrClass, (Float)attrVal);
} else if (Double.class.isAssignableFrom(attrClass)) {
ret = DoublePredicate.getLTEPredicate(attrName, attrClass, (Double)attrVal);
} else if (Byte.class.isAssignableFrom(attrClass)) {
ret = BytePredicate.getLTEPredicate(attrName, attrClass, (Byte)attrVal);
} else if (BigInteger.class.isAssignableFrom(attrClass)) {
ret = BigIntegerPredicate.getLTEPredicate(attrName, attrClass, (BigInteger)attrVal);
} else if (BigDecimal.class.isAssignableFrom(attrClass)) {
ret = BigDecimalPredicate.getLTEPredicate(attrName, attrClass, (BigDecimal)attrVal);
} else if (String.class.isAssignableFrom(attrClass)) {
ret = StringPredicate.getLTEPredicate(attrName, attrClass, (String)attrVal);
} else {
ret = ALWAYS_FALSE;
}
return ret;
}
};
if (LOG.isDebugEnabled()) {
LOG.debug("<== getLTEPredicateGenerator");
}
return ret;
}
public static VertexAttributePredicateGenerator getGTEPredicateGenerator() {
if (LOG.isDebugEnabled()) {
LOG.debug("==> getGTEPredicateGenerator");
}
VertexAttributePredicateGenerator ret = new VertexAttributePredicateGenerator() {
@Override
public Predicate generatePredicate(final String attrName, final Object attrVal, final Class attrClass) {
final Predicate ret;
if (attrName == null || attrClass == null || attrVal == null) {
ret = ALWAYS_FALSE;
} else if (Short.class.isAssignableFrom(attrClass)) {
ret = ShortPredicate.getGTEPredicate(attrName, attrClass, (Short)attrVal);
} else if (Integer.class.isAssignableFrom(attrClass)) {
ret = IntegerPredicate.getGTEPredicate(attrName, attrClass, (Integer)attrVal);
} else if (Long.class.isAssignableFrom(attrClass)) {
ret = LongPredicate.getGTEPredicate(attrName, attrClass, (Long)attrVal);
} else if (Float.class.isAssignableFrom(attrClass)) {
ret = FloatPredicate.getGTEPredicate(attrName, attrClass, (Float)attrVal);
} else if (Double.class.isAssignableFrom(attrClass)) {
ret = DoublePredicate.getGTEPredicate(attrName, attrClass, (Double)attrVal);
} else if (Byte.class.isAssignableFrom(attrClass)) {
ret = BytePredicate.getGTEPredicate(attrName, attrClass, (Byte)attrVal);
} else if (BigInteger.class.isAssignableFrom(attrClass)) {
ret = BigIntegerPredicate.getGTEPredicate(attrName, attrClass, (BigInteger)attrVal);
} else if (BigDecimal.class.isAssignableFrom(attrClass)) {
ret = BigDecimalPredicate.getGTEPredicate(attrName, attrClass, (BigDecimal)attrVal);
} else if (String.class.isAssignableFrom(attrClass)) {
ret = StringPredicate.getGTEPredicate(attrName, attrClass, (String)attrVal);
} else {
ret = ALWAYS_FALSE;
}
return ret;
}
};
if (LOG.isDebugEnabled()) {
LOG.debug("<- getGTEPredicateGenerator");
}
return ret;
}
public static VertexAttributePredicateGenerator getEQPredicateGenerator() {
if (LOG.isDebugEnabled()) {
LOG.debug("==> getEQPredicateGenerator");
}
VertexAttributePredicateGenerator ret = new VertexAttributePredicateGenerator() {
@Override
public Predicate generatePredicate(final String attrName, final Object attrVal, final Class attrClass) {
final Predicate ret;
if (attrName == null || attrClass == null || attrVal == null) {
ret = ALWAYS_FALSE;
} else if (Short.class.isAssignableFrom(attrClass)) {
ret = ShortPredicate.getEQPredicate(attrName, attrClass, (Short)attrVal);
} else if (Integer.class.isAssignableFrom(attrClass)) {
ret = IntegerPredicate.getEQPredicate(attrName, attrClass, (Integer)attrVal);
} else if (Long.class.isAssignableFrom(attrClass)) {
ret = LongPredicate.getEQPredicate(attrName, attrClass, (Long)attrVal);
} else if (Float.class.isAssignableFrom(attrClass)) {
ret = FloatPredicate.getEQPredicate(attrName, attrClass, (Float)attrVal);
} else if (Double.class.isAssignableFrom(attrClass)) {
ret = DoublePredicate.getEQPredicate(attrName, attrClass, (Double)attrVal);
} else if (Byte.class.isAssignableFrom(attrClass)) {
ret = BytePredicate.getEQPredicate(attrName, attrClass, (Byte)attrVal);
} else if (BigInteger.class.isAssignableFrom(attrClass)) {
ret = BigIntegerPredicate.getEQPredicate(attrName, attrClass, (BigInteger)attrVal);
} else if (BigDecimal.class.isAssignableFrom(attrClass)) {
ret = BigDecimalPredicate.getEQPredicate(attrName, attrClass, (BigDecimal)attrVal);
} else if (String.class.isAssignableFrom(attrClass)) {
ret = StringPredicate.getEQPredicate(attrName, attrClass, (String)attrVal);
} else {
ret = ALWAYS_FALSE;
}
return ret;
}
};
if (LOG.isDebugEnabled()) {
LOG.debug("<== getEQPredicateGenerator");
}
return ret;
}
public static VertexAttributePredicateGenerator getNEQPredicateGenerator() {
if (LOG.isDebugEnabled()) {
LOG.debug("==> getNEQPredicateGenerator");
}
VertexAttributePredicateGenerator ret = new VertexAttributePredicateGenerator() {
@Override
public Predicate generatePredicate(final String attrName, final Object attrVal, final Class attrClass) {
final Predicate ret;
if (attrName == null || attrClass == null || attrVal == null) {
ret = ALWAYS_FALSE;
} else if (Short.class.isAssignableFrom(attrClass)) {
ret = ShortPredicate.getNEQPredicate(attrName, attrClass, (Short)attrVal);
} else if (Integer.class.isAssignableFrom(attrClass)) {
ret = IntegerPredicate.getNEQPredicate(attrName, attrClass, (Integer)attrVal);
} else if (Long.class.isAssignableFrom(attrClass)) {
ret = LongPredicate.getNEQPredicate(attrName, attrClass, (Long)attrVal);
} else if (Float.class.isAssignableFrom(attrClass)) {
ret = FloatPredicate.getNEQPredicate(attrName, attrClass, (Float)attrVal);
} else if (Double.class.isAssignableFrom(attrClass)) {
ret = DoublePredicate.getNEQPredicate(attrName, attrClass, (Double)attrVal);
} else if (Byte.class.isAssignableFrom(attrClass)) {
ret = BytePredicate.getNEQPredicate(attrName, attrClass, (Byte)attrVal);
} else if (BigInteger.class.isAssignableFrom(attrClass)) {
ret = BigIntegerPredicate.getNEQPredicate(attrName, attrClass, (BigInteger)attrVal);
} else if (BigDecimal.class.isAssignableFrom(attrClass)) {
ret = BigDecimalPredicate.getNEQPredicate(attrName, attrClass, (BigDecimal)attrVal);
} else if (String.class.isAssignableFrom(attrClass)) {
ret = StringPredicate.getNEQPredicate(attrName, attrClass, (String)attrVal);
} else {
ret = ALWAYS_FALSE;
}
return ret;
}
};
if (LOG.isDebugEnabled()) {
LOG.debug("<== getNEQPredicateGenerator");
}
return ret;
}
public static VertexAttributePredicateGenerator getINPredicateGenerator() {
if (LOG.isDebugEnabled()) {
LOG.debug("==> getINPredicateGenerator");
}
VertexAttributePredicateGenerator ret = new VertexAttributePredicateGenerator() {
@Override
public Predicate generatePredicate(final String attrName, final Object attrVal, final Class attrClass) {
final Predicate ret;
if (attrName == null || attrClass == null || attrVal == null) {
ret = ALWAYS_FALSE;
} else {
ret = new VertexAttributePredicate(attrName, attrClass) {
@Override
public boolean compareValue(final Object value) {
return (value instanceof List) ? ((List) value).contains(attrVal) : false;
}
};
}
return ret;
}
};
if (LOG.isDebugEnabled()) {
LOG.debug("<== getINPredicateGenerator");
}
return ret;
}
public static VertexAttributePredicateGenerator getLIKEPredicateGenerator() {
if (LOG.isDebugEnabled()) {
LOG.debug("==> getLIKEPredicateGenerator");
}
VertexAttributePredicateGenerator ret = new VertexAttributePredicateGenerator() {
@Override
public Predicate generatePredicate(final String attrName, final Object attrVal, final Class attrClass) {
final Predicate ret;
if (attrName == null || attrClass == null || attrVal == null) {
ret = ALWAYS_FALSE;
} else if (String.class.isAssignableFrom(attrClass)) {
ret = StringPredicate.getContainsPredicate(attrName, attrClass, (String)attrVal);
} else {
ret = ALWAYS_FALSE;
}
return ret;
}
};
if (LOG.isDebugEnabled()) {
LOG.debug("<== getLIKEPredicateGenerator");
}
return ret;
}
public static VertexAttributePredicateGenerator getStartsWithPredicateGenerator() {
if (LOG.isDebugEnabled()) {
LOG.debug("==> getStartsWithPredicateGenerator");
}
VertexAttributePredicateGenerator ret = new VertexAttributePredicateGenerator() {
@Override
public Predicate generatePredicate(final String attrName, final Object attrVal, final Class attrClass) {
final Predicate ret;
if (attrName == null || attrClass == null || attrVal == null) {
ret = ALWAYS_FALSE;
} else if (String.class.isAssignableFrom(attrClass)) {
ret = StringPredicate.getStartsWithPredicate(attrName, attrClass, (String)attrVal);
} else {
ret = ALWAYS_FALSE;
}
return ret;
}
};
if (LOG.isDebugEnabled()) {
LOG.debug("<== getStartsWithPredicateGenerator");
}
return ret;
}
public static VertexAttributePredicateGenerator getEndsWithPredicateGenerator() {
if (LOG.isDebugEnabled()) {
LOG.debug("==> getEndsWithPredicateGenerator");
}
VertexAttributePredicateGenerator ret = new VertexAttributePredicateGenerator() {
@Override
public Predicate generatePredicate(final String attrName, final Object attrVal, final Class attrClass) {
final Predicate ret;
if (attrName == null || attrClass == null || attrVal == null) {
ret = ALWAYS_FALSE;
} else if (String.class.isAssignableFrom(attrClass)) {
ret = StringPredicate.getEndsWithPredicate(attrName, attrClass, (String)attrVal);
} else {
ret = ALWAYS_FALSE;
}
return ret;
}
};
if (LOG.isDebugEnabled()) {
LOG.debug("<== getEndsWithPredicateGenerator");
}
return ret;
}
public static VertexAttributePredicateGenerator getContainsPredicateGenerator() {
if (LOG.isDebugEnabled()) {
LOG.debug("==> getContainsPredicateGenerator");
}
VertexAttributePredicateGenerator ret = new VertexAttributePredicateGenerator() {
@Override
public Predicate generatePredicate(final String attrName, final Object attrVal, final Class attrClass) {
final Predicate ret;
if (attrName == null || attrClass == null || attrVal == null) {
ret = ALWAYS_FALSE;
} else if (String.class.isAssignableFrom(attrClass)) {
ret = StringPredicate.getContainsPredicate(attrName, attrClass, (String)attrVal);
} else {
ret = ALWAYS_FALSE;
}
return ret;
}
};
if (LOG.isDebugEnabled()) {
LOG.debug("<== getContainsPredicateGenerator");
}
return ret;
}
public interface VertexAttributePredicateGenerator {
Predicate generatePredicate(String attrName, Object attrVal, Class attrClass);
}
static abstract class VertexAttributePredicate implements Predicate {
final String attrName;
final Class attrClass;
VertexAttributePredicate(String attrName, Class attrClass) {
this.attrName = attrName;
this.attrClass = attrClass;
}
@Override
public boolean evaluate(final Object object) {
final boolean ret;
AtlasVertex vertex = (object instanceof AtlasVertex) ? (AtlasVertex)object : null;
if (vertex != null) {
Object attrValue = AtlasGraphUtilsV1.getProperty(vertex, attrName, attrClass);
if (attrValue != null) {
ret = compareValue(attrValue);
} else {
ret = false;
}
} else {
ret = false;
}
return ret;
}
protected abstract boolean compareValue(Object value);
}
static abstract class ShortPredicate extends VertexAttributePredicate {
final Short value;
ShortPredicate(String attrName, Class attrClass, Short value) {
super(attrName, attrClass);
this.value = value;
}
static VertexAttributePredicate getEQPredicate(String attrName, Class attrClass, Short value) {
return new ShortPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Short)value).compareTo(this.value) == 0;
}
};
}
static VertexAttributePredicate getNEQPredicate(String attrName, Class attrClass, Short value) {
return new ShortPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Short)value).compareTo(this.value) != 0;
}
};
}
static VertexAttributePredicate getLTPredicate(String attrName, Class attrClass, Short value) {
return new ShortPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Short)value).compareTo(this.value) < 0;
}
};
}
static VertexAttributePredicate getLTEPredicate(String attrName, Class attrClass, Short value) {
return new ShortPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Short)value).compareTo(this.value) <= 0;
}
};
}
static VertexAttributePredicate getGTPredicate(String attrName, Class attrClass, Short value) {
return new ShortPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Short)value).compareTo(this.value) > 0;
}
};
}
static VertexAttributePredicate getGTEPredicate(String attrName, Class attrClass, Short value) {
return new ShortPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Short)value).compareTo(this.value) >= 0;
}
};
}
}
static abstract class IntegerPredicate extends VertexAttributePredicate {
final Integer value;
IntegerPredicate(String attrName, Class attrClass, Integer value) {
super(attrName, attrClass);
this.value = value;
}
static VertexAttributePredicate getEQPredicate(String attrName, Class attrClass, Integer value) {
return new IntegerPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Integer)value).compareTo(this.value) == 0;
}
};
}
static VertexAttributePredicate getNEQPredicate(String attrName, Class attrClass, Integer value) {
return new IntegerPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Integer)value).compareTo(this.value) != 0;
}
};
}
static VertexAttributePredicate getLTPredicate(String attrName, Class attrClass, Integer value) {
return new IntegerPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Integer)value).compareTo(this.value) < 0;
}
};
}
static VertexAttributePredicate getLTEPredicate(String attrName, Class attrClass, Integer value) {
return new IntegerPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Integer)value).compareTo(this.value) <= 0;
}
};
}
static VertexAttributePredicate getGTPredicate(String attrName, Class attrClass, Integer value) {
return new IntegerPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Integer)value).compareTo(this.value) > 0;
}
};
}
static VertexAttributePredicate getGTEPredicate(String attrName, Class attrClass, Integer value) {
return new IntegerPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Integer)value).compareTo(this.value) >= 0;
}
};
}
}
static abstract class LongPredicate extends VertexAttributePredicate {
final Long value;
LongPredicate(String attrName, Class attrClass, Long value) {
super(attrName, attrClass);
this.value = value;
}
static VertexAttributePredicate getEQPredicate(String attrName, Class attrClass, Long value) {
return new LongPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Long)value).compareTo(this.value) == 0;
}
};
}
static VertexAttributePredicate getNEQPredicate(String attrName, Class attrClass, Long value) {
return new LongPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Long)value).compareTo(this.value) != 0;
}
};
}
static VertexAttributePredicate getLTPredicate(String attrName, Class attrClass, Long value) {
return new LongPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Long)value).compareTo(this.value) < 0;
}
};
}
static VertexAttributePredicate getLTEPredicate(String attrName, Class attrClass, Long value) {
return new LongPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Long)value).compareTo(this.value) <= 0;
}
};
}
static VertexAttributePredicate getGTPredicate(String attrName, Class attrClass, Long value) {
return new LongPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Long)value).compareTo(this.value) > 0;
}
};
}
static VertexAttributePredicate getGTEPredicate(String attrName, Class attrClass, Long value) {
return new LongPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Long)value).compareTo(this.value) >= 0;
}
};
}
}
static abstract class FloatPredicate extends VertexAttributePredicate {
final Float value;
FloatPredicate(String attrName, Class attrClass, Float value) {
super(attrName, attrClass);
this.value = value;
}
static VertexAttributePredicate getEQPredicate(String attrName, Class attrClass, Float value) {
return new FloatPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Float)value).compareTo(this.value) == 0;
}
};
}
static VertexAttributePredicate getNEQPredicate(String attrName, Class attrClass, Float value) {
return new FloatPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Float)value).compareTo(this.value) != 0;
}
};
}
static VertexAttributePredicate getLTPredicate(String attrName, Class attrClass, Float value) {
return new FloatPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Float)value).compareTo(this.value) < 0;
}
};
}
static VertexAttributePredicate getLTEPredicate(String attrName, Class attrClass, Float value) {
return new FloatPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Float)value).compareTo(this.value) <= 0;
}
};
}
static VertexAttributePredicate getGTPredicate(String attrName, Class attrClass, Float value) {
return new FloatPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Float)value).compareTo(this.value) > 0;
}
};
}
static VertexAttributePredicate getGTEPredicate(String attrName, Class attrClass, Float value) {
return new FloatPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Float)value).compareTo(this.value) >= 0;
}
};
}
}
static abstract class DoublePredicate extends VertexAttributePredicate {
final Double value;
DoublePredicate(String attrName, Class attrClass, Double value) {
super(attrName, attrClass);
this.value = value;
}
static VertexAttributePredicate getEQPredicate(String attrName, Class attrClass, Double value) {
return new DoublePredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Double)value).compareTo(this.value) == 0;
}
};
}
static VertexAttributePredicate getNEQPredicate(String attrName, Class attrClass, Double value) {
return new DoublePredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Double)value).compareTo(this.value) != 0;
}
};
}
static VertexAttributePredicate getLTPredicate(String attrName, Class attrClass, Double value) {
return new DoublePredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Double)value).compareTo(this.value) < 0;
}
};
}
static VertexAttributePredicate getLTEPredicate(String attrName, Class attrClass, Double value) {
return new DoublePredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Double)value).compareTo(this.value) <= 0;
}
};
}
static VertexAttributePredicate getGTPredicate(String attrName, Class attrClass, Double value) {
return new DoublePredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Double)value).compareTo(this.value) > 0;
}
};
}
static VertexAttributePredicate getGTEPredicate(String attrName, Class attrClass, Double value) {
return new DoublePredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Double)value).compareTo(this.value) >= 0;
}
};
}
}
static abstract class BytePredicate extends VertexAttributePredicate {
final Byte value;
BytePredicate(String attrName, Class attrClass, Byte value) {
super(attrName, attrClass);
this.value = value;
}
static VertexAttributePredicate getEQPredicate(String attrName, Class attrClass, Byte value) {
return new BytePredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Byte)value).compareTo(this.value) == 0;
}
};
}
static VertexAttributePredicate getNEQPredicate(String attrName, Class attrClass, Byte value) {
return new BytePredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Byte)value).compareTo(this.value) != 0;
}
};
}
static VertexAttributePredicate getLTPredicate(String attrName, Class attrClass, Byte value) {
return new BytePredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Byte)value).compareTo(this.value) < 0;
}
};
}
static VertexAttributePredicate getLTEPredicate(String attrName, Class attrClass, Byte value) {
return new BytePredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Byte)value).compareTo(this.value) <= 0;
}
};
}
static VertexAttributePredicate getGTPredicate(String attrName, Class attrClass, Byte value) {
return new BytePredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Byte)value).compareTo(this.value) > 0;
}
};
}
static VertexAttributePredicate getGTEPredicate(String attrName, Class attrClass, Byte value) {
return new BytePredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((Byte)value).compareTo(this.value) >= 0;
}
};
}
}
static abstract class BigIntegerPredicate extends VertexAttributePredicate {
final BigInteger value;
BigIntegerPredicate(String attrName, Class attrClass, BigInteger value) {
super(attrName, attrClass);
this.value = value;
}
static VertexAttributePredicate getEQPredicate(String attrName, Class attrClass, BigInteger value) {
return new BigIntegerPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((BigInteger)value).compareTo(this.value) == 0;
}
};
}
static VertexAttributePredicate getNEQPredicate(String attrName, Class attrClass, BigInteger value) {
return new BigIntegerPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((BigInteger)value).compareTo(this.value) != 0;
}
};
}
static VertexAttributePredicate getLTPredicate(String attrName, Class attrClass, BigInteger value) {
return new BigIntegerPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((BigInteger)value).compareTo(this.value) < 0;
}
};
}
static VertexAttributePredicate getLTEPredicate(String attrName, Class attrClass, BigInteger value) {
return new BigIntegerPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((BigInteger)value).compareTo(this.value) <= 0;
}
};
}
static VertexAttributePredicate getGTPredicate(String attrName, Class attrClass, BigInteger value) {
return new BigIntegerPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((BigInteger)value).compareTo(this.value) > 0;
}
};
}
static VertexAttributePredicate getGTEPredicate(String attrName, Class attrClass, BigInteger value) {
return new BigIntegerPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((BigInteger)value).compareTo(this.value) >= 0;
}
};
}
}
static abstract class BigDecimalPredicate extends VertexAttributePredicate {
final BigDecimal value;
BigDecimalPredicate(String attrName, Class attrClass, BigDecimal value) {
super(attrName, attrClass);
this.value = value;
}
static VertexAttributePredicate getEQPredicate(String attrName, Class attrClass, BigDecimal value) {
return new BigDecimalPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((BigDecimal)value).compareTo(this.value) == 0;
}
};
}
static VertexAttributePredicate getNEQPredicate(String attrName, Class attrClass, BigDecimal value) {
return new BigDecimalPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((BigDecimal)value).compareTo(this.value) != 0;
}
};
}
static VertexAttributePredicate getLTPredicate(String attrName, Class attrClass, BigDecimal value) {
return new BigDecimalPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((BigDecimal)value).compareTo(this.value) < 0;
}
};
}
static VertexAttributePredicate getLTEPredicate(String attrName, Class attrClass, BigDecimal value) {
return new BigDecimalPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((BigDecimal)value).compareTo(this.value) <= 0;
}
};
}
static VertexAttributePredicate getGTPredicate(String attrName, Class attrClass, BigDecimal value) {
return new BigDecimalPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((BigDecimal)value).compareTo(this.value) > 0;
}
};
}
static VertexAttributePredicate getGTEPredicate(String attrName, Class attrClass, BigDecimal value) {
return new BigDecimalPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((BigDecimal)value).compareTo(this.value) >= 0;
}
};
}
}
static abstract class StringPredicate extends VertexAttributePredicate {
final String value;
StringPredicate(String attrName, Class attrClass, String value) {
super(attrName, attrClass);
this.value = value;
}
static VertexAttributePredicate getEQPredicate(String attrName, Class attrClass, String value) {
return new StringPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((String)value).compareTo(this.value) == 0;
}
};
}
static VertexAttributePredicate getNEQPredicate(String attrName, Class attrClass, String value) {
return new StringPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((String)value).compareTo(this.value) != 0;
}
};
}
static VertexAttributePredicate getLTPredicate(String attrName, Class attrClass, String value) {
return new StringPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((String)value).compareTo(this.value) < 0;
}
};
}
static VertexAttributePredicate getLTEPredicate(String attrName, Class attrClass, String value) {
return new StringPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((String)value).compareTo(this.value) <= 0;
}
};
}
static VertexAttributePredicate getGTPredicate(String attrName, Class attrClass, String value) {
return new StringPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((String)value).compareTo(this.value) > 0;
}
};
}
static VertexAttributePredicate getGTEPredicate(String attrName, Class attrClass, String value) {
return new StringPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((String)value).compareTo(this.value) >= 0;
}
};
}
static VertexAttributePredicate getContainsPredicate(String attrName, Class attrClass, String value) {
return new StringPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((String)value).contains(this.value);
}
};
}
static VertexAttributePredicate getStartsWithPredicate(String attrName, Class attrClass, String value) {
return new StringPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((String)value).startsWith(this.value);
}
};
}
static VertexAttributePredicate getEndsWithPredicate(String attrName, Class attrClass, String value) {
return new StringPredicate(attrName, attrClass, value) {
protected boolean compareValue(Object value) {
return ((String)value).endsWith(this.value);
}
};
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment