[dna-commits] DNA SVN: r1328 - in trunk/dna-search/src/main/java/org/jboss/dna/search: query and 1 other directory.

dna-commits at lists.jboss.org dna-commits at lists.jboss.org
Wed Nov 18 14:37:57 EST 2009


Author: rhauch
Date: 2009-11-18 14:37:57 -0500 (Wed, 18 Nov 2009)
New Revision: 1328

Added:
   trunk/dna-search/src/main/java/org/jboss/dna/search/query/CompareLengthQuery.java
Modified:
   trunk/dna-search/src/main/java/org/jboss/dna/search/DualIndexLayout.java
   trunk/dna-search/src/main/java/org/jboss/dna/search/KitchenSinkIndexLayout.java
Log:
DNA-467 Added query to constrain the length of a field.

Modified: trunk/dna-search/src/main/java/org/jboss/dna/search/DualIndexLayout.java
===================================================================
--- trunk/dna-search/src/main/java/org/jboss/dna/search/DualIndexLayout.java	2009-11-17 23:30:25 UTC (rev 1327)
+++ trunk/dna-search/src/main/java/org/jboss/dna/search/DualIndexLayout.java	2009-11-18 19:37:57 UTC (rev 1328)
@@ -87,6 +87,7 @@
 import org.jboss.dna.graph.query.QueryEngine;
 import org.jboss.dna.graph.query.QueryResults;
 import org.jboss.dna.graph.query.QueryResults.Columns;
+import org.jboss.dna.graph.query.model.Length;
 import org.jboss.dna.graph.query.model.NodeDepth;
 import org.jboss.dna.graph.query.model.NodeLocalName;
 import org.jboss.dna.graph.query.model.NodeName;
@@ -106,6 +107,7 @@
 import org.jboss.dna.graph.query.process.QueryProcessor;
 import org.jboss.dna.graph.request.ChangeRequest;
 import org.jboss.dna.search.IndexRules.Rule;
+import org.jboss.dna.search.query.CompareLengthQuery;
 import org.jboss.dna.search.query.CompareNameQuery;
 import org.jboss.dna.search.query.ComparePathQuery;
 import org.jboss.dna.search.query.CompareStringQuery;
@@ -958,6 +960,36 @@
             return query;
         }
 
+        protected Query findNodesWith( Length propertyLength,
+                                       Operator operator,
+                                       Object value ) {
+            assert propertyLength != null;
+            assert value != null;
+            PropertyValue propertyValue = propertyLength.getPropertyValue();
+            String field = stringFactory.create(propertyValue.getPropertyName());
+            ValueFactories factories = context.getValueFactories();
+            int length = factories.getLongFactory().create(value).intValue();
+            switch (operator) {
+                case EQUAL_TO:
+                    return CompareLengthQuery.createQueryForNodesWithFieldEqualTo(length, field, factories);
+                case NOT_EQUAL_TO:
+                    return CompareLengthQuery.createQueryForNodesWithFieldNotEqualTo(length, field, factories);
+                case GREATER_THAN:
+                    return CompareLengthQuery.createQueryForNodesWithFieldGreaterThan(length, field, factories);
+                case GREATER_THAN_OR_EQUAL_TO:
+                    return CompareLengthQuery.createQueryForNodesWithFieldGreaterThanOrEqualTo(length, field, factories);
+                case LESS_THAN:
+                    return CompareLengthQuery.createQueryForNodesWithFieldLessThan(length, field, factories);
+                case LESS_THAN_OR_EQUAL_TO:
+                    return CompareLengthQuery.createQueryForNodesWithFieldLessThanOrEqualTo(length, field, factories);
+                case LIKE:
+                    // This is not allowed ...
+                    assert false;
+                    break;
+            }
+            return null;
+        }
+
         protected Query findNodesWith( PropertyValue propertyValue,
                                        Operator operator,
                                        Object value,
@@ -1111,7 +1143,6 @@
                     return null;
             }
             return null;
