Package org.exoplatform.services.jcr.impl.core.query.lucene

Source Code of org.exoplatform.services.jcr.impl.core.query.lucene.DerefQuery

/*
* 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.exoplatform.services.jcr.impl.core.query.lucene;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermDocs;
import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.Similarity;
import org.apache.lucene.search.Weight;
import org.exoplatform.services.jcr.datamodel.InternalQName;
import org.exoplatform.services.jcr.impl.core.query.lucene.hits.AbstractHitCollector;

import java.io.IOException;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
* Implements a lucene <code>Query</code> which returns the nodes selected by
* a reference property of the context node.
*/
class DerefQuery extends Query
{

   /**
    * The context query
    */
   private final Query contextQuery;

   /**
    * The name of the reference property.
    */
   private final String refProperty;

   /**
    * The nameTest to apply on target node, or <code>null</code> if all
    * target nodes should be selected.
    */
   private final InternalQName nameTest;

   /**
    * The index format version.
    */
   private final IndexFormatVersion version;

   /**
    * The internal namespace mappings.
    */
   private final NamespaceMappings nsMappings;

   /**
    * The scorer of the context query
    */
   private Scorer contextScorer;

   /**
    * The scorer of the name test query
    */
   private Scorer nameTestScorer;

   /**
    * Creates a new <code>DerefQuery</code> based on a <code>context</code>
    * query.
    *
    * @param context the context for this query.
    * @param refProperty the name of the reference property.
    * @param nameTest a name test or <code>null</code> if any node is
    *  selected.
    * @param version the index format version.
    * @param nsMappings the namespace mappings.
    */
   DerefQuery(Query context, String refProperty, InternalQName nameTest, IndexFormatVersion version,
      NamespaceMappings nsMappings)
   {
      this.contextQuery = context;
      this.refProperty = refProperty;
      this.nameTest = nameTest;
      this.version = version;
      this.nsMappings = nsMappings;
   }

   /**
    * Creates a <code>Weight</code> instance for this query.
    *
    * @param searcher the <code>Searcher</code> instance to use.
    * @return a <code>DerefWeight</code>.
    */
   @Override
   public Weight createWeight(Searcher searcher)
   {
      return new DerefWeight(searcher);
   }

