/*
 * 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.jcr.query;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.jcr.query.InvalidQueryException;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import javax.jcr.query.qom.QueryObjectModelFactory;

import org.apache.jackrabbit.JcrConstants;
import org.apache.jackrabbit.oak.api.PropertyValue;
import org.apache.jackrabbit.oak.api.QueryEngine;
import org.apache.jackrabbit.oak.api.Result;
import org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate;
import org.apache.jackrabbit.oak.jcr.query.qom.QueryObjectModelFactoryImpl;
import org.apache.jackrabbit.oak.jcr.session.SessionContext;
import org.apache.jackrabbit.oak.plugins.memory.PropertyStates;
import org.apache.jackrabbit.oak.spi.query.PropertyValues;

/**
 * The implementation of the corresponding JCR interface.
 */
public class QueryManagerImpl implements QueryManager {

    private final SessionDelegate sessionDelegate;
    private final SessionContext sessionContext;
    private final QueryObjectModelFactoryImpl qomFactory;
    private final QueryEngine queryEngine;
    private final HashSet<String> supportedQueryLanguages = new HashSet<String>();

    public QueryManagerImpl(SessionContext sessionContext) {
        this.sessionDelegate = sessionContext.getSessionDelegate();
        this.sessionContext = sessionContext;
        qomFactory = new QueryObjectModelFactoryImpl(this, sessionContext);
        queryEngine = sessionDelegate.getQueryEngine();
        supportedQueryLanguages.addAll(queryEngine.getSupportedQueryLanguages());
    }

    @Override
    public QueryImpl createQuery(String statement, String language) throws RepositoryException {
        if (!supportedQueryLanguages.contains(language)) {
            throw new InvalidQueryException("The specified language is not supported: " + language);
        }
        return new QueryImpl(this, statement, language, sessionContext);
    }

    @Override
    public QueryObjectModelFactory getQOMFactory() {
        return qomFactory;
    }

    @Override
    public Query getQuery(Node node) throws RepositoryException {
        if (!node.isNodeType(JcrConstants.NT_QUERY)) {
            throw new InvalidQueryException("Not an nt:query node: " + node.getPath());
        }
        String statement = node.getProperty(JcrConstants.JCR_STATEMENT).getString();
        String language = node.getProperty(JcrConstants.JCR_LANGUAGE).getString();
        QueryImpl query = createQuery(statement, language);
        query.setStoredQueryPath(node.getPath());
        return query;
    }

    @Override
    public String[] getSupportedQueryLanguages() throws RepositoryException {
        ArrayList<String> list = new ArrayList<String>(queryEngine.getSupportedQueryLanguages());
        // JQOM is supported in this level only (converted to JCR_SQL2)
        list.add(Query.JCR_JQOM);
        // create a new instance each time because the array is mutable
        // (the caller could modify it)
        return list.toArray(new String[list.size()]);
    }

    /**
     * Parse the query and get the bind variable names.
     * 
     * @param statement the query statement
     * @param language the query language
     * @return the bind variable names
     */
    public List<String> parse(String statement, String language) throws InvalidQueryException {
        try {
            return queryEngine.getBindVariableNames(
                    statement, language,
                    sessionContext.getSessionLocalMappings());
        } catch (ParseException e) {
            throw new InvalidQueryException(e);
        }
    }

    public QueryResult executeQuery(String statement, String language,
            long limit, long offset, HashMap<String, Value> bindVariableMap) throws RepositoryException {
        try {
            Map<String, PropertyValue> bindMap = convertMap(bindVariableMap);
            long t = System.nanoTime();
            Result r = queryEngine.executeQuery(
                    statement, language, limit, offset, bindMap,
                    sessionContext.getSessionLocalMappings());
            sessionContext.getStatisticManager()
                    .logQueryEvaluationTime(language, statement, (System.nanoTime() - t) / 1000000);
            return new QueryResultImpl(sessionContext, r);
        } catch (IllegalArgumentException e) {
            throw new InvalidQueryException(e);
        } catch (ParseException e) {
            throw new InvalidQueryException(e);
        }
    }

    private static Map<String, PropertyValue> convertMap(
            HashMap<String, Value> bindVariableMap) throws RepositoryException {
        HashMap<String, PropertyValue> map = new HashMap<String, PropertyValue>();
        for (Entry<String, Value> e : bindVariableMap.entrySet()) {
            map.put(e.getKey(),
                    PropertyValues.create(PropertyStates.createProperty("",
                            e.getValue())));
        }
        return map;
    }

    void ensureIsAlive() throws RepositoryException {
        // check session status
        if (!sessionDelegate.isAlive()) {
            throw new RepositoryException("This session has been closed.");
        }
    }

}

@@ -843,24 +843,198 @@
 jcr.query;%0A%0A
+import static org.apache.jackrabbit.api.stats.RepositoryStatistics.Type.QUERY_COUNT;%0Aimport static org.apache.jackrabbit.api.stats.RepositoryStatistics.Type.QUERY_DURATION;%0A%0A
 import java.
@@ -1206,16 +1206,63 @@
 p.Entry;
+%0Aimport java.util.concurrent.atomic.AtomicLong;
 %0A%0Aimport
@@ -2462,16 +2462,101 @@
 ring%3E();
+%0A    private final AtomicLong queryCount;%0A    private final AtomicLong queryDuration;
 %0A%0A    pu
@@ -2934,24 +2934,152 @@
 nguages());%0A
+        queryCount = sessionContext.getCounter(QUERY_COUNT);%0A        queryDuration = sessionContext.getCounter(QUERY_DURATION);%0A
     %7D%0A%0A    @
@@ -5405,16 +5405,17 @@
   long t
+0
  = Syste
@@ -5597,32 +5597,173 @@
 calMappings());%0A
+            queryCount.incrementAndGet();%0A            long dt = (System.nanoTime() - t0) / 1000000;%0A            queryDuration.addAndGet(dt);%0A
             sess
@@ -5864,41 +5864,10 @@
 nt, 
-(System.nanoTime() - t) / 1000000
+dt
 );%0A 

Implement QueryStat from Jackrabbit API Implement repository statistics: query count and query duration