Skip to content

Commit

Permalink
Apply code review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
julienlancelot committed Jan 29, 2015
1 parent 18dba45 commit ae73f53
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 23 deletions.
Expand Up @@ -20,6 +20,7 @@


package org.sonar.server.component.db; package org.sonar.server.component.db;


import com.google.common.base.Function;
import org.sonar.api.ServerComponent; import org.sonar.api.ServerComponent;
import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.Scopes; import org.sonar.api.resources.Scopes;
Expand Down Expand Up @@ -103,19 +104,19 @@ public List<FilePathWithHashDto> selectModuleFilesTree(DbSession session, String
} }


public List<ComponentDto> getByUuids(final DbSession session, Collection<String> uuids) { public List<ComponentDto> getByUuids(final DbSession session, Collection<String> uuids) {
return DaoUtils.partitionSelect(uuids, new DaoUtils.Function<ComponentDto, String>() { return DaoUtils.executeLargeInputs(uuids, new Function<List<String>, List<ComponentDto>>() {
@Override @Override
public List<ComponentDto> execute(List<String> partition) { public List<ComponentDto> apply(List<String> partition) {
return mapper(session).findByUuids(partition); return mapper(session).findByUuids(partition);
} }
}); });
} }


public List<String> selectUuidsByUuids(final DbSession session, Collection<String> uuids) { public List<String> selectExistingUuids(final DbSession session, Collection<String> uuids) {
return DaoUtils.partitionSelect(uuids, new DaoUtils.Function<String, String>() { return DaoUtils.executeLargeInputs(uuids, new Function<List<String>, List<String>>() {
@Override @Override
public List<String> execute(List<String> partition) { public List<String> apply(List<String> partition) {
return mapper(session).selectUuidsByUuids(partition); return mapper(session).selectExistingUuids(partition);
} }
}); });
} }
Expand Down
Expand Up @@ -58,7 +58,7 @@ private void purgeRemovedViews() {
DbSession session = dbClient.openSession(false); DbSession session = dbClient.openSession(false);
try { try {
Set<String> viewUuidsInIndex = newHashSet(index.findAllViewUuids()); Set<String> viewUuidsInIndex = newHashSet(index.findAllViewUuids());
Set<String> viewUuidInDb = newHashSet(dbClient.componentDao().selectUuidsByUuids(session, viewUuidsInIndex)); Set<String> viewUuidInDb = newHashSet(dbClient.componentDao().selectExistingUuids(session, viewUuidsInIndex));
Set<String> viewsToRemove = Sets.difference(viewUuidsInIndex, viewUuidInDb); Set<String> viewsToRemove = Sets.difference(viewUuidsInIndex, viewUuidInDb);
index.delete(viewsToRemove); index.delete(viewsToRemove);
} finally { } finally {
Expand Down
Expand Up @@ -201,14 +201,14 @@ public void get_by_uuids_on_removed_components() {
} }


@Test @Test
public void select_uuids_by_uuids() { public void select_existing_uuids() {
setupData("shared"); setupData("shared");


List<String> results = dao.selectUuidsByUuids(session, newArrayList("KLMN")); List<String> results = dao.selectExistingUuids(session, newArrayList("KLMN"));
assertThat(results).containsOnly("KLMN"); assertThat(results).containsOnly("KLMN");


assertThat(dao.selectUuidsByUuids(session, newArrayList("KLMN", "unknown"))).hasSize(1); assertThat(dao.selectExistingUuids(session, newArrayList("KLMN", "unknown"))).hasSize(1);
assertThat(dao.selectUuidsByUuids(session, newArrayList("unknown"))).isEmpty(); assertThat(dao.selectExistingUuids(session, newArrayList("unknown"))).isEmpty();
} }


@Test @Test
Expand Down
Expand Up @@ -67,7 +67,7 @@ public interface ComponentMapper {


List<ComponentDto> findByUuids(@Param("uuids") Collection<String> uuids); List<ComponentDto> findByUuids(@Param("uuids") Collection<String> uuids);


List<String> selectUuidsByUuids(@Param("uuids") Collection<String> uuids); List<String> selectExistingUuids(@Param("uuids") Collection<String> uuids);


/** /**
* Return all project (PRJ/TRK) uuids * Return all project (PRJ/TRK) uuids
Expand Down
23 changes: 13 additions & 10 deletions sonar-core/src/main/java/org/sonar/core/persistence/DaoUtils.java
Expand Up @@ -19,6 +19,7 @@
*/ */
package org.sonar.core.persistence; package org.sonar.core.persistence;


import com.google.common.base.Function;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import org.sonar.core.dashboard.ActiveDashboardDao; import org.sonar.core.dashboard.ActiveDashboardDao;
Expand Down Expand Up @@ -92,21 +93,23 @@ public static List<Class> getDaoClasses() {
); );
} }


public static <F, T> List<F> partitionSelect(Collection<T> input, Function<F, T> select) { /**
* Partition by 1000 elements a list of input and execute a function on each part.
*
* The goal is to prevent issue with ORACLE when there's more than 1000 elements in a 'in ('X', 'Y', ...)'
* and with MsSQL when there's more than 2000 parameters in a query
*/
public static <OUTPUT, INPUT> List<OUTPUT> executeLargeInputs(Collection<INPUT> input, Function<List<INPUT>, List<OUTPUT>> function) {
if (input.isEmpty()) { if (input.isEmpty()) {
return Collections.emptyList(); return Collections.emptyList();
} }
List<F> results = newArrayList(); List<OUTPUT> results = newArrayList();
List<List<T>> partitionList = Lists.partition(newArrayList(input), PARTITION_SIZE_FOR_ORACLE); List<List<INPUT>> partitionList = Lists.partition(newArrayList(input), PARTITION_SIZE_FOR_ORACLE);
for (List<T> partition : partitionList) { for (List<INPUT> partition : partitionList) {
List<F> dtos = select.execute(partition); List<OUTPUT> subResults = function.apply(partition);
results.addAll(dtos); results.addAll(subResults);
} }
return results; return results;
} }


public static interface Function<F, T> {
List<F> execute(List<T> partition);
}

} }
Expand Up @@ -97,7 +97,7 @@
</where> </where>
</select> </select>


<select id="selectUuidsByUuids" parameterType="String" resultType="String"> <select id="selectExistingUuids" parameterType="String" resultType="String">
select p.uuid select p.uuid
from projects p from projects p
<where> <where>
Expand Down

0 comments on commit ae73f53

Please sign in to comment.