List of usage examples for java.util HashSet remove
public boolean remove(Object o)
From source file:org.apache.hadoop.hbase.master.procedure.TestMasterProcedureScheduler.java
/** * Verify that "write" operations for a single table are serialized, * but different tables can be executed in parallel. *//*from w w w . ja v a 2 s .c o m*/ @Test(timeout = 90000) public void testConcurrentWriteOps() throws Exception { final TestTableProcSet procSet = new TestTableProcSet(queue); final int NUM_ITEMS = 10; final int NUM_TABLES = 4; final AtomicInteger opsCount = new AtomicInteger(0); for (int i = 0; i < NUM_TABLES; ++i) { TableName tableName = TableName.valueOf(String.format("testtb-%04d", i)); for (int j = 1; j < NUM_ITEMS; ++j) { procSet.addBack(new TestTableProcedure(i * 100 + j, tableName, TableProcedureInterface.TableOperationType.EDIT)); opsCount.incrementAndGet(); } } assertEquals(opsCount.get(), queue.size()); final Thread[] threads = new Thread[NUM_TABLES * 2]; final HashSet<TableName> concurrentTables = new HashSet<TableName>(); final ArrayList<String> failures = new ArrayList<String>(); final AtomicInteger concurrentCount = new AtomicInteger(0); for (int i = 0; i < threads.length; ++i) { threads[i] = new Thread() { @Override public void run() { while (opsCount.get() > 0) { try { Procedure proc = procSet.acquire(); if (proc == null) { queue.signalAll(); if (opsCount.get() > 0) { continue; } break; } TableName tableId = procSet.getTableName(proc); synchronized (concurrentTables) { assertTrue("unexpected concurrency on " + tableId, concurrentTables.add(tableId)); } assertTrue(opsCount.decrementAndGet() >= 0); try { long procId = proc.getProcId(); int concurrent = concurrentCount.incrementAndGet(); assertTrue("inc-concurrent=" + concurrent + " 1 <= concurrent <= " + NUM_TABLES, concurrent >= 1 && concurrent <= NUM_TABLES); LOG.debug("[S] tableId=" + tableId + " procId=" + procId + " concurrent=" + concurrent); Thread.sleep(2000); concurrent = concurrentCount.decrementAndGet(); LOG.debug("[E] tableId=" + tableId + " procId=" + procId + " concurrent=" + concurrent); assertTrue("dec-concurrent=" + concurrent, concurrent < NUM_TABLES); } finally { synchronized (concurrentTables) { assertTrue(concurrentTables.remove(tableId)); } procSet.release(proc); } } catch (Throwable e) { LOG.error("Failed " + e.getMessage(), e); synchronized (failures) { failures.add(e.getMessage()); } } finally { queue.signalAll(); } } } }; threads[i].start(); } for (int i = 0; i < threads.length; ++i) { threads[i].join(); } assertTrue(failures.toString(), failures.isEmpty()); assertEquals(0, opsCount.get()); assertEquals(0, queue.size()); for (int i = 1; i <= NUM_TABLES; ++i) { TableName table = TableName.valueOf(String.format("testtb-%04d", i)); assertTrue("queue should be deleted, table=" + table, queue.markTableAsDeleted(table)); } }
From source file:och.comp.billing.standalone.BillingSyncService.java
public int doSyncWork(boolean checkWorkTime, Date nowPreset, CallableVoid beforeDbUpdateListener) throws Exception { Date now = nowPreset != null ? nowPreset : new Date(); if (props.getBoolVal(billing_sync_debug_DisableSync)) return -1; if (props.getBoolVal(toolMode)) return -1; // ? ?/* w ww .j av a 2 s . c o m*/ if (checkWorkTime && props.getBoolVal(billing_sync_debug_CheckWorkTime)) { int dayOfMonth = dayOfMonth(now); int endDay = props.getIntVal(billing_sync_endSyncDay); int startDay = props.getIntVal(billing_sync_startSyncDay); if (dayOfMonth < startDay) return -2; if (dayOfMonth > endDay) return -3; HoursAndMinutes nowHHmm = getHoursAndMinutes(now); if (dayOfMonth == startDay) { HoursAndMinutes startHHmm = tryParseHHmm(props.getStrVal(billing_sync_startSyncTime), null); if (startHHmm != null && nowHHmm.compareTo(startHHmm) < 0) return -2; } if (dayOfMonth == endDay) { HoursAndMinutes endHHmm = tryParseHHmm(props.getStrVal(billing_sync_endSyncTime), null); if (endHHmm != null && nowHHmm.compareTo(endHHmm) > 0) return -3; } } Date curMonthStart = monthStart(now); //get all accs HashSet<Long> needPayAccs = new HashSet<Long>(); HashMap<Long, ChatAccount> accsById = new HashMap<>(); List<ChatAccount> allAccs = universal.select(new GetAllChatAccounts()); for (ChatAccount acc : allAccs) { accsById.put(acc.id, acc); if (isNeedToPay(acc, curMonthStart)) needPayAccs.add(acc.id); } if (props.getBoolVal(billing_sync_log)) log.info("sync accs to pay (" + needPayAccs.size() + "): " + needPayAccs); if (isEmpty(needPayAccs)) { saveLastSyncInfo(0); return 0; } //get tariffs List<Tariff> tariffs = universal.select(new GetAllTariffs()); HashMap<Long, Tariff> tariffsById = new HashMap<>(); for (Tariff t : tariffs) tariffsById.put(t.id, t); //find owners HashMap<Long, Set<ChatAccount>> accsByUser = new HashMap<>(); List<ChatAccountPrivileges> allUsersPrivs = universal.select(new GetAllChatAccountPrivileges()); for (ChatAccountPrivileges data : allUsersPrivs) { if (data.privileges.contains(CHAT_OWNER)) { ChatAccount acc = accsById.get(data.accId); if (acc == null) continue; putToSetMap(accsByUser, data.userId, acc); } } if (beforeDbUpdateListener != null) beforeDbUpdateListener.call(); //sync by owners ArrayList<SyncPayError> syncErrors = new ArrayList<>(); for (Entry<Long, Set<ChatAccount>> entry : accsByUser.entrySet()) { Long userId = entry.getKey(); Set<ChatAccount> userAccs = entry.getValue(); try { if (syncAccsListener != null) syncAccsListener.call(); List<SyncPayError> curErrors = syncUserAccs(userId, userAccs, tariffsById, curMonthStart, now); if (curErrors.size() > 0) syncErrors.addAll(curErrors); } // ? , ? ? //? ? ? catch (ConcurrentUpdateSqlException e) { //? for (ChatAccount acc : userAccs) needPayAccs.remove(acc.id); } catch (Throwable t) { log.error("can't sync accs for user=" + userId + ": " + t); syncErrors.add(new SyncPayError(userId, userAccs, t)); } } if (syncErrors.size() > 0) sendSyncErrorMailToAdmin("Sync billing errors", syncErrors); int updated = needPayAccs.size(); saveLastSyncInfo(updated); return updated; }
From source file:mrfu.blurstaggered.lib.view.StaggeredGridView.java
/** * Should be called with mPopulating set to true * * @param fromPosition Position to start filling from * @param overhang the number of extra pixels to fill beyond the current bottom edge * @return the max overhang beyond the end of the view of any added items at the bottom *//*from w w w. java 2 s.c o m*/ final int fillDown(int fromPosition, int overhang) { final int paddingLeft = getPaddingLeft(); final int paddingRight = getPaddingRight(); final int itemMargin = mItemMargin; final int colWidth = (getWidth() - paddingLeft - paddingRight - itemMargin * (mColCount - 1)) / mColCount; final int gridBottom = getHeight() - getPaddingBottom(); final int fillTo = gridBottom + overhang; int nextCol = getNextColumnDown(fromPosition); int position = fromPosition; while (nextCol >= 0 && mItemBottoms[nextCol] < fillTo && position < mItemCount) { final View child = obtainView(position, null); // if (child == null) continue; if (child == null) break; LayoutParams lp = (LayoutParams) child.getLayoutParams(); if (lp == null) { lp = this.generateDefaultLayoutParams(); child.setLayoutParams(lp); } if (child.getParent() != this) { if (mInLayout) { addViewInLayout(child, -1, lp); } else { addView(child); } } final int span = Math.min(mColCount, lp.span); final int widthSize = colWidth * span + itemMargin * (span - 1); final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY); LayoutRecord rec; if (span > 1) { rec = getNextRecordDown(position, span); // nextCol = rec.column; nextCol = 0; } else { rec = mLayoutRecords.get(position); } boolean invalidateAfter = false; if (rec == null) { rec = new LayoutRecord(); mLayoutRecords.put(position, rec); rec.column = nextCol; rec.span = span; } else if (span != rec.span) { rec.span = span; rec.column = nextCol; invalidateAfter = true; } else { // nextCol = rec.column; } if (mHasStableIds) { final long id = mAdapter.getItemId(position); rec.id = id; lp.id = id; } lp.column = nextCol; /** * Magic does not exist */ // child.measure(MeasureSpec.EXACTLY, MeasureSpec.UNSPECIFIED); final int heightSpec; if (lp.height == LayoutParams.WRAP_CONTENT) { heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); } else { heightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY); } child.measure(widthSpec, heightSpec); final int childHeight = child.getMeasuredHeight(); if (invalidateAfter || (childHeight != rec.height && rec.height > 0)) { invalidateLayoutRecordsAfterPosition(position); } rec.height = childHeight; final int startFrom; if (span > 1) { int lowest = mItemBottoms[nextCol]; // final int colEnd = Math.min(mColCount, nextCol + lp.span); // Only for span = maxCol for (int i = 0; i < mColCount; i++) { final int bottom = mItemBottoms[i]; if (bottom > lowest) { lowest = bottom; } } startFrom = lowest; } else { startFrom = mItemBottoms[nextCol]; } final int childTop = startFrom + itemMargin; final int childBottom = childTop + childHeight; final int childLeft; if (span > 1) { childLeft = paddingLeft; } else { childLeft = paddingLeft + nextCol * (colWidth + itemMargin); } final int childRight = childLeft + child.getMeasuredWidth(); child.layout(childLeft, childTop, childRight, childBottom); rec.left = childLeft; rec.top = childTop; rec.right = childRight; rec.bottom = childBottom; rec.hasRecRecord = true; // add the position to the mapping if (!mColMappings.get(nextCol).contains(position)) { // check to see if the mapping exists in other columns // this would happen if list has been updated // for (ArrayList<Integer> list : mColMappings) { for (HashSet<Integer> list : mColMappings) { if (list.contains(position)) { list.remove((Integer) position); } } mColMappings.get(nextCol).add(position); } final int colEnd = Math.min(mColCount, nextCol + lp.span); for (int i = nextCol; i < colEnd; i++) { mItemBottoms[i] = childBottom + rec.getMarginBelow(i - nextCol); } position++; nextCol = getNextColumnDown(position); } int lowestView = 0; for (int i = 0; i < mColCount; i++) { if (mItemBottoms[i] > lowestView) { lowestView = mItemBottoms[i]; } } return lowestView - gridBottom; }
From source file:com.haulmont.cuba.desktop.gui.components.DesktopAbstractTable.java
protected List<Integer> getSelectionIndexes(Collection<? extends Entity> items) { if (items.isEmpty()) { return Collections.emptyList(); }/*www . ja v a 2 s. co m*/ List<Integer> indexes = Lists.newArrayList(); if (datasource instanceof CollectionDatasource.Ordered) { HashSet<Entity> itemSet = new HashSet<>(items); int itemIndex = 0; CollectionDatasource.Ordered orderedDs = (CollectionDatasource.Ordered) datasource; Object id = orderedDs.firstItemId(); while (id != null && !itemSet.isEmpty()) { int rowIndex = impl.convertRowIndexToView(itemIndex); // noinspection unchecked Entity itemById = datasource.getItem(id); if (itemSet.contains(itemById)) { indexes.add(rowIndex); itemSet.remove(itemById); } // noinspection unchecked id = orderedDs.nextItemId(id); itemIndex++; } } else { for (Entity item : items) { int idx = tableModel.getRowIndex(item); if (idx != -1) { indexes.add(impl.convertColumnIndexToView(idx)); } } } return indexes; }
From source file:org.apache.hadoop.hdfs.tools.offlineImageViewer.OfflineImageReconstructor.java
/** * Processes the XML file back into an fsimage. *//* w w w. ja v a2s.co m*/ private void processXml() throws Exception { LOG.debug("Loading <fsimage>."); expectTag("fsimage", false); // Read the <version> tag. readVersion(); // Write the HDFSIMG1 magic number which begins the fsimage file. out.write(FSImageUtil.MAGIC_HEADER); // Write a series of fsimage sections. sectionStartOffset = FSImageUtil.MAGIC_HEADER.length; final HashSet<String> unprocessedSections = new HashSet<>(sections.keySet()); while (!unprocessedSections.isEmpty()) { XMLEvent ev = expectTag("[section header]", true); if (ev.getEventType() == XMLStreamConstants.END_ELEMENT) { if (ev.asEndElement().getName().getLocalPart().equals("fsimage")) { throw new IOException("FSImage XML ended prematurely, without " + "including section(s) " + StringUtils.join(", ", unprocessedSections)); } throw new IOException( "Got unexpected tag end event for " + ev.asEndElement().getName().getLocalPart() + " while looking " + "for section header tag."); } else if (ev.getEventType() != XMLStreamConstants.START_ELEMENT) { throw new IOException( "Expected section header START_ELEMENT; " + "got event of type " + ev.getEventType()); } String sectionName = ev.asStartElement().getName().getLocalPart(); if (!unprocessedSections.contains(sectionName)) { throw new IOException("Unknown or duplicate section found for " + sectionName); } SectionProcessor sectionProcessor = sections.get(sectionName); if (sectionProcessor == null) { throw new IOException("Unknown FSImage section " + sectionName + ". Valid section names are [" + StringUtils.join(", ", sections.keySet()) + "]"); } unprocessedSections.remove(sectionName); sectionProcessor.process(); } // Write the StringTable section to disk. // This has to be done after the other sections, since some of them // add entries to the string table. writeStringTableSection(); // Write the FileSummary section to disk. // This section is always last. long prevOffset = out.getCount(); FileSummary fileSummary = fileSummaryBld.build(); if (LOG.isDebugEnabled()) { LOG.debug("Writing FileSummary: {" + TextFormat.printToString(fileSummary) + "}"); } // Even though the last 4 bytes of the file gives the FileSummary length, // we still write a varint first that also contains the length. fileSummary.writeDelimitedTo(out); // Write the length of the FileSummary section as a fixed-size big // endian 4-byte quantity. int summaryLen = Ints.checkedCast(out.getCount() - prevOffset); byte[] summaryLenBytes = new byte[4]; ByteBuffer.wrap(summaryLenBytes).asIntBuffer().put(summaryLen); out.write(summaryLenBytes); }
From source file:com.ikanow.infinit.e.harvest.enrichment.custom.UnstructuredAnalysisHarvester.java
public void processMetadataChain(DocumentPojo doc, List<MetadataSpecPojo> metadataFields, SourceRssConfigPojo feedConfig, HashSet<String> unstoredFields) throws IOException { // Map metadata list to a legacy meta format (they're really similar...) UnstructuredAnalysisConfigPojo.metaField mappedEl = new UnstructuredAnalysisConfigPojo.metaField(); boolean textSet = false; for (MetadataSpecPojo meta : metadataFields) { mappedEl.fieldName = meta.fieldName; mappedEl.context = Context.All; mappedEl.flags = meta.flags;/*from w ww. ja va 2 s. c o m*/ if (null == mappedEl.flags) { mappedEl.flags = ""; } boolean scriptLangNeedsText = doesScriptLangNeedText(meta.scriptlang); // (js you can operate only on metadata, other languages you need the text ... // actually that is not true, because of chaining ugh - we'll just ignore that for now) if (scriptLangNeedsText || (null == mappedEl.flags) || mappedEl.flags.isEmpty() || mappedEl.flags.contains("t")) { if (!textSet) { getRawTextFromUrlIfNeeded(doc, feedConfig); textSet = true; } } //TESTED (content_needed_test) mappedEl.scriptlang = meta.scriptlang; mappedEl.script = meta.script; mappedEl.replace = meta.replace; mappedEl.groupNum = null; //(no group num - just use replace, and flags "o" for xpath/gN:-1) // Storage of fields: if ((null != meta.store) && !meta.store) { unstoredFields.add(meta.fieldName); } else if ((null != unstoredFields) && unstoredFields.contains(meta.fieldName)) { unstoredFields.remove(meta.fieldName); } this.processMeta(doc, mappedEl, doc.getFullText(), null, null); } //TESTED (storageSettings_advanced.json) }
From source file:org.unitime.timetable.test.UpdateExamConflicts.java
public void updateConflicts(ExamAssignmentInfo assignment, org.hibernate.Session hibSession) throws Exception { Transaction tx = null;//from w w w. j a v a 2s. c o m try { if (hibSession.getTransaction() == null || !hibSession.getTransaction().isActive()) tx = hibSession.beginTransaction(); Exam exam = assignment.getExam(hibSession); HashSet<Exam> otherExams = new HashSet(); HashSet<ExamConflict> conflicts = new HashSet(exam.getConflicts()); for (DirectConflict dc : assignment.getDirectConflicts()) { if (dc.getOtherExam() == null) continue; ExamConflict conf = null; for (ExamConflict c : conflicts) { if (c.getConflictType() != ExamConflict.sConflictTypeDirect) continue; if (c.getNrStudents() == 0) continue; Exam other = null; for (Iterator i = c.getExams().iterator(); i.hasNext();) { Exam x = (Exam) i.next(); if (x.getUniqueId().equals(dc.getOtherExam().getExamId())) { other = x; break; } } if (other == null) continue; conf = c; break; } HashSet<Student> students = getStudents(hibSession, dc.getStudents()); if (exam.getUniqueId().compareTo(dc.getOtherExam().getExamId()) < 0) iTotal[sStudents][ExamConflict.sConflictTypeDirect] += students.size(); if (conf == null) { debug(" new direct " + assignment.getExamName() + " " + dc.getOtherExam().getExamName() + " (" + students.size() + " students)"); conf = new ExamConflict(); conf.setConflictType(ExamConflict.sConflictTypeDirect); conf.setStudents(students); conf.setNrStudents(students.size()); exam.getConflicts().add(conf); Exam other = dc.getOtherExam().getExam(hibSession); other.getConflicts().add(conf); otherExams.add(other); conf.setExams(new HashSet()); conf.getExams().add(exam); conf.getExams().add(other); hibSession.save(conf); iCnt[sStudents][conf.getConflictType()][sCreate] += conf.getNrStudents(); } else { conflicts.remove(conf); boolean change = (students.size() != conf.getStudents().size() || !students.containsAll(conf.getStudents())); if (change) { debug(" update direct " + assignment.getExamName() + " " + dc.getOtherExam().getExamName() + " (" + conf.getNrStudents() + "->" + students.size() + " students)"); conf.setStudents(students); conf.setNrStudents(students.size()); hibSession.update(conf); iCnt[sStudents][conf.getConflictType()][sUpdate] += conf.getNrStudents(); } } } for (DirectConflict dc : assignment.getInstructorDirectConflicts()) { if (dc.getOtherExam() == null) continue; ExamConflict conf = null; for (ExamConflict c : conflicts) { if (c.getConflictType() != ExamConflict.sConflictTypeDirect) continue; if (c.getNrInstructors() == 0) continue; Exam other = null; for (Iterator i = c.getExams().iterator(); i.hasNext();) { Exam x = (Exam) i.next(); if (x.getUniqueId().equals(dc.getOtherExam().getExamId())) { other = x; break; } } if (other == null) continue; conf = c; break; } HashSet<DepartmentalInstructor> instructors = getInstructors(hibSession, dc.getStudents()); if (exam.getUniqueId().compareTo(dc.getOtherExam().getExamId()) < 0) iTotal[sInstructors][ExamConflict.sConflictTypeDirect] += instructors.size(); if (conf == null) { debug(" new direct " + assignment.getExamName() + " " + dc.getOtherExam().getExamName() + " (" + instructors.size() + " instructors)"); conf = new ExamConflict(); conf.setConflictType(ExamConflict.sConflictTypeDirect); conf.setInstructors(instructors); conf.setNrInstructors(instructors.size()); exam.getConflicts().add(conf); Exam other = dc.getOtherExam().getExam(hibSession); other.getConflicts().add(conf); otherExams.add(other); conf.setExams(new HashSet()); conf.getExams().add(exam); conf.getExams().add(other); hibSession.save(conf); iCnt[sInstructors][conf.getConflictType()][sCreate] += conf.getNrInstructors(); } else { conflicts.remove(conf); boolean change = (instructors.size() != conf.getInstructors().size() || !instructors.containsAll(conf.getInstructors())); if (change) { debug(" update direct " + assignment.getExamName() + " " + dc.getOtherExam().getExamName() + " (" + conf.getNrInstructors() + "->" + instructors.size() + " instructors)"); conf.setInstructors(instructors); conf.setNrInstructors(instructors.size()); hibSession.update(conf); iCnt[sInstructors][conf.getConflictType()][sUpdate] += conf.getNrInstructors(); } } } for (BackToBackConflict btb : assignment.getBackToBackConflicts()) { int type = (btb.isDistance() ? ExamConflict.sConflictTypeBackToBackDist : ExamConflict.sConflictTypeBackToBack); if (btb.getOtherExam() == null) continue; ExamConflict conf = null; for (ExamConflict c : conflicts) { if (c.getConflictType() != type) continue; if (c.getNrStudents() == 0) continue; Exam other = null; for (Iterator i = c.getExams().iterator(); i.hasNext();) { Exam x = (Exam) i.next(); if (x.getUniqueId().equals(btb.getOtherExam().getExamId())) { other = x; break; } } if (other == null) continue; conf = c; break; } HashSet<Student> students = getStudents(hibSession, btb.getStudents()); if (exam.getUniqueId().compareTo(btb.getOtherExam().getExamId()) < 0) iTotal[sStudents][type] += students.size(); if (conf == null) { debug(" new btb " + assignment.getExamName() + " " + btb.getOtherExam().getExamName() + " (" + students.size() + " students)"); conf = new ExamConflict(); conf.setConflictType(type); conf.setStudents(students); conf.setNrStudents(students.size()); conf.setDistance(btb.getDistance()); exam.getConflicts().add(conf); Exam other = btb.getOtherExam().getExam(hibSession); other.getConflicts().add(conf); otherExams.add(other); conf.setExams(new HashSet()); conf.getExams().add(exam); conf.getExams().add(other); hibSession.save(conf); iCnt[sStudents][conf.getConflictType()][sCreate] += conf.getNrStudents(); } else { conflicts.remove(conf); boolean change = (students.size() != conf.getStudents().size() || !students.containsAll(conf.getStudents())); if (change) { debug(" update btb " + assignment.getExamName() + " " + btb.getOtherExam().getExamName() + " (" + conf.getNrStudents() + "->" + students.size() + " students)"); conf.setStudents(students); conf.setNrStudents(students.size()); conf.setDistance(btb.getDistance()); hibSession.update(conf); iCnt[sStudents][conf.getConflictType()][sUpdate] += conf.getNrStudents(); } else if (conf.getDistance() == null || Math.abs(conf.getDistance() - btb.getDistance()) > 1.0) { debug(" update btb " + assignment.getExamName() + " " + btb.getOtherExam().getExamName() + " (distance " + conf.getDistance() + " -> " + btb.getDistance() + ")"); conf.setDistance(btb.getDistance()); hibSession.update(conf); iCnt[sStudents][conf.getConflictType()][sUpdate] += conf.getNrStudents(); } } } for (BackToBackConflict btb : assignment.getInstructorBackToBackConflicts()) { int type = (btb.isDistance() ? ExamConflict.sConflictTypeBackToBackDist : ExamConflict.sConflictTypeBackToBack); if (btb.getOtherExam() == null) continue; ExamConflict conf = null; for (ExamConflict c : conflicts) { if (c.getConflictType() != type) continue; if (c.getNrInstructors() == 0) continue; Exam other = null; for (Iterator i = c.getExams().iterator(); i.hasNext();) { Exam x = (Exam) i.next(); if (x.getUniqueId().equals(btb.getOtherExam().getExamId())) { other = x; break; } } if (other == null) continue; conf = c; break; } HashSet<DepartmentalInstructor> instructors = getInstructors(hibSession, btb.getStudents()); if (exam.getUniqueId().compareTo(btb.getOtherExam().getExamId()) < 0) iTotal[sInstructors][type] += instructors.size(); if (conf == null) { debug(" new btb " + assignment.getExamName() + " " + btb.getOtherExam().getExamName() + " (" + instructors.size() + " instructors)"); conf = new ExamConflict(); conf.setConflictType(type); conf.setInstructors(instructors); conf.setNrInstructors(instructors.size()); if (btb.isDistance()) conf.setDistance(btb.getDistance()); exam.getConflicts().add(conf); Exam other = btb.getOtherExam().getExam(hibSession); other.getConflicts().add(conf); otherExams.add(other); conf.setExams(new HashSet()); conf.getExams().add(exam); conf.getExams().add(other); hibSession.save(conf); iCnt[sInstructors][conf.getConflictType()][sCreate] += conf.getNrInstructors(); } else { conflicts.remove(conf); boolean change = (instructors.size() != conf.getStudents().size() || !instructors.containsAll(conf.getInstructors())); if (change) { debug(" update btb " + assignment.getExamName() + " " + btb.getOtherExam().getExamName() + " (" + conf.getNrInstructors() + "->" + instructors.size() + " instructors)"); conf.setInstructors(instructors); conf.setNrInstructors(instructors.size()); conf.setDistance(btb.getDistance()); hibSession.update(conf); iCnt[sInstructors][conf.getConflictType()][sUpdate] += conf.getNrInstructors(); } else if (conf.getDistance() == null || Math.abs(conf.getDistance() - btb.getDistance()) > 1.0) { debug(" update btb " + assignment.getExamName() + " " + btb.getOtherExam().getExamName() + " (distance " + conf.getDistance() + " -> " + btb.getDistance() + ")"); conf.setDistance(btb.getDistance()); hibSession.update(conf); iCnt[sInstructors][conf.getConflictType()][sUpdate] += conf.getNrInstructors(); } } } for (MoreThanTwoADayConflict m2d : assignment.getMoreThanTwoADaysConflicts()) { if (m2d.getOtherExams() == null || m2d.getOtherExams().isEmpty()) continue; ExamConflict conf = null; conf: for (ExamConflict c : conflicts) { if (c.getConflictType() != ExamConflict.sConflictTypeMoreThanTwoADay) continue; if (c.getNrStudents() == 0) continue; if (c.getExams().size() != 1 + m2d.getOtherExams().size()) continue; for (Iterator i = c.getExams().iterator(); i.hasNext();) { Exam other = (Exam) i.next(); if (other.getUniqueId().equals(exam.getUniqueId())) continue; boolean contain = false; for (ExamAssignment x : m2d.getOtherExams()) { if (x.getExamId().equals(other.getUniqueId())) { contain = true; break; } } if (!contain) continue conf; } conf = c; break; } HashSet<Student> students = getStudents(hibSession, m2d.getStudents()); boolean smallest = true; for (ExamAssignment x : m2d.getOtherExams()) if (exam.getUniqueId().compareTo(x.getExamId()) > 0) { smallest = false; break; } if (smallest) iTotal[sStudents][ExamConflict.sConflictTypeMoreThanTwoADay] += students.size(); String name = assignment.getExamName(); for (ExamAssignment x : m2d.getOtherExams()) { name += " " + x.getExamName(); } if (conf == null) { debug(" new m2d " + name + " (" + students.size() + " students)"); conf = new ExamConflict(); conf.setConflictType(ExamConflict.sConflictTypeMoreThanTwoADay); conf.setStudents(students); conf.setNrStudents(students.size()); exam.getConflicts().add(conf); conf.setExams(new HashSet()); conf.getExams().add(exam); for (ExamAssignment x : m2d.getOtherExams()) { Exam other = x.getExam(hibSession); other.getConflicts().add(conf); otherExams.add(other); conf.getExams().add(other); } hibSession.save(conf); iCnt[sStudents][conf.getConflictType()][sCreate] += conf.getNrStudents(); } else { conflicts.remove(conf); boolean change = (students.size() != conf.getStudents().size() || !students.containsAll(conf.getStudents())); if (change) { debug(" update m2d " + name + " (" + conf.getNrStudents() + "->" + students.size() + " students)"); conf.setStudents(students); conf.setNrStudents(students.size()); hibSession.update(conf); iCnt[sStudents][conf.getConflictType()][sUpdate] += conf.getNrStudents(); } } } for (MoreThanTwoADayConflict m2d : assignment.getInstructorMoreThanTwoADaysConflicts()) { if (m2d.getOtherExams() == null || m2d.getOtherExams().isEmpty()) continue; ExamConflict conf = null; conf: for (ExamConflict c : conflicts) { if (c.getConflictType() != ExamConflict.sConflictTypeMoreThanTwoADay) continue; if (c.getNrInstructors() == 0) continue; if (c.getExams().size() != 1 + m2d.getOtherExams().size()) continue; for (Iterator i = c.getExams().iterator(); i.hasNext();) { Exam other = (Exam) i.next(); if (other.getUniqueId().equals(exam.getUniqueId())) continue; boolean contain = false; for (ExamAssignment x : m2d.getOtherExams()) { if (x.getExamId().equals(other.getUniqueId())) { contain = true; break; } } if (!contain) continue conf; } conf = c; break; } HashSet<DepartmentalInstructor> instructors = getInstructors(hibSession, m2d.getStudents()); boolean smallest = true; for (ExamAssignment x : m2d.getOtherExams()) if (exam.getUniqueId().compareTo(x.getExamId()) > 0) { smallest = false; break; } if (smallest) iTotal[sInstructors][ExamConflict.sConflictTypeMoreThanTwoADay] += instructors.size(); String name = assignment.getExamName(); for (ExamAssignment x : m2d.getOtherExams()) { name += " " + x.getExamName(); } if (conf == null) { debug(" new btb " + name + " (" + instructors.size() + " instructors)"); conf = new ExamConflict(); conf.setConflictType(ExamConflict.sConflictTypeMoreThanTwoADay); conf.setInstructors(instructors); conf.setNrInstructors(instructors.size()); exam.getConflicts().add(conf); conf.setExams(new HashSet()); conf.getExams().add(exam); for (ExamAssignment x : m2d.getOtherExams()) { Exam other = x.getExam(hibSession); other.getConflicts().add(conf); otherExams.add(other); conf.getExams().add(other); } hibSession.save(conf); iCnt[sInstructors][conf.getConflictType()][sCreate] += conf.getNrInstructors(); } else { conflicts.remove(conf); boolean change = (instructors.size() != conf.getStudents().size() || !instructors.containsAll(conf.getInstructors())); if (change) { debug(" update btb " + name + " (" + conf.getNrInstructors() + "->" + instructors.size() + " instructors)"); conf.setInstructors(instructors); conf.setNrInstructors(instructors.size()); hibSession.update(conf); iCnt[sInstructors][conf.getConflictType()][sUpdate] += conf.getNrInstructors(); } } } for (ExamConflict conf : conflicts) { String name = ""; if (conf.getConflictType() == ExamConflict.sConflictTypeDirect) name = "direct"; else if (conf.getConflictType() == ExamConflict.sConflictTypeMoreThanTwoADay) name = "m2d"; else if (conf.getConflictType() == ExamConflict.sConflictTypeBackToBack) name = "btb"; else if (conf.getConflictType() == ExamConflict.sConflictTypeBackToBackDist) name = "btb"; if (conf.getNrInstructors() != null) iCnt[sInstructors][conf.getConflictType()][sDelete] += conf.getNrInstructors(); if (conf.getNrStudents() != null) iCnt[sStudents][conf.getConflictType()][sDelete] += conf.getNrStudents(); for (Iterator i = conf.getExams().iterator(); i.hasNext();) { Exam other = (Exam) i.next(); name += " " + other.getLabel(); other.getConflicts().remove(conf); if (!other.equals(exam)) otherExams.add(other); } debug(" delete " + name + " (" + (conf.getNrStudents() != null && conf.getNrStudents() > 0 ? conf.getNrStudents() + " students" : "") + (conf.getNrInstructors() != null && conf.getNrInstructors() > 0 ? conf.getNrInstructors() + " instructors" : "") + ")"); hibSession.delete(conf); } hibSession.update(exam); for (Exam other : otherExams) hibSession.update(other); //hibSession.flush(); if (tx != null) tx.commit(); } catch (Exception e) { if (tx != null) tx.rollback(); throw e; } }
From source file:org.apache.sysml.runtime.util.ProgramConverter.java
/** * This creates a deep copy of a function program block. The central reference to singletons of function program blocks * poses the need for explicit copies in order to prevent conflicting writes of temporary variables (see ExternalFunctionProgramBlock. * //w w w .j av a2 s .com * @param namespace function namespace * @param oldName ? * @param pid ? * @param IDPrefix ? * @param prog runtime program * @param fnStack ? * @param fnCreated ? * @param plain ? */ public static void createDeepCopyFunctionProgramBlock(String namespace, String oldName, long pid, int IDPrefix, Program prog, HashSet<String> fnStack, HashSet<String> fnCreated, boolean plain) { //fpb guaranteed to be non-null (checked inside getFunctionProgramBlock) FunctionProgramBlock fpb = prog.getFunctionProgramBlock(namespace, oldName); String fnameNew = (plain) ? oldName : (oldName + Lop.CP_CHILD_THREAD + pid); String fnameNewKey = DMLProgram.constructFunctionKey(namespace, fnameNew); if (prog.getFunctionProgramBlocks().containsKey(fnameNewKey)) return; //prevent redundant deep copy if already existent //create deep copy FunctionProgramBlock copy = null; ArrayList<DataIdentifier> tmp1 = new ArrayList<>(); ArrayList<DataIdentifier> tmp2 = new ArrayList<>(); if (fpb.getInputParams() != null) tmp1.addAll(fpb.getInputParams()); if (fpb.getOutputParams() != null) tmp2.addAll(fpb.getOutputParams()); if (fpb instanceof ExternalFunctionProgramBlockCP) { ExternalFunctionProgramBlockCP efpb = (ExternalFunctionProgramBlockCP) fpb; HashMap<String, String> tmp3 = efpb.getOtherParams(); if (IDPrefix != -1) copy = new ExternalFunctionProgramBlockCP(prog, tmp1, tmp2, tmp3, saveReplaceFilenameThreadID( efpb.getBaseDir(), Lop.CP_CHILD_THREAD + IDPrefix, Lop.CP_CHILD_THREAD + pid)); else copy = new ExternalFunctionProgramBlockCP(prog, tmp1, tmp2, tmp3, saveReplaceFilenameThreadID( efpb.getBaseDir(), Lop.CP_ROOT_THREAD_ID, Lop.CP_CHILD_THREAD + pid)); } else if (fpb instanceof ExternalFunctionProgramBlock) { ExternalFunctionProgramBlock efpb = (ExternalFunctionProgramBlock) fpb; HashMap<String, String> tmp3 = efpb.getOtherParams(); if (IDPrefix != -1) copy = new ExternalFunctionProgramBlock(prog, tmp1, tmp2, tmp3, saveReplaceFilenameThreadID( efpb.getBaseDir(), Lop.CP_CHILD_THREAD + IDPrefix, Lop.CP_CHILD_THREAD + pid)); else copy = new ExternalFunctionProgramBlock(prog, tmp1, tmp2, tmp3, saveReplaceFilenameThreadID( efpb.getBaseDir(), Lop.CP_ROOT_THREAD_ID, Lop.CP_CHILD_THREAD + pid)); } else { if (!fnStack.contains(fnameNewKey)) { fnStack.add(fnameNewKey); copy = new FunctionProgramBlock(prog, tmp1, tmp2); copy.setChildBlocks(rcreateDeepCopyProgramBlocks(fpb.getChildBlocks(), pid, IDPrefix, fnStack, fnCreated, plain, fpb.isRecompileOnce())); copy.setRecompileOnce(fpb.isRecompileOnce()); copy.setThreadID(pid); fnStack.remove(fnameNewKey); } else //stop deep copy for recursive function calls copy = fpb; } //copy.setVariables( (LocalVariableMap) fpb.getVariables() ); //implicit cloning //note: instructions not used by function program block //put prog.addFunctionProgramBlock(namespace, fnameNew, copy); fnCreated.add(DMLProgram.constructFunctionKey(namespace, fnameNew)); }
From source file:org.apache.sysml.runtime.controlprogram.parfor.ProgramConverter.java
/** * This creates a deep copy of a function program block. The central reference to singletons of function program blocks * poses the need for explicit copies in order to prevent conflicting writes of temporary variables (see ExternalFunctionProgramBlock. * /*from w ww. j a v a 2s. co m*/ * @param namespace function namespace * @param oldName ? * @param pid ? * @param IDPrefix ? * @param prog runtime program * @param fnStack ? * @param fnCreated ? * @param plain ? * @throws DMLRuntimeException if DMLRuntimeException occurs */ public static void createDeepCopyFunctionProgramBlock(String namespace, String oldName, long pid, int IDPrefix, Program prog, HashSet<String> fnStack, HashSet<String> fnCreated, boolean plain) throws DMLRuntimeException { //fpb guaranteed to be non-null (checked inside getFunctionProgramBlock) FunctionProgramBlock fpb = prog.getFunctionProgramBlock(namespace, oldName); String fnameNew = (plain) ? oldName : (oldName + CP_CHILD_THREAD + pid); String fnameNewKey = DMLProgram.constructFunctionKey(namespace, fnameNew); if (prog.getFunctionProgramBlocks().containsKey(fnameNewKey)) return; //prevent redundant deep copy if already existent //create deep copy FunctionProgramBlock copy = null; ArrayList<DataIdentifier> tmp1 = new ArrayList<DataIdentifier>(); ArrayList<DataIdentifier> tmp2 = new ArrayList<DataIdentifier>(); if (fpb.getInputParams() != null) tmp1.addAll(fpb.getInputParams()); if (fpb.getOutputParams() != null) tmp2.addAll(fpb.getOutputParams()); if (fpb instanceof ExternalFunctionProgramBlockCP) { ExternalFunctionProgramBlockCP efpb = (ExternalFunctionProgramBlockCP) fpb; HashMap<String, String> tmp3 = efpb.getOtherParams(); if (IDPrefix != -1) copy = new ExternalFunctionProgramBlockCP(prog, tmp1, tmp2, tmp3, saveReplaceFilenameThreadID( efpb.getBaseDir(), CP_CHILD_THREAD + IDPrefix, CP_CHILD_THREAD + pid)); else copy = new ExternalFunctionProgramBlockCP(prog, tmp1, tmp2, tmp3, saveReplaceFilenameThreadID(efpb.getBaseDir(), CP_ROOT_THREAD_ID, CP_CHILD_THREAD + pid)); } else if (fpb instanceof ExternalFunctionProgramBlock) { ExternalFunctionProgramBlock efpb = (ExternalFunctionProgramBlock) fpb; HashMap<String, String> tmp3 = efpb.getOtherParams(); if (IDPrefix != -1) copy = new ExternalFunctionProgramBlock(prog, tmp1, tmp2, tmp3, saveReplaceFilenameThreadID( efpb.getBaseDir(), CP_CHILD_THREAD + IDPrefix, CP_CHILD_THREAD + pid)); else copy = new ExternalFunctionProgramBlock(prog, tmp1, tmp2, tmp3, saveReplaceFilenameThreadID(efpb.getBaseDir(), CP_ROOT_THREAD_ID, CP_CHILD_THREAD + pid)); } else { if (!fnStack.contains(fnameNewKey)) { fnStack.add(fnameNewKey); copy = new FunctionProgramBlock(prog, tmp1, tmp2); copy.setChildBlocks(rcreateDeepCopyProgramBlocks(fpb.getChildBlocks(), pid, IDPrefix, fnStack, fnCreated, plain, fpb.isRecompileOnce())); copy.setRecompileOnce(fpb.isRecompileOnce()); copy.setThreadID(pid); fnStack.remove(fnameNewKey); } else //stop deep copy for recursive function calls copy = fpb; } //copy.setVariables( (LocalVariableMap) fpb.getVariables() ); //implicit cloning //note: instructions not used by function program block //put prog.addFunctionProgramBlock(namespace, fnameNew, copy); fnCreated.add(DMLProgram.constructFunctionKey(namespace, fnameNew)); }
From source file:com.ibm.bi.dml.runtime.controlprogram.parfor.ProgramConverter.java
/** * This creates a deep copy of a function program block. The central reference to singletons of function program blocks * poses the need for explicit copies in order to prevent conflicting writes of temporary variables (see ExternalFunctionProgramBlock. * // w ww. j av a 2s . c o m * @param oldName * @param pid * @throws DMLRuntimeException * @throws DMLUnsupportedOperationException */ public static void createDeepCopyFunctionProgramBlock(String namespace, String oldName, long pid, int IDPrefix, Program prog, HashSet<String> fnStack, HashSet<String> fnCreated, boolean plain) throws DMLRuntimeException, DMLUnsupportedOperationException { //fpb guaranteed to be non-null (checked inside getFunctionProgramBlock) FunctionProgramBlock fpb = prog.getFunctionProgramBlock(namespace, oldName); String fnameNew = (plain) ? oldName : (oldName + CP_CHILD_THREAD + pid); String fnameNewKey = DMLProgram.constructFunctionKey(namespace, fnameNew); if (prog.getFunctionProgramBlocks().containsKey(fnameNewKey)) return; //prevent redundant deep copy if already existent //create deep copy FunctionProgramBlock copy = null; ArrayList<DataIdentifier> tmp1 = new ArrayList<DataIdentifier>(); ArrayList<DataIdentifier> tmp2 = new ArrayList<DataIdentifier>(); if (fpb.getInputParams() != null) tmp1.addAll(fpb.getInputParams()); if (fpb.getOutputParams() != null) tmp2.addAll(fpb.getOutputParams()); if (fpb instanceof ExternalFunctionProgramBlockCP) { ExternalFunctionProgramBlockCP efpb = (ExternalFunctionProgramBlockCP) fpb; HashMap<String, String> tmp3 = efpb.getOtherParams(); if (IDPrefix != -1) copy = new ExternalFunctionProgramBlockCP(prog, tmp1, tmp2, tmp3, saveReplaceFilenameThreadID( efpb.getBaseDir(), CP_CHILD_THREAD + IDPrefix, CP_CHILD_THREAD + pid)); else copy = new ExternalFunctionProgramBlockCP(prog, tmp1, tmp2, tmp3, saveReplaceFilenameThreadID(efpb.getBaseDir(), CP_ROOT_THREAD_ID, CP_CHILD_THREAD + pid)); } else if (fpb instanceof ExternalFunctionProgramBlock) { ExternalFunctionProgramBlock efpb = (ExternalFunctionProgramBlock) fpb; HashMap<String, String> tmp3 = efpb.getOtherParams(); if (IDPrefix != -1) copy = new ExternalFunctionProgramBlock(prog, tmp1, tmp2, tmp3, saveReplaceFilenameThreadID( efpb.getBaseDir(), CP_CHILD_THREAD + IDPrefix, CP_CHILD_THREAD + pid)); else copy = new ExternalFunctionProgramBlock(prog, tmp1, tmp2, tmp3, saveReplaceFilenameThreadID(efpb.getBaseDir(), CP_ROOT_THREAD_ID, CP_CHILD_THREAD + pid)); } else { if (!fnStack.contains(fnameNewKey)) { fnStack.add(fnameNewKey); copy = new FunctionProgramBlock(prog, tmp1, tmp2); copy.setChildBlocks(rcreateDeepCopyProgramBlocks(fpb.getChildBlocks(), pid, IDPrefix, fnStack, fnCreated, plain, fpb.isRecompileOnce())); copy.setRecompileOnce(fpb.isRecompileOnce()); copy.setThreadID(pid); fnStack.remove(fnameNewKey); } else //stop deep copy for recursive function calls copy = fpb; } //copy.setVariables( (LocalVariableMap) fpb.getVariables() ); //implicit cloning //note: instructions not used by function program block //put prog.addFunctionProgramBlock(namespace, fnameNew, copy); fnCreated.add(DMLProgram.constructFunctionKey(namespace, fnameNew)); }