1 2 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 ; 27 import java.util.ArrayList ; 28 29 32 public final class QueryResultContainer implements FastExternalizable { 33 34 public static final Object [] EMPTY_ARRAY = new Object [0]; 35 38 private CompiledQuery cq; 39 40 public StatesReturned container; 41 45 public boolean qFinished; 46 49 private Object [] 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 72 public void addRow(Object 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 [DEFAULT_SIZE]; 84 } else if (size == data.length) { 85 Object [] t = new Object [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 103 public void reset() { 104 data = null; 105 container.clear(); 106 qFinished = false; 107 size = 0; 108 allResults = false; 109 } 110 111 114 public void init(CompiledQuery cq, int capacity) { 115 reset(); 116 this.cq = cq; 117 if (capacity < DEFAULT_SIZE) { 118 data = new Object [capacity]; 119 } else { 120 data = new Object [DEFAULT_SIZE]; 121 } 122 } 123 124 128 public void init(CompiledQuery cq) { 129 reset(); 130 this.cq = cq; 131 } 132 133 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 149 public Object [] toResolvedObject(PersistenceManager pm) { 150 int size = this.size; 151 if (size == 0) { 152 return EMPTY_ARRAY; 153 } 154 Object [] 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 getUnique() { 166 if (data == null) return null; 167 else return data[0]; 168 } 169 170 public Object get(int index) { 171 return data[index]; 172 } 173 174 177 public void resolveAndAddTo(List 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 188 public void addResultsTo(CachedQueryResult cacheResults, 189 boolean copy) { 190 int size = this.size; 191 if (cacheResults.results == null) { 192 cacheResults.results = new ArrayList (size); 193 } 194 195 if (copy 196 197 ) { 198 for (int i = 0; i < size; i++) { 200 Object [] val = (Object [])data[i]; 201 Object [] cop = new Object [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 219 public void fillFrom(CachedQueryResult qCacheContainer) { 220 final ArrayList lData = qCacheContainer.results; 221 final int n = size = lData.size(); 222 if (data == null || n > data.length) { 223 data = new Object [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 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 [] row = (Object [])data[i]; 290 for (int j = 0; j < row.length; j++) { 291 Object 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 { 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 [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 [] row = new Object [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 368 public Object [] getDataArray() { 369 return data; 370 } 371 } 372 | Popular Tags |