1 20 package org.enhydra.dods.cache; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.HashMap ; 25 import org.enhydra.dods.cache.Condition; 26 import org.enhydra.dods.cache.QueryCacheItem; 27 import com.lutris.appserver.server.sql.DatabaseManagerException; 28 import com.lutris.dods.builder.generator.dataobject.GenericDO; 29 import com.lutris.appserver.server.sql.CoreDataStruct; 30 31 38 public class QueryCacheItemImpl implements QueryCacheItem { 39 40 43 protected String queryId; 44 45 49 protected HashMap OIds; 50 51 54 protected int resultNum = 0; 55 56 59 protected boolean completeRes = false; 60 61 64 protected ListItem head; 65 66 69 protected ListItem tail; 70 71 76 protected boolean modifiedQuery = false; 77 78 81 protected int time; 82 83 87 protected ArrayList conds; 88 89 92 protected String originDatabase; 93 94 99 public QueryCacheItemImpl(String origDb) { 100 OIds = new HashMap (); 101 resultNum = 0; 102 completeRes = false; 103 modifiedQuery = false; 104 this.time = 0; 105 conds = new ArrayList (); 106 originDatabase = origDb; 107 } 108 109 118 public QueryCacheItemImpl(String qId, HashMap OIds, int time, ArrayList conditions, String origDb) { 119 queryId = qId; 120 this.OIds = OIds; 121 resultNum = 0; 122 completeRes = false; 123 modifiedQuery = false; 124 this.time = time; 125 conds = conditions; 126 originDatabase = origDb; 127 } 128 129 135 public String getQueryId() { 136 return queryId; 137 } 138 139 145 public void setQueryId(String queryId) { 146 this.queryId = queryId; 147 } 148 149 154 public Collection getOIds() { 155 ArrayList list = new ArrayList (); 156 ListItem iter = head; 157 158 while (iter != null) { 159 list.add(iter.handle); 160 iter = iter.next; 161 } 162 return list; 163 } 164 165 170 public int getResultNum() { 171 return resultNum; 172 } 173 174 179 public boolean isCompleteResult() { 180 return completeRes; 181 } 182 183 190 public void setCompleteResult(boolean newCompleteRes) { 191 completeRes = newCompleteRes; 192 } 193 194 201 public boolean isModifiedQuery() { 202 return modifiedQuery; 203 } 204 205 211 public void setModifiedQuery(boolean mod) { 212 modifiedQuery = mod; 213 } 214 215 220 public int getTime() { 221 return time; 222 } 223 224 229 public void setTime(int time) { 230 this.time = time; 231 } 232 233 238 public ArrayList getConds() { 239 return conds; 240 } 241 242 247 public void setConds(ArrayList conds) { 248 this.conds = conds; 249 } 250 251 256 public void addCond(Condition cond) { 257 conds.add(cond); 258 } 259 260 266 public String getOriginDatabase() { 267 return get_OriginDatabase(); 268 } 269 270 275 public String get_OriginDatabase() { 276 return originDatabase; 277 } 278 279 286 public boolean checkConditions(GenericDO obj) { 287 for (int i = 0; i < conds.size(); i++) { 288 if (!obj.compareCond((Condition) conds.get(i))) { 289 return false; 290 } 291 } 292 return true; 293 } 294 295 303 public boolean checkConditions(CoreDataStruct obj) { 304 for (int i = 0; i < conds.size(); i++) { 305 if (!obj.compareCond((Condition) conds.get(i))) { 306 return false; 307 } 308 } 309 return true; 310 } 311 312 318 public void update(GenericDO obj) { 319 if (obj.get_OriginDatabase().equals(originDatabase)) { 320 try { 321 String key = obj.get_Handle(); 322 323 if (checkConditions(obj)) { 324 ListItem tmp = null; 325 326 tmp = (ListItem) OIds.get(key); 327 if (tmp == null) { 328 addHandle(key); 329 } 330 } else { 331 removeHandle(key); 332 } 333 } catch (DatabaseManagerException e) {} 334 } 335 } 336 337 344 public void update(CoreDataStruct obj) { 345 if (obj.get_Database().equals(originDatabase)) { 346 try { 347 String key = obj.get_Handle(); 348 349 if (checkConditions(obj)) { 350 ListItem tmp = null; 351 352 tmp = (ListItem) OIds.get(key); 353 if (tmp == null) { 354 addHandle(key); 355 } 356 } else { 357 removeHandle(key); 358 } 359 } catch (DatabaseManagerException e) {} 360 } 361 } 362 363 368 public void delete(GenericDO obj) { 369 if (obj.get_OriginDatabase().equals(originDatabase)) { 370 try { 371 String key = null; 372 373 key = obj.get_Handle(); 374 if (key != null) { 375 removeHandle(key); 376 } 377 } catch (DatabaseManagerException e) {} 378 } 379 } 380 381 386 public void delete(CoreDataStruct obj) { 387 if (obj.get_Database().equals(originDatabase)) { 388 try { 389 String key = null; 390 391 key = obj.get_Handle(); 392 if (key != null) { 393 removeHandle(key); 394 } 395 } catch (DatabaseManagerException e) {} 396 } 397 } 398 399 404 public void add(GenericDO obj) { 405 if (obj.get_OriginDatabase().equals(originDatabase)) { 406 try { 407 String key = null; 408 409 key = obj.get_Handle(); 410 if (key != null) { 411 addHandle(key); 412 } 413 } catch (DatabaseManagerException e) {} 414 } 415 } 416 417 422 public void add(CoreDataStruct obj) { 423 if (obj != null && obj.get_Database() != null) { 424 if (obj.get_Database().equals(originDatabase)) { 425 try { 426 427 String key = null; 428 429 key = obj.get_Handle(); 430 if (key != null) { 431 addHandle(key); 432 } 433 } catch (DatabaseManagerException e) {} 434 } 435 } 436 } 437 438 442 public String toString() { 443 StringBuffer ret = new StringBuffer (); 444 GenericDO DO = null; 445 446 ret.append("\n QueryCacheItemImpl: "); 447 ret.append("\n queryId: " + queryId); 448 ret.append("\n OIds : " + OIds); 449 if (OIds != null) { 450 for (ListItem iter = head; iter != null; iter = iter.next) { 451 ret.append(" " + iter.handle); 452 } 453 } 454 ret.append("\n time : " + time); 455 ret.append("\n conds : " + conds); 456 ret.append("\n originDatabase : " + originDatabase); 457 return ret.toString(); 458 } 459 460 protected void addHandle(String handle) { 461 ListItem item = new ListItem(handle); 462 463 item.prev = tail; 464 if (tail != null) { 465 tail.next = item; 466 } 467 if (head == null) { 468 head = item; 469 } 470 tail = item; 471 ListItem value = (ListItem) OIds.get(handle); 472 473 if (value == null) { 474 OIds.put(handle, item); 475 } else { 476 item.sameNext = value; 477 OIds.put(handle, item); 478 } 479 resultNum++; 480 } 481 482 protected void removeHandle(String handle) { 483 ListItem curr = (ListItem) OIds.get(handle); 484 485 if (curr != null) { 486 if (head == curr) { 487 head = curr.next; 488 } 489 if (tail == curr) { 490 tail = curr.prev; 491 } 492 } 493 ListItem temp; 494 495 while (curr != null) { 496 temp = curr; 497 curr = curr.unlink().sameNext; 498 temp.sameNext = null; 499 resultNum--; 500 } 501 } 502 static class ListItem { 503 public String handle; 504 public ListItem prev = null; 505 public ListItem next = null; 506 public ListItem sameNext = null; 507 ListItem() {} 508 509 ListItem(String hnd) { 510 handle = hnd; 511 } 512 513 ListItem unlink() { 514 if (prev != null) { 515 prev.next = next; 516 } 517 if (next != null) { 518 next.prev = prev; 519 } 520 return this; 521 } 522 } 523 } 524 | Popular Tags |