List of usage examples for org.hibernate ScrollableResults isLast
boolean isLast();
From source file:com.ikon.module.db.stuff.IndexHelper.java
License:Open Source License
protected int doRebuildIndex() throws Exception { FullTextSession fullTextSession = (FullTextSession) entityManager.getDelegate(); fullTextSession.setFlushMode(org.hibernate.FlushMode.MANUAL); fullTextSession.setCacheMode(org.hibernate.CacheMode.IGNORE); fullTextSession.purgeAll(NodeDocumentVersion.class); fullTextSession.getSearchFactory().optimize(NodeDocumentVersion.class); String query = "select ndv from NodeDocumentVersion ndv"; ScrollableResults cursor = fullTextSession.createQuery(query).scroll(); cursor.last();//from w w w .j av a 2s .co m int count = cursor.getRowNumber() + 1; log.warn("Re-building Wine index for " + count + " objects."); if (count > 0) { int batchSize = 300; cursor.first(); // Reset to first result row int i = 0; while (true) { fullTextSession.index(cursor.get(0)); if (++i % batchSize == 0) { fullTextSession.flushToIndexes(); fullTextSession.clear(); // Clear persistence context for each batch log.info("Flushed index update " + i + " from Thread " + Thread.currentThread().getName()); } if (cursor.isLast()) { break; } cursor.next(); } } cursor.close(); fullTextSession.flushToIndexes(); fullTextSession.clear(); // Clear persistence context for each batch fullTextSession.getSearchFactory().optimize(NodeDocumentVersion.class); return count; }
From source file:org.gbif.portal.dao.DAOUtils.java
License:Open Source License
/** * //from w w w.ja va2 s .co m * @param resultsOutputter * @param session * @param sr * @param associationTraverser * @param batchSize * @throws IOException */ public static void processScrollableResults(final ResultsOutputter resultsOutputter, Session session, ScrollableResults sr, AssociationTraverser associationTraverser, int batchSize) throws IOException { //indicate end of resultset boolean eor = false; int batchNo = 0; do { if (logger.isDebugEnabled()) { logger.debug("Running batch: " + (batchNo++)); } if (associationTraverser != null) associationTraverser.batchPreprocess(batchSize, sr, session); //process in batches for (int i = 0; i < batchSize && !eor; i++) { Object record = sr.get(); Map beanMap = null; //assemble all required model objects for rendering a single row if (associationTraverser != null) { beanMap = associationTraverser.traverse(record, session); } else { beanMap = new HashMap<String, Object>(); if (record != null && record instanceof Object[] && ((Object[]) record).length > 0) { beanMap.put("record", ((Object[]) record)[0]); } else { beanMap.put("record", record); } } //write out result resultsOutputter.write(beanMap); if (beanMap != null) { //evict from the session to keep memory footprint down for (Object recordElement : beanMap.entrySet()) { session.evict(recordElement); } beanMap = null; } //check to see if this is the last element in resultset if (sr.isLast()) { eor = true; } else { sr.next(); } } //post process if (associationTraverser != null) associationTraverser.batchPostprocess(batchSize, sr, session); //flush between batches - to remove objects from the session session.flush(); session.clear(); } while (!eor); }
From source file:org.gbif.portal.dao.occurrence.OccurrenceAssociationTraverser.java
License:Open Source License
/** * @see org.gbif.portal.dao.AssociationTraverser#batchPreprocess(int, org.hibernate.ScrollableResults, org.hibernate.Session) *///from ww w .j a v a 2 s . c o m public void batchPreprocess(int batchSize, ScrollableResults scrollableResults, Session session) { if (!retrieveIdentifiers) return; if (logger.isDebugEnabled()) logger.debug("Current row number:" + scrollableResults.getRowNumber()); List<Long> occurrenceRecordIds = new ArrayList<Long>(); boolean eor = false; int numberScrolled = 0; for (numberScrolled = 0; numberScrolled < batchSize - 1 && !eor; numberScrolled++) { //retrieve the id Long recordId = (Long) scrollableResults.get(0); occurrenceRecordIds.add(recordId); if (scrollableResults.isLast()) { eor = true; numberScrolled--; } else { scrollableResults.next(); } } scrollableResults.scroll(-numberScrolled); if (logger.isDebugEnabled()) { logger.debug("Number scrolled through: " + numberScrolled); logger.debug("Scrolled back to: " + scrollableResults.getRowNumber()); } //retrieve image records for this batch - and process into Map - List<ORImage> orImageList = imageRecordDAO.getImageRecordsForOccurrenceRecords(occurrenceRecordIds); this.orImageUrlMap = new HashMap<Long, String>(); for (ORImage orImage : orImageList) { //only storing the first image url we find if (this.orImageUrlMap.get(orImage.getOccurrenceRecordId()) == null) { this.orImageUrlMap.put(orImage.getOccurrenceRecordId(), orImage.getUrl()); } session.evict(orImage); } if (logger.isDebugEnabled()) logger.debug("Number of images found for batch: " + this.orImageUrlMap.size()); //retrieve type status for this batch List<TypeStatus> typeStatusList = typificationRecordDAO .getTypeStatusForOccurrenceRecords(occurrenceRecordIds); this.typeStatusMap = new HashMap<Long, String>(); for (TypeStatus typeStatus : typeStatusList) { //only storing the first type status we find if (this.typeStatusMap.get(typeStatus.getOccurrenceRecordId()) == null) { this.typeStatusMap.put(typeStatus.getOccurrenceRecordId(), typeStatus.getTypeStatus()); } session.evict(typeStatus); } if (logger.isDebugEnabled()) logger.debug("Number of type status found for batch: " + this.typeStatusMap.size()); //retrieve identifiers for this batch List<IdentifierRecord> identifierList = identifierRecordDAO .getIdentifierRecordsForOccurrenceRecords(occurrenceRecordIds); this.identifiersMap = new HashMap<Long, List<IdentifierRecord>>(); for (IdentifierRecord ir : identifierList) { List<IdentifierRecord> irs = this.identifiersMap.get(ir.getOccurrenceRecordId()); if (irs == null) { irs = new ArrayList<IdentifierRecord>(); irs.add(ir); this.identifiersMap.put(ir.getOccurrenceRecordId(), irs); } else { irs.add(ir); } session.evict(ir); } if (logger.isDebugEnabled()) logger.debug("Number of identifiers found for batch: " + this.identifiersMap.size()); }
From source file:org.jasig.ssp.util.csvwriter.AbstractCsvWriterHelper.java
License:Apache License
public void write(final ScrollableResults model, final Long maxCount) throws IOException { doWrite(new Iterator<T>() { @Override//ww w .j ava 2 s . c o m public boolean hasNext() { return !(model.isLast()); } @Override public T next() { if (!(model.next())) { throw new NoSuchElementException(); // per Iterator interface } return (T) model.get()[0]; } @Override public void remove() { throw new UnsupportedOperationException(); } }, maxCount); }
From source file:org.openbravo.costing.LandedCostDistributionByAmount.java
License:Open Source License
@Override public void distributeAmount(LandedCostCost lcCost, boolean isMatching) { // Calculate total amount of all receipt lines assigned to the landed cost. lcCost = (LandedCostCost) OBDal.getInstance().getProxy(LandedCostCost.ENTITY_NAME, lcCost.getId()); LandedCost landedCost = lcCost.getLandedCost(); // Get the currency of the Landed Cost Cost String strCurId = lcCost.getCurrency().getId(); String strOrgId = landedCost.getOrganization().getId(); Date dateReference = landedCost.getReferenceDate(); int precission = lcCost.getCurrency().getCostingPrecision().intValue(); BigDecimal baseAmt;/*from w ww . j a v a 2 s.c o m*/ if (isMatching) { baseAmt = lcCost.getMatchingAmount().subtract(lcCost.getAmount()); } else { baseAmt = lcCost.getAmount(); } BigDecimal totalAmt = BigDecimal.ZERO; // Loop to get all receipts amounts and calculate the total. OBCriteria<LCReceipt> critLCRL = OBDal.getInstance().createCriteria(LCReceipt.class); critLCRL.add(Restrictions.eq(LCReceipt.PROPERTY_LANDEDCOST, landedCost)); ScrollableResults receiptCosts = getReceiptCosts(landedCost, false); int i = 0; try { while (receiptCosts.next()) { String strTrxCur = (String) receiptCosts.get()[2]; BigDecimal trxAmt = (BigDecimal) receiptCosts.get()[3]; if (!strTrxCur.equals(strCurId)) { trxAmt = getConvertedAmount(trxAmt, strTrxCur, strCurId, dateReference, strOrgId); } totalAmt = totalAmt.add(trxAmt); if (i % 100 == 0) { OBDal.getInstance().flush(); OBDal.getInstance().getSession().clear(); } i++; } } finally { receiptCosts.close(); } BigDecimal pendingAmt = baseAmt; // Loop to calculate the corresponding adjustment amount for each receipt line. receiptCosts = getReceiptCosts(landedCost, true); i = 0; while (receiptCosts.next()) { ShipmentInOutLine receiptline = OBDal.getInstance().get(ShipmentInOutLine.class, receiptCosts.get()[1]); String strTrxCurId = (String) receiptCosts.get()[2]; BigDecimal trxAmt = (BigDecimal) receiptCosts.get()[3]; if (!strTrxCurId.equals(strCurId)) { trxAmt = getConvertedAmount(trxAmt, strTrxCurId, strCurId, dateReference, strOrgId); } BigDecimal receiptAmt = BigDecimal.ZERO; if (receiptCosts.isLast()) { // Insert pending amount on receipt with higher cost to avoid rounding issues. receiptAmt = pendingAmt; } else { receiptAmt = baseAmt.multiply(trxAmt).divide(totalAmt, precission, RoundingMode.HALF_UP); } pendingAmt = pendingAmt.subtract(receiptAmt); LCReceipt lcrl = (LCReceipt) OBDal.getInstance().getProxy(LCReceipt.ENTITY_NAME, receiptCosts.get()[0]); LCReceiptLineAmt lcrla = OBProvider.getInstance().get(LCReceiptLineAmt.class); lcrla.setLandedCostCost( (LandedCostCost) OBDal.getInstance().getProxy(LandedCostCost.ENTITY_NAME, lcCost.getId())); lcCost = (LandedCostCost) OBDal.getInstance().getProxy(LandedCostCost.ENTITY_NAME, lcCost.getId()); lcrla.setLandedCostReceipt(lcrl); lcrla.setGoodsShipmentLine(receiptline); lcrla.setMatchingAdjustment(isMatching); lcrla.setAmount(receiptAmt); lcrla.setOrganization(lcCost.getOrganization()); OBDal.getInstance().save(lcrla); if (i % 100 == 0) { OBDal.getInstance().flush(); OBDal.getInstance().getSession().clear(); } i++; } }