1 2 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 33 public final class QueryResultIterator implements JDOListIterator { 34 35 private PMProxy pmProxy; 36 39 private boolean closed = false; 40 43 private boolean queryFinished = false; 44 47 private QueryResultWrapper qrsIF = null; 48 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 [] params, boolean doNotFlush) { 58 this.pmProxy = pmProxy; 59 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 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 realPM.addToCache(container.container); 116 container.addToQueryStack(stack); 118 } 119 if (container != null && container.qFinished) queryFinished = true; 120 if (container != null) container.reset(); 121 } 122 } 123 124 public Object 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 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 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 o) { 165 throw BindingSupportImpl.getInstance().unsupportedOperation(null); 166 } 167 168 public void add(Object o) { 169 throw BindingSupportImpl.getInstance().unsupportedOperation(null); 170 } 171 } 172 | Popular Tags |