KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdo > QueryResultIterator


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.jdo;
13
14 import com.versant.core.common.Debug;
15 import com.versant.core.common.QueryResultContainer;
16 import com.versant.core.common.Stack;
17 import com.versant.core.server.CompiledQuery;
18 import com.versant.core.server.QueryResultWrapper;
19
20 import com.versant.core.common.BindingSupportImpl;
21 import com.versant.core.storagemanager.ExecuteQueryReturn;
22
23 /**
24  * Forward only Iterator for query results. Data is fetched lazily in batches
25  * as it is required. Instances of this class are created when iterator()
26  * is called on the results of a JDOQL query. This supports multithreaded
27  * PM access by synchronizing some operations on the PM proxy.
28  *
29  * @see com.versant.core.jdo.ForwardQueryResult
30  * @see RandomAccessQueryResult
31  * @see QueryDetails#setResultBatchSize
32  */

33 public final class QueryResultIterator implements JDOListIterator {
34
35     private PMProxy pmProxy;
36     /**
37      * Indicates that the iterator is closed.
38      */

39     private boolean closed = false;
40     /**
41      * Indicates that all results from the jdbc query has been processed.
42      */

43     private boolean queryFinished = false;
44     /**
45      * This is used as a token to identify the serverside queryResult.
46      */

47     private QueryResultWrapper qrsIF = null;
48     /**
49      * A stack where all retrieved results is kept till it is iterated past.
50      */

51     public final Stack stack = new Stack();
52
53     public int toSkip;
54     private int nextIndex;
55
56     public QueryResultIterator(PMProxy pmProxy, QueryDetails queryDetails,
57             CompiledQuery compiledQuery, Object JavaDoc[] params, boolean doNotFlush) {
58         this.pmProxy = pmProxy;
59         // no need to synchronize on pmProxy here as we are created inside
60
// synchronized block
61
if (!doNotFlush && !queryDetails.isIgnoreCache()) {
62             pmProxy.flushIfDepOn(compiledQuery.getEvictionClassBits());
63         }
64         qrsIF = pmProxy.executeQuery(compiledQuery, params);
65     }
66
67     public QueryResultIterator(PMProxy pmProxy, QueryResultWrapper qrsw,
68             QueryResultContainer container, int index) {
69         this.pmProxy = pmProxy;
70         qrsIF = qrsw;
71         nextIndex = index;
72         if (container != null) container.addToQueryStack(stack);
73         if (container != null && container.qFinished) queryFinished = true;
74     }
75
76     public int nextIndex() {
77         return nextIndex;
78     }
79
80     public boolean hasNext() {
81         if (closed) return false;
82         if (stack.size() > 0) return true;
83         addToQueryStack();
84         return (stack.size() > 0);
85     }
86
87     /**
88      * Try and add more results to the query stack. This also triggers
89      * processing of the ReferenceQuery for the PMs local cache. This stops
90      * long running reporting type queries from leaking SoftReferences.
91      */

92     private void addToQueryStack() {
93         final VersantPersistenceManagerImp realPM = pmProxy.getRealPM();
94         synchronized (pmProxy) {
95             realPM.processLocalCacheReferenceQueue();
96             QueryResultContainer container = null;
97             if (!queryFinished && stack.isEmpty()) {
98                 container = realPM.getStorageManager().fetchNextQueryResult(
99                         realPM, ((ExecuteQueryReturn)qrsIF).getRunningQuery(),
100                         toSkip);
101                 toSkip = 0;
102
103                 if (container == null) {
104                     closed = true;
105                     return;
106                 }
107
108                 if (container.qFinished) queryFinished = true;
109                 if (Debug.DEBUG) {
110                     Debug.OUT.println("############ amount recieved = " + container.size()
111                             + " for queryId = " + qrsIF + " (inner container size " +
112                             container.container.size() + ")");
113                 }
114                 //add to managed cache.
115
realPM.addToCache(container.container);
116                 //add the results to the stack.
117
container.addToQueryStack(stack);
118             }
119             if (container != null && container.qFinished) queryFinished = true;
120             if (container != null) container.reset();
121         }
122     }
123
124     public Object JavaDoc next() {
125         if (!hasNext()) throw BindingSupportImpl.getInstance().noSuchElement("");
126         nextIndex++;
127         return QueryResultBase.resolveRow(stack.pop(), pmProxy);
128     }
129
130     public final void close() {
131         if (closed) return;
132         try {
133             stack.close();
134             if (qrsIF != null) {
135                 synchronized (pmProxy) {
136                     pmProxy.getRealPM().getStorageManager().closeQuery(
137                             ((ExecuteQueryReturn)qrsIF).getRunningQuery());
138                 }
139                 qrsIF = null;
140             }
141         } catch (Exception JavaDoc e) {
142             throw BindingSupportImpl.getInstance().internal(e.getMessage(), e);
143         } finally {
144             closed = true;
145         }
146     }
147
148     public final void remove() {
149         throw BindingSupportImpl.getInstance().unsupported("Not allowed to modify");
150     }
151
152     public boolean hasPrevious() {
153         throw BindingSupportImpl.getInstance().unsupportedOperation(null);
154     }
155
156     public Object JavaDoc previous() {
157         throw BindingSupportImpl.getInstance().unsupportedOperation(null);
158     }
159
160     public int previousIndex() {
161         throw BindingSupportImpl.getInstance().unsupportedOperation(null);
162     }
163
164     public void set(Object JavaDoc o) {
165         throw BindingSupportImpl.getInstance().unsupportedOperation(null);
166     }
167
168     public void add(Object JavaDoc o) {
169         throw BindingSupportImpl.getInstance().unsupportedOperation(null);
170     }
171 }
172
Popular Tags