Search in sources :

Example 31 with Block

use of org.sonar.duplications.block.Block in project sonarqube by SonarSource.

the class SuffixTreeCloneDetectionAlgorithmTest method noDuplications.

/**
 * Given: file without duplications
 * Expected: {@link Collections#EMPTY_LIST} (no need to construct suffix-tree)
 */
@Test
public void noDuplications() {
    CloneIndex index = createIndex();
    Block[] fileBlocks = newBlocks("a", "1 2 3");
    List<CloneGroup> result = detect(index, fileBlocks);
    assertThat(result, sameInstance(Collections.EMPTY_LIST));
}
Also used : Block(org.sonar.duplications.block.Block) CloneIndex(org.sonar.duplications.index.CloneIndex) CloneGroup(org.sonar.duplications.index.CloneGroup) CloneGroupMatcher.hasCloneGroup(org.sonar.duplications.detector.CloneGroupMatcher.hasCloneGroup) Test(org.junit.Test)

Example 32 with Block

use of org.sonar.duplications.block.Block in project sonarqube by SonarSource.

the class SuffixTreeCloneDetectionAlgorithmTest method huge.

/**
 * See SONAR-3060
 * <p>
 * In case when file contains a lot of duplicated blocks suffix-tree works better than original algorithm,
 * which works more than 5 minutes for this example.
 * </p><p>
 * However should be noted that current implementation with suffix-tree also is not optimal,
 * even if it works for this example couple of seconds,
 * because duplications should be filtered in order to remove fully-covered.
 * But such cases nearly never appear in real-world, so current implementation is acceptable for the moment.
 * </p>
 */
@Test
public void huge() {
    CloneIndex index = createIndex();
    Block[] fileBlocks = new Block[5000];
    for (int i = 0; i < 5000; i++) {
        fileBlocks[i] = newBlock("x", new ByteArray("01"), i);
    }
    List<CloneGroup> result = detect(index, fileBlocks);
    assertEquals(1, result.size());
}
Also used : Block(org.sonar.duplications.block.Block) ByteArray(org.sonar.duplications.block.ByteArray) CloneIndex(org.sonar.duplications.index.CloneIndex) CloneGroup(org.sonar.duplications.index.CloneGroup) CloneGroupMatcher.hasCloneGroup(org.sonar.duplications.detector.CloneGroupMatcher.hasCloneGroup) Test(org.junit.Test)

Example 33 with Block

use of org.sonar.duplications.block.Block in project sonarqube by SonarSource.

the class SonarCpdBlockIndex method insert.

public void insert(InputFile inputFile, Collection<Block> blocks) {
    if (settings.isCrossProjectDuplicationEnabled()) {
        int id = ((DefaultInputFile) inputFile).scannerId();
        if (publisher.getWriter().hasComponentData(FileStructure.Domain.CPD_TEXT_BLOCKS, id)) {
            throw new UnsupportedOperationException("Trying to save CPD tokens twice for the same file is not supported: " + inputFile.absolutePath());
        }
        final ScannerReport.CpdTextBlock.Builder builder = ScannerReport.CpdTextBlock.newBuilder();
        publisher.getWriter().writeCpdTextBlocks(id, blocks.stream().map(block -> {
            builder.clear();
            builder.setStartLine(block.getStartLine());
            builder.setEndLine(block.getEndLine());
            builder.setStartTokenIndex(block.getStartUnit());
            builder.setEndTokenIndex(block.getEndUnit());
            builder.setHash(block.getBlockHash().toHexString());
            return builder.build();
        }).collect(Collectors.toList()));
    }
    for (Block block : blocks) {
        mem.insert(block);
    }
    if (blocks.isEmpty()) {
        LOG.debug("Not enough content in '{}' to have CPD blocks, it will not be part of the duplication detection", inputFile.relativePath());
    }
    indexedFiles.add(inputFile);
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Block(org.sonar.duplications.block.Block)

Example 34 with Block

use of org.sonar.duplications.block.Block in project sonarqube by SonarSource.

the class SuffixTreeCloneDetectionAlgorithm method retrieveFromIndex.

private static Map<String, List<Block>> retrieveFromIndex(CloneIndex index, String originResourceId, Set<ByteArray> hashes) {
    Map<String, List<Block>> collection = new HashMap<>();
    for (ByteArray hash : hashes) {
        Collection<Block> blocks = index.getBySequenceHash(hash);
        for (Block blockFromIndex : blocks) {
            // Godin: skip blocks for this file if they come from index
            String resourceId = blockFromIndex.getResourceId();
            if (!originResourceId.equals(resourceId)) {
                List<Block> list = collection.get(resourceId);
                if (list == null) {
                    list = new ArrayList<>();
                    collection.put(resourceId, list);
                }
                list.add(blockFromIndex);
            }
        }
    }
    return collection;
}
Also used : HashMap(java.util.HashMap) ByteArray(org.sonar.duplications.block.ByteArray) Block(org.sonar.duplications.block.Block) ArrayList(java.util.ArrayList) List(java.util.List)

Example 35 with Block

use of org.sonar.duplications.block.Block in project sonarqube by SonarSource.

the class SuffixTreeCloneDetectionAlgorithm method createTextSet.

private static TextSet createTextSet(CloneIndex index, Collection<Block> fileBlocks) {
    Set<ByteArray> hashes = new HashSet<>();
    for (Block fileBlock : fileBlocks) {
        hashes.add(fileBlock.getBlockHash());
    }
    String originResourceId = fileBlocks.iterator().next().getResourceId();
    Map<String, List<Block>> fromIndex = retrieveFromIndex(index, originResourceId, hashes);
    if (fromIndex.isEmpty() && hashes.size() == fileBlocks.size()) {
        // optimization for the case when there is no duplications
        return null;
    }
    return createTextSet(fileBlocks, fromIndex);
}
Also used : ByteArray(org.sonar.duplications.block.ByteArray) Block(org.sonar.duplications.block.Block) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Aggregations

Block (org.sonar.duplications.block.Block)56 Test (org.junit.Test)37 ByteArray (org.sonar.duplications.block.ByteArray)29 CloneGroup (org.sonar.duplications.index.CloneGroup)18 CloneIndex (org.sonar.duplications.index.CloneIndex)16 CloneGroupMatcher.hasCloneGroup (org.sonar.duplications.detector.CloneGroupMatcher.hasCloneGroup)15 ArrayList (java.util.ArrayList)13 MemoryCloneIndex (org.sonar.duplications.index.MemoryCloneIndex)12 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)8 ClonePart (org.sonar.duplications.index.ClonePart)5 IOException (java.io.IOException)4 List (java.util.List)4 InputFile (org.sonar.api.batch.fs.InputFile)4 Statement (org.sonar.duplications.statement.Statement)3 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2