From: Julien HENRY Date: Tue, 2 Dec 2014 16:56:08 +0000 (+0100) Subject: SONAR-5869 Remove persistence of highlighting and symbols in snapshot_data X-Git-Tag: 5.0-RC1~94 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=d05179c1879b20aeb0d38d1e025516e72babdee5;p=sonarqube.git SONAR-5869 Remove persistence of highlighting and symbols in snapshot_data --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/source/HtmlSourceDecorator.java b/server/sonar-server/src/main/java/org/sonar/server/source/HtmlSourceDecorator.java index 6eafe835590..52caca4d161 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/source/HtmlSourceDecorator.java +++ b/server/sonar-server/src/main/java/org/sonar/server/source/HtmlSourceDecorator.java @@ -19,38 +19,30 @@ */ package org.sonar.server.source; -import com.google.common.base.Strings; -import com.google.common.collect.Lists; +import org.apache.commons.lang.StringUtils; import org.sonar.api.ServerComponent; -import org.sonar.core.source.SnapshotDataTypes; -import org.sonar.core.source.db.SnapshotDataDto; import javax.annotation.CheckForNull; import javax.annotation.Nullable; -import java.util.Collection; import java.util.List; public class HtmlSourceDecorator implements ServerComponent { - private static final String SINGLE_LINE_SYMBOLS = "single_line_symbols"; - @CheckForNull public String getDecoratedSourceAsHtml(@Nullable String sourceLine, @Nullable String highlighting, @Nullable String symbols) { - Collection snapshotDataEntries = Lists.newArrayList(); - if (highlighting != null) { - SnapshotDataDto highlightingDto = new SnapshotDataDto(); - highlightingDto.setData(highlighting); - highlightingDto.setDataType(SnapshotDataTypes.SYNTAX_HIGHLIGHTING); - snapshotDataEntries.add(highlightingDto); + if (sourceLine == null) { + return null; + } + DecorationDataHolder decorationDataHolder = new DecorationDataHolder(); + if (StringUtils.isNotBlank(highlighting)) { + decorationDataHolder.loadSyntaxHighlightingData(highlighting); } - if (symbols != null) { - SnapshotDataDto symbolsDto = new SnapshotDataDto(); - symbolsDto.setData(symbols); - symbolsDto.setDataType(SINGLE_LINE_SYMBOLS); - snapshotDataEntries.add(symbolsDto); + if (StringUtils.isNotBlank(symbols)) { + decorationDataHolder.loadLineSymbolReferences(symbols); } - List decoratedSource = decorate(sourceLine, snapshotDataEntries, 1, 1); + HtmlTextDecorator textDecorator = new HtmlTextDecorator(); + List decoratedSource = textDecorator.decorateTextWithHtml(sourceLine, decorationDataHolder, 1, 1); if (decoratedSource == null) { return null; } else { @@ -62,29 +54,4 @@ public class HtmlSourceDecorator implements ServerComponent { } } - @CheckForNull - private List decorate(@Nullable String snapshotSource, Collection snapshotDataEntries, @Nullable Integer from, @Nullable Integer to) { - if (snapshotSource != null) { - DecorationDataHolder decorationDataHolder = new DecorationDataHolder(); - for (SnapshotDataDto snapshotDataEntry : snapshotDataEntries) { - loadSnapshotData(decorationDataHolder, snapshotDataEntry); - } - - HtmlTextDecorator textDecorator = new HtmlTextDecorator(); - return textDecorator.decorateTextWithHtml(snapshotSource, decorationDataHolder, from, to); - } - return null; - } - - private void loadSnapshotData(DecorationDataHolder dataHolder, SnapshotDataDto entry) { - if (!Strings.isNullOrEmpty(entry.getData())) { - if (SnapshotDataTypes.SYNTAX_HIGHLIGHTING.equals(entry.getDataType())) { - dataHolder.loadSyntaxHighlightingData(entry.getData()); - } else if (SnapshotDataTypes.SYMBOL_HIGHLIGHTING.equals(entry.getDataType())) { - dataHolder.loadSymbolReferences(entry.getData()); - } else if (SINGLE_LINE_SYMBOLS.equals(entry.getDataType())) { - dataHolder.loadLineSymbolReferences(entry.getData()); - } - } - } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/ComponentDataPersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/ComponentDataPersister.java deleted file mode 100644 index f53f82675a4..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/index/ComponentDataPersister.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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 this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.index; - -import org.sonar.api.database.model.Snapshot; -import org.sonar.core.persistence.DbSession; -import org.sonar.core.persistence.MyBatis; -import org.sonar.core.source.db.SnapshotDataDao; -import org.sonar.core.source.db.SnapshotDataDto; - -import java.util.Map; - -public class ComponentDataPersister implements ScanPersister { - private final ComponentDataCache data; - private final SnapshotCache snapshots; - private final SnapshotDataDao dao; - private final MyBatis mybatis; - - public ComponentDataPersister(ComponentDataCache data, SnapshotCache snapshots, - SnapshotDataDao dao, MyBatis mybatis) { - this.data = data; - this.snapshots = snapshots; - this.dao = dao; - this.mybatis = mybatis; - } - - @Override - public void persist() { - DbSession session = mybatis.openSession(true); - try { - for (Map.Entry componentEntry : snapshots.snapshots()) { - String componentKey = componentEntry.getKey(); - Snapshot snapshot = componentEntry.getValue(); - for (Cache.Entry dataEntry : data.entries(componentKey)) { - Data value = dataEntry.value(); - if (value != null) { - SnapshotDataDto dto = new SnapshotDataDto(); - dto.setSnapshotId(snapshot.getId()); - dto.setResourceId(snapshot.getResourceId()); - dto.setDataType(dataEntry.key()[1].toString()); - dto.setData(value.writeString()); - dao.insert(session, dto); - } - } - } - session.commit(); - } finally { - MyBatis.closeQuietly(session); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/FileHashesPersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/FileHashesPersister.java new file mode 100644 index 00000000000..9ecc91bf0cf --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/index/FileHashesPersister.java @@ -0,0 +1,70 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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 this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.index; + +import org.sonar.api.database.model.Snapshot; +import org.sonar.core.persistence.DbSession; +import org.sonar.core.persistence.MyBatis; +import org.sonar.core.source.SnapshotDataTypes; +import org.sonar.core.source.db.SnapshotDataDao; +import org.sonar.core.source.db.SnapshotDataDto; + +import java.util.Map; + +/** + * Store file hashes in snapshot_data for reuse in next analysis to know if a file is modified + */ +public class FileHashesPersister implements ScanPersister { + private final ComponentDataCache data; + private final SnapshotCache snapshots; + private final SnapshotDataDao dao; + private final MyBatis mybatis; + + public FileHashesPersister(ComponentDataCache data, SnapshotCache snapshots, + SnapshotDataDao dao, MyBatis mybatis) { + this.data = data; + this.snapshots = snapshots; + this.dao = dao; + this.mybatis = mybatis; + } + + @Override + public void persist() { + DbSession session = mybatis.openSession(true); + try { + for (Map.Entry componentEntry : snapshots.snapshots()) { + String componentKey = componentEntry.getKey(); + Snapshot snapshot = componentEntry.getValue(); + String fileHashesdata = data.getStringData(componentKey, SnapshotDataTypes.FILE_HASHES); + if (fileHashesdata != null) { + SnapshotDataDto dto = new SnapshotDataDto(); + dto.setSnapshotId(snapshot.getId()); + dto.setResourceId(snapshot.getResourceId()); + dto.setDataType(SnapshotDataTypes.FILE_HASHES); + dto.setData(fileHashesdata); + dao.insert(session, dto); + } + } + session.commit(); + } finally { + MyBatis.closeQuietly(session); + } + } +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java index b22723814e0..d9f23dc2a39 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java @@ -46,7 +46,7 @@ import org.sonar.batch.duplication.BlockCache; import org.sonar.batch.duplication.DuplicationCache; import org.sonar.batch.index.Caches; import org.sonar.batch.index.ComponentDataCache; -import org.sonar.batch.index.ComponentDataPersister; +import org.sonar.batch.index.FileHashesPersister; import org.sonar.batch.index.DefaultIndex; import org.sonar.batch.index.DefaultPersistenceManager; import org.sonar.batch.index.DefaultResourcePersister; @@ -154,7 +154,7 @@ public class ProjectScanContainer extends ComponentContainer { SnapshotCache.class, ResourceCache.class, ComponentDataCache.class, - ComponentDataPersister.class, + FileHashesPersister.class, DefaultUserFinder.class, // file system diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/ComponentDataPersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/ComponentDataPersisterTest.java deleted file mode 100644 index 1fef4414d61..00000000000 --- a/sonar-batch/src/test/java/org/sonar/batch/index/ComponentDataPersisterTest.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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 this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.index; - -import org.junit.After; -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.sonar.api.database.model.Snapshot; -import org.sonar.core.persistence.AbstractDaoTestCase; -import org.sonar.core.source.db.SnapshotDataDao; - -public class ComponentDataPersisterTest extends AbstractDaoTestCase { - - @ClassRule - public static TemporaryFolder temp = new TemporaryFolder(); - - SnapshotCache snapshots = new SnapshotCache(); - ComponentDataCache data; - Caches caches; - - @Before - public void start() throws Exception { - caches = CachesTest.createCacheOnTemp(temp); - caches.start(); - } - - @After - public void stop() { - caches.stop(); - } - - @Test - public void should_persist_component_data() throws Exception { - setupData("should_persist_component_data"); - Snapshot snapshot = new Snapshot(); - snapshot.setId(100); - snapshot.setResourceId(200); - snapshots.put("org/struts/Action.java", snapshot); - - data = new ComponentDataCache(caches); - data.setStringData("org/struts/Action.java", "SYMBOL", "content of symbol"); - data.setStringData("org/struts/Action.java", "SYNTAX", "content of syntax"); - data.setStringData("org/struts/Other.java", "SYMBOL", "unregistered component, should not be persisted"); - - SnapshotDataDao dataDao = new SnapshotDataDao(getMyBatis()); - ComponentDataPersister persister = new ComponentDataPersister(data, snapshots, dataDao, getMyBatis()); - persister.persist(); - - checkTables("should_persist_component_data", new String[] {"id", "created_at", "updated_at"}, "snapshot_data"); - } -} diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/FileHashesPersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/FileHashesPersisterTest.java new file mode 100644 index 00000000000..29c9e0cef1e --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/index/FileHashesPersisterTest.java @@ -0,0 +1,69 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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 this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.index; + +import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.api.database.model.Snapshot; +import org.sonar.core.persistence.AbstractDaoTestCase; +import org.sonar.core.source.SnapshotDataTypes; +import org.sonar.core.source.db.SnapshotDataDao; + +public class FileHashesPersisterTest extends AbstractDaoTestCase { + + @ClassRule + public static TemporaryFolder temp = new TemporaryFolder(); + + SnapshotCache snapshots = new SnapshotCache(); + ComponentDataCache data; + Caches caches; + + @Before + public void start() throws Exception { + caches = CachesTest.createCacheOnTemp(temp); + caches.start(); + } + + @After + public void stop() { + caches.stop(); + } + + @Test + public void should_persist_component_data() throws Exception { + setupData("should_persist_component_data"); + Snapshot snapshot = new Snapshot(); + snapshot.setId(100); + snapshot.setResourceId(200); + snapshots.put("myProject", snapshot); + + data = new ComponentDataCache(caches); + data.setStringData("myProject", SnapshotDataTypes.FILE_HASHES, "org/struts/Action.java=123ABC"); + + SnapshotDataDao dataDao = new SnapshotDataDao(getMyBatis()); + FileHashesPersister persister = new FileHashesPersister(data, snapshots, dataDao, getMyBatis()); + persister.persist(); + + checkTables("should_persist_component_data", new String[] {"id", "created_at", "updated_at"}, "snapshot_data"); + } +} diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/ComponentDataPersisterTest/should_persist_component_data-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/ComponentDataPersisterTest/should_persist_component_data-result.xml deleted file mode 100644 index 647650b1da0..00000000000 --- a/sonar-batch/src/test/resources/org/sonar/batch/index/ComponentDataPersisterTest/should_persist_component_data-result.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - - \ No newline at end of file diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/ComponentDataPersisterTest/should_persist_component_data.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/ComponentDataPersisterTest/should_persist_component_data.xml deleted file mode 100644 index a83b8aad101..00000000000 --- a/sonar-batch/src/test/resources/org/sonar/batch/index/ComponentDataPersisterTest/should_persist_component_data.xml +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/FileHashesPersisterTest/should_persist_component_data-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/FileHashesPersisterTest/should_persist_component_data-result.xml new file mode 100644 index 00000000000..b710ad3c272 --- /dev/null +++ b/sonar-batch/src/test/resources/org/sonar/batch/index/FileHashesPersisterTest/should_persist_component_data-result.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/FileHashesPersisterTest/should_persist_component_data.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/FileHashesPersisterTest/should_persist_component_data.xml new file mode 100644 index 00000000000..a83b8aad101 --- /dev/null +++ b/sonar-batch/src/test/resources/org/sonar/batch/index/FileHashesPersisterTest/should_persist_component_data.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/sonar-core/src/main/java/org/sonar/core/source/SnapshotDataTypes.java b/sonar-core/src/main/java/org/sonar/core/source/SnapshotDataTypes.java index cb7ff46ff60..745b21b8c97 100644 --- a/sonar-core/src/main/java/org/sonar/core/source/SnapshotDataTypes.java +++ b/sonar-core/src/main/java/org/sonar/core/source/SnapshotDataTypes.java @@ -24,7 +24,6 @@ public interface SnapshotDataTypes { String SYNTAX_HIGHLIGHTING = "highlight_syntax"; String SYMBOL_HIGHLIGHTING = "symbol"; - String TOKEN = "token"; /** * Key-values [relative path, hash] of all files. Stored on modules.