Skip to content

Commit

Permalink
JCR-2853: QOM utility classes in jcr-commons
Browse files Browse the repository at this point in the history
Move OperandEvaluator to jcr-commons.

Add a simple value factory class to simplify testing.

git-svn-id: https://svn.apache.org/repos/asf/jackrabbit/trunk@1080235 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
jukka committed Mar 10, 2011
1 parent 37ee126 commit b5fad2b
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@
import org.apache.jackrabbit.commons.predicate.Predicate;
import org.apache.jackrabbit.commons.predicate.Predicates;
import org.apache.jackrabbit.commons.predicate.RowPredicate;
import org.apache.jackrabbit.commons.query.qom.OperandEvaluator;
import org.apache.jackrabbit.core.NodeImpl;
import org.apache.jackrabbit.core.SessionImpl;
import org.apache.jackrabbit.core.id.NodeId;
import org.apache.jackrabbit.core.query.lucene.join.OperandEvaluator;
import org.apache.jackrabbit.core.query.lucene.join.SelectorRow;
import org.apache.jackrabbit.core.query.lucene.join.ValueComparator;
import org.apache.jackrabbit.spi.Name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import javax.jcr.query.qom.Operand;
import javax.jcr.query.qom.PropertyValue;

import org.apache.jackrabbit.commons.query.qom.OperandEvaluator;