-
         }
 
         protected Query findNodesWithNumericRange( PropertyValue propertyValue,

Modified: trunk/dna-search/src/main/java/org/jboss/dna/search/KitchenSinkIndexLayout.java
===================================================================
--- trunk/dna-search/src/main/java/org/jboss/dna/search/KitchenSinkIndexLayout.java	2009-11-17 23:30:25 UTC (rev 1327)
+++ trunk/dna-search/src/main/java/org/jboss/dna/search/KitchenSinkIndexLayout.java	2009-11-18 19:37:57 UTC (rev 1328)
@@ -449,10 +449,7 @@
             } else if (left instanceof PropertyValue) {
                 return session.findNodesWith((PropertyValue)left, operator, value, caseSensitive);
             } else if (left instanceof Length) {
-                Length length = (Length)left;
-                PropertyValue nested = length.getPropertyValue();
-
-                return null;
+                return session.findNodesWith((Length)left, operator, right);
             } else if (left instanceof LowerCase) {
                 LowerCase lowercase = (LowerCase)left;
                 return createQuery(lowercase.getOperand(), operator, right, false);

Added: trunk/dna-search/src/main/java/org/jboss/dna/search/query/CompareLengthQuery.java
===================================================================
--- trunk/dna-search/src/main/java/org/jboss/dna/search/query/CompareLengthQuery.java	                        (rev 0)
+++ trunk/dna-search/src/main/java/org/jboss/dna/search/query/CompareLengthQuery.java	2009-11-18 19:37:57 UTC (rev 1328)
@@ -0,0 +1,254 @@
+/*
+ * JBoss DNA (http://www.jboss.org/dna)
+ * See the COPYRIGHT.txt file distributed with this work for information
+ * regarding copyright ownership.  Some portions may be licensed
+ * to Red Hat, Inc. under one or more contributor license agreements.
+ * See the AUTHORS.txt file in the distribution for a full listing of 
+ * individual contributors.
+ *
+ * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
+ * is licensed to you under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ * 
+ * JBoss DNA 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.dna.search.query;
+
+import java.io.IOException;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.Searcher;
+import org.apache.lucene.search.Weight;
+import org.jboss.dna.graph.property.ValueFactories;
+import org.jboss.dna.graph.property.ValueFactory;
+import org.jboss.dna.graph.query.model.Length;
+
+/**
+ * A Lucene {@link Query} implementation that is used to apply a {@link Length} constraint against a string field. This query
+ * implementation works by using the {@link Query#weight(Searcher) weight} and
+ * {@link Weight#scorer(IndexReader, boolean, boolean) scorer} of the wrapped query to score (and return) only those documents
+ * with string fields that satisfy the constraint.
+ */
+public class CompareLengthQuery extends CompareQuery<Integer> {
+
+    private static final long serialVersionUID = 1L;
+    protected static final Evaluator<Integer> EQUAL_TO = new Evaluator<Integer>() {
+        private static final long serialVersionUID = 1L;
+
+        public boolean satisfiesConstraint( Integer nodeValue,
+                                            Integer length ) {
+            return nodeValue == length;
+        }
+
+        @Override
+        public String toString() {
+            return " = ";
+        }
+    };
+    protected static final Evaluator<Integer> NOT_EQUAL_TO = new Evaluator<Integer>() {
+        private static final long serialVersionUID = 1L;
+
+        public boolean satisfiesConstraint( Integer nodeValue,
+                                            Integer length ) {
+            return nodeValue == length;
+        }
+
+        @Override
+        public String toString() {
+            return " != ";
+        }
+    };
+    protected static final Evaluator<Integer> IS_LESS_THAN = new Evaluator<Integer>() {
+        private static final long serialVersionUID = 1L;
+
+        public boolean satisfiesConstraint( Integer nodeValue,
+                                            Integer length ) {
+            return nodeValue < length;
+        }
+
+        @Override
+        public String toString() {
+            return " < ";
+        }
+    };
+    protected static final Evaluator<Integer> IS_LESS_THAN_OR_EQUAL_TO = new Evaluator<Integer>() {
+        private static final long serialVersionUID = 1L;
+
+        public boolean satisfiesConstraint( Integer nodeValue,
+                                            Integer length ) {
+            return nodeValue < length;
+        }
+
+        @Override
+        public String toString() {
+            return " <= ";
+        }
+    };
+    protected static final Evaluator<Integer> IS_GREATER_THAN = new Evaluator<Integer>() {
+        private static final long serialVersionUID = 1L;
+
+        public boolean satisfiesConstraint( Integer nodeValue,
+                                            Integer length ) {
+            return nodeValue < length;
+        }
+
+        @Override
+        public String toString() {
+            return " > ";
+        }
+    };
+    protected static final Evaluator<Integer> IS_GREATER_THAN_OR_EQUAL_TO = new Evaluator<Integer>() {
+        private static final long serialVersionUID = 1L;
+
+        public boolean satisfiesConstraint( Integer nodeValue,
+                                            Integer length ) {
+            return nodeValue < length;
+        }
+
+        @Override
+        public String toString() {
+            return " >= ";
+        }
+    };
+
+    /**
+     * Construct a {@link Query} implementation that scores documents with a field length that is equal to the supplied constraint
+     * value.
+     * 
+     * @param constraintValue the constraint value; may not be null
+     * @param fieldName the name of the document field containing the value; may not be null
+     * @param factories the value factories that can be used during the scoring; may not be null
+     * @return the query; never null
+     */
+    public static CompareLengthQuery createQueryForNodesWithFieldEqualTo( Integer constraintValue,
+                                                                          String fieldName,
+                                                                          ValueFactories factories ) {
+        return new CompareLengthQuery(fieldName, constraintValue, factories.getStringFactory(), IS_GREATER_THAN);
+    }
+
+    /**
+     * Construct a {@link Query} implementation that scores documents with a field length that is not equal to the supplied
+     * constraint value.
+     * 
+     * @param constraintValue the constraint value; may not be null
+     * @param fieldName the name of the document field containing the value; may not be null
+     * @param factories the value factories that can be used during the scoring; may not be null
+     * @return the query; never null
+     */
+    public static CompareLengthQuery createQueryForNodesWithFieldNotEqualTo( Integer constraintValue,
+                                                                             String fieldName,
+                                                                             ValueFactories factories ) {
+        return new CompareLengthQuery(fieldName, constraintValue, factories.getStringFactory(), IS_GREATER_THAN);
+    }
+
+    /**
+     * Construct a {@link Query} implementation that scores documents with a field length that is greater than the supplied
+     * constraint value.
+     * 
+     * @param constraintValue the constraint value; may not be null
+     * @param fieldName the name of the document field containing the value; may not be null
+     * @param factories the value factories that can be used during the scoring; may not be null
+     * @return the query; never null
+     */
+    public static CompareLengthQuery createQueryForNodesWithFieldGreaterThan( Integer constraintValue,
+                                                                              String fieldName,
+                                                                              ValueFactories factories ) {
+        return new CompareLengthQuery(fieldName, constraintValue, factories.getStringFactory(), IS_GREATER_THAN);
+    }
+
+    /**
+     * Construct a {@link Query} implementation that scores documents with a field length that is greater than or equal to the
+     * supplied constraint value.
+     * 
+     * @param constraintValue the constraint value; may not be null
+     * @param fieldName the name of the document field containing the value; may not be null
+     * @param factories the value factories that can be used during the scoring; may not be null
+     * @return the query; never null
+     */
+    public static CompareLengthQuery createQueryForNodesWithFieldGreaterThanOrEqualTo( Integer constraintValue,
+                                                                                       String fieldName,
+                                                                                       ValueFactories factories ) {
+        return new CompareLengthQuery(fieldName, constraintValue, factories.getStringFactory(), IS_GREATER_THAN_OR_EQUAL_TO);
+    }
+
+    /**
+     * Construct a {@link Query} implementation that scores documents with a field length that is less than the supplied
+     * constraint value.
+     * 
+     * @param constraintValue the constraint value; may not be null
+     * @param fieldName the name of the document field containing the value; may not be null
+     * @param factories the value factories that can be used during the scoring; may not be null
+     * @return the query; never null
+     */
+    public static CompareLengthQuery createQueryForNodesWithFieldLessThan( Integer constraintValue,
+                                                                           String fieldName,
+                                                                           ValueFactories factories ) {
+        return new CompareLengthQuery(fieldName, constraintValue, factories.getStringFactory(), IS_LESS_THAN);
+    }
+
+    /**
+     * Construct a {@link Query} implementation that scores documents with a field length that is less than or equal to the
+     * supplied constraint value.
+     * 
+     * @param constraintValue the constraint value; may not be null
+     * @param fieldName the name of the document field containing the value; may not be null
+     * @param factories the value factories that can be used during the scoring; may not be null
+     * @return the query; never null
+     */
+    public static CompareLengthQuery createQueryForNodesWithFieldLessThanOrEqualTo( Integer constraintValue,
+                                                                                    String fieldName,
+                                                                                    ValueFactories factories ) {
+        return new CompareLengthQuery(fieldName, constraintValue, factories.getStringFactory(), IS_LESS_THAN_OR_EQUAL_TO);
+    }
+
+    /**
+     * Construct a {@link Query} implementation that scores nodes according to the supplied comparator.
+     * 
+     * @param fieldName the name of the document field containing the value; may not be null
+     * @param constraintValue the constraint value; may not be null
+     * @param stringFactory the string factory that can be used during the scoring; may not be null
+     * @param evaluator the {@link CompareQuery.Evaluator} implementation that returns whether the node path satisfies the
+     *        constraint; may not be null
+     */
+    protected CompareLengthQuery( String fieldName,
+                                  Integer constraintValue,
+                                  ValueFactory<String> stringFactory,
+                                  Evaluator<Integer> evaluator ) {
+        super(fieldName, constraintValue, null, stringFactory, evaluator);
+    }
+
+    /**
+     * {@inheritDoc}
+     * 
+     * @see org.jboss.dna.search.query.CompareQuery#readFromDocument(org.apache.lucene.index.IndexReader, int)
+     */
+    @Override
+    protected Integer readFromDocument( IndexReader reader,
+                                        int docId ) throws IOException {
+        // This implementation reads the length of the field ...
+        Document doc = reader.document(docId, fieldSelector);
+        String valueString = doc.get(fieldName);
+        String value = stringFactory.create(valueString);
+        return value != null ? value.length() : 0;
+    }
+
+    /**
+     * {@inheritDoc}
+     * 
+     * @see org.apache.lucene.search.Query#clone()
+     */
+    @Override
+    public Object clone() {
+        return new CompareLengthQuery(fieldName, constraintValue, stringFactory, evaluator);
+    }
+}


Property changes on: trunk/dna-search/src/main/java/org/jboss/dna/search/query/CompareLengthQuery.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF



More information about the dna-commits mailing list