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));
}
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());
}
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);
}
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;
}
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);
}
Aggregations