001    /*
002     * Cumulus4j - Securing your data in the cloud - http://cumulus4j.org
003     * Copyright (C) 2011 NightLabs Consulting GmbH
004     *
005     * This program is free software: you can redistribute it and/or modify
006     * it under the terms of the GNU Affero General Public License as
007     * published by the Free Software Foundation, either version 3 of the
008     * License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU Affero General Public License for more details.
014     *
015     * You should have received a copy of the GNU Affero General Public License
016     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017     */
018    package org.cumulus4j.store.query;
019    
020    import java.util.ArrayList;
021    import java.util.Collection;
022    import java.util.Collections;
023    import java.util.List;
024    import java.util.Map;
025    import java.util.Set;
026    
027    import javax.jdo.PersistenceManager;
028    
029    import org.cumulus4j.store.Cumulus4jStoreManager;
030    import org.cumulus4j.store.PersistenceManagerConnection;
031    import org.cumulus4j.store.model.ClassMeta;
032    import org.datanucleus.query.evaluator.JPQLEvaluator;
033    import org.datanucleus.query.evaluator.JavaQueryEvaluator;
034    import org.datanucleus.store.ExecutionContext;
035    import org.datanucleus.store.connection.ManagedConnection;
036    import org.datanucleus.store.query.AbstractJPQLQuery;
037    import org.datanucleus.util.NucleusLogger;
038    
039    /**
040     * JPQL query implementation. Delegates to the query-language-agnostic {@link QueryEvaluator} via
041     * its thin wrapper sub-class {@link JDOQueryEvaluator}.
042     */
043    public class JPQLQuery extends AbstractJPQLQuery {
044    
045            private static final long serialVersionUID = 1L;
046    
047    // BEGIN DataNucleus 3.0.0-m6 and 3.0.0-release
048            public JPQLQuery(ExecutionContext ec, AbstractJPQLQuery q) {
049                    super(ec, q);
050            }
051    
052            public JPQLQuery(ExecutionContext ec, String query) {
053                    super(ec, query);
054            }
055    
056            public JPQLQuery(ExecutionContext ec) {
057                    super(ec);
058            }
059    // END DataNucleus 3.0.0-m6 and 3.0.0-release
060    
061    // BEGIN DataNucleus 3.0.1 and newer
062    //      public JPQLQuery(StoreManager storeMgr, ExecutionContext ec, AbstractJPQLQuery q) {
063    //              super(storeMgr, ec, q);
064    //      }
065    //
066    //      public JPQLQuery(StoreManager storeMgr, ExecutionContext ec, String query) {
067    //              super(storeMgr, ec, query);
068    //      }
069    //
070    //      public JPQLQuery(StoreManager storeMgr, ExecutionContext ec) {
071    //              super(storeMgr, ec);
072    //      }
073    // END DataNucleus 3.0.1 and newer
074    
075            @Override
076            protected Object performExecute(@SuppressWarnings("rawtypes") Map parameters) {
077                    ManagedConnection mconn = ec.getStoreManager().getConnection(ec);
078                    try {
079                            PersistenceManagerConnection pmConn = (PersistenceManagerConnection)mconn.getConnection();
080                            PersistenceManager pmData = pmConn.getDataPM();
081    
082                            boolean inMemory = evaluateInMemory();
083                            boolean inMemory_applyFilter = true;
084                            List<Object> candidates = null;
085                            if (this.candidateCollection != null) {
086                                    if (candidateCollection.isEmpty()) {
087                                            return Collections.EMPTY_LIST;
088                                    }
089    
090                                    @SuppressWarnings("unchecked")
091                                    Collection<? extends Object> c = this.candidateCollection;
092                                    candidates = new ArrayList<Object>(c);
093                            }
094                            else {
095                                    if (candidateExtent != null) {
096                                            this.setCandidateClass(candidateExtent.getCandidateClass());
097                                            this.setSubclasses(candidateExtent.hasSubclasses());
098                                    }
099    
100                                    if (inMemory) {
101                                            // Retrieve all candidates and perform all evaluation in-memory
102                                            Set<ClassMeta> classMetas = QueryHelper.getCandidateClassMetas((Cumulus4jStoreManager) ec.getStoreManager(),
103                                                            ec, candidateClass, subclasses);
104                                            candidates = QueryHelper.getAllPersistentObjectsForCandidateClasses(pmData, ec, classMetas);
105                                    }
106                                    else {
107                                            try
108                                            {
109                                                    // Apply filter in datastore
110                                                    @SuppressWarnings("unchecked")
111                                                    Map<String, Object> parameterValues = parameters;
112                                                    JDOQueryEvaluator queryEvaluator = new JDOQueryEvaluator(this, compilation, parameterValues, clr, pmConn);
113                                                    candidates = queryEvaluator.execute();
114                                                    if (queryEvaluator.isComplete()) {
115                                                            inMemory_applyFilter = false;
116                                                    }
117                                                    else {
118                                                            NucleusLogger.QUERY.debug("Query evaluation of filter in datastore was incomplete so doing further work in-memory");
119                                                    }
120                                            }
121                                            catch (UnsupportedOperationException uoe) {
122                                                    // Some part of the filter is not yet supported, so fallback to in-memory evaluation
123                                                    // Retrieve all candidates and perform all evaluation in-memory
124                                                    NucleusLogger.QUERY.info("Query filter is not totally evaluatable in-datastore using Cumulus4j currently, so evaluating in-memory : "+uoe.getMessage());
125                                                    Set<ClassMeta> classMetas = QueryHelper.getCandidateClassMetas((Cumulus4jStoreManager) ec.getStoreManager(),
126                                                                    ec, candidateClass, subclasses);
127                                                    candidates = QueryHelper.getAllPersistentObjectsForCandidateClasses(pmData, ec, classMetas);
128                                            }
129                                    }
130                            }
131    
132                            // Evaluate any remaining query components in-memory
133                            JavaQueryEvaluator evaluator = new JPQLEvaluator(this, candidates, compilation, parameters, ec.getClassLoaderResolver());
134                            return evaluator.execute(inMemory_applyFilter, true, true, true, true);
135                    } finally {
136                            mconn.release();
137                    }
138            }
139    }