diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2011-12-23 12:27:39 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2011-12-23 12:28:50 +0100 |
commit | dffb2a58206c68b7ae69cbcdebdfb7c30cdbf579 (patch) | |
tree | 5435e6cf451e9c8668d8a63ed4860c0564ffd559 | |
parent | 5d6ab3e62e41fc1d9928156557c96c301bc50ce5 (diff) | |
download | sonarqube-dffb2a58206c68b7ae69cbcdebdfb7c30cdbf579.tar.gz sonarqube-dffb2a58206c68b7ae69cbcdebdfb7c30cdbf579.zip |
SONAR-983 provide the component org.sonar.core.resource.ResourceIndexer that is a layer over ResourceIndexerDao
+ move derby SQL files to org.sonar.core.persistence
12 files changed, 109 insertions, 41 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java index 21f6824c713..b40aa67a45a 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java @@ -27,6 +27,7 @@ import org.sonar.batch.MavenPluginExecutor; import org.sonar.batch.ServerMetadata; import org.sonar.batch.config.BatchSettings; import org.sonar.batch.config.BatchSettingsEnhancer; +import org.sonar.core.resource.ResourceIndexer; import org.sonar.jpa.session.DatabaseSessionProvider; import org.sonar.jpa.session.DefaultDatabaseConnector; import org.sonar.jpa.session.ThreadLocalDatabaseSessionFactory; @@ -66,6 +67,7 @@ public class BootstrapModule extends Module { addCoreSingleton(BatchDatabase.class); addCoreSingleton(MyBatis.class); + addCoreSingleton(ResourceIndexer.class); addCoreSingleton(DefaultDatabaseConnector.class); addCoreSingleton(ThreadLocalDatabaseSessionFactory.class); for (Class daoClass : DaoUtils.getDaoClasses()) { diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java index a681b32dabf..5386b61414d 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java @@ -27,6 +27,7 @@ import org.sonar.api.database.model.ResourceModel; import org.sonar.api.database.model.Snapshot; import org.sonar.api.resources.*; import org.sonar.api.utils.SonarException; +import org.sonar.core.resource.ResourceIndexer; import org.sonar.core.resource.ResourceIndexerDao; import javax.persistence.NonUniqueResultException; @@ -40,9 +41,9 @@ public final class DefaultResourcePersister implements ResourcePersister { private DatabaseSession session; private Map<Resource, Snapshot> snapshotsByResource = Maps.newHashMap(); - private ResourceIndexerDao indexer; + private ResourceIndexer indexer; - public DefaultResourcePersister(DatabaseSession session, ResourceIndexerDao indexer) { + public DefaultResourcePersister(DatabaseSession session, ResourceIndexer indexer) { this.session = session; this.indexer = indexer; } @@ -126,15 +127,14 @@ public final class DefaultResourcePersister implements ResourcePersister { if (resource instanceof Project) { // should not occur, please use the method saveProject() snapshot = persistProject((Project) resource, project); - indexer.index(resource.getName(), snapshot.getQualifier(), snapshot.getResourceId(), snapshot.getRootProjectId()); } else if (resource instanceof Library) { snapshot = persistLibrary(project, (Library) resource); } else { snapshot = persistFileOrDirectory(project, resource, parent); - indexer.index(resource.getName(), snapshot.getQualifier(), snapshot.getResourceId(), snapshot.getRootProjectId()); } + indexer.index(resource.getName(), snapshot.getQualifier(), snapshot.getResourceId(), snapshot.getRootProjectId()); return snapshot; } diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java index 7a7877287ea..35277ff3436 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java @@ -26,7 +26,7 @@ import org.sonar.api.resources.JavaPackage; import org.sonar.api.resources.Library; import org.sonar.api.resources.Project; import org.sonar.jpa.test.AbstractDbUnitTestCase; -import org.sonar.core.resource.ResourceIndexerDao; +import org.sonar.core.resource.ResourceIndexer; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -67,7 +67,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { public void shouldSaveNewProject() { setupData("shared"); - ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourceIndexerDao.class)); + ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourceIndexer.class)); persister.saveProject(singleProject, null); checkTables("shouldSaveNewProject", "projects", "snapshots"); @@ -77,7 +77,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { public void shouldSaveNewMultiModulesProject() throws ParseException { setupData("shared"); - ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourceIndexerDao.class)); + ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourceIndexer.class)); persister.saveProject(multiModuleProject, null); persister.saveProject(moduleA, multiModuleProject); persister.saveProject(moduleB, multiModuleProject); @@ -90,7 +90,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { public void shouldSaveNewDirectory() { setupData("shared"); - ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourceIndexerDao.class)); + ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourceIndexer.class)); persister.saveProject(singleProject, null); persister.saveResource(singleProject, new JavaPackage("org.foo").setEffectiveKey("foo:org.foo")); @@ -102,7 +102,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { public void shouldSaveNewLibrary() { setupData("shared"); - ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourceIndexerDao.class)); + ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourceIndexer.class)); persister.saveProject(singleProject, null); persister.saveResource(singleProject, new Library("junit:junit", "4.8.2").setEffectiveKey("junit:junit")); persister.saveResource(singleProject, new Library("junit:junit", "4.8.2").setEffectiveKey("junit:junit"));// do nothing, already saved @@ -115,7 +115,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { public void shouldClearResourcesExceptProjects() { setupData("shared"); - DefaultResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourceIndexerDao.class)); + DefaultResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourceIndexer.class)); persister.saveProject(multiModuleProject, null); persister.saveProject(moduleA, multiModuleProject); persister.saveResource(moduleA, new JavaPackage("org.foo").setEffectiveKey("a:org.foo")); @@ -131,7 +131,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { public void shouldUpdateExistingResource() { setupData("shouldUpdateExistingResource"); - ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourceIndexerDao.class)); + ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourceIndexer.class)); singleProject.setName("new name"); singleProject.setDescription("new description"); persister.saveProject(singleProject, null); diff --git a/sonar-batch/src/test/java/org/sonar/batch/phases/UpdateStatusJobTest.java b/sonar-batch/src/test/java/org/sonar/batch/phases/UpdateStatusJobTest.java index d7d211fab8d..21b784d1ff3 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/phases/UpdateStatusJobTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/phases/UpdateStatusJobTest.java @@ -24,8 +24,8 @@ import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.Snapshot; import org.sonar.batch.ServerMetadata; import org.sonar.batch.index.DefaultResourcePersister; +import org.sonar.core.resource.ResourceIndexer; import org.sonar.jpa.test.AbstractDbUnitTestCase; -import org.sonar.core.resource.ResourceIndexerDao; import javax.persistence.Query; @@ -51,7 +51,7 @@ public class UpdateStatusJobTest extends AbstractDbUnitTestCase { private void assertAnalysis(int snapshotId, String fixture) { setupData("sharedFixture", fixture); DatabaseSession session = getSession(); - UpdateStatusJob sensor = new UpdateStatusJob(mock(ServerMetadata.class), session, new DefaultResourcePersister(session, mock(ResourceIndexerDao.class)), loadSnapshot(snapshotId)); + UpdateStatusJob sensor = new UpdateStatusJob(mock(ServerMetadata.class), session, new DefaultResourcePersister(session, mock(ResourceIndexer.class)), loadSnapshot(snapshotId)); sensor.execute(); getSession().stop(); diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DdlUtils.java b/sonar-core/src/main/java/org/sonar/core/persistence/DdlUtils.java index 95f42e51369..179f7648810 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/DdlUtils.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/DdlUtils.java @@ -44,8 +44,8 @@ public final class DdlUtils { * The connection is commited in this method but not closed. */ public static void createSchema(Connection connection, String dialect) { - executeScript(connection, "org/sonar/persistence/schema-" + dialect + ".ddl"); - executeScript(connection, "org/sonar/persistence/rows-" + dialect + ".sql"); + executeScript(connection, "org/sonar/core/persistence/schema-" + dialect + ".ddl"); + executeScript(connection, "org/sonar/core/persistence/rows-" + dialect + ".sql"); } private static void executeScript(Connection connection, String path) { diff --git a/sonar-core/src/main/java/org/sonar/core/resource/ResourceIndexer.java b/sonar-core/src/main/java/org/sonar/core/resource/ResourceIndexer.java new file mode 100644 index 00000000000..32c8df9a261 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/resource/ResourceIndexer.java @@ -0,0 +1,74 @@ +/* + * 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.core.resource; + +import com.google.common.annotations.Beta; +import org.apache.commons.lang.ArrayUtils; +import org.sonar.api.BatchComponent; +import org.sonar.api.ServerComponent; +import org.sonar.api.resources.Qualifiers; +import org.sonar.api.resources.Scopes; + +/** + * This component will be automatically called in v3.0 when a resource is created or updated. + * It means that it will not be exposed to plugin API. + * + * @since 2.13 + */ +@Beta +public class ResourceIndexer implements BatchComponent, ServerComponent { + private ResourceIndexerDao dao; + + /** + * Hardcoded list of qualifiers to index. Need to be configurable. + * Directories and packages are explicitly excluded. + */ + private String[] INDEXABLE_QUALIFIERS = { + Qualifiers.VIEW, + Qualifiers.SUBVIEW, + Qualifiers.PROJECT, + Qualifiers.MODULE, + Qualifiers.FILE, + Qualifiers.CLASS, + Qualifiers.UNIT_TEST_FILE + }; + + public ResourceIndexer(ResourceIndexerDao dao) { + this.dao = dao; + } + + public ResourceIndexer index(String resourceName, String qualifier, int resourceId, int rootProjectId) { + if (ArrayUtils.contains(INDEXABLE_QUALIFIERS, qualifier)) { + dao.index(resourceName, qualifier, resourceId, rootProjectId); + } + return this; + } + + /** + * Used only for the migration from a version less than 2.13. + */ + public ResourceIndexer indexAll() { + ResourceIndexerFilter filter = ResourceIndexerFilter.create() + .setScopes(new String[]{Scopes.PROJECT, Scopes.FILE}) + .setQualifiers(INDEXABLE_QUALIFIERS); + dao.index(filter); + return this; + } +} diff --git a/sonar-core/src/main/resources/org/sonar/persistence/rows-derby.sql b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-derby.sql index 2be852b3d1c..2be852b3d1c 100644 --- a/sonar-core/src/main/resources/org/sonar/persistence/rows-derby.sql +++ b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-derby.sql diff --git a/sonar-core/src/main/resources/org/sonar/persistence/schema-derby.ddl b/sonar-core/src/main/resources/org/sonar/core/persistence/schema-derby.ddl index b01f46b5494..b01f46b5494 100644 --- a/sonar-core/src/main/resources/org/sonar/persistence/schema-derby.ddl +++ b/sonar-core/src/main/resources/org/sonar/core/persistence/schema-derby.ddl 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 9ff7a031df8..8b4e966fb3c 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 @@ -44,6 +44,7 @@ import org.sonar.core.persistence.DatabaseMigrator; import org.sonar.core.persistence.DefaultDatabase; import org.sonar.core.persistence.MyBatis; import org.sonar.core.qualitymodel.DefaultModelFinder; +import org.sonar.core.resource.ResourceIndexer; import org.sonar.core.rule.DefaultRuleFinder; import org.sonar.core.user.DefaultUserFinder; import org.sonar.jpa.dao.DaoFacade; @@ -133,6 +134,7 @@ public final class Platform { rootContainer.addSingleton(EmbeddedDatabaseFactory.class); rootContainer.addSingleton(DefaultDatabase.class); rootContainer.addSingleton(MyBatis.class); + rootContainer.addSingleton(ResourceIndexer.class); // for the migration -> see org.sonar.server.startup.IndexProjects rootContainer.addSingleton(DefaultDatabaseConnector.class); rootContainer.addSingleton(DefaultServerUpgradeStatus.class); rootContainer.addSingleton(DatabaseMigrator.class); 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 index 40556756f23..14403cadb79 100644 --- a/sonar-server/src/main/java/org/sonar/server/startup/IndexProjects.java +++ b/sonar-server/src/main/java/org/sonar/server/startup/IndexProjects.java @@ -21,12 +21,9 @@ 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.core.resource.ResourceIndexer; import org.sonar.jpa.entity.SchemaMigration; -import org.sonar.core.resource.ResourceIndexerDao; -import org.sonar.core.resource.ResourceIndexerFilter; /** * Index existing projects during migration to 2.13. Since this latter version, resources are automatically indexed @@ -37,11 +34,11 @@ import org.sonar.core.resource.ResourceIndexerFilter; public class IndexProjects implements ServerComponent { private ServerUpgradeStatus upgradeStatus; - private ResourceIndexerDao indexerDao; + private ResourceIndexer indexer; - public IndexProjects(ServerUpgradeStatus upgradeStatus, ResourceIndexerDao indexerDao) { + public IndexProjects(ServerUpgradeStatus upgradeStatus, ResourceIndexer indexer) { this.upgradeStatus = upgradeStatus; - this.indexerDao = indexerDao; + this.indexer = indexer; } public void start() { @@ -56,14 +53,8 @@ public class IndexProjects implements ServerComponent { private void index() { TimeProfiler profiler = new TimeProfiler().start("Index projects"); - indexerDao.index(newFilter()); + indexer.indexAll(); profiler.stop(); } - private static ResourceIndexerFilter newFilter() { - return ResourceIndexerFilter.create() - .setQualifiers(new String[]{Qualifiers.PROJECT, Qualifiers.MODULE, Qualifiers.VIEW, Qualifiers.SUBVIEW, Qualifiers.DIRECTORY, Qualifiers.PACKAGE, Qualifiers.FILE, Qualifiers.CLASS, Qualifiers.UNIT_TEST_FILE}) - .setScopes(new String[]{Scopes.PROJECT, Scopes.DIRECTORY, Scopes.FILE}); - } - } diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/README.txt b/sonar-server/src/main/webapp/WEB-INF/db/migrate/README.txt index a02204611a2..d8c5c3a85d4 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/README.txt +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/README.txt @@ -3,7 +3,7 @@ HOW TO ADD A MIGRATION * Jump some versions when adding the first Ruby on Rails migration of a new sonar version. For example if sonar 2.10 is 193, then sonar 2.11 should start at 200. * Complete the DDL files for Derby : + sonar-core/src/main/resources/org/sonar/persistence/schema-derby.ddl - + sonar-core/src/main/resources/org/sonar/persistence/rows-derby.sql : + + sonar-core/src/main/resources/org/sonar/core/persistence/rows-derby.sql : - add "INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('<THE MIGRATION ID>')" * Update the migration id defined in sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java * If a table is addded or removed, then edit sonar-core/src/main/java/org/sonar/persistence/DatabaseUtils.java 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 index 3676c081dd6..b3d1f4692a4 100644 --- a/sonar-server/src/test/java/org/sonar/server/startup/IndexProjectsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/startup/IndexProjectsTest.java @@ -21,9 +21,8 @@ package org.sonar.server.startup; import org.junit.Test; import org.sonar.api.platform.ServerUpgradeStatus; +import org.sonar.core.resource.ResourceIndexer; import org.sonar.jpa.entity.SchemaMigration; -import org.sonar.core.resource.ResourceIndexerDao; -import org.sonar.core.resource.ResourceIndexerFilter; import static org.mockito.Mockito.*; @@ -31,39 +30,39 @@ public class IndexProjectsTest { @Test public void doNotIndexOnFreshInstalls() { - ResourceIndexerDao indexerDao = mock(ResourceIndexerDao.class); + ResourceIndexer indexer = mock(ResourceIndexer.class); ServerUpgradeStatus status = mock(ServerUpgradeStatus.class); when(status.isUpgraded()).thenReturn(false); when(status.isFreshInstall()).thenReturn(true); - new IndexProjects(status, indexerDao).start(); + new IndexProjects(status, indexer).start(); - verifyZeroInteractions(indexerDao); + verifyZeroInteractions(indexer); } @Test public void doNotIndexOnUpgradesSince213() { - ResourceIndexerDao indexerDao = mock(ResourceIndexerDao.class); + ResourceIndexer indexer = mock(ResourceIndexer.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(); + new IndexProjects(status, indexer).start(); - verifyZeroInteractions(indexerDao); + verifyZeroInteractions(indexer); } @Test public void doIndexOnUpgradeBefore213() { - ResourceIndexerDao indexerDao = mock(ResourceIndexerDao.class); + ResourceIndexer indexer = mock(ResourceIndexer.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(); + new IndexProjects(status, indexer).start(); - verify(indexerDao).index(any(ResourceIndexerFilter.class)); + verify(indexer).indexAll(); } } |