
com.redhat.lightblue.eval.FieldComparisonEvaluator Maven / Gradle / Ivy
/*
Copyright 2013 Red Hat, Inc. and/or its affiliates.
This file is part of lightblue.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
package com.redhat.lightblue.eval;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.redhat.lightblue.crud.CrudConstants;
import com.redhat.lightblue.metadata.FieldTreeNode;
import com.redhat.lightblue.metadata.SimpleField;
import com.redhat.lightblue.metadata.ArrayField;
import com.redhat.lightblue.metadata.SimpleArrayElement;
import com.redhat.lightblue.metadata.types.ArrayType;
import com.redhat.lightblue.metadata.Type;
import com.redhat.lightblue.query.BinaryComparisonOperator;
import com.redhat.lightblue.query.FieldComparisonExpression;
import com.redhat.lightblue.util.Path;
import com.redhat.lightblue.util.KeyValueCursor;
/**
* Evaluates lfield op rfield type comparisons. Both fields can be simple fields
* or arrays. Here's the semantics of the operation:
*
*
* - lfield op rfield : true if lfield op rfield
* - lfield op rarray: true if lfield op rarray[i] for all elements i
* - larray op rarray: true if larray[i] op rarray[i] for all elements i. for
* op other than $ne, array sizes must be equal
*
*
*
*/
public class FieldComparisonEvaluator extends QueryEvaluator {
private static final Logger LOGGER = LoggerFactory.getLogger(FieldComparisonEvaluator.class);
private final FieldTreeNode fieldMd;
private final FieldTreeNode rfieldMd;
private final Path relativePath;
private final Path rfieldRelativePath;
private final BinaryComparisonOperator operator;
/**
* Constructs evaluator for {field op field} style comparison
*
* @param expr The expression
* @param md Entity metadata
* @param context The path relative to which the expression will be
* evaluated
*/
public FieldComparisonEvaluator(FieldComparisonExpression expr, FieldTreeNode context) {
this.relativePath = expr.getField();
this.rfieldRelativePath = expr.getRfield();
fieldMd = context.resolve(relativePath);
if (fieldMd == null) {
throw new EvaluationError(expr, CrudConstants.ERR_FIELD_NOT_THERE + " " + relativePath);
}
rfieldMd = context.resolve(rfieldRelativePath);
if (rfieldMd == null) {
throw new EvaluationError(expr, CrudConstants.ERR_FIELD_NOT_THERE + " " + rfieldRelativePath);
}
// Both fields must be simple fields or simple arrays
if (!(fieldMd instanceof SimpleField
|| (fieldMd instanceof ArrayField && ((ArrayField) fieldMd).getElement() instanceof SimpleArrayElement))) {
throw new EvaluationError(expr, CrudConstants.ERR_EXPECTED_SIMPLE_FIELD_OR_SIMPLE_ARRAY + " " + relativePath);
}
if (!(rfieldMd instanceof SimpleField
|| (rfieldMd instanceof ArrayField && ((ArrayField) rfieldMd).getElement() instanceof SimpleArrayElement))) {
throw new EvaluationError(expr, CrudConstants.ERR_EXPECTED_SIMPLE_FIELD_OR_SIMPLE_ARRAY + " " + rfieldRelativePath);
}
operator = expr.getOp();
LOGGER.debug("ctor {} {} {}", relativePath, operator, rfieldRelativePath);
}
@Override
public boolean evaluate(QueryEvaluationContext ctx) {
LOGGER.debug("evaluate {} {} {}", relativePath, operator, rfieldRelativePath);
KeyValueCursor lcursor = ctx.getNodes(relativePath);
ctx.setResult(false);
if (lcursor != null) {
while (lcursor.hasNext() && !ctx.getResult()) {
lcursor.next();
JsonNode lvalueNode = lcursor.getCurrentValue();
Object ldocValue = null;
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy