KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > storage > BatchQueryExplorer


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 /*
24  * QueryExplorer.java
25  *
26  * Created on 18 avril 2002, 17:15
27  */

28
29 package org.xquark.mapper.storage;
30
31 import java.sql.SQLException JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38 import org.xquark.mapper.RepositoryException;
39 import org.xquark.mapper.dbms.*;
40 import org.xquark.mapper.mapping.TableMapping;
41 import org.xquark.mapper.metadata.CollectionMappingInfo;
42 import org.xquark.mapper.metadata.CollectionMetadata;
43 import org.xquark.xml.xdbc.XMLDBCException;
44
45 /**
46  *
47  */

48 class BatchQueryExplorer extends QueryExplorer
49 {
50     private static final String JavaDoc RCSRevision = "$Revision: 1.2 $";
51     private static final String JavaDoc RCSName = "$Name: $";
52     
53     private static Log log = LogFactory.getLog(BatchQueryExplorer.class);
54     
55     private VarInfoSet tempTable;
56     
57     private static TableSpec tmpTableSpecs;
58     
59     static
60     {
61         tmpTableSpecs = TableSpecLoader.getInstance().getTableSpec(
62             TableSpec.CAT_QUERY_TMP,
63             TableSpec.TYPE_QUERY_TMP
64             );
65     }
66     
67     /** Creates a new instance of QueryExplorer */
68     BatchQueryExplorer(_RepositoryCollection collection) throws SQLException JavaDoc, XMLDBCException
69     {
70         super(collection);
71         CollectionMetadata meta = collection.getMetadata();
72         wNode = qNode = new BatchQueryStructExplorer(meta.getTableInfo(TableSpec.TYPE_STRUCT), meta.getUOIDManager());
73         extraNode = qExtraNode = new BatchQueryExtraDataExplorer(meta.getTableInfo(TableSpec.TYPE_EXTRA));
74     }
75     
76     private VarInfoSet prepareVarTable(VarInfoSet varInfoSet) throws SQLException JavaDoc
77     {
78         VarInfoSet tempTable = null;
79         List JavaDoc collapseVars = varInfoSet.getCollapseColumns();
80         if (collapseVars.size() > 0)
81         {
82             AbstractConnection connection = collection.getRepositoryConnection().getConnection();
83             ColumnSpec[] columns = tmpTableSpecs.getColumns();
84             StringBuffer JavaDoc subQuery = new StringBuffer JavaDoc("SELECT MIN(");
85             subQuery.append(varInfoSet.getRankColumnName());
86             subQuery.append(") as ");
87             subQuery.append(columns[0].getName());
88             subQuery.append(',');
89             subQuery.append(varInfoSet.getFirstColumnName());
90             subQuery.append(" as ");
91             subQuery.append(columns[1].getName());
92             subQuery.append(',');
93             subQuery.append(varInfoSet.getLastColumnName());
94             subQuery.append(" as ");
95             subQuery.append(columns[2].getName());
96             subQuery.append(',');
97             subQuery.append(varInfoSet.getPathColumnName());
98             subQuery.append(" as ");
99             subQuery.append(columns[3].getName());
100             subQuery.append(" FROM ");
101             subQuery.append(varInfoSet.getTableName());
102             subQuery.append(" GROUP BY ");
103             
104             for (int i = 0; i < collapseVars.size(); i++)
105             {
106                 if (i != 0)
107                     subQuery.append(',');
108                 if (collapseVars.get(i).equals(varInfoSet.getFirstColumnName()))
109                 {
110                     subQuery.append(varInfoSet.getFirstColumnName());
111                     subQuery.append(',');
112                     subQuery.append(varInfoSet.getLastColumnName());
113                     subQuery.append(',');
114                     subQuery.append(varInfoSet.getPathColumnName());
115                 }
116                 else
117                     subQuery.append((String JavaDoc)collapseVars.get(i));
118                 
119             }
120             String JavaDoc tableName = connection.createTemporaryTableAs(subQuery.toString());
121             tempTable = new VarBuildInfoSet
122             (
123             tableName,
124             columns[0].getName(),
125             columns[1].getName(),
126             columns[2].getName(),
127             columns[3].getName()
128             );
129         }
130         return tempTable;
131     }
132     
133     /**
134      */

135     void close() throws SQLException JavaDoc, RepositoryException
136     {
137         super.close();
138         
139         if (tempTable != null)
140         {
141             collection.getRepositoryConnection().getConnection().dropTemporaryTable(tempTable.getTableName());
142             tempTable = null;
143         }
144     }
145     
146     /**
147      */

148     void reset() throws SQLException JavaDoc, RepositoryException
149     {
150         super.reset();
151         
152         if (tempTable != null)
153         {
154             collection.getRepositoryConnection().getConnection().dropTemporaryTable(tempTable.getTableName());
155             tempTable = null;
156         }
157     }
158     
159     /** Perform Tuple allocation and therefore query execution
160      * @param subPaths a {PathNode subPath, PathNode variablePath} map giving
161      * for an absolute subpath the variable root path
162      * @param varPaths 0 index PathNode contains the variable root path other are
163      * this path subpaths
164      */

165     void initExploration(VarInfoSet varInfoSet, AbstractConnection connection) throws SQLException JavaDoc, XMLDBCException
166     {
167         if (log.isDebugEnabled())
168             log.debug("QueryExplorer::initExploration [varInfoSet]=" + varInfoSet);
169         
170         // Intermediate table creation
171
tempTable = prepareVarTable(varInfoSet);
172         
173         // Table partitioning
174
Object JavaDoc[] ret = dispatchSubPaths(varInfoSet);
175         Map JavaDoc tableMappingSubset = (Map JavaDoc)ret[0];
176         List JavaDoc subPathNodes = (List JavaDoc)ret[1];
177         
178         // Table subset browsing for statement execution
179
Iterator JavaDoc it = tableMappingSubset.entrySet().iterator();
180         Map.Entry JavaDoc entry;
181         CollectionMappingInfo tableStmts;
182         int tableIndex;
183         
184         CollectionMetadata meta = collection.getMetadata();
185         while (it.hasNext())
186         {
187             entry = (Map.Entry JavaDoc)it.next();
188             tableIndex = ((TableMapping)entry.getKey()).getTableIndex();
189             tableStmts = (CollectionMappingInfo)meta.getMappingStatements(tableIndex);
190             if (queryTuples[tableIndex] == null)
191                 queryTuples[tableIndex] = new BatchQueryTuple(tableStmts, meta.getMappingPID(tableIndex));
192             ((BatchQueryTuple)queryTuples[tableIndex]).execQuery((List JavaDoc)entry.getValue(),
193             tempTable == null?varInfoSet:tempTable, meta, connection);
194         }
195         
196         // initialize structure retrieval
197
((BatchQueryStructExplorer)qNode).getBranch(subPathNodes,
198         tempTable == null?varInfoSet:tempTable, connection);
199         ((BatchQueryExtraDataExplorer)qExtraNode).getBranch(subPathNodes,
200         tempTable == null?varInfoSet:tempTable, connection);
201     }
202     
203     void setCurrentRank(int rank)
204     {
205         for (int i=0; i < queryTuples.length; i++)
206         {
207             if (queryTuples[i] != null)
208                 ((BatchQueryTuple)queryTuples[i]).setCurrentRank(rank);
209         }
210         ((BatchQueryStructExplorer)qNode).setCurrentRank(rank);
211         ((BatchQueryExtraDataExplorer)qExtraNode).setCurrentRank(rank);
212     }
213     
214 }
215
Popular Tags