abstract class AbstractRow implements Row {

private final Map<String, PropertyValue> columns;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import javax.jcr.query.qom.PropertyValue;
import javax.jcr.query.qom.QueryObjectModelFactory;

import org.apache.jackrabbit.commons.query.qom.OperandEvaluator;

class ChildNodeJoinMerger extends JoinMerger {

private final String childSelector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import javax.jcr.query.qom.PropertyValue;
import javax.jcr.query.qom.QueryObjectModelFactory;

import org.apache.jackrabbit.commons.query.qom.OperandEvaluator;

class DescendantNodeJoinMerger extends JoinMerger {

private final String descendantSelector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import javax.jcr.query.qom.PropertyValue;
import javax.jcr.query.qom.QueryObjectModelFactory;

import org.apache.jackrabbit.commons.query.qom.OperandEvaluator;

class EquiJoinMerger extends JoinMerger {

private final PropertyValue leftProperty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

import org.apache.jackrabbit.commons.iterator.RowIterable;
import org.apache.jackrabbit.commons.iterator.RowIteratorAdapter;
import org.apache.jackrabbit.commons.query.qom.OperandEvaluator;

/**
* A join merger is used by the {@link QueryEngine} class to efficiently
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import javax.jcr.query.Row;
import javax.jcr.query.qom.PropertyValue;

import org.apache.jackrabbit.commons.query.qom.OperandEvaluator;

public class JoinRow extends AbstractRow {

private final Row leftRow;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@

import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.jackrabbit.commons.iterator.RowIteratorAdapter;
import org.apache.jackrabbit.commons.query.qom.OperandEvaluator;
import org.apache.jackrabbit.core.query.lucene.LuceneQueryFactory;

public class QueryEngine {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import javax.jcr.query.qom.QueryObjectModelFactory;
import javax.jcr.query.qom.SameNodeJoinCondition;

import org.apache.jackrabbit.commons.query.qom.OperandEvaluator;

class SameNodeJoinMerger extends JoinMerger {

private final String selector1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import javax.jcr.RepositoryException;
import javax.jcr.query.qom.PropertyValue;

import org.apache.jackrabbit.commons.query.qom.OperandEvaluator;

/**
* A row implementation for a query with just a single selector.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.jackrabbit.commons;

import javax.jcr.ValueFormatException;

import org.apache.jackrabbit.value.AbstractValueFactory;

/**
* Simple value factory implementation for use mainly in testing.
* Complex value types such as names, paths and references are kept
* just as strings, and no format checks nor any namespace prefix
* updates are made.
*
* @since Apache Jackrabbit 2.3
*/
public class SimpleValueFactory extends AbstractValueFactory {

@Override
protected void checkPathFormat(String pathValue)
throws ValueFormatException {
}

@Override
protected void checkNameFormat(String nameValue)
throws ValueFormatException {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jackrabbit.core.query.lucene.join;

import static java.util.Locale.ENGLISH;
import static javax.jcr.PropertyType.NAME;
package org.apache.jackrabbit.commons.query.qom;

import java.util.Locale;
import java.util.Map;

import javax.jcr.Node;
Expand Down Expand Up @@ -55,20 +53,53 @@ public class OperandEvaluator {
/** Bind variables */
private final Map<String, Value> variables;

/** The locale to use in upper- and lower-case conversion. */
private final Locale locale;

/**
* Creates an operand evaluator for the given value factory and set of
* bind variables.
* bind variables. Upper- and lower-case conversions are performed using
* the given locale.
*
* @param factory value factory
* @param variables bind variables
* @param locale locale to use in upper- and lower-case conversions
*/
public OperandEvaluator(
ValueFactory factory, Map<String, Value> variables) {
ValueFactory factory, Map<String, Value> variables, Locale locale) {
this.factory = factory;
this.variables = variables;
this.locale = locale;
}

public Value getValue(StaticOperand operand, int type) throws RepositoryException {
/**
* Creates an operand evaluator for the given value factory and set of
* bind variables. Upper- and lower-case conversions are performed using
* the {@link Locale#ENGLISH}.
*
* @param factory value factory
* @param variables bind variables
* @param locale locale to use in upper- and lower-case conversions
*/
public OperandEvaluator(
ValueFactory factory, Map<String, Value> variables) {
this(factory, variables, Locale.ENGLISH);
}

/**
* Returns the value of the given static operand
* ({@link Literal literal} or {@link BindVariableValue bind variable})
* casted to the given type.
*
* @param operand static operand to be evaluated
* @param type expected value type
* @return evaluated value, casted to the given type
* @throws RepositoryException if a named bind variable is not found,
* if the operand type is unknown, or
* if the type conversion fails
*/
public Value getValue(StaticOperand operand, int type)
throws RepositoryException {
Value value = getValue(operand);
if (type == PropertyType.UNDEFINED || type == value.getType()) {
return value;
Expand Down Expand Up @@ -120,7 +151,7 @@ public Value getValue(StaticOperand operand) throws RepositoryException {
* @param operand operand to be evaluated
* @param row query result row
* @return evaluated value
* @throws RepositoryException
* @throws RepositoryException if the operand can't be evaluated
*/
public Value getValue(Operand operand, Row row) throws RepositoryException {
Value[] values = getValues(operand, row);
Expand Down Expand Up @@ -157,8 +188,10 @@ public Value[] getValues(Operand operand, Row row)
return new Value[] { factory.createValue(score) };
} else if (operand instanceof NodeName) {
NodeName nn = (NodeName) operand;
Node node = row.getNode(nn.getSelectorName());
return new Value[] { factory.createValue(node.getName(), NAME) };
Value value = factory.createValue(
row.getNode(nn.getSelectorName()).getName(),
PropertyType.NAME);
return new Value[] { value };
} else if (operand instanceof Length) {
return getLengthValues((Length) operand, row);
} else if (operand instanceof LowerCase) {
Expand Down Expand Up @@ -215,7 +248,7 @@ private Value[] getLowerCaseValues(LowerCase operand, Row row)
Value[] values = getValues(operand.getOperand(), row);
for (int i = 0; i < values.length; i++) {
String value = values[i].getString();
String lower = value.toLowerCase(ENGLISH);
String lower = value.toLowerCase(locale);
if (!value.equals(lower)) {
values[i] = factory.createValue(lower);
}
Expand All @@ -236,7 +269,7 @@ private Value[] getUpperCaseValues(UpperCase operand, Row row)
Value[] values = getValues(operand.getOperand(), row);
for (int i = 0; i < values.length; i++) {
String value = values[i].getString();
String upper = value.toUpperCase(ENGLISH);
String upper = value.toUpperCase(locale);
if (!value.equals(upper)) {
values[i] = factory.createValue(upper);
}
Expand All @@ -259,7 +292,7 @@ private Value[] getNodeLocalNameValues(NodeLocalName operand, Row row)
if (colon != -1) {
name = name.substring(colon + 1);
}
return new Value[] { factory.createValue(name, NAME) };
return new Value[] { factory.createValue(name, PropertyType.NAME) };
}

/**
Expand Down

0 comments on commit b5fad2b

Please sign in to comment.