Search in sources :

Example 1 with RangeIndexQueryCriterion

use of org.apache.ignite.internal.cache.query.RangeIndexQueryCriterion in project ignite by apache.

the class IndexQueryCriteriaBuilder method gt.

/**
 * Greater Then.
 *
 * @param field Index field to apply criterion.
 * @param val Exclusive lower bound.
 * @return Criterion.
 */
public static IndexQueryCriterion gt(String field, Object val) {
    A.notNullOrEmpty(field, "field");
    RangeIndexQueryCriterion c = new RangeIndexQueryCriterion(field, val, null);
    c.upperIncl(true);
    c.lowerNull(val == null);
    return c;
}
Also used : RangeIndexQueryCriterion(org.apache.ignite.internal.cache.query.RangeIndexQueryCriterion)

Example 2 with RangeIndexQueryCriterion

use of org.apache.ignite.internal.cache.query.RangeIndexQueryCriterion in project ignite by apache.

the class IndexQueryCriteriaBuilder method lte.

/**
 * Less Then or Equal To.
 *
 * @param field Index field to apply criterion.
 * @param val Inclusive upper bound.
 * @return Criterion.
 */
public static IndexQueryCriterion lte(String field, Object val) {
    A.notNullOrEmpty(field, "field");
    RangeIndexQueryCriterion c = new RangeIndexQueryCriterion(field, null, val);
    c.lowerIncl(true);
    c.upperIncl(true);
    c.upperNull(val == null);
    return c;
}
Also used : RangeIndexQueryCriterion(org.apache.ignite.internal.cache.query.RangeIndexQueryCriterion)

Example 3 with RangeIndexQueryCriterion

use of org.apache.ignite.internal.cache.query.RangeIndexQueryCriterion in project ignite by apache.

the class IndexQueryProcessor method rangeDesc.

/**
 * @return Modified description for criterion in case of error.
 */
private static String rangeDesc(RangeIndexQueryCriterion c, String fldName, Object lower, Object upper) {
    String fld = fldName == null ? c.field() : fldName;
    Object l = lower == null ? c.lower() : lower;
    Object u = upper == null ? c.upper() : upper;
    RangeIndexQueryCriterion r = new RangeIndexQueryCriterion(fld, l, u);
    r.lowerIncl(c.lowerIncl());
    r.upperIncl(c.upperIncl());
    return r.toString();
}
Also used : CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) RangeIndexQueryCriterion(org.apache.ignite.internal.cache.query.RangeIndexQueryCriterion)

Example 4 with RangeIndexQueryCriterion

use of org.apache.ignite.internal.cache.query.RangeIndexQueryCriterion in project ignite by apache.

the class RepeatedFieldIndexQueryTest method testRangeQueriesWithTwoCriteriaSingleField.

/**
 */
@Test
public void testRangeQueriesWithTwoCriteriaSingleField() {
    int lower = new Random().nextInt(CNT / 2);
    int upper = CNT / 2 + new Random().nextInt(CNT / 2 - 1);
    List<IndexQueryCriterion> criteria = criteria(fldName, lower, upper);
    List<T2<RangeIndexQueryCriterion, RangeIndexQueryCriterion>> checks = new ArrayList<>();
    for (int i = 0; i < criteria.size(); i++) {
        for (int j = 0; j < criteria.size(); j++) {
            checks.add(new T2<>((RangeIndexQueryCriterion) criteria.get(i), (RangeIndexQueryCriterion) criteria.get(j)));
        }
    }
    checks.forEach(c -> checkTwoCriteria(c.get1(), c.get2()));
}
Also used : Random(java.util.Random) RangeIndexQueryCriterion(org.apache.ignite.internal.cache.query.RangeIndexQueryCriterion) ArrayList(java.util.ArrayList) RangeIndexQueryCriterion(org.apache.ignite.internal.cache.query.RangeIndexQueryCriterion) T2(org.apache.ignite.internal.util.typedef.T2) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 5 with RangeIndexQueryCriterion

use of org.apache.ignite.internal.cache.query.RangeIndexQueryCriterion in project ignite by apache.

the class IndexQueryProcessor method mergeIndexQueryCriteria.

/**
 * Merges multiple criteria for the same field into single criterion.
 */
