From 156eaff96010dfffa751cd436a387527e65e5003 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 19 Dec 2011 17:23:03 +0100 Subject: [PATCH] SONAR-983 index existing projects during upgrade + remove unused SELECT operations from MyBatis dao --- .../org/sonar/jpa/entity/SchemaMigration.java | 9 +- .../java/org/sonar/persistence/DaoUtils.java | 4 +- .../java/org/sonar/persistence/MyBatis.java | 4 +- ...eIndexDao.java => ResourceIndexerDao.java} | 30 +----- .../resource/ResourceIndexerFilter.java | 26 ++++- ...Mapper.java => ResourceIndexerMapper.java} | 6 +- .../resource/ResourceIndexMapper.xml | 50 ---------- .../resource/ResourceIndexerMapper.xml | 33 +++++++ .../resource/ResourceIndexDaoTest.java | 95 ------------------- .../resource/ResourceIndexerDaoTest.java | 53 +++++++++++ .../ResourceIndexDaoTest/testSearch.xml | 18 ---- .../testIndex-result.xml | 0 .../testIndex.xml | 0 .../testIndexAll-result.xml | 0 .../testIndexAll.xml | 0 .../org/sonar/server/platform/Platform.java | 1 + .../sonar/server/startup/IndexProjects.java | 69 ++++++++++++++ .../java/org/sonar/server/ui/JRubyFacade.java | 8 +- .../app/controllers/search_controller.rb | 4 +- .../WEB-INF/app/views/search/index.html.erb | 2 +- .../server/startup/IndexProjectsTest.java | 80 ++++++++++++++++ 21 files changed, 273 insertions(+), 219 deletions(-) rename sonar-core/src/main/java/org/sonar/persistence/resource/{ResourceIndexDao.java => ResourceIndexerDao.java} (76%) rename sonar-core/src/main/java/org/sonar/persistence/resource/{ResourceIndexMapper.java => ResourceIndexerMapper.java} (88%) delete mode 100644 sonar-core/src/main/resources/org/sonar/persistence/resource/ResourceIndexMapper.xml create mode 100644 sonar-core/src/main/resources/org/sonar/persistence/resource/ResourceIndexerMapper.xml delete mode 100644 sonar-core/src/test/java/org/sonar/persistence/resource/ResourceIndexDaoTest.java create mode 100644 sonar-core/src/test/java/org/sonar/persistence/resource/ResourceIndexerDaoTest.java delete mode 100644 sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexDaoTest/testSearch.xml rename sonar-core/src/test/resources/org/sonar/persistence/resource/{ResourceIndexDaoTest => ResourceIndexerDaoTest}/testIndex-result.xml (100%) rename sonar-core/src/test/resources/org/sonar/persistence/resource/{ResourceIndexDaoTest => ResourceIndexerDaoTest}/testIndex.xml (100%) rename sonar-core/src/test/resources/org/sonar/persistence/resource/{ResourceIndexDaoTest => ResourceIndexerDaoTest}/testIndexAll-result.xml (100%) rename sonar-core/src/test/resources/org/sonar/persistence/resource/{ResourceIndexDaoTest => ResourceIndexerDaoTest}/testIndexAll.xml (100%) create mode 100644 sonar-server/src/main/java/org/sonar/server/startup/IndexProjects.java create mode 100644 sonar-server/src/test/java/org/sonar/server/startup/IndexProjectsTest.java diff --git a/sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java b/sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java index 67661c1ce02..31b4b9338b2 100644 --- a/sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java +++ b/sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java @@ -34,15 +34,8 @@ public class SchemaMigration { public final static int VERSION_UNKNOWN = -1; - /* - - IMPORTANT : HOW TO ADD A DATABASE MIGRATION : - - set the following constant LAST_VERSION - - add the activerecord migration script into sonar-server/src/main/webapp/WEB-INF/db/migrate - - complete the Derby DDL file used for unit tests : sonar-testing-harness/src/main/resources/org/sonar/test/persistence/sonar-test.ddl - - */ public static final int LAST_VERSION = 239; + public static final int VERSION_2_13 = 230; public final static String TABLE_NAME = "schema_migrations"; diff --git a/sonar-core/src/main/java/org/sonar/persistence/DaoUtils.java b/sonar-core/src/main/java/org/sonar/persistence/DaoUtils.java index 64fb1b64980..4e48002290c 100644 --- a/sonar-core/src/main/java/org/sonar/persistence/DaoUtils.java +++ b/sonar-core/src/main/java/org/sonar/persistence/DaoUtils.java @@ -22,7 +22,7 @@ package org.sonar.persistence; import org.sonar.persistence.dashboard.ActiveDashboardDao; import org.sonar.persistence.dashboard.DashboardDao; import org.sonar.persistence.duplication.DuplicationDao; -import org.sonar.persistence.resource.ResourceIndexDao; +import org.sonar.persistence.resource.ResourceIndexerDao; import org.sonar.persistence.review.ReviewDao; import org.sonar.persistence.rule.RuleDao; import org.sonar.persistence.template.LoadedTemplateDao; @@ -41,7 +41,7 @@ public final class DaoUtils { DashboardDao.class, DuplicationDao.class, LoadedTemplateDao.class, - ResourceIndexDao.class, + ResourceIndexerDao.class, ReviewDao.class, RuleDao.class); } diff --git a/sonar-core/src/main/java/org/sonar/persistence/MyBatis.java b/sonar-core/src/main/java/org/sonar/persistence/MyBatis.java index 1edfd4e3236..a8851e1d598 100644 --- a/sonar-core/src/main/java/org/sonar/persistence/MyBatis.java +++ b/sonar-core/src/main/java/org/sonar/persistence/MyBatis.java @@ -32,7 +32,7 @@ import org.sonar.persistence.duplication.DuplicationMapper; import org.sonar.persistence.duplication.DuplicationUnitDto; import org.sonar.persistence.resource.ResourceDto; import org.sonar.persistence.resource.ResourceIndexDto; -import org.sonar.persistence.resource.ResourceIndexMapper; +import org.sonar.persistence.resource.ResourceIndexerMapper; import org.sonar.persistence.review.ReviewDto; import org.sonar.persistence.review.ReviewMapper; import org.sonar.persistence.rule.RuleDto; @@ -74,7 +74,7 @@ public class MyBatis implements BatchComponent, ServerComponent { loadMapper(conf, DuplicationMapper.class); loadMapper(conf, LoadedTemplateMapper.class); loadMapper(conf, ReviewMapper.class); - loadMapper(conf, ResourceIndexMapper.class); + loadMapper(conf, ResourceIndexerMapper.class); loadMapper(conf, RuleMapper.class); loadMapper(conf, WidgetMapper.class); loadMapper(conf, WidgetPropertyMapper.class); diff --git a/sonar-core/src/main/java/org/sonar/persistence/resource/ResourceIndexDao.java b/sonar-core/src/main/java/org/sonar/persistence/resource/ResourceIndexerDao.java similarity index 76% rename from sonar-core/src/main/java/org/sonar/persistence/resource/ResourceIndexDao.java rename to sonar-core/src/main/java/org/sonar/persistence/resource/ResourceIndexerDao.java index 83c86353e23..485539a2e5a 100644 --- a/sonar-core/src/main/java/org/sonar/persistence/resource/ResourceIndexDao.java +++ b/sonar-core/src/main/java/org/sonar/persistence/resource/ResourceIndexerDao.java @@ -25,36 +25,18 @@ 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 org.sonar.api.utils.TimeProfiler; import org.sonar.persistence.MyBatis; -import java.util.Collections; -import java.util.List; +public class ResourceIndexerDao { -public class ResourceIndexDao { - - public static final int MINIMUM_SEARCH_SIZE = 3; public static final int MINIMUM_KEY_SIZE = 3; private final MyBatis mybatis; - public ResourceIndexDao(MyBatis mybatis) { + public ResourceIndexerDao(MyBatis mybatis) { this.mybatis = mybatis; } - public List search(String keyword) { - if (StringUtils.isBlank(keyword) || keyword.length() < MINIMUM_SEARCH_SIZE) { - return Collections.emptyList(); - } - SqlSession sqlSession = mybatis.openSession(); - try { - ResourceIndexMapper mapper = sqlSession.getMapper(ResourceIndexMapper.class); - return mapper.selectByKeyword(normalize(keyword) + "%"); - } finally { - sqlSession.close(); - } - } - void index(ResourceDto resource, SqlSession session) { String name = resource.getName(); if (StringUtils.isBlank(name)) { @@ -62,7 +44,7 @@ public class ResourceIndexDao { } String normalizedName = normalize(name); if (normalizedName.length() >= MINIMUM_KEY_SIZE) { - ResourceIndexMapper mapper = session.getMapper(ResourceIndexMapper.class); + ResourceIndexerMapper mapper = session.getMapper(ResourceIndexerMapper.class); Integer rootId; if (resource.getRootId() != null) { @@ -103,7 +85,6 @@ public class ResourceIndexDao { public void index(ResourceIndexerFilter filter) { - TimeProfiler profiler = new TimeProfiler().start("Index resources"); final SqlSession sqlSession = mybatis.openSession(ExecutorType.BATCH); try { sqlSession.select("selectResourcesToIndex", filter, new ResultHandler() { @@ -114,15 +95,10 @@ public class ResourceIndexDao { }); } finally { sqlSession.close(); - profiler.stop(); } } static String normalize(String input) { return StringUtils.lowerCase(input); } - - public static boolean isValidInput(String input) { - return StringUtils.isNotBlank(input) && input.length() >= MINIMUM_SEARCH_SIZE; - } } diff --git a/sonar-core/src/main/java/org/sonar/persistence/resource/ResourceIndexerFilter.java b/sonar-core/src/main/java/org/sonar/persistence/resource/ResourceIndexerFilter.java index 01527375cba..fe038623575 100644 --- a/sonar-core/src/main/java/org/sonar/persistence/resource/ResourceIndexerFilter.java +++ b/sonar-core/src/main/java/org/sonar/persistence/resource/ResourceIndexerFilter.java @@ -19,11 +19,17 @@ */ package org.sonar.persistence.resource; -import org.sonar.api.resources.Scopes; - public final class ResourceIndexerFilter { private boolean enabled = true; - private String[] scopes = new String[]{Scopes.PROJECT, Scopes.DIRECTORY, Scopes.FILE}; + private String[] scopes = null; + private String[] qualifiers = null; + + private ResourceIndexerFilter() { + } + + public static ResourceIndexerFilter create() { + return new ResourceIndexerFilter(); + } public boolean isEnabled() { return enabled; @@ -32,4 +38,18 @@ public final class ResourceIndexerFilter { public String[] getScopes() { return scopes; } + + public String[] getQualifiers() { + return qualifiers; + } + + public ResourceIndexerFilter setScopes(String[] scopes) { + this.scopes = scopes; + return this; + } + + public ResourceIndexerFilter setQualifiers(String[] qualifiers) { + this.qualifiers = qualifiers; + return this; + } } diff --git a/sonar-core/src/main/java/org/sonar/persistence/resource/ResourceIndexMapper.java b/sonar-core/src/main/java/org/sonar/persistence/resource/ResourceIndexerMapper.java similarity index 88% rename from sonar-core/src/main/java/org/sonar/persistence/resource/ResourceIndexMapper.java rename to sonar-core/src/main/java/org/sonar/persistence/resource/ResourceIndexerMapper.java index 2d28ed4cc95..209126ce466 100644 --- a/sonar-core/src/main/java/org/sonar/persistence/resource/ResourceIndexMapper.java +++ b/sonar-core/src/main/java/org/sonar/persistence/resource/ResourceIndexerMapper.java @@ -19,11 +19,7 @@ */ package org.sonar.persistence.resource; -import java.util.List; - -public interface ResourceIndexMapper { - - List selectByKeyword(String keyword); +public interface ResourceIndexerMapper { ResourceDto selectRootId(int id); diff --git a/sonar-core/src/main/resources/org/sonar/persistence/resource/ResourceIndexMapper.xml b/sonar-core/src/main/resources/org/sonar/persistence/resource/ResourceIndexMapper.xml deleted file mode 100644 index c521824b6bc..00000000000 --- a/sonar-core/src/main/resources/org/sonar/persistence/resource/ResourceIndexMapper.xml +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - - - - - kee, position, resource_id, project_id - - - - - - - - - - - insert into resource_index (kee, position, name_size, resource_id, project_id) - values (#{key}, #{position}, #{nameSize}, #{resourceId}, #{projectId}) - - - diff --git a/sonar-core/src/main/resources/org/sonar/persistence/resource/ResourceIndexerMapper.xml b/sonar-core/src/main/resources/org/sonar/persistence/resource/ResourceIndexerMapper.xml new file mode 100644 index 00000000000..5b0c389d3d1 --- /dev/null +++ b/sonar-core/src/main/resources/org/sonar/persistence/resource/ResourceIndexerMapper.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + insert into resource_index (kee, position, name_size, resource_id, project_id) + values (#{key}, #{position}, #{nameSize}, #{resourceId}, #{projectId}) + + + diff --git a/sonar-core/src/test/java/org/sonar/persistence/resource/ResourceIndexDaoTest.java b/sonar-core/src/test/java/org/sonar/persistence/resource/ResourceIndexDaoTest.java deleted file mode 100644 index c803812edb4..00000000000 --- a/sonar-core/src/test/java/org/sonar/persistence/resource/ResourceIndexDaoTest.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2011 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.persistence.resource; - -import org.hamcrest.core.Is; -import org.junit.Before; -import org.junit.Test; -import org.sonar.persistence.DaoTestCase; - -import java.util.List; - -import static org.junit.Assert.assertThat; - -public class ResourceIndexDaoTest extends DaoTestCase { - - private static ResourceIndexDao dao; - - @Before - public void createDao() { - dao = new ResourceIndexDao(getMyBatis()); - } - - @Test - public void testSearch() { - setupData("testSearch"); - - List rows = dao.search("zip"); - assertThat(rows.size(), Is.is(1)); - assertThat(rows.get(0).getPosition(), Is.is(0)); - assertThat(rows.get(0).getKey(), Is.is("ziputils")); - - rows = dao.search("util"); - assertThat(rows.size(), Is.is(2)); - assertThat(rows.get(0).getPosition(), Is.is(3)); - assertThat(rows.get(0).getKey(), Is.is("utils")); - assertThat(rows.get(0).getResourceId(), Is.is(10)); - - assertThat(rows.get(1).getPosition(), Is.is(4)); - assertThat(rows.get(1).getKey(), Is.is("utils")); - assertThat(rows.get(1).getResourceId(), Is.is(130)); - } - - @Test - public void shouldNotBeCaseSensitiveSearch() { - setupData("testSearch"); - - List rows = dao.search("ZipU"); - assertThat(rows.size(), Is.is(1)); - assertThat(rows.get(0).getKey(), Is.is("ziputils")); - } - - @Test - public void testMinimumSizeOfSearchInput() { - setupData("testSearch"); - - List rows = dao.search("zi"); - assertThat(rows.size(), Is.is(0)); - } - - @Test - public void testIndex() { - setupData("testIndex"); - - dao.index("ZipUtils", 10, 8); - - checkTables("testIndex", "resource_index"); - } - - @Test - public void testIndexAll() { - setupData("testIndexAll"); - - dao.index(new ResourceIndexerFilter()); - - checkTables("testIndexAll", "resource_index"); - } - -} diff --git a/sonar-core/src/test/java/org/sonar/persistence/resource/ResourceIndexerDaoTest.java b/sonar-core/src/test/java/org/sonar/persistence/resource/ResourceIndexerDaoTest.java new file mode 100644 index 00000000000..08eeaf64efe --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/persistence/resource/ResourceIndexerDaoTest.java @@ -0,0 +1,53 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.persistence.resource; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.persistence.DaoTestCase; + +public class ResourceIndexerDaoTest extends DaoTestCase { + + private static ResourceIndexerDao dao; + + @Before + public void createDao() { + dao = new ResourceIndexerDao(getMyBatis()); + } + + @Test + public void testIndex() { + setupData("testIndex"); + + dao.index("ZipUtils", 10, 8); + + checkTables("testIndex", "resource_index"); + } + + @Test + public void testIndexAll() { + setupData("testIndexAll"); + + dao.index(ResourceIndexerFilter.create()); + + checkTables("testIndexAll", "resource_index"); + } + +} diff --git a/sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexDaoTest/testSearch.xml b/sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexDaoTest/testSearch.xml deleted file mode 100644 index a885e5ffa72..00000000000 --- a/sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexDaoTest/testSearch.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexDaoTest/testIndex-result.xml b/sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexerDaoTest/testIndex-result.xml similarity index 100% rename from sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexDaoTest/testIndex-result.xml rename to sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexerDaoTest/testIndex-result.xml diff --git a/sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexDaoTest/testIndex.xml b/sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexerDaoTest/testIndex.xml similarity index 100% rename from sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexDaoTest/testIndex.xml rename to sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexerDaoTest/testIndex.xml diff --git a/sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexDaoTest/testIndexAll-result.xml b/sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexerDaoTest/testIndexAll-result.xml similarity index 100% rename from sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexDaoTest/testIndexAll-result.xml rename to sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexerDaoTest/testIndexAll-result.xml diff --git a/sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexDaoTest/testIndexAll.xml b/sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexerDaoTest/testIndexAll.xml similarity index 100% rename from sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexDaoTest/testIndexAll.xml rename to sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexerDaoTest/testIndexAll.xml diff --git a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java index 8b42b3add9e..a67b40b3271 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java @@ -234,6 +234,7 @@ public final class Platform { startupContainer.addSingleton(DeleteDeprecatedMeasures.class); startupContainer.addSingleton(GeneratePluginIndex.class); startupContainer.addSingleton(RegisterNewDashboards.class); + startupContainer.addSingleton(IndexProjects.class); startupContainer.startComponents(); startupContainer.getComponentByType(ServerLifecycleNotifier.class).notifyStart(); diff --git a/sonar-server/src/main/java/org/sonar/server/startup/IndexProjects.java b/sonar-server/src/main/java/org/sonar/server/startup/IndexProjects.java new file mode 100644 index 00000000000..c1013e1a739 --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/startup/IndexProjects.java @@ -0,0 +1,69 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.server.startup; + +import org.sonar.api.ServerComponent; +import org.sonar.api.platform.ServerUpgradeStatus; +import org.sonar.api.resources.Qualifiers; +import org.sonar.api.resources.Scopes; +import org.sonar.api.utils.TimeProfiler; +import org.sonar.jpa.entity.SchemaMigration; +import org.sonar.persistence.resource.ResourceIndexerDao; +import org.sonar.persistence.resource.ResourceIndexerFilter; + +/** + * Index existing projects during migration to 2.13. Since this latter version, resources are automatically indexed + * during project analysis. + * + * @since 2.13 + */ +public class IndexProjects implements ServerComponent { + + private ServerUpgradeStatus upgradeStatus; + private ResourceIndexerDao indexerDao; + + public IndexProjects(ServerUpgradeStatus upgradeStatus, ResourceIndexerDao indexerDao) { + this.upgradeStatus = upgradeStatus; + this.indexerDao = indexerDao; + } + + public void start() { + if (shouldIndex()) { + index(); + } + } + + private boolean shouldIndex() { + return upgradeStatus.isUpgraded() && upgradeStatus.getInitialDbVersion() < SchemaMigration.VERSION_2_13; + } + + private void index() { + TimeProfiler profiler = new TimeProfiler().start("Index projects"); + indexerDao.index(newFilter()); + profiler.stop(); + } + + private static ResourceIndexerFilter newFilter() { + return ResourceIndexerFilter.create() + .setQualifiers(new String[]{Qualifiers.PROJECT, Qualifiers.VIEW, Qualifiers.SUBVIEW}) + .setScopes(new String[]{Scopes.PROJECT}); + } + +} diff --git a/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java b/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java index 424dbef4868..e16879be2ad 100644 --- a/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java +++ b/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java @@ -37,7 +37,7 @@ import org.sonar.core.i18n.RuleI18nManager; import org.sonar.markdown.Markdown; import org.sonar.persistence.Database; import org.sonar.persistence.DatabaseMigrator; -import org.sonar.persistence.resource.ResourceIndexDao; +import org.sonar.persistence.resource.ResourceIndexerDao; import org.sonar.persistence.resource.ResourceIndexerFilter; import org.sonar.server.configuration.Backup; import org.sonar.server.configuration.ProfilesManager; @@ -384,10 +384,6 @@ public final class JRubyFacade { // RESOURCE SEARCH ENGINE public void indexResources() { - getContainer().getComponentByType(ResourceIndexDao.class).index(new ResourceIndexerFilter()); - } - - public boolean isValidResourceSearchInput(String input) { - return ResourceIndexDao.isValidInput(input); + getContainer().getComponentByType(ResourceIndexerDao.class).index(ResourceIndexerFilter.create()); } } diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/search_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/search_controller.rb index d68792f8143..1808fa1a1ea 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/search_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/search_controller.rb @@ -30,8 +30,8 @@ class SearchController < ApplicationController def index @start_time = Time.now @search = params[:s] - if @search - if java_facade.isValidResourceSearchInput(@search.to_s) + unless @search.empty? + if @search.to_s.size>=3 normalized_search = @search.downcase @results = ResourceIndex.find(:all, :conditions => ["resource_index.kee like ?", normalized_search + '%'], diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/search/index.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/search/index.html.erb index d9b0de36c0d..def02ef059c 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/search/index.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/search/index.html.erb @@ -1,4 +1,4 @@ -
+
diff --git a/sonar-server/src/test/java/org/sonar/server/startup/IndexProjectsTest.java b/sonar-server/src/test/java/org/sonar/server/startup/IndexProjectsTest.java new file mode 100644 index 00000000000..359d57f22ed --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/startup/IndexProjectsTest.java @@ -0,0 +1,80 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.server.startup; + +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.junit.Test; +import org.sonar.api.platform.ServerUpgradeStatus; +import org.sonar.api.resources.Scopes; +import org.sonar.jpa.entity.SchemaMigration; +import org.sonar.persistence.resource.ResourceIndexerDao; +import org.sonar.persistence.resource.ResourceIndexerFilter; + +import static org.mockito.Mockito.*; + +public class IndexProjectsTest { + + @Test + public void doNotIndexOnFreshInstalls() { + ResourceIndexerDao indexerDao = mock(ResourceIndexerDao.class); + ServerUpgradeStatus status = mock(ServerUpgradeStatus.class); + when(status.isUpgraded()).thenReturn(false); + when(status.isFreshInstall()).thenReturn(true); + + new IndexProjects(status, indexerDao).start(); + + verifyZeroInteractions(indexerDao); + } + + @Test + public void doNotIndexOnUpgradesSince213() { + ResourceIndexerDao indexerDao = mock(ResourceIndexerDao.class); + ServerUpgradeStatus status = mock(ServerUpgradeStatus.class); + when(status.isUpgraded()).thenReturn(true); + when(status.isFreshInstall()).thenReturn(false); + when(status.getInitialDbVersion()).thenReturn(SchemaMigration.VERSION_2_13 + 10); + + new IndexProjects(status, indexerDao).start(); + + verifyZeroInteractions(indexerDao); + } + + @Test + public void doIndexOnUpgradeBefore213() { + ResourceIndexerDao indexerDao = mock(ResourceIndexerDao.class); + ServerUpgradeStatus status = mock(ServerUpgradeStatus.class); + when(status.isUpgraded()).thenReturn(true); + when(status.isFreshInstall()).thenReturn(false); + when(status.getInitialDbVersion()).thenReturn(SchemaMigration.VERSION_2_13 - 10); + + new IndexProjects(status, indexerDao).start(); + + verify(indexerDao).index(argThat(new BaseMatcher() { + public boolean matches(Object o) { + ResourceIndexerFilter filter = (ResourceIndexerFilter) o; + return filter.getScopes().length == 1 && filter.getScopes()[0].equals(Scopes.PROJECT); + } + + public void describeTo(Description description) { + } + })); + } +} -- 2.39.5