use of org.apache.ignite.internal.cache.query.index.sorted.IndexRowComparator 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;
}
Aggregations