private Map<String, RangeIndexQueryCriterion> mergeIndexQueryCriteria(InlineIndexImpl idx, SortedIndexDefinition idxDef, IndexQueryDesc idxQryDesc) throws IgniteCheckedException {
    Map<String, RangeIndexQueryCriterion> mergedCriteria = new HashMap<>();
    Map<String, IndexKeyDefinition> idxFlds = idxDef.indexKeyDefinitions();
    IndexKeyTypeSettings keyTypeSettings = idx.segment(0).rowHandler().indexKeyTypeSettings();
    CacheObjectContext coctx = idx.segment(0).cacheGroupContext().cacheObjectContext();
    IndexRowComparator keyCmp = idxDef.rowComparator();
    for (IndexQueryCriterion c : idxQryDesc.criteria()) {
        RangeIndexQueryCriterion crit = (RangeIndexQueryCriterion) c;
        String fldName = idxFlds.containsKey(crit.field()) ? crit.field() : QueryUtils.normalizeObjectName(crit.field(), false);
        IndexKeyDefinition keyDef = idxFlds.get(fldName);
        if (keyDef == null)
            throw failIndexQuery("Index doesn't match criteria", idxDef, idxQryDesc);
        IndexKey l = key(crit.lower(), crit.lowerNull(), keyDef, keyTypeSettings, coctx);
        IndexKey u = key(crit.upper(), crit.upperNull(), keyDef, keyTypeSettings, coctx);
        if (l != null && u != null && keyCmp.compareKey(l, u) > 0) {
            throw failIndexQuery("Illegal criterion: lower boundary is greater than the upper boundary: " + rangeDesc(crit, fldName, null, null), idxDef, idxQryDesc);
        }
        boolean lowIncl = crit.lowerIncl();
        boolean upIncl = crit.upperIncl();
        boolean lowNull = crit.lowerNull();
        boolean upNull = crit.upperNull();
        if (mergedCriteria.containsKey(fldName)) {
            RangeIndexQueryCriterion prev = mergedCriteria.get(fldName);
            IndexKey prevLower = (IndexKey) prev.lower();
            IndexKey prevUpper = (IndexKey) prev.upper();
            // Validate merged criteria.
            if (!checkBoundaries(l, prevUpper, crit.lowerIncl(), prev.upperIncl(), keyCmp) || !checkBoundaries(prevLower, u, prev.lowerIncl(), crit.upperIncl(), keyCmp)) {
                String prevDesc = rangeDesc(prev, null, prevLower == null ? null : prevLower.key(), prevUpper == null ? null : prevUpper.key());
                throw failIndexQuery("Failed to merge criterion " + rangeDesc(crit, fldName, null, null) + " with previous criteria range " + prevDesc, idxDef, idxQryDesc);
            }
            int lowCmp = 0;
            // Use previous lower boudary, as it's greater than the current.
            if (l == null || (prevLower != null && (lowCmp = keyCmp.compareKey(prevLower, l)) >= 0)) {
                l = prevLower;
                lowIncl = lowCmp != 0 ? prev.lowerIncl() : prev.lowerIncl() ? lowIncl : prev.lowerIncl();
                lowNull = prev.lowerNull();
            }
            int upCmp = 0;
            // Use previous upper boudary, as it's less than the current.
            if (u == null || (prevUpper != null && (upCmp = keyCmp.compareKey(prevUpper, u)) <= 0)) {
                u = prevUpper;
                upIncl = upCmp != 0 ? prev.upperIncl() : prev.upperIncl() ? upIncl : prev.upperIncl();
                upNull = prev.upperNull();
            }
        }
        RangeIndexQueryCriterion idxKeyCrit = new RangeIndexQueryCriterion(fldName, l, u);
        idxKeyCrit.lowerIncl(lowIncl);
        idxKeyCrit.upperIncl(upIncl);
        idxKeyCrit.lowerNull(lowNull);
        idxKeyCrit.upperNull(upNull);
        mergedCriteria.put(fldName, idxKeyCrit);
    }
    return mergedCriteria;
}
Also used : IndexKey(org.apache.ignite.internal.cache.query.index.sorted.keys.IndexKey) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IndexKeyDefinition(org.apache.ignite.internal.cache.query.index.sorted.IndexKeyDefinition) RangeIndexQueryCriterion(org.apache.ignite.internal.cache.query.RangeIndexQueryCriterion) IndexQueryCriterion(org.apache.ignite.cache.query.IndexQueryCriterion) CacheObjectContext(org.apache.ignite.internal.processors.cache.CacheObjectContext) RangeIndexQueryCriterion(org.apache.ignite.internal.cache.query.RangeIndexQueryCriterion) IndexKeyTypeSettings(org.apache.ignite.internal.cache.query.index.sorted.IndexKeyTypeSettings) IndexRowComparator(org.apache.ignite.internal.cache.query.index.sorted.IndexRowComparator)

Aggregations

RangeIndexQueryCriterion (org.apache.ignite.internal.cache.query.RangeIndexQueryCriterion)10 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 IndexKeyDefinition (org.apache.ignite.internal.cache.query.index.sorted.IndexKeyDefinition)2 IndexKey (org.apache.ignite.internal.cache.query.index.sorted.keys.IndexKey)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Random (java.util.Random)1 IndexQueryCriterion (org.apache.ignite.cache.query.IndexQueryCriterion)1 IndexKeyTypeSettings (org.apache.ignite.internal.cache.query.index.sorted.IndexKeyTypeSettings)1 IndexRowComparator (org.apache.ignite.internal.cache.query.index.sorted.IndexRowComparator)1 IndexSearchRowImpl (org.apache.ignite.internal.cache.query.index.sorted.IndexSearchRowImpl)1 InlineIndexRowHandler (org.apache.ignite.internal.cache.query.index.sorted.InlineIndexRowHandler)1 SortedIndexDefinition (org.apache.ignite.internal.cache.query.index.sorted.SortedIndexDefinition)1 InlineIndexImpl (org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndexImpl)1 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)1 CacheObjectContext (org.apache.ignite.internal.processors.cache.CacheObjectContext)1 T2 (org.apache.ignite.internal.util.typedef.T2)1 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)1 Test (org.junit.Test)1