KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > common > QueryResultContainer


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.common;
13
14 import com.versant.core.jdo.VersantPersistenceManager;
15 import com.versant.core.jdo.QueryResultBase;
16 import com.versant.core.metadata.MDStatics;
17 import com.versant.core.server.CompiledQuery;
18 import com.versant.core.server.CachedQueryResult;
19 import com.versant.core.storagemanager.ApplicationContext;
20 import com.versant.core.util.OIDObjectOutput;
21 import com.versant.core.util.OIDObjectInput;
22 import com.versant.core.util.FastExternalizable;
23
24 import javax.jdo.PersistenceManager;
25 import java.io.*;
26 import java.util.List JavaDoc;
27 import java.util.ArrayList JavaDoc;
28
29 /**
30  * This is used to transport the query results to the client.
31  */

32 public final class QueryResultContainer implements FastExternalizable {
33
34     public static final Object JavaDoc[] EMPTY_ARRAY = new Object JavaDoc[0];
35     /**
36      * The compiled qeury that this results is for.
37      */

38     private CompiledQuery cq;
39
40     public StatesReturned container;
41     /**
42      * A flag to indicate that this is the last results from the results.
43      * i.e. for a forward only result set this will be the end.
44      */

45     public boolean qFinished;
46     /**
47      * The query results.
48      */

49     private Object JavaDoc[] data;
50     private int size;
51     private static final int DEFAULT_SIZE = 20;
52     public boolean allResults;
53
54     public QueryResultContainer() {
55     }
56
57     public QueryResultContainer(StatesReturned container) {
58         this.container = container;
59     }
60
61     public QueryResultContainer(ApplicationContext context,
62             CompiledQuery cq) {
63         container = new StatesReturned(context);
64         init(cq);
65     }
66
67     /**
68      * Add a row to the results.
69      *
70      * @param val
71      */

72     public void addRow(Object JavaDoc val) {
73         if (val == null) {
74             throw BindingSupportImpl.getInstance().internal(
75                     "Adding a 'Null' value to query result is not supported.s");
76         }
77         checkSize();
78         data[size++] = val;
79     }
80
81     private void checkSize() {
82         if (data == null) {
83             data = new Object JavaDoc[DEFAULT_SIZE];
84         } else if (size == data.length) {
85             Object JavaDoc[] t = new Object JavaDoc[size * 2];
86             System.arraycopy(data, 0, t, 0, size);
87             data = t;
88         }
89     }
90
91     public boolean isqFinished() {
92         return qFinished;
93     }
94
95     public void setqFinished(boolean qFinished) {
96         this.qFinished = qFinished;
97     }
98
99     /**
100      * This must be called after use.
101      * This clears all the internal datastructures.
102      */

103     public void reset() {
104         data = null;
105         container.clear();
106         qFinished = false;
107         size = 0;
108         allResults = false;
109     }
110
111     /**
112      * This must be called on a query before use.
113      */

114     public void init(CompiledQuery cq, int capacity) {
115         reset();
116         this.cq = cq;
117         if (capacity < DEFAULT_SIZE) {
118             data = new Object JavaDoc[capacity];
119         } else {
120             data = new Object JavaDoc[DEFAULT_SIZE];
121         }
122     }
123
124     /**
125      * This must be called on a query before use.
126      * This will not init the data array.
127      */

128     public void init(CompiledQuery cq) {
129         reset();
130         this.cq = cq;
131     }
132
133     /**
134      * Add this query results to the stack.
135      * The container will be in a 'reset' state after calling this.
136      */

137     public void addToQueryStack(Stack stack) {
138         if (size != 0) {
139             stack.add(data, size);
140         }
141         data = null;
142         size = 0;
143     }
144
145     /**
146      * This return's the backing array of the container itself.
147      * You must use size() or iterate to first null to determine the amount of valid entries.
148      */

149     public Object JavaDoc[] toResolvedObject(PersistenceManager pm) {
150         int size = this.size;
151         if (size == 0) {
152             return EMPTY_ARRAY;
153         }
154         Object JavaDoc[] res = data;
155         for (int i = 0; i < size; i++) {
156             res[i] = QueryResultBase.resolveRow(res[i], pm);
157         }
158         return res;
159     }
160
161     public int size() {
162         return size;
163     }
164
165     public Object JavaDoc getUnique() {
166         if (data == null) return null;
167         else return data[0];
168     }
169
170     public Object JavaDoc get(int index) {
171         return data[index];
172     }
173
174     /**
175      * Add the query results to the list and resolve any oids while doing so.
176      */

177     public void resolveAndAddTo(List JavaDoc l, VersantPersistenceManager pm) {
178         int size = this.size;
179         for (int i = 0; i < size; i++) {
180             l.add(QueryResultBase.resolveRow(data[i], pm));
181         }
182     }
183
184     /**
185      * Add all our results to the cache container. This is used when the query results
186      * can be cached.
187      */

188     public void addResultsTo(CachedQueryResult cacheResults,
189             boolean copy) {
190         int size = this.size;
191         if (cacheResults.results == null) {
192             cacheResults.results = new ArrayList JavaDoc(size);
193         }
194
195         if (copy
196
197             ) {
198             //this is only done for if the row is a object[] and it contains oids.
199
for (int i = 0; i < size; i++) {
200                 Object JavaDoc[] val = (Object JavaDoc[])data[i];
201                 Object JavaDoc[] cop = new Object JavaDoc[val.length];
202                 System.arraycopy(val, 0, cop, 0, val.length);
203                 cacheResults.results.add(cop);
204             }
205         } else {
206             for (int i = 0; i < size; i++) {
207                 if (data[i] == null) {
208                     throw BindingSupportImpl.getInstance().internal("");
209                 }
210                 cacheResults.results.add(data[i]);
211             }
212         }
213         container.addIndirectOIDs(cacheResults);
214     }
215
216     /**
217      * This is used to fill this container from cached data.
218      */

219     public void fillFrom(CachedQueryResult qCacheContainer) {
220         final ArrayList JavaDoc lData = qCacheContainer.results;
221         final int n = size = lData.size();
222         if (data == null || n > data.length) {
223             data = new Object JavaDoc[n];
224             for (int i = 0; i < n; i++) {
225                 data[i] = lData.get(i);
226             }
227         }
228         size = n;
229         for (int i = 0; i < n; i++) {
230             data[i] = lData.get(i);
231         }
232
233     }
234
235     public void dump() {
236         System.out.println(
237                 "\n\n$$$$$$$$$$$$$ START QueryResultContainer.dump $$$$$$$$$$$$$");
238         System.out.println("results = " + size);
239         int n = size;
240         for (int i = 0; i < n; i++) {
241             System.out.println("results.get(i) = " + data[i]);
242         }
243         System.out.println(
244                 "$$$$$$$$$$$$$ END QueryResultContainer.dump $$$$$$$$$$$$$\n\n");
245     }
246
247     public void writeExternal(OIDObjectOutput out) throws IOException {
248         if (cq.isDefaultResult()) {
249             container.ensureDirect(data);
250         }
251         container.writeExternal(out);
252         if (allResults) {
253             out.writeByte(1);
254         } else {
255             out.writeByte(0);
256         }
257         out.writeBoolean(qFinished);
258
259         if (data == null) {
260             out.writeInt(-1);
261         } else {
262             out.writeInt(size);
263             if (!cq.isDefaultResult()) {
264                 out.writeBoolean(false);
265                 int[] typeCodes = cq.getResultTypeCodes();
266                 if (typeCodes == null || typeCodes.length == 0) {
267                     out.writeInt(0);
268                     for (int i = 0; i < size; i++) {
269                         out.writeObject(data[i]);
270                     }
271                 } else {
272                     out.writeInt(typeCodes.length);
273                     for (int i = 0; i < typeCodes.length; i++) {
274                         out.writeInt(typeCodes[i]);
275                     }
276                     if (typeCodes.length == 1) {
277                         int typeCode = typeCodes[0];
278                         for (int i = 0; i < size; i++) {
279                             Object JavaDoc o = data[i];
280                             if (typeCode == MDStatics.OID) {
281                                 OID oid = (OID)o;
282                                 out.write(oid);
283                             } else {
284                                 SerUtils.writeSimpleField(typeCode, out, o);
285                             }
286                         }
287                     } else {
288                         for (int i = 0; i < size; i++) {
289                             Object JavaDoc[] row = (Object JavaDoc[])data[i];
290                             for (int j = 0; j < row.length; j++) {
291                                 Object JavaDoc o = row[j];
292                                 if (typeCodes[j] == MDStatics.OID) {
293                                     OID oid = (OID)o;
294                                     out.write(oid);
295                                 } else {
296                                     SerUtils.writeSimpleField(typeCodes[j], out, o);
297                                 }
298                             }
299                         }
300                     }
301                 }
302             } else {
303                 out.writeBoolean(true);
304                 for (int i = 0; i < size; i++) {
305                     out.writeObject(data[i]);
306                 }
307             }
308         }
309     }
310
311     public void readExternal(OIDObjectInput in) throws IOException,
312             ClassNotFoundException JavaDoc {
313         container = new StatesReturned();
314         container.readExternal(in);
315         allResults = in.readByte() == 1;
316         qFinished = in.readBoolean();
317         size = in.readInt();
318         if (size < 0) {
319             data = null;
320             size = 0;
321         } else {
322             data = new Object JavaDoc[size];
323             if (!in.readBoolean()) {
324                 int tcLen = in.readInt();
325                 if (tcLen == 0) {
326                     for (int i = 0; i < size; i++) {
327                         data[i] = in.readObject();
328                     }
329                 } else {
330                     int[] typeCodes = new int[tcLen];
331                     for (int i = 0; i < typeCodes.length; i++) {
332                         typeCodes[i] = in.readInt();
333                     }
334                     if (typeCodes.length == 1) {
335                         int typeCode = typeCodes[0];
336                         for (int i = 0; i < size; i++) {
337                             if (typeCode == MDStatics.OID) {
338                                 data[i] = in.readOID();
339                             } else {
340                                 data[i] = SerUtils.readSimpleField(typeCode, in);
341                             }
342                         }
343                     } else {
344                         for (int i = 0; i < size; i++) {
345                             Object JavaDoc[] row = new Object JavaDoc[typeCodes.length];
346                             data[i] = row;
347                             for (int j = 0; j < row.length; j++) {
348                                 if (typeCodes[j] == MDStatics.OID) {
349                                     row[j] = in.readOID();
350                                 } else {
351                                     row[j] = SerUtils.readSimpleField(typeCodes[j], in);
352                                 }
353                             }
354                         }
355                     }
356                 }
357             } else {
358                 for (int i = 0; i < size; i++) {
359                     data[i] = in.readObject();
360                 }
361             }
362         }
363     }
364
365     /**
366      * Will this be null if there is no data?
367      */

368     public Object JavaDoc[] getDataArray() {
369         return data;
370     }
371 }
372
Popular Tags