Merge branch 'NCBACK-25' into dev
# Conflicts: # src/main/java/ru/soune/nocopy/repository/SimilarImageProjection.java # src/main/java/ru/soune/nocopy/service/FileSimilarityService.java
This commit is contained in:
@@ -5,12 +5,14 @@ import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.soune.nocopy.dto.file.SimilarFileResponse;
|
||||
import ru.soune.nocopy.dto.file.SimilarFileDTO;
|
||||
import ru.soune.nocopy.dto.file.SimilarityFilter;
|
||||
import ru.soune.nocopy.repository.ImageHashRepository;
|
||||
import ru.soune.nocopy.repository.ImageSimilarityRepository;
|
||||
import ru.soune.nocopy.repository.SimilarImageProjection;
|
||||
import ru.soune.nocopy.util.FileUtil;
|
||||
|
||||
import java.util.AbstractMap;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
@@ -19,26 +21,28 @@ import java.util.stream.Collectors;
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class FileSimilarityService {
|
||||
|
||||
private final ImageSimilarityRepository repository;
|
||||
|
||||
private final ImageHashRepository hashRepository;
|
||||
|
||||
public List<SimilarFileResponse> findSimilarFiles(String fileId) {
|
||||
private final FileUtil fileUtil;
|
||||
|
||||
public List<SimilarFileDTO> findSimilarFiles(String fileId) {
|
||||
var imageHashEntity = hashRepository.findById(fileId)
|
||||
.orElseThrow(() -> new RuntimeException("Hash not found"));
|
||||
|
||||
Integer hash64Hi = imageHashEntity.getHash64Hi();
|
||||
Integer hash64Lo = imageHashEntity.getHash64Lo();
|
||||
Long hash64Hi = imageHashEntity.getHash64Hi();
|
||||
Long hash64Lo = imageHashEntity.getHash64Lo();
|
||||
|
||||
List<SimilarImageProjection> candidates =
|
||||
repository.findCandidates(fileId);
|
||||
List<SimilarImageProjection> candidates = repository.findCandidates(fileId);
|
||||
|
||||
return candidates.stream()
|
||||
.map(c -> {
|
||||
Integer cHi = c.getHash64Hi();
|
||||
Integer cLo = c.getHash64Lo();
|
||||
Long cHi = c.getHash64Hi();
|
||||
Long cLo = c.getHash64Lo();
|
||||
|
||||
int hamming = hamming64(hash64Hi, hash64Lo, cHi, cLo);
|
||||
int hamming = fileUtil.hamming64(hash64Hi, hash64Lo, cHi, cLo);
|
||||
|
||||
String level;
|
||||
if (hamming <= 5) {
|
||||
@@ -49,7 +53,7 @@ public class FileSimilarityService {
|
||||
level = "DIFFERENT";
|
||||
}
|
||||
|
||||
return SimilarFileResponse.builder()
|
||||
return SimilarFileDTO.builder()
|
||||
.fileId(c.getId())
|
||||
.originalFileName(c.getOriginalFileName())
|
||||
.fileSize(c.getFileSize())
|
||||
@@ -62,7 +66,53 @@ public class FileSimilarityService {
|
||||
.toList();
|
||||
}
|
||||
|
||||
public List<SimilarImageProjection> findDuplicatedByHash(Integer hash64Hi, Integer hash64Lo) {
|
||||
public List<SimilarFileDTO> findDuplicateByHammingDistance(String fileId, int hammingDistance,
|
||||
int duplicate, int similar) {
|
||||
List<SimilarImageProjection> candidates = repository.findCandidates(fileId);
|
||||
|
||||
var imageHashEntity = hashRepository.findById(fileId)
|
||||
.orElseThrow(() -> new RuntimeException("Hash not found"));
|
||||
|
||||
Long hash64Hi = imageHashEntity.getHash64Hi();
|
||||
Long hash64Lo = imageHashEntity.getHash64Lo();
|
||||
|
||||
return candidates.stream()
|
||||
.map(c -> {
|
||||
Long cHi = c.getHash64Hi();
|
||||
Long cLo = c.getHash64Lo();
|
||||
|
||||
int hamming = fileUtil.hamming64(hash64Hi, hash64Lo, cHi, cLo);
|
||||
|
||||
return new AbstractMap.SimpleEntry<>(c, hamming);
|
||||
})
|
||||
.filter(entry -> entry.getValue() <= hammingDistance)
|
||||
.sorted((a, b) -> Integer.compare(a.getValue(), b.getValue()))
|
||||
.map(entry -> {
|
||||
SimilarImageProjection similarImageProjection = entry.getKey();
|
||||
int hamming = entry.getValue();
|
||||
|
||||
String level;
|
||||
if (hamming <= duplicate) {
|
||||
level = "DUPLICATE";
|
||||
} else if (hamming <= similar) {
|
||||
level = "SIMILAR";
|
||||
} else {
|
||||
level = "DIFFERENT";
|
||||
}
|
||||
|
||||
return SimilarFileDTO.builder()
|
||||
.fileId(similarImageProjection.getFileId())
|
||||
.originalFileName(similarImageProjection.getOriginalFileName())
|
||||
.fileSize(similarImageProjection.getFileSize())
|
||||
.hammingDistance(hamming)
|
||||
.similarityLevel(level)
|
||||
.ownerId(similarImageProjection.getUserId())
|
||||
.build();
|
||||
})
|
||||
.toList();
|
||||
}
|
||||
|
||||
public List<SimilarImageProjection> findDuplicatedByHash(Long hash64Hi, Long hash64Lo) {
|
||||
List<SimilarImageProjection> duplicates = repository.findExactDuplicates(hash64Hi,hash64Lo);
|
||||
|
||||
if (duplicates.isEmpty()) {
|
||||
@@ -72,12 +122,12 @@ public class FileSimilarityService {
|
||||
return duplicates;
|
||||
}
|
||||
|
||||
public Page<SimilarFileResponse> findSimilarFiles(String fileId, SimilarityFilter filter, Pageable pageable) {
|
||||
public Page<SimilarFileDTO> findSimilarFiles(String fileId, SimilarityFilter filter, Pageable pageable) {
|
||||
var imageHashEntity = hashRepository.findById(fileId)
|
||||
.orElseThrow(() -> new RuntimeException("Hash not found"));
|
||||
|
||||
Integer hash64Hi = imageHashEntity.getHash64Hi();
|
||||
Integer hash64Lo = imageHashEntity.getHash64Lo();
|
||||
Long hash64Hi = imageHashEntity.getHash64Hi();
|
||||
Long hash64Lo = imageHashEntity.getHash64Lo();
|
||||
|
||||
List<SimilarImageProjection> candidates = repository.findCandidates(fileId);
|
||||
|
||||
@@ -85,10 +135,10 @@ public class FileSimilarityService {
|
||||
? filter.getSimilarityLevels()
|
||||
: List.of("DUPLICATE", "SIMILAR", "DIFFERENT");
|
||||
|
||||
List<SimilarFileResponse> allResults = candidates.stream()
|
||||
List<SimilarFileDTO> allResults = candidates.stream()
|
||||
.map(c -> createSimilarFileResponse(c, hash64Hi, hash64Lo))
|
||||
.filter(response -> similarityLevels.contains(response.getSimilarityLevel()))
|
||||
.sorted(Comparator.comparingInt(SimilarFileResponse::getHammingDistance))
|
||||
.sorted(Comparator.comparingInt(SimilarFileDTO::getHammingDistance))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
int total = allResults.size();
|
||||
@@ -98,17 +148,17 @@ public class FileSimilarityService {
|
||||
int fromIndex = Math.min(page * size, total);
|
||||
int toIndex = Math.min(fromIndex + size, total);
|
||||
|
||||
List<SimilarFileResponse> pageContent = allResults.subList(fromIndex, toIndex);
|
||||
List<SimilarFileDTO> pageContent = allResults.subList(fromIndex, toIndex);
|
||||
|
||||
return new PageImpl<>(pageContent, pageable, total);
|
||||
}
|
||||
|
||||
private SimilarFileResponse createSimilarFileResponse(SimilarImageProjection similarImageProjection,
|
||||
Integer hash64Hi, Integer hash64Lo) {
|
||||
Integer imageProjectionHash64Hi = similarImageProjection.getHash64Hi();
|
||||
Integer similarImageProjectionHash64Lo = similarImageProjection.getHash64Lo();
|
||||
private SimilarFileDTO createSimilarFileResponse(SimilarImageProjection similarImageProjection,
|
||||
Long hash64Hi, Long hash64Lo) {
|
||||
Long imageProjectionHash64Hi = similarImageProjection.getHash64Hi();
|
||||
Long similarImageProjectionHash64Lo = similarImageProjection.getHash64Lo();
|
||||
|
||||
int hamming = hamming64(hash64Hi, hash64Lo, imageProjectionHash64Hi, similarImageProjectionHash64Lo);
|
||||
int hamming = fileUtil.hamming64(hash64Hi, hash64Lo, imageProjectionHash64Hi, similarImageProjectionHash64Lo);
|
||||
|
||||
String level;
|
||||
if (hamming <= 5) {
|
||||
@@ -119,7 +169,7 @@ public class FileSimilarityService {
|
||||
level = "DIFFERENT";
|
||||
}
|
||||
|
||||
return SimilarFileResponse.builder()
|
||||
return SimilarFileDTO.builder()
|
||||
.fileId(similarImageProjection.getId())
|
||||
.originalFileName(similarImageProjection.getOriginalFileName())
|
||||
.fileSize(similarImageProjection.getFileSize())
|
||||
@@ -127,9 +177,4 @@ public class FileSimilarityService {
|
||||
.similarityLevel(level)
|
||||
.build();
|
||||
}
|
||||
|
||||
private int hamming64(int aHi, int aLo, int bHi, int bLo) {
|
||||
return Integer.bitCount(aHi ^ bHi)
|
||||
+ Integer.bitCount(aLo ^ bLo);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user