*/
package org.sonar.plugins.dbcleaner.period;
-import com.google.common.annotations.VisibleForTesting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.config.Settings;
private void delete(List<PurgeableSnapshotDto> snapshots) {
for (PurgeableSnapshotDto snapshot : snapshots) {
- LOG.debug("<- Delete snapshot: " + DateUtils.formatDateTime(snapshot.getDate()) + " [" + snapshot.getSnapshotId() + "]");
+ LOG.info("<- Delete snapshot: " + DateUtils.formatDateTime(snapshot.getDate()) + " [" + snapshot.getSnapshotId() + "]");
purgeDao.deleteSnapshots(PurgeSnapshotQuery.create().setRootSnapshotId(snapshot.getSnapshotId()));
}
}
@Override
void log() {
- LoggerFactory.getLogger(getClass()).debug("-> Delete data prior to: " + DateUtils.formatDate(before));
+ LoggerFactory.getLogger(getClass()).info("-> Delete data prior to: " + DateUtils.formatDate(before));
}
}
@Override
void log() {
- LoggerFactory.getLogger(getClass()).debug("-> Keep one snapshot per " + label + " between " + DateUtils.formatDate(start) + " and " + DateUtils.formatDate(end));
+ LoggerFactory.getLogger(getClass()).info("-> Keep one snapshot per " + label + " between " + DateUtils.formatDate(start) + " and " + DateUtils.formatDate(end));
}
private void appendSnapshotsToDelete(Interval interval, List<PurgeableSnapshotDto> toDelete) {
* Note that generated ids are not returned.
*/
public void insert(Collection<DuplicationUnitDto> units) {
- SqlSession session = mybatis.openSession(ExecutorType.BATCH);
+ SqlSession session = mybatis.openBatchSession();
try {
DuplicationMapper mapper = session.getMapper(DuplicationMapper.class);
for (DuplicationUnitDto unit : units) {
*/
package org.sonar.core.persistence;
+import org.apache.ibatis.executor.BatchResult;
+import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
+import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
-public final class BatchSession {
+import java.sql.Connection;
+import java.util.List;
+import java.util.Map;
+
+public final class BatchSession implements SqlSession {
public static final int MAX_BATCH_SIZE = 1000;
*/
public BatchSession increment(int nbSqlRequests) {
count += nbSqlRequests;
- if (count > batchSize) {
+ if (count >= batchSize) {
commit();
}
return this;
}
- public BatchSession commit() {
+ private void reset() {
+ count=0;
+ }
+
+ public void select(String statement, Object parameter, ResultHandler handler) {
+ reset();
+ session.select(statement, parameter, handler);
+ }
+
+ public void select(String statement, ResultHandler handler) {
+ reset();
+ session.select(statement, handler);
+ }
+
+ public Object selectOne(String statement) {
+ reset();
+ return session.selectOne(statement);
+ }
+
+ public Object selectOne(String statement, Object parameter) {
+ reset();
+ return session.selectOne(statement, parameter);
+ }
+
+ public List selectList(String statement) {
+ reset();
+ return session.selectList(statement);
+ }
+
+ public List selectList(String statement, Object parameter) {
+ reset();
+ return session.selectList(statement, parameter);
+ }
+
+ public List selectList(String statement, Object parameter, RowBounds rowBounds) {
+ reset();
+ return session.selectList(statement, parameter, rowBounds);
+ }
+
+ public Map selectMap(String statement, String mapKey) {
+ reset();
+ return session.selectMap(statement, mapKey);
+ }
+
+ public Map selectMap(String statement, Object parameter, String mapKey) {
+ reset();
+ return session.selectMap(statement, parameter, mapKey);
+ }
+
+ public Map selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) {
+ reset();
+ return session.selectMap(statement, parameter, mapKey, rowBounds);
+ }
+
+ public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) {
+ reset();
+ session.select(statement, parameter, rowBounds, handler);
+ }
+
+ public int insert(String statement) {
+ increment(1);
+ return session.insert(statement);
+ }
+
+ public int insert(String statement, Object parameter) {
+ increment(1);
+ return session.insert(statement, parameter);
+ }
+
+ public int update(String statement) {
+ increment(1);
+ return session.update(statement);
+ }
+
+ public int update(String statement, Object parameter) {
+ increment(1);
+ return session.update(statement, parameter);
+ }
+
+ public int delete(String statement) {
+ increment(1);
+ return session.delete(statement);
+ }
+
+ public int delete(String statement, Object parameter) {
+ increment(1);
+ return session.delete(statement, parameter);
+ }
+
+ public void commit() {
session.commit();
- count = 0;
- return this;
+ reset();
}
- public <T> T getMapper(Class<T> type) {
- return session.getMapper(type);
+ public void commit(boolean force) {
+ session.commit(force);
+ reset();
}
- public SqlSession getSqlSession() {
- return session;
+ public void rollback() {
+ session.rollback();
+ reset();
}
- public void select(String statement, Object parameter, ResultHandler handler) {
- session.select(statement, parameter, handler);
+ public void rollback(boolean force) {
+ session.rollback(force);
+ reset();
}
- public void select(String statement, ResultHandler handler) {
- session.select(statement, handler);
+ public List<BatchResult> flushStatements() {
+ List<BatchResult> batchResults = session.flushStatements();
+ reset();
+ return batchResults;
+ }
+
+ public void close() {
+ session.close();
+ }
+
+ public void clearCache() {
+ session.clearCache();
+ }
+
+ public Configuration getConfiguration() {
+ return session.getConfiguration();
+ }
+
+ public <T> T getMapper(Class<T> type) {
+ return session.getMapper(type);
+ }
+
+ public Connection getConnection() {
+ return session.getConnection();
}
}
}
public SqlSession openSession() {
- return sessionFactory.openSession();
- }
-
- public SqlSession openSession(ExecutorType type) {
- return sessionFactory.openSession(type);
+ return sessionFactory.openSession(ExecutorType.REUSE);
}
public BatchSession openBatchSession() {
- SqlSession session = openSession(ExecutorType.BATCH);
+ SqlSession session = sessionFactory.openSession(ExecutorType.BATCH);
return new BatchSession(session);
}
}
}
- public static void closeQuietly(BatchSession session) {
- if (session != null) {
- closeQuietly(session.getSqlSession());
- }
- }
-
private void loadMapper(Configuration conf, Class mapperClass) throws IOException {
// trick to use database-specific XML files for a single Mapper Java interface
InputStream input = getPathToMapper(mapperClass);
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Lists;
-import com.google.common.collect.Sets;
-import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.SqlSession;
import java.util.Collections;
import java.util.List;
-import java.util.SortedSet;
public class PurgeDao {
private final MyBatis mybatis;
}
public PurgeDao purgeProject(long rootProjectId) {
- SqlSession session = mybatis.openSession(ExecutorType.BATCH);
+ SqlSession session = mybatis.openBatchSession();
PurgeMapper purgeMapper = session.getMapper(PurgeMapper.class);
try {
List<Long> projectIds = resourceDao.getDescendantProjectIdsAndSelf(rootProjectId, session);
}
public List<PurgeableSnapshotDto> selectPurgeableSnapshots(long resourceId) {
- SqlSession session = mybatis.openSession(ExecutorType.REUSE);
+ SqlSession session = mybatis.openBatchSession();
try {
PurgeMapper mapper = session.getMapper(PurgeMapper.class);
List<PurgeableSnapshotDto> result = Lists.newArrayList();
final BatchSession session = mybatis.openBatchSession();
try {
final PurgeMapper mapper = session.getMapper(PurgeMapper.class);
- List<Long> projectIds = resourceDao.getDescendantProjectIdsAndSelf(rootProjectId, session.getSqlSession());
+ List<Long> projectIds = resourceDao.getDescendantProjectIdsAndSelf(rootProjectId, session);
for (Long projectId : projectIds) {
session.select("org.sonar.core.purge.PurgeMapper.selectResourceIdsByRootId", projectId, new ResultHandler() {
public void handleResult(ResultContext context) {
session.select("org.sonar.core.purge.PurgeMapper.selectSnapshotIdsByResource", new ResultHandler() {
public void handleResult(ResultContext context) {
Long snapshotId = (Long) context.getResultObject();
- session.increment(deleteSnapshot(snapshotId, mapper));
+ deleteSnapshot(snapshotId, mapper);
}
});
// TODO optimization: filter requests according to resource scope
mapper.deleteResourceReviews(resourceId);
mapper.deleteResourceEvents(resourceId);
mapper.deleteResource(resourceId);
- session.increment(9);
}
@VisibleForTesting
- int disableResource(long resourceId, PurgeMapper mapper) {
+ void disableResource(long resourceId, PurgeMapper mapper) {
mapper.deleteResourceIndex(resourceId);
mapper.setSnapshotIsLastToFalse(resourceId);
mapper.disableResource(resourceId);
mapper.closeResourceReviews(resourceId);
- return 4; // nb of SQL requests
}
session.select("org.sonar.core.purge.PurgeMapper.selectSnapshotIds", query, new ResultHandler() {
public void handleResult(ResultContext context) {
Long snapshotId = (Long) context.getResultObject();
- session.increment(deleteSnapshot(snapshotId, mapper));
+ deleteSnapshot(snapshotId, mapper);
}
});
session.commit();
}
@VisibleForTesting
- int purgeSnapshot(long snapshotId, PurgeMapper mapper) {
- mapper.deleteSnapshotEvents(snapshotId);
+ void purgeSnapshot(long snapshotId, PurgeMapper mapper) {
+ // note that events are not deleted
mapper.deleteSnapshotDependencies(snapshotId);
mapper.deleteSnapshotDuplications(snapshotId);
mapper.deleteSnapshotSource(snapshotId);
mapper.deleteSnapshotViolations(snapshotId);
mapper.deleteSnapshotWastedMeasures(snapshotId);
mapper.updatePurgeStatusToOne(snapshotId);
- return 7; // nb of SQL requests
}
@VisibleForTesting
- int deleteSnapshot(Long snapshotId, PurgeMapper mapper) {
+ void deleteSnapshot(Long snapshotId, PurgeMapper mapper) {
mapper.deleteSnapshotDependencies(snapshotId);
mapper.deleteSnapshotDuplications(snapshotId);
mapper.deleteSnapshotEvents(snapshotId);
mapper.deleteSnapshotSource(snapshotId);
mapper.deleteSnapshotViolations(snapshotId);
mapper.deleteSnapshot(snapshotId);
- return 8; // nb of SQL requests
}
}
* This method is reentrant. It can be executed even if the project is already indexed.
*/
public ResourceIndexerDao indexProject(final int rootProjectId) {
- SqlSession session = mybatis.openSession(ExecutorType.BATCH);
+ SqlSession session = mybatis.openBatchSession();
try {
ResourceIndexerMapper mapper = session.getMapper(ResourceIndexerMapper.class);
doIndexProject(rootProjectId, session, mapper);
* This method is reentrant. It can be executed even if some projects are already indexed.
*/
public ResourceIndexerDao indexProjects() {
- final SqlSession session = mybatis.openSession(ExecutorType.BATCH);
+ final SqlSession session = mybatis.openBatchSession();
try {
final ResourceIndexerMapper mapper = session.getMapper(ResourceIndexerMapper.class);
session.select("selectRootProjectIds", /* workaround to get booleans */ResourceIndexerQuery.create(), new ResultHandler() {
}
@Test
- public void shouldOpenSession() throws IOException {
- SqlSession session = myBatis.openSession(ExecutorType.BATCH);
+ public void shouldOpenBatchSession() throws IOException {
+ SqlSession session = myBatis.openBatchSession();
try {
assertThat(session.getConnection(), notNullValue());
assertThat(session.getMapper(RuleMapper.class), notNullValue());
<dataset>
<!-- snapshot to keep -->
- <snapshots id="1" purge_status="[null]" period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+ <snapshots id="1" project_id="1" parent_snapshot_id="[null]" root_project_id="[null]" root_snapshot_id="[null]"
+ status="P" islast="false" purge_status="[null]"
+ period1_mode="[null]" period1_param="[null]" period1_date="[null]"
period2_mode="[null]" period2_param="[null]" period2_date="[null]"
- period3_mode="[null]" period3_param="[null]"
- period3_date="[null]" period4_mode="[null]" period4_param="[null]" period4_date="[null]"
+ period3_mode="[null]" period3_param="[null]" period3_date="[null]"
+ period4_mode="[null]" period4_param="[null]" period4_date="[null]"
period5_mode="[null]" period5_param="[null]" period5_date="[null]"
- depth="[null]"
- scope="PRJ" qualifier="TRK" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00"
- version="[null]"
- project_id="1"
- parent_snapshot_id="[null]" root_project_id="[null]" root_snapshot_id="[null]" status="P" islast="false"
- path="[null]"/>
+ depth="[null]" scope="PRJ" qualifier="TRK" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00"
+ version="[null]" path="[null]"/>
<SNAPSHOT_SOURCES ID="1" SNAPSHOT_ID="1" DATA="foo"/>
<rule_failures switched_off="[null]" permanent_id="[null]" ID="1" SNAPSHOT_ID="1" RULE_ID="1" FAILURE_LEVEL="2"
<duplications_index project_snapshot_id="1" snapshot_id="1" hash="bb" index_in_file="0" start_line="0" end_line="0"/>
<!-- snapshot to remove, id 5 on resource 5-->
- <snapshots id="5" purge_status="[null]" period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+ <snapshots id="5" project_id="5" parent_snapshot_id="[null]" root_project_id="[null]" root_snapshot_id="[null]"
+ status="P" islast="true" purge_status="[null]"
+ period1_mode="[null]" period1_param="[null]" period1_date="[null]"
period2_mode="[null]" period2_param="[null]" period2_date="[null]"
- period3_mode="[null]" period3_param="[null]"
- period3_date="[null]" period4_mode="[null]" period4_param="[null]" period4_date="[null]"
+ period3_mode="[null]" period3_param="[null]" period3_date="[null]"
+ period4_mode="[null]" period4_param="[null]" period4_date="[null]"
period5_mode="[null]" period5_param="[null]" period5_date="[null]"
- depth="[null]"
- scope="PRJ" qualifier="TRK" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00"
- version="[null]"
- project_id="5"
- parent_snapshot_id="[null]" root_project_id="[null]" root_snapshot_id="[null]" status="P" islast="true"
- path="[null]"/>
+ depth="[null]" scope="PRJ" qualifier="TRK" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00"
+ version="[null]" path="[null]"/>
<SNAPSHOT_SOURCES ID="2" SNAPSHOT_ID="5" DATA="foo"/>
<rule_failures switched_off="[null]" permanent_id="[null]" ID="3" SNAPSHOT_ID="5" RULE_ID="1" FAILURE_LEVEL="2"
* snapshot.purge_status=1
* commented-out rows are deleted
-Note that measure and review are not deleted.
+Note that measures, events and reviews are not deleted.
-->
<dataset>
<!--parent_dependency_id="[null]" project_snapshot_id="2"-->
<!--dep_usage="USES" dep_weight="1" from_scope="LIB" to_scope="PRJ"/>-->
- <!--<events id="1" resource_id="1" snapshot_id="1"-->
- <!--category="VERSION" description="[null]" name="Version 1.0" event_date="2008-12-02 13:58:00.00" created_at="[null]"/>-->
+ <events id="1" resource_id="1" snapshot_id="1"
+ category="VERSION" description="[null]" name="Version 1.0" event_date="2008-12-02 13:58:00.00" created_at="[null]"/>
<!--<duplications_index project_snapshot_id="1" snapshot_id="1"-->
<!--hash="bb" index_in_file="0" start_line="0" end_line="0"/>-->