/*
 * 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.oak.plugins.index.nodetype;

import org.apache.jackrabbit.JcrConstants;
import org.apache.jackrabbit.oak.spi.query.Cursor;
import org.apache.jackrabbit.oak.spi.query.Cursors;
import org.apache.jackrabbit.oak.spi.query.Filter;
import org.apache.jackrabbit.oak.spi.query.QueryIndex;
import org.apache.jackrabbit.oak.spi.state.NodeState;

/**
 * <code>NodeTypeIndex</code> implements a {@link QueryIndex} using
 * {@link PropertyIndexLookup}s on <code>jcr:primaryType</code> and
 * <code>jcr:mixinTypes</code> to evaluate a node type restriction on
 * {@link Filter}. The cost for this index is the sum of the costs of the
 * {@link PropertyIndexLookup} for queries on <code>jcr:primaryType</code> and
 * <code>jcr:mixinTypes</code>.
 */
class NodeTypeIndex implements QueryIndex, JcrConstants {

    @Override
    public double getCost(Filter filter, NodeState root) {
        // TODO don't call getCost for such queries
        if (filter.getFullTextConstraint() != null) {
            // not an appropriate index for full-text search
            return Double.POSITIVE_INFINITY;
        }
        if (filter.containsNativeConstraint()) {
            // not an appropriate index for native search
            return Double.POSITIVE_INFINITY;
        }
        if (!hasNodeTypeRestriction(filter)) {
            // this is not an appropriate index if the filter
            // doesn't have a node type restriction
            return Double.POSITIVE_INFINITY;
        }
        NodeTypeIndexLookup lookup = new NodeTypeIndexLookup(root);
        if (lookup.isIndexed(filter.getPath(), filter)) {
            return lookup.getCost(filter);
        } else {
            return Double.POSITIVE_INFINITY;
        }
    }

    @Override
    public Cursor query(Filter filter, NodeState root) {
        NodeTypeIndexLookup lookup = new NodeTypeIndexLookup(root);
        if (!hasNodeTypeRestriction(filter) || !lookup.isIndexed(filter.getPath(), filter)) {
            throw new IllegalStateException(
                    "NodeType index is used even when no index is available for filter " + filter);
        }
        return Cursors.newPathCursorDistinct(lookup.query(filter), filter.getQueryEngineSettings());
    }
    
    @Override
    public String getPlan(Filter filter, NodeState root) {
        return "nodeType " + filter.toString();
    }

    @Override
    public String getIndexName() {
        return "nodeType";
    }
    
    //----------------------------< internal >----------------------------------

    private static boolean hasNodeTypeRestriction(Filter filter) {
        return !filter.matchesAllTypes();
    }

}

@@ -1174,22 +1174,23 @@
 %0A/**%0A * 
-%3C
+%7B@
 code
-%3E
+ 
 NodeType
@@ -1194,23 +1194,17 @@
 ypeIndex
-%3C/code%3E
+%7D
  impleme
@@ -1236,32 +1236,81 @@
 using%0A * %7B@link 
+org.apache.jackrabbit.oak.plugins.index.property.
 PropertyIndexLoo
@@ -1314,26 +1314,30 @@
 Lookup%7Ds
+%0A *
  on 
-%3C
+%7B@
 code
-%3E
+ 
 jcr:prim
@@ -1343,37 +1343,29 @@
 maryType
-%3C/code%3E and%0A * %3C
+%7D and %7B@
 code
-%3E
+ 
 jcr:mixi
@@ -1370,23 +1370,17 @@
 xinTypes
-%3C/code%3E
+%7D
  to eval
@@ -1387,32 +1387,35 @@
 uate a node type
+%0A *
  restriction on%0A
@@ -1413,19 +1413,16 @@
 ction on
-%0A *
  %7B@link 
@@ -1481,26 +1481,26 @@
 osts
+%0A *
  of the
-%0A *
  %7B@link 
 Prop
@@ -1495,16 +1495,65 @@
  %7B@link 
+org.apache.jackrabbit.oak.plugins.index.property.
 Property
@@ -1564,16 +1564,19 @@
 xLookup%7D
+%0A *
  for que
@@ -1583,22 +1583,23 @@
 ries on 
-%3C
+%7B@
 code
-%3E
+ 
 jcr:prim
@@ -1609,29 +1609,21 @@
 Type
-%3C/code%3E and%0A * %3C
+%7D and %7B@
 code
-%3E
+ 
 jcr:
@@ -1636,15 +1636,9 @@
 ypes
-%3C/code%3E
+%7D
 .%0A *

fix broken link to PropertyIndexLookup