KickJava   Java API By Example, From Geeks To Geeks.

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


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  * TableExplorer.java
25  *
26  */

27
28 package org.xquark.mapper.storage;
29
30 import java.sql.SQLException JavaDoc;
31 import java.util.Collection JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 import org.xquark.mapper.RepositoryException;
35 import org.xquark.mapper.mapping.ColumnMapping;
36 import org.xquark.mapper.mapping.RepositoryMapping;
37 import org.xquark.mapper.mapping.TableMapping;
38 import org.xquark.mapper.metadata.PathSet;
39 import org.xquark.schema.Declaration;
40 import org.xquark.schema.SchemaException;
41 import org.xquark.schema.validation.ValidationContextProvider;
42 import org.xquark.xml.xdbc.XMLDBCException;
43
44 /**
45  *
46  * @see org.xquark.mapper.XMLCollection
47  */

48 public abstract class TableExplorer
49 {
50     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
51     private static final String JavaDoc RCSName = "$Name: $";
52
53     public final static int NOT_FOUND = -3;
54     public final static int STRUCT_TUPLE = -2;
55     public final static int EXTRA_TUPLE = -1;
56     public final static int MIN_DATA = 0;
57     
58     protected PathSet pathSet;
59     protected Tuple[] tuples;
60     protected StructExplorer wNode;
61     protected ExtraDataExplorer extraNode;
62  // private RepositoryMapping collectionMapping;
63
protected _RepositoryCollection collection;
64     
65     TableExplorer(_RepositoryCollection collection)
66     throws SQLException JavaDoc, XMLDBCException
67     {
68         this.collection = collection;
69         pathSet = collection.getMetadata().getPathSet();
70         RepositoryMapping collectionMapping = collection.getMetadata().getMapping();
71     }
72     
73     /* only for document reading */
74     /** Return the tuple at the given index
75      */

76     Tuple get(int index) throws SQLException JavaDoc
77     {
78         return tuples[index];
79     }
80     
81     StructExplorer getStructNode() throws SQLException JavaDoc
82     {
83         return wNode;
84     }
85     
86     ExtraDataExplorer getExtraNode() throws SQLException JavaDoc
87     {
88         return extraNode;
89     }
90     
91     Tuple pollData(Collection JavaDoc tableMappings, long first, long last) throws SQLException JavaDoc
92     {
93         int minIndex = NOT_FOUND, i;
94         long minOID = Long.MAX_VALUE; // so that it is overwritten by the first...
95
if (tableMappings == null)
96             return null;
97         Iterator JavaDoc it = tableMappings.iterator();
98         
99         while (it.hasNext())
100         {
101             i = ((TableMapping)it.next()).getTableIndex();
102             if ((tuples[i] != null)
103             && tuples[i].queryPending()
104             && (tuples[i].getUOID() <= minOID) // inferior or equal
105
&& (tuples[i].isInRange(first, last)))
106             {
107                 minIndex= i;
108                 minOID = tuples[i].getUOID();
109             }
110         }
111         if (minIndex == NOT_FOUND)
112             return null;
113         else
114             return get(minIndex);
115     }
116     
117     /* look for a struct node or a text node in the range */
118     int poll(long first, long last)
119     throws SQLException JavaDoc
120     {
121         int minIndex = NOT_FOUND;
122         long minOID = Long.MAX_VALUE; // so that it is overwritten by the first...
123

124         if ((getStructNode().getUOID() < minOID) // strictly inferior
125
&& getStructNode().isInRange(first, last))
126         {
127             minIndex= STRUCT_TUPLE;
128             minOID = getStructNode().getUOID();
129         }
130         int defaultMappingIndex = collection.getMetadata().getDefaultMappingIndex();
131         if ((tuples[defaultMappingIndex] != null)
132             && tuples[defaultMappingIndex].queryPending()
133             && (tuples[defaultMappingIndex].getUOID() < minOID) // inferior
134
&& tuples[defaultMappingIndex].isInRange(first, last))
135             minIndex= defaultMappingIndex;
136         return minIndex;
137     }
138
139     /* Fetch the next row for all tuples */
140     boolean lookup() throws SQLException JavaDoc
141     {
142         boolean rows = wNode.fetchNextRow();
143         rows |= extraNode.fetchNextRow();
144         for (int i = 0; i < tuples.length; i++)
145         {
146             if (tuples[i] != null)
147             {
148                 if (tuples[i].queryPending())
149                     tuples[i].fetchNextRow();
150                 rows |= tuples[i].queryPending();
151             }
152         }
153         return rows;
154     }
155     
156     void close() throws SQLException JavaDoc, RepositoryException
157     {
158         wNode.close();
159         extraNode.close();
160         
161             /* Releasing JDBC resources used for value tables */
162         for (int i = 0; i < tuples.length; i++)
163         {
164             if (tuples[i] != null)
165                 tuples[i].close();
166         }
167     }
168     
169     /**
170      */

171     void reset() throws SQLException JavaDoc, RepositoryException
172     {
173         wNode.reset();
174         extraNode.reset();
175         
176         // Releasing JDBC resources used for value tables
177
for (int i = 0; i < tuples.length; i++)
178         {
179             if (tuples[i] != null)
180                 tuples[i].reset();
181         }
182     }
183     
184     String JavaDoc getValue(ColumnMapping column, Declaration decl, ValidationContextProvider nsContext)
185     throws SQLException JavaDoc, SchemaException, XMLDBCException
186     {
187         return get(column.getTableIndex()).getValue(column, decl, nsContext);
188     }
189 }
190
Popular Tags