All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.datanucleus.store.db4o.query.JDOQLQuery Maven / Gradle / Ivy

/**********************************************************************
Copyright (c) 2007 Erik Bengtson and others. All rights reserved.
Licensed 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.

Contributors:
2008 Andy Jefferson - compilation process
    ...
 **********************************************************************/
package org.datanucleus.store.db4o.query;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.datanucleus.ClassLoaderResolver;
import org.datanucleus.metadata.AbstractClassMetaData;
import org.datanucleus.query.compiler.QueryCompilation;
import org.datanucleus.query.evaluator.JDOQLEvaluator;
import org.datanucleus.query.evaluator.JavaQueryEvaluator;
import org.datanucleus.store.ExecutionContext;
import org.datanucleus.store.connection.ManagedConnection;
import org.datanucleus.store.db4o.DB4OStoreManager;
import org.datanucleus.store.db4o.DB4OUtils;
import org.datanucleus.store.query.AbstractJDOQLQuery;
import org.datanucleus.util.NucleusLogger;

import com.db4o.ObjectContainer;
import com.db4o.query.Candidate;
import com.db4o.query.Evaluation;
import com.db4o.query.Query;

/**
 * DB4O representation of a JDOQL query for use by DataNucleus.
 * The query can be specified via method calls, or via a single-string form.
 */
public class JDOQLQuery extends AbstractJDOQLQuery
{
    /**
     * Constructs a new query instance that uses the given persistence manager.
     * @param ec execution context
     */
    public JDOQLQuery(ExecutionContext ec)
    {
        this(ec, (JDOQLQuery) null);
    }

    /**
     * Constructs a new query instance having the same criteria as the given query.
     * @param ec execution context
     * @param q The query from which to copy criteria.
     */
    public JDOQLQuery(ExecutionContext ec, JDOQLQuery q)
    {
        super(ec, q);
    }

    /**
     * Constructor for a JDOQL query where the query is specified using the "Single-String" format.
     * @param ec execution context
     * @param query The query string
     */
    public JDOQLQuery(ExecutionContext ec, String query)
    {
        super(ec, query);
    }

    protected Object performExecute(Map parameters)
    {
        ClassLoaderResolver clr = ec.getClassLoaderResolver();

        if (candidateCollection != null && candidateCollection.isEmpty())
        {
            return Collections.EMPTY_LIST;
        }

        boolean inMemory = evaluateInMemory();
        ManagedConnection mconn = ec.getStoreManager().getConnection(ec);
        ObjectContainer cont = (ObjectContainer) mconn.getConnection();
        try
        {
            // Execute the query
            long startTime = System.currentTimeMillis();
            if (NucleusLogger.QUERY.isDebugEnabled())
            {
                NucleusLogger.QUERY.debug(LOCALISER.msg("021046", "JDOQL", getSingleStringQuery(), null));
            }
            List candidates = null;
            boolean filterInMemory = false;
            boolean orderingInMemory = false;
            if (candidateCollection == null)
            {
                // Create the SODA query optionally with the candidate and filter restrictions
                Query query = createSODAQuery(cont, compilation, parameters, inMemory);
                candidates = query.execute();
                if (inMemory)
                {
                    filterInMemory = true;
                    orderingInMemory = true;
                }
            }
            else
            {
                candidates = new ArrayList(candidateCollection);
                filterInMemory = true;
                orderingInMemory = true;
            }

            // Apply any restrictions to the results (that we can't use in the input SODA query)
            JavaQueryEvaluator resultMapper =
                new JDOQLEvaluator(this, candidates, compilation, parameters, clr);
            Collection results = resultMapper.execute(filterInMemory, orderingInMemory, true, true, true);

            if (NucleusLogger.QUERY.isDebugEnabled())
            {
                NucleusLogger.QUERY.debug(LOCALISER.msg("021074", "JDOQL", "" + (System.currentTimeMillis() - startTime)));
            }

            Iterator iter = results.iterator();
            while (iter.hasNext())
            {
                Object obj = iter.next();
                if (result == null)
                {
                    // Assign StateManagers to any returned objects
                    AbstractClassMetaData cmd = ec.getMetaDataManager().getMetaDataForClass(obj.getClass(), clr);
                    DB4OUtils.prepareDB4OObjectForUse(obj, ec, cont, cmd, (DB4OStoreManager)ec.getStoreManager());
                }
            }
            return results;
        }
        finally
        {
            mconn.release();
        }
    }

    /**
     * Method to create the SODA query object for the candidate class, and with the possible
     * restrictions we can apply to the filter.
     * @param cont ObjectContainer
     * @param compilation The compilation
     * @param parameters Any parameters
     * @param inMemory whether to process everything in-memory
     * @return The DB4O SODA Query
     */
    private Query createSODAQuery(ObjectContainer cont, QueryCompilation compilation, Map parameters,
            boolean inMemory)
    {
        Query query = cont.query();
        query.constrain(candidateClass);
        if (!subclasses)
        {
            query.constrain(new Evaluation()
            {
                public void evaluate(Candidate c)
                {
                    c.include(c.getObject().getClass() == candidateClass);
                }
            });
        }

        if (!inMemory)
        {
            // Constrain the query with filter and ordering constraints
            new QueryToSODAMapper(query, compilation, parameters).compile();
        }
        return query;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy