List of usage examples for org.apache.ibatis.session ResultHandler ResultHandler
ResultHandler
From source file:org.sonar.batch.index.SourcePersister.java
License:Open Source License
@Override public void persist() { // Don't use batch insert for file_sources since keeping all data in memory can produce OOM for big files DbSession session = mybatis.openSession(false); try {/*w w w. jav a2s . c om*/ final Map<String, FileSourceDto> fileSourceDtoByFileUuid = new HashMap<String, FileSourceDto>(); session.select("org.sonar.core.source.db.FileSourceMapper.selectAllFileDataHashByProject", projectTree.getRootProject().getUuid(), new ResultHandler() { @Override public void handleResult(ResultContext context) { FileSourceDto dto = (FileSourceDto) context.getResultObject(); fileSourceDtoByFileUuid.put(dto.getFileUuid(), dto); } }); FileSourceMapper mapper = session.getMapper(FileSourceMapper.class); for (InputPath inputPath : inputPathCache.all()) { if (inputPath instanceof InputFile) { persist(session, mapper, inputPath, fileSourceDtoByFileUuid); } } } catch (Exception e) { throw new IllegalStateException("Unable to save file sources", e); } finally { MyBatis.closeQuietly(session); } }
From source file:org.sonar.batch.issue.tracking.InitialOpenIssuesSensor.java
License:Open Source License
@Override public void analyse(Project project, SensorContext context) { // Adding one second is a hack for resolving conflicts with concurrent user // changes during issue persistence final Date now = DateUtils.addSeconds(DateUtils.truncate(new Date(), Calendar.MILLISECOND), 1); issueDao.selectNonClosedIssuesByModule(project.getId(), new ResultHandler() { @Override// ww w . ja v a2 s . c om public void handleResult(ResultContext rc) { IssueDto dto = (IssueDto) rc.getResultObject(); dto.setSelectedAt(now.getTime()); initialOpenIssuesStack.addIssue(dto); } }); issueChangeDao.selectChangelogOnNonClosedIssuesByModuleAndType(project.getId(), new ResultHandler() { @Override public void handleResult(ResultContext rc) { IssueChangeDto dto = (IssueChangeDto) rc.getResultObject(); initialOpenIssuesStack.addChangelog(dto); } }); }
From source file:org.sonar.ce.task.projectanalysis.issue.ComponentIssuesLoader.java
License:Open Source License
/** * To be efficient both in term of memory and speed: * <ul>//from w w w . j av a 2 s . c o m * <li>only diff changes are loaded from DB, sorted by issue and then change creation date</li> * <li>data from DB is streamed</li> * <li>only the latest change(s) with status and resolution are added to the {@link DefaultIssue} objects</li> * </ul> */ private void loadLatestDiffChangesForReopeningOfClosedIssues(DbSession dbSession, Collection<DefaultIssue> issues) { Map<String, DefaultIssue> issuesByKey = issues.stream().collect(uniqueIndex(DefaultIssue::key)); dbClient.issueChangeDao().scrollDiffChangesOfIssues(dbSession, issuesByKey.keySet(), new ResultHandler<IssueChangeDto>() { private DefaultIssue currentIssue = null; private boolean previousStatusFound = false; private boolean previousResolutionFound = false; @Override public void handleResult(ResultContext<? extends IssueChangeDto> resultContext) { IssueChangeDto issueChangeDto = resultContext.getResultObject(); if (currentIssue == null || !currentIssue.key().equals(issueChangeDto.getIssueKey())) { currentIssue = issuesByKey.get(issueChangeDto.getIssueKey()); previousStatusFound = false; previousResolutionFound = false; } if (currentIssue != null) { FieldDiffs fieldDiffs = issueChangeDto.toFieldDiffs(); boolean hasPreviousStatus = fieldDiffs.get("status") != null; boolean hasPreviousResolution = fieldDiffs.get("resolution") != null; if ((!previousStatusFound && hasPreviousStatus) || (!previousResolutionFound && hasPreviousResolution)) { currentIssue.addChange(fieldDiffs); } previousStatusFound |= hasPreviousStatus; previousResolutionFound |= hasPreviousResolution; } } }); }
From source file:org.sonar.core.dependency.DependencyMapperTest.java
License:Open Source License
@Test public void should_find_all() { setupData("fixture"); final List<DependencyDto> dependencies = Lists.newArrayList(); SqlSession session = getMyBatis().openSession(); try {//w w w. ja v a 2s . c o m session.getMapper(DependencyMapper.class).selectAll(new ResultHandler() { public void handleResult(ResultContext context) { dependencies.add((DependencyDto) context.getResultObject()); } }); } finally { MyBatis.closeQuietly(session); } assertThat(dependencies).hasSize(2); DependencyDto dep = dependencies.get(0); assertThat(dep.getId()).isEqualTo(1L); assertThat(dep.getFromSnapshotId()).isEqualTo(1000L); assertThat(dep.getToSnapshotId()).isEqualTo(1001L); assertThat(dep.getUsage()).isEqualTo("compile"); }
From source file:org.sonar.core.dependency.ResourceSnapshotMapperTest.java
License:Open Source License
@Test public void should_find_all() { setupData("fixture"); final List<ResourceSnapshotDto> snapshots = Lists.newArrayList(); SqlSession session = getMyBatis().openSession(); try {//ww w . ja v a2 s . c om session.getMapper(ResourceSnapshotMapper.class).selectAll(new ResultHandler() { public void handleResult(ResultContext context) { snapshots.add((ResourceSnapshotDto) context.getResultObject()); } }); } finally { MyBatis.closeQuietly(session); } assertThat(snapshots).hasSize(2); ResourceSnapshotDto dep = snapshots.get(0); assertThat(dep.getId()).isEqualTo(1L); assertThat(dep.getProjectId()).isEqualTo(1000L); assertThat(dep.getVersion()).isEqualTo("1.0"); }
From source file:org.sonar.core.purge.PurgeDao.java
License:Open Source License
private void disableOrphanResources(final ResourceDto project, final SqlSession session, final PurgeMapper purgeMapper, final PurgeListener purgeListener) { session.select("org.sonar.core.purge.PurgeMapper.selectResourceIdsToDisable", project.getId(), new ResultHandler() { @Override//ww w . ja v a 2s . c o m public void handleResult(ResultContext resultContext) { IdUuidPair resourceIdUuid = (IdUuidPair) resultContext.getResultObject(); if (resourceIdUuid.getId() != null) { disableResource(resourceIdUuid, purgeMapper); purgeListener.onComponentDisabling(resourceIdUuid.getUuid()); } } }); session.commit(); }
From source file:org.sonar.core.resource.ResourceIndexerDao.java
License:Open Source License
/** * This method is reentrant. It can be executed even if some projects are already indexed. *//*from ww w. j a v a2s.co m*/ public ResourceIndexerDao indexProjects() { final DbSession session = mybatis.openSession(true); try { final ResourceIndexerMapper mapper = session.getMapper(ResourceIndexerMapper.class); session.select("org.sonar.core.resource.ResourceIndexerMapper.selectRootProjectIds", /* workaround to get booleans */ResourceIndexerQuery.create(), new ResultHandler() { @Override public void handleResult(ResultContext context) { Integer rootProjectId = (Integer) context.getResultObject(); doIndexProject(rootProjectId, session, mapper); session.commit(); } }); return this; } finally { MyBatis.closeQuietly(session); } }
From source file:org.sonar.core.resource.ResourceIndexerDao.java
License:Open Source License
private void doIndexProject(int rootProjectId, SqlSession session, final ResourceIndexerMapper mapper) { // non indexed resources ResourceIndexerQuery query = ResourceIndexerQuery.create().setNonIndexedOnly(true) .setQualifiers(NOT_RENAMABLE_QUALIFIERS).setScopes(NOT_RENAMABLE_SCOPES) .setRootProjectId(rootProjectId); session.select(SELECT_RESOURCES, query, new ResultHandler() { @Override//from w ww. ja v a2s .c o m public void handleResult(ResultContext context) { ResourceDto resource = (ResourceDto) context.getResultObject(); doIndex(resource, mapper); } }); // some resources can be renamed, so index must be regenerated // -> delete existing rows and create them again query = ResourceIndexerQuery.create().setNonIndexedOnly(false).setQualifiers(RENAMABLE_QUALIFIERS) .setScopes(RENAMABLE_SCOPES).setRootProjectId(rootProjectId); session.select(SELECT_RESOURCES, query, new ResultHandler() { @Override public void handleResult(ResultContext context) { ResourceDto resource = (ResourceDto) context.getResultObject(); mapper.deleteByResourceId(resource.getId()); doIndex(resource, mapper); } }); }
From source file:org.sonar.db.component.ComponentDaoTest.java
License:Open Source License
private ListAssert<String> assertSelectForIndexing(@Nullable String projectUuid) { db.prepareDbUnit(getClass(), "selectForIndexing.xml"); List<ComponentDto> components = new ArrayList<>(); underTest.selectForIndexing(dbSession, projectUuid, new ResultHandler() { @Override//from w w w . j a v a2 s.c o m public void handleResult(ResultContext context) { components.add((ComponentDto) context.getResultObject()); } }); return assertThat(components).extracting(ComponentDto::uuid); }
From source file:org.sonar.db.component.ResourceIndexDao.java
License:Open Source License
/** * This method is reentrant. It can be executed even if some projects are already indexed. *///w w w .j a v a2s . co m public ResourceIndexDao indexProjects() { final DbSession session = myBatis().openSession(true); try { final ResourceIndexMapper mapper = session.getMapper(ResourceIndexMapper.class); session.select(ResourceIndexMapper.class.getName() + ".selectRootProjectIds", /* workaround to get booleans */ResourceIndexQuery.create(), new ResultHandler() { @Override public void handleResult(ResultContext context) { Integer rootProjectId = (Integer) context.getResultObject(); doIndexProject(rootProjectId, session, mapper); session.commit(); } }); return this; } finally { MyBatis.closeQuietly(session); } }