   /**
    * Always returns 'DerefQuery'.
    *
    * @param field the name of a field.
    * @return 'DerefQuery'.
    */
   @Override
   public String toString(String field)
   {
      return "DerefQuery";
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public void extractTerms(Set terms)
   {
      // no terms to extract
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public Query rewrite(IndexReader reader) throws IOException
   {
      Query cQuery = contextQuery.rewrite(reader);
      if (cQuery == contextQuery) // NOSONAR
      {
         return this;
      }
      else
      {
         return new DerefQuery(cQuery, refProperty, nameTest, version, nsMappings);
      }
   }

   //-------------------< DerefWeight >------------------------------------

   /**
    * The <code>Weight</code> implementation for this <code>DerefQuery</code>.
    */
   private class DerefWeight extends Weight
   {

      /**
       * The searcher in use
       */
      private final Searcher searcher;

      /**
       * Creates a new <code>DerefWeight</code> instance using
       * <code>searcher</code>.
       *
       * @param searcher a <code>Searcher</code> instance.
       */
      private DerefWeight(Searcher searcher)
      {
         this.searcher = searcher;
      }

      /**
       * Returns this <code>DerefQuery</code>.
       *
       * @return this <code>DerefQuery</code>.
       */
      @Override
      public Query getQuery()
      {
         return DerefQuery.this;
      }

      /**
       * {@inheritDoc}
       */
      @Override
      public float getValue()
      {
         return 1.0f;
      }

      /**
       * {@inheritDoc}
       */
      @Override
      public float sumOfSquaredWeights() throws IOException
      {
         return 1.0f;
      }

      /**
       * {@inheritDoc}
       */
      @Override
      public void normalize(float norm)
      {
      }

      /**
       * Creates a scorer for this <code>DerefQuery</code>.
       *
       * @param reader a reader for accessing the index.
       * @return a <code>DerefScorer</code>.
       * @throws IOException if an error occurs while reading from the index.
       */
      @Override
      public Scorer scorer(IndexReader reader, boolean scoreDocsInOrder, boolean topScorer) throws IOException
      {
         contextScorer = contextQuery.weight(searcher).scorer(reader, scoreDocsInOrder, topScorer);
         if (nameTest != null)
         {
            nameTestScorer =
               new NameQuery(nameTest, version, nsMappings).weight(searcher)
                  .scorer(reader, scoreDocsInOrder, topScorer);
         }
         return new DerefScorer(searcher.getSimilarity(), reader);
      }

      /**
       * {@inheritDoc}
       */
      @Override
      public Explanation explain(IndexReader reader, int doc) throws IOException
      {
         return new Explanation();
      }
   }

   //----------------------< DerefScorer >---------------------------------

   /**
    * Implements a <code>Scorer</code> for this <code>DerefQuery</code>.
    */
   private class DerefScorer extends Scorer
   {

      /**
       * An <code>IndexReader</code> to access the index.
       */
      private final IndexReader reader;

      /**
       * BitSet storing the id's of selected documents
       */
      private final BitSet hits;

      /**
       * List of UUIDs of selected nodes
       */
      private List uuids = null;

      /**
       * The next document id to return
       */
      private int nextDoc = -1;

      /**
       * Creates a new <code>DerefScorer</code>.
       *
       * @param similarity the <code>Similarity</code> instance to use.
       * @param reader     for index access.
       */
      protected DerefScorer(Similarity similarity, IndexReader reader)
      {
         super(similarity);
         this.reader = reader;
         this.hits = new BitSet(reader.maxDoc());
      }

      /**
       * {@inheritDoc}
       */
      @Override
      public boolean next() throws IOException
      {
         calculateChildren();
         nextDoc = hits.nextSetBit(nextDoc + 1);
         return nextDoc > -1;
      }

      /**
       * {@inheritDoc}
       */
      @Override
      public int doc()
      {
         return nextDoc;
      }

      /**
       * {@inheritDoc}
       */
      @Override
      public float score() throws IOException
      {
         return 1.0f;
      }

      /**
       * {@inheritDoc}
       */
      @Override
      public boolean skipTo(int target) throws IOException
      {
         calculateChildren();
         nextDoc = hits.nextSetBit(target);
         return nextDoc > -1;
      }

      /**
       * {@inheritDoc}
       *
       * @throws UnsupportedOperationException this implementation always
       *                                       throws an <code>UnsupportedOperationException</code>.
       */
      @Override
      public Explanation explain(int doc) throws IOException
      {
         throw new UnsupportedOperationException();
      }

      private void calculateChildren() throws IOException
      {
         if (uuids == null)
         {
            uuids = new ArrayList();
            contextScorer.score(new AbstractHitCollector()
            {
               @Override
               public void collect(int doc, float score)
               {
                  hits.set(doc);
               }
            });

            // collect nameTest hits
            final BitSet nameTestHits = new BitSet();
            if (nameTestScorer != null)
            {
               nameTestScorer.score(new AbstractHitCollector()
               {
                  @Override
                  public void collect(int doc, float score)
                  {
                     nameTestHits.set(doc);
                  }
               });
            }

            // retrieve uuids of target nodes
            String prefix = FieldNames.createNamedValue(refProperty, "");
            for (int i = hits.nextSetBit(0); i >= 0; i = hits.nextSetBit(i + 1))
            {
               String[] values = reader.document(i).getValues(FieldNames.PROPERTIES);
               if (values == null)
               {
                  // no reference properties at all on this node
                  continue;
               }
               for (int v = 0; v < values.length; v++)
               {
                  if (values[v].startsWith(prefix))
                  {
                     uuids.add(values[v].substring(prefix.length()));
                  }
               }
            }

            // collect the doc ids of all target nodes. we reuse the existing
            // bitset.
            hits.clear();
            for (Iterator it = uuids.iterator(); it.hasNext();)
            {
               TermDocs node = reader.termDocs(new Term(FieldNames.UUID, (String)it.next()));
               try
               {
                  while (node.next())
                  {
                     hits.set(node.doc());
                  }
               }
               finally
               {
                  node.close();
               }
            }
            // filter out the target nodes that do not match the name test
            // if there is any name test at all.
            if (nameTestScorer != null)
            {
               hits.and(nameTestHits);
            }
         }
      }
   }
}
TOP

Related Classes of org.exoplatform.services.jcr.impl.core.query.lucene.DerefQuery

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.