Search in sources :

Example 6 with NotSupportException

use of com.baidu.hugegraph.exception.NotSupportException in project incubator-hugegraph by apache.

the class TraversalUtil method parsePredicate.

public static P<Object> parsePredicate(String predicate) {
    /*
         * Extract P from json string like {"properties": {"age": "P.gt(18)"}}
         * the `predicate` may actually be like "P.gt(18)"
         */
    Pattern pattern = Pattern.compile("^P\\.([a-z]+)\\(([\\S ]*)\\)$");
    Matcher matcher = pattern.matcher(predicate);
    if (!matcher.find()) {
        throw new HugeException("Invalid predicate: %s", predicate);
    }
    String method = matcher.group(1);
    String value = matcher.group(2);
    switch(method) {
        case "eq":
            return P.eq(predicateNumber(value));
        case "neq":
            return P.neq(predicateNumber(value));
        case "lt":
            return P.lt(predicateNumber(value));
        case "lte":
            return P.lte(predicateNumber(value));
        case "gt":
            return P.gt(predicateNumber(value));
        case "gte":
            return P.gte(predicateNumber(value));
        case "between":
            Number[] params = predicateNumbers(value, 2);
            return P.between(params[0], params[1]);
        case "inside":
            params = predicateNumbers(value, 2);
            return P.inside(params[0], params[1]);
        case "outside":
            params = predicateNumbers(value, 2);
            return P.outside(params[0], params[1]);
        case "within":
            return P.within(predicateArgs(value));
        case "textcontains":
            return ConditionP.textContains(predicateArg(value));
        case "contains":
            // Just for inner use case like auth filter
            return ConditionP.contains(predicateArg(value));
        default:
            throw new NotSupportException("predicate '%s'", method);
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) NotSupportException(com.baidu.hugegraph.exception.NotSupportException) HugeException(com.baidu.hugegraph.HugeException)

Example 7 with NotSupportException

use of com.baidu.hugegraph.exception.NotSupportException in project incubator-hugegraph by apache.

the class TraversalUtil method parsePredicate.

public static Condition parsePredicate(PropertyKey pk, String predicate) {
    Pattern pattern = Pattern.compile("^P\\.([a-z]+)\\(([\\S ]*)\\)$");
    Matcher matcher = pattern.matcher(predicate);
    if (!matcher.find()) {
        throw new HugeException("Invalid predicate: %s", predicate);
    }
    String method = matcher.group(1);
    String value = matcher.group(2);
    Object validValue;
    switch(method) {
        case "eq":
            validValue = validPropertyValue(predicateNumber(value), pk);
            return Condition.eq(pk.id(), validValue);
        case "neq":
            validValue = validPropertyValue(predicateNumber(value), pk);
            return Condition.neq(pk.id(), validValue);
        case "lt":
            validValue = validPropertyValue(predicateNumber(value), pk);
            return Condition.lt(pk.id(), validValue);
        case "lte":
            validValue = validPropertyValue(predicateNumber(value), pk);
            return Condition.lte(pk.id(), validValue);
        case "gt":
            validValue = validPropertyValue(predicateNumber(value), pk);
            return Condition.gt(pk.id(), validValue);
        case "gte":
            validValue = validPropertyValue(predicateNumber(value), pk);
            return Condition.gte(pk.id(), validValue);
        case "between":
            Number[] params = predicateNumbers(value, 2);
            Object v1 = validPropertyValue(params[0], pk);
            Object v2 = validPropertyValue(params[1], pk);
            return Condition.and(Condition.gte(pk.id(), v1), Condition.lt(pk.id(), v2));
        case "inside":
            params = predicateNumbers(value, 2);
            v1 = validPropertyValue(params[0], pk);
            v2 = validPropertyValue(params[1], pk);
            return Condition.and(Condition.gt(pk.id(), v1), Condition.lt(pk.id(), v2));
        case "outside":
            params = predicateNumbers(value, 2);
            v1 = validPropertyValue(params[0], pk);
            v2 = validPropertyValue(params[1], pk);
            return Condition.and(Condition.lt(pk.id(), v1), Condition.gt(pk.id(), v2));
        case "within":
            List<T> values = predicateArgs(value);
            List<T> validValues = new ArrayList<>(values.size());
            for (T v : values) {
                validValues.add(validPropertyValue(v, pk));
            }
            return Condition.in(pk.id(), validValues);
        case "textcontains":
            validValue = validPropertyValue(value, pk);
            return Condition.textContains(pk.id(), (String) validValue);
        case "contains":
            validValue = validPropertyValue(value, pk);
            return Condition.contains(pk.id(), validValue);
        default:
            throw new NotSupportException("predicate '%s'", method);
    }
}
Also used : Pattern(java.util.regex.Pattern) T(org.apache.tinkerpop.gremlin.structure.T) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) NotSupportException(com.baidu.hugegraph.exception.NotSupportException) HugeException(com.baidu.hugegraph.HugeException)

Example 8 with NotSupportException

use of com.baidu.hugegraph.exception.NotSupportException in project incubator-hugegraph by apache.

the class HbaseTable method queryByCond.

protected <R> R queryByCond(HbaseSession<R> session, ConditionQuery query) {
    if (query.containsScanRelation()) {
        E.checkArgument(query.relations().size() == 1, "Invalid scan with multi conditions: %s", query);
        Relation scan = query.relations().iterator().next();
        Shard shard = (Shard) scan.value();
        return this.queryByRange(session, shard, query.page());
    }
    throw new NotSupportException("query: %s", query);
}
Also used : Relation(com.baidu.hugegraph.backend.query.Condition.Relation) NotSupportException(com.baidu.hugegraph.exception.NotSupportException) Shard(com.baidu.hugegraph.backend.store.Shard)

Example 9 with NotSupportException

use of com.baidu.hugegraph.exception.NotSupportException in project incubator-hugegraph by apache.

the class Reflection method registerFieldsToFilter.

public static void registerFieldsToFilter(Class<?> containingClass, String... fieldNames) {
    if (REGISTER_FILEDS_TO_FILTER_METHOD == null) {
        throw new NotSupportException("Reflection.registerFieldsToFilter()");
    }
    try {
        REGISTER_FILEDS_TO_FILTER_METHOD.setAccessible(true);
        REGISTER_FILEDS_TO_FILTER_METHOD.invoke(REFLECTION_CLAZZ, containingClass, fieldNames);
    } catch (IllegalAccessException | InvocationTargetException e) {
        throw new HugeException("Failed to register class '%s' fields to filter: %s", containingClass, Arrays.toString(fieldNames));
    }
}
Also used : NotSupportException(com.baidu.hugegraph.exception.NotSupportException) HugeException(com.baidu.hugegraph.HugeException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 10 with NotSupportException

use of com.baidu.hugegraph.exception.NotSupportException in project incubator-hugegraph by apache.

the class GraphIndexTransaction method queryByUserprop.

@Watched(prefix = "index")
private IdHolderList queryByUserprop(ConditionQuery query) {
    // related index labels
    if (!this.graph().readMode().showOlap()) {
        for (Id pkId : query.userpropKeys()) {
            PropertyKey propertyKey = this.graph().propertyKey(pkId);
            if (propertyKey.olap()) {
                throw new NotAllowException("Not allowed to query by olap property key '%s'" + " when graph-read-mode is '%s'", propertyKey, this.graph().readMode());
            }
        }
    }
    Set<MatchedIndex> indexes = this.collectMatchedIndexes(query);
    if (indexes.isEmpty()) {
        Id label = query.condition(HugeKeys.LABEL);
        throw noIndexException(this.graph(), query, label);
    }
    // Value type of Condition not matched
    boolean paging = query.paging();
    if (!validQueryConditionValues(this.graph(), query)) {
        return IdHolderList.empty(paging);
    }
    // Do index query
    IdHolderList holders = new IdHolderList(paging);
    for (MatchedIndex index : indexes) {
        for (IndexLabel il : index.indexLabels()) {
            validateIndexLabel(il);
        }
        if (paging && index.indexLabels().size() > 1) {
            throw new NotSupportException("joint index query in paging");
        }
        if (index.containsSearchIndex()) {
            // Do search-index query
            holders.addAll(this.doSearchIndex(query, index));
        } else {
            // Do secondary-index, range-index or shard-index query
            IndexQueries queries = index.constructIndexQueries(query);
            assert !paging || queries.size() <= 1;
            IdHolder holder = this.doSingleOrJointIndex(queries);
            holders.add(holder);
        }
    /*
             * NOTE: need to skip the offset if offset > 0, but can't handle
             * it here because the query may a sub-query after flatten,
             * so the offset will be handle in QueryList.IndexQuery
             *
             * TODO: finish early here if records exceeds required limit with
             *       FixedIdHolder.
             */
    }
    return holders;
}
Also used : SortByCountIdHolderList(com.baidu.hugegraph.backend.page.SortByCountIdHolderList) IdHolderList(com.baidu.hugegraph.backend.page.IdHolderList) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) IdHolder(com.baidu.hugegraph.backend.page.IdHolder) FixedIdHolder(com.baidu.hugegraph.backend.page.IdHolder.FixedIdHolder) PagingIdHolder(com.baidu.hugegraph.backend.page.IdHolder.PagingIdHolder) BatchIdHolder(com.baidu.hugegraph.backend.page.IdHolder.BatchIdHolder) Id(com.baidu.hugegraph.backend.id.Id) NotSupportException(com.baidu.hugegraph.exception.NotSupportException) NotAllowException(com.baidu.hugegraph.exception.NotAllowException) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Aggregations

NotSupportException (com.baidu.hugegraph.exception.NotSupportException)13 HugeException (com.baidu.hugegraph.HugeException)4 Id (com.baidu.hugegraph.backend.id.Id)3 Relation (com.baidu.hugegraph.backend.query.Condition.Relation)3 BackendEntry (com.baidu.hugegraph.backend.store.BackendEntry)3 Shard (com.baidu.hugegraph.backend.store.Shard)3 RestResult (com.baidu.hugegraph.rest.RestResult)3 Aggregate (com.baidu.hugegraph.backend.query.Aggregate)2 TextBackendEntry (com.baidu.hugegraph.backend.serializer.TextBackendEntry)2 ArrayList (java.util.ArrayList)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 BackendException (com.baidu.hugegraph.backend.BackendException)1 IdHolder (com.baidu.hugegraph.backend.page.IdHolder)1 BatchIdHolder (com.baidu.hugegraph.backend.page.IdHolder.BatchIdHolder)1 FixedIdHolder (com.baidu.hugegraph.backend.page.IdHolder.FixedIdHolder)1 PagingIdHolder (com.baidu.hugegraph.backend.page.IdHolder.PagingIdHolder)1 IdHolderList (com.baidu.hugegraph.backend.page.IdHolderList)1 PageState (com.baidu.hugegraph.backend.page.PageState)1 SortByCountIdHolderList (com.baidu.hugegraph.backend.page.SortByCountIdHolderList)1