private static void populateIndex(CloneIndex duplicationIndex, Collection<Block> duplicationBlocks) { for (Block block : duplicationBlocks) { duplicationIndex.insert(block); } }
protected static CloneIndex createIndex(Block[]... blocks) { CloneIndex cloneIndex = new MemoryCloneIndex(); for (Block[] b : blocks) { for (Block block : b) { cloneIndex.insert(block); } } return cloneIndex; }
private void addToIndex(File file) throws IOException { List<Block> blocks = bridge.chunk(file.getAbsolutePath(), file.getAbsolutePath(), Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)); for (Block block : blocks) { index.insert(block); } }
/** * Given: index with initial capacity 1. * Expected: size and capacity should be increased after insertion of two blocks. */ @Test public void should_increase_capacity() { CloneIndex index = new PackedMemoryCloneIndex(8, 1); index.insert(newBlock("a", 1)); index.insert(newBlock("a", 2)); assertThat(index.getByResourceId("a").size(), is(2)); }
/** * Given: index, which accepts blocks with 4-byte hash. * Expected: exception during insertion of block with 8-byte hash. */ @Test(expected = IllegalArgumentException.class) public void attempt_to_insert_hash_of_incorrect_size() { CloneIndex index = new PackedMemoryCloneIndex(4, 1); index.insert(newBlock("a", 1)); }
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); }
private static void addToIndex(CloneIndex index, String resourceId, String sourceCode) { List<Statement> statements = STATEMENT_CHUNKER.chunk(TOKEN_CHUNKER.chunk(sourceCode)); BlockChunker blockChunker = new BlockChunker(2); List<Block> blocks = blockChunker.chunk(resourceId, statements); for (Block block : blocks) { index.insert(block); } }
private static void populateIndex(CloneIndex duplicationIndex, Collection<Block> duplicationBlocks) { for (Block block : duplicationBlocks) { duplicationIndex.insert(block); } }
public void insert(InputFile inputFile, Collection<Block> blocks) { for (Block block : blocks) { mem.insert(block); } if (db != null) { db.insert(inputFile, blocks); } }
public void insert(InputFile inputFile, Collection<Block> blocks) { if (isCrossProjectDuplicationEnabled(settings)) { int id = batchComponentCache.get(inputFile).batchId(); final BatchReport.CpdTextBlock.Builder builder = BatchReport.CpdTextBlock.newBuilder(); publisher.getWriter().writeCpdTextBlocks(id, Iterables.transform(blocks, new Function<Block, BatchReport.CpdTextBlock>() { @Override public BatchReport.CpdTextBlock apply(Block input) { builder.clear(); builder.setStartLine(input.getStartLine()); builder.setEndLine(input.getEndLine()); builder.setStartTokenIndex(input.getStartUnit()); builder.setEndTokenIndex(input.getEndUnit()); builder.setHash(input.getBlockHash().toHexString()); return builder.build(); } })); } for (Block block : blocks) { mem.insert(block); } }
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); }