From d05179c1879b20aeb0d38d1e025516e72babdee5 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Tue, 2 Dec 2014 17:56:08 +0100 Subject: [PATCH] SONAR-5869 Remove persistence of highlighting and symbols in snapshot_data --- .../server/source/HtmlSourceDecorator.java | 55 ++++--------------- ...ersister.java => FileHashesPersister.java} | 26 +++++---- .../batch/scan/ProjectScanContainer.java | 4 +- ...Test.java => FileHashesPersisterTest.java} | 11 ++-- .../should_persist_component_data-result.xml | 5 -- .../should_persist_component_data-result.xml | 4 ++ .../should_persist_component_data.xml | 0 .../sonar/core/source/SnapshotDataTypes.java | 1 - 8 files changed, 36 insertions(+), 70 deletions(-) rename sonar-batch/src/main/java/org/sonar/batch/index/{ComponentDataPersister.java => FileHashesPersister.java} (72%) rename sonar-batch/src/test/java/org/sonar/batch/index/{ComponentDataPersisterTest.java => FileHashesPersisterTest.java} (80%) delete mode 100644 sonar-batch/src/test/resources/org/sonar/batch/index/ComponentDataPersisterTest/should_persist_component_data-result.xml create mode 100644 sonar-batch/src/test/resources/org/sonar/batch/index/FileHashesPersisterTest/should_persist_component_data-result.xml rename sonar-batch/src/test/resources/org/sonar/batch/index/{ComponentDataPersisterTest => FileHashesPersisterTest}/should_persist_component_data.xml (100%) 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/FileHashesPersister.java similarity index 72% rename from sonar-batch/src/main/java/org/sonar/batch/index/ComponentDataPersister.java rename to sonar-batch/src/main/java/org/sonar/batch/index/FileHashesPersister.java index f53f82675a4..9ecc91bf0cf 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/ComponentDataPersister.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/FileHashesPersister.java @@ -22,18 +22,22 @@ 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; -public class ComponentDataPersister implements ScanPersister { +/** + * 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 ComponentDataPersister(ComponentDataCache data, SnapshotCache snapshots, + public FileHashesPersister(ComponentDataCache data, SnapshotCache snapshots, SnapshotDataDao dao, MyBatis mybatis) { this.data = data; this.snapshots = snapshots; @@ -48,16 +52,14 @@ public class ComponentDataPersister implements ScanPersister { 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); - } + 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(); 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/FileHashesPersisterTest.java similarity index 80% rename from sonar-batch/src/test/java/org/sonar/batch/index/ComponentDataPersisterTest.java rename to sonar-batch/src/test/java/org/sonar/batch/index/FileHashesPersisterTest.java index 1fef4414d61..29c9e0cef1e 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/ComponentDataPersisterTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/FileHashesPersisterTest.java @@ -26,9 +26,10 @@ 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 ComponentDataPersisterTest extends AbstractDaoTestCase { +public class FileHashesPersisterTest extends AbstractDaoTestCase { @ClassRule public static TemporaryFolder temp = new TemporaryFolder(); @@ -54,15 +55,13 @@ public class ComponentDataPersisterTest extends AbstractDaoTestCase { Snapshot snapshot = new Snapshot(); snapshot.setId(100); snapshot.setResourceId(200); - snapshots.put("org/struts/Action.java", snapshot); + snapshots.put("myProject", 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"); + data.setStringData("myProject", SnapshotDataTypes.FILE_HASHES, "org/struts/Action.java=123ABC"); SnapshotDataDao dataDao = new SnapshotDataDao(getMyBatis()); - ComponentDataPersister persister = new ComponentDataPersister(data, snapshots, dataDao, 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/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/ComponentDataPersisterTest/should_persist_component_data.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/FileHashesPersisterTest/should_persist_component_data.xml similarity index 100% rename from sonar-batch/src/test/resources/org/sonar/batch/index/ComponentDataPersisterTest/should_persist_component_data.xml rename to sonar-batch/src/test/resources/org/sonar/batch/index/FileHashesPersisterTest/should_persist_component_data.xml 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. -- 2.39.5