Changeset 7123
- Timestamp:
- Apr 19, 2016, 2:14:44 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/core/net/sf/basedb/core/AnnotationBatcher.java
r7122 r7123 78 78 */ 79 79 public class AnnotationBatcher 80 extends AbstractBatcher 80 81 { 81 private final DbControl dc; 82 83 private static final org.slf4j.Logger log = 84 org.slf4j.LoggerFactory.getLogger("net.sf.basedb.core.AnnotationBatcher"); 85 private final boolean isDebugEnabled = log.isDebugEnabled(); 86 82 87 private final Item itemType; 83 88 … … 113 118 private final UpdateBatcher updateItemTable; 114 119 115 // All update batchers (to make it easy to flush all at once) 116 private final List<UpdateBatcher> allBatchers; 120 // All batchers (to make it easy to flush/close all at once) 121 private final List<UpdateBatcher> allUpdateBatchers; 122 private final List<InsertBatcher> allInsertBatchers; 117 123 118 124 private final UpdateBatcher deleteFromAnnotations; … … 127 133 private Annotatable currentItem; 128 134 private int currentAnnotationSetId; 135 private boolean deleteSnapshotIfModifed; 129 136 130 137 // Lists for temporary keeping track of value IDs for … … 136 143 private Map<Integer, CurrentAnnotationInfo> currentInfo; 137 144 145 // List of AnnotationSet IDs that should be deleted 146 // from the snapshot manager 147 private final List<Integer> snapshotsToDelete; 148 138 149 /** 139 150 Create a new batcher instance. … … 150 161 } 151 162 152 this.dc = dc;153 163 this.itemType = itemType; 154 164 this.batchDate = new Date(); … … 174 184 this.currentInfo = new HashMap<Integer, CurrentAnnotationInfo>(); 175 185 176 this.allBatchers = new ArrayList<UpdateBatcher>(); 186 this.allUpdateBatchers = new ArrayList<UpdateBatcher>(); 187 this.allInsertBatchers = new ArrayList<InsertBatcher>(); 188 this.snapshotsToDelete = new ArrayList<Integer>(); 177 189 try 178 190 { … … 272 284 throw new BaseException(ex); 273 285 } 274 } 286 287 setDbControl(dc); 288 } 289 290 291 /* 292 From the Batcher interface 293 -------------------------- 294 */ 295 296 /** 297 Flush the batchers. 298 */ 299 @Override 300 public void flush() 301 { 302 System.out.println(new Date() + ": " + numItems + " done; flushing batchers"); 303 String currentSql = null; 304 try 305 { 306 for (UpdateBatcher b : allUpdateBatchers) 307 { 308 currentSql = b.getSql(); 309 b.flush(); 310 } 311 } 312 catch (SQLException ex) 313 { 314 log.error(currentSql, ex); 315 LogUtil.logSQLExceptionChain(log, ex); 316 throw new BaseException(ex); 317 } 318 } 319 // ------------------------ 320 321 /* 322 From the AbstractBatcher class 323 ------------------------------ 324 */ 325 @Override 326 protected void onBeforeClose() 327 { 328 System.out.println(new Date() + "; done!! " + numItems + " items."); 329 for (UpdateBatcher b : allUpdateBatchers) 330 { 331 b.close(); 332 } 333 for (InsertBatcher b : allInsertBatchers) 334 { 335 b.close(); 336 } 337 for (Integer id : snapshotsToDelete) 338 { 339 SnapshotManager.removeSnapshot(currentAnnotationSetId); 340 } 341 } 342 // --------------------------- 275 343 276 344 private InsertBatcher createInsertBatcher(Connection c, String sql, int... types) … … 279 347 sql = sql.replace('[', dialect.openQuote()).replace(']', dialect.closeQuote()); 280 348 InsertBatcher b = new InsertBatcher(c, sql, types); 349 allInsertBatchers.add(b); 281 350 return b; 282 351 } … … 287 356 sql = sql.replace('[', dialect.openQuote()).replace(']', dialect.closeQuote()); 288 357 UpdateBatcher b = new UpdateBatcher(c, sql, types); 289 all Batchers.add(b);358 allUpdateBatchers.add(b); 290 359 return b; 291 360 } 292 361 293 /**294 Flush the batchers.295 */296 private void flushBatchers()297 {298 try299 {300 for (UpdateBatcher b : allBatchers)301 {302 b.flush();303 }304 }305 catch (SQLException ex)306 {307 throw new BaseException(ex);308 }309 }310 362 311 363 /** … … 318 370 public void addAnnotationTypes(Collection<AnnotationType> types) 319 371 { 372 if (isClosed()) throw new ConnectionClosedException(); 320 373 if (currentItem != null) 321 374 { … … 327 380 { 328 381 if (currentInfo.containsKey(at.getId())) continue; // Already added 382 383 log.debug("Adding annotation type: " + at); 329 384 330 385 at.checkPermission(Permission.USE); … … 354 409 public void setCurrentItem(Annotatable item) 355 410 { 411 if (isClosed()) throw new ConnectionClosedException(); 356 412 if (currentInfo.size() == 0) 357 413 { … … 365 421 item.checkPermission(Permission.WRITE); 366 422 367 if (numItems > 0 && numItems % 100 == 0) 368 { 369 System.out.println(new Date() + ": " + numItems + " done; flushing batchers"); 370 flushBatchers(); 423 log.debug("Setting current item: " + item); 424 425 if (numItems > 0 && numItems % getBatchSize() == 0) 426 { 427 flush(); 371 428 } 372 429 … … 375 432 this.currentItem = item; 376 433 this.currentAnnotationSetId = 0; 434 this.deleteSnapshotIfModifed = false; 377 435 378 436 // Read current values … … 381 439 currentAnnotationSetId = ((AnnotatableData)((BasicItem)item).getData()).getAnnotationSet().getId(); 382 440 loadAnnotationInfo.setInteger("annotationSet", currentAnnotationSetId); 441 deleteSnapshotIfModifed = true; 442 443 if (isDebugEnabled) 444 { 445 log.debug("Loading annotations for current item: " + item); 446 } 383 447 384 448 // Maps value_id -> Annotation info 385 449 Map<Integer, CurrentAnnotationInfo> tmp = new HashMap<Integer, CurrentAnnotationInfo>(); 386 450 List<Object[]> list = HibernateUtil.loadList(Object[].class, loadAnnotationInfo, null); 451 452 if (isDebugEnabled) 453 { 454 log.debug("Found " + list.size() + " annotations for current item: " + item); 455 } 387 456 388 457 for (Object[] data : list) … … 394 463 // This annotation type is not among the specified annotation types 395 464 continue; // with the next annotation 465 } 466 467 if (isDebugEnabled) 468 { 469 log.trace("Found annotation: AnnotationType.id=" + info.annotationTypeId); 396 470 } 397 471 … … 419 493 if (valueIds[typeIndex].isEmpty()) continue; // No annotation with this value type 420 494 495 if (isDebugEnabled) 496 { 497 log.debug("Loading " + t.name() + " values: " + valueIds[typeIndex]); 498 } 499 421 500 org.hibernate.Query query = loadAnnotationValues[typeIndex]; 422 501 query.setParameterList("listOfIds", valueIds[typeIndex], Type.INT.getTypeWrapper().getHibernateType()); 423 502 424 503 list = HibernateUtil.loadList(Object[].class, query, null); 504 if (isDebugEnabled) 505 { 506 log.debug("Found " + list.size() + " values"); 507 } 425 508 for (Object[] data : list) 426 509 { 427 510 Integer valueId = (Integer)data[0]; 428 511 tmp.get(valueId).values.add(data[1]); 512 if (isDebugEnabled) log.trace(valueId + ": "+data[1]); 429 513 } 430 514 … … 479 563 throw new IllegalStateException("Not allowed to call this method with the same annotation more than once: " + annotationType); 480 564 } 565 566 if (isDebugEnabled) 567 { 568 log.trace("Setting values for annotation type " + annotationType + ": " + values); 569 } 570 481 571 info.hasBeenUsed = true; 482 572 info.itemId = currentItem.getId(); … … 489 579 if (infoIsForCurrentItem) 490 580 { 581 if (isDebugEnabled) 582 { 583 log.trace("Deleting " + annotationType + " from current item " + currentItem); 584 } 585 491 586 // Delete the annotation 492 587 changeType = Change.DELETED; … … 540 635 if (!EqualsHelper.invariantEquals(convertedValues, info.values)) 541 636 { 637 if (isDebugEnabled) 638 { 639 log.trace("Updating " + annotationType + " for current item " + currentItem); 640 } 641 542 642 // Update 543 643 changeType = Change.UPDATED; … … 555 655 updateAnnotations.addToBatch(info.version+1, unit != null ? unit.getId() : info.unitId, batchDate, info.annotationId, info.version); 556 656 } 657 else 658 { 659 if (isDebugEnabled) 660 { 661 log.trace("No change " + annotationType + " for current item " + currentItem); 662 } 663 } 557 664 } 558 665 else 559 666 { 667 if (isDebugEnabled) 668 { 669 log.trace("Creating " + annotationType + " for current item " + currentItem); 670 } 671 560 672 // Create a new annotation 561 673 changeType = Change.ADDED; … … 589 701 } 590 702 591 if (changeType != Change.NO_CHANGE) 592 { 593 SnapshotManager.removeSnapshot(currentAnnotationSetId); 703 // Store the annotation set ID so that we can tell 704 // the SnapshotManager to clear the cache when this 705 // batcher is closed 706 if (changeType != Change.NO_CHANGE && deleteSnapshotIfModifed) 707 { 708 deleteSnapshotIfModifed = false; 709 snapshotsToDelete.add(currentAnnotationSetId); 594 710 } 595 711 596 712 return changeType; 597 }598 599 public void close()600 {601 System.out.println(new Date() + "; done!! " + numItems + " items.");602 flushBatchers();603 713 } 604 714 … … 690 800 throws SQLException 691 801 { 802 log.debug(sql); 692 803 this.sql = sql; 693 804 this.statement = c.prepareStatement(sql, new String[] { "id" } ); … … 695 806 } 696 807 808 /** 809 The SQL statement this batcher is executing. 810 */ 811 String getSql() 812 { 813 return sql; 814 } 815 697 816 /** 698 817 Execute an insert SQL statement with the given parameter … … 721 840 return keys.getInt(1); 722 841 } 842 843 /** 844 Close the batcher. 845 */ 846 void close() 847 { 848 try 849 { 850 statement.close(); 851 } 852 catch (SQLException ex) 853 { 854 log.error(ex.getMessage(), ex); 855 LogUtil.logSQLExceptionChain(log, ex); 856 } 857 } 858 723 859 } 724 860 … … 741 877 throws SQLException 742 878 { 879 log.debug(sql); 743 880 this.sql = sql; 744 881 this.statement = c.prepareStatement(sql); … … 747 884 748 885 /** 886 The SQL statement this batcher is executing. 887 */ 888 String getSql() 889 { 890 return sql; 891 } 892 893 /** 749 894 Add a new entry to the batch. The number of values must match the number of 750 895 parameter types. … … 780 925 } 781 926 } 927 928 /** 929 Close the batcher. 930 */ 931 void close() 932 { 933 try 934 { 935 statement.close(); 936 } 937 catch (SQLException ex) 938 { 939 log.error(ex.getMessage(), ex); 940 LogUtil.logSQLExceptionChain(log, ex); 941 } 942 } 782 943 } 783 944
Note: See TracChangeset
for help on using the changeset viewer.