RED-6467: Implemented undeletion of dictionary entries via the criteria builder.
Hopefully, this solves the db-driver issue with large numbers of entries.
This commit is contained in:
parent
e72c68843c
commit
62dbe67d63
@ -4,6 +4,8 @@ import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
@ -33,6 +35,8 @@ public class EntryPersistenceService {
|
||||
private final FalseRecommendationEntryRepository falseRecommendationEntryRepository;
|
||||
private final JDBCWriteUtils jdbcWriteUtils;
|
||||
|
||||
private final EntityManager entityManager;
|
||||
|
||||
|
||||
@Transactional
|
||||
public void deleteEntries(String typeId, List<String> values, long version, DictionaryEntryType dictionaryEntryType) {
|
||||
@ -110,7 +114,7 @@ public class EntryPersistenceService {
|
||||
case ENTRY -> {
|
||||
// TODO Remove all but one implementation for undeletion, once it is clear which one works
|
||||
//var undeletedEntries = entryRepository.undeleteEntries(typeId, entries, version);
|
||||
List<String> undeletedEntries = undeleteEntries(typeId, entries, version);
|
||||
var undeletedEntries = undeleteEntries(typeId, entries, version);
|
||||
|
||||
undeletedEntries.forEach(entries::remove);
|
||||
|
||||
@ -126,7 +130,7 @@ public class EntryPersistenceService {
|
||||
jdbcWriteUtils.saveBatch(entryEntities);
|
||||
}
|
||||
case FALSE_POSITIVE -> {
|
||||
var undeletedEntries = falsePositiveEntryRepository.undeleteEntries(typeId, entries, version);
|
||||
var undeletedEntries = undeleteFalsePositives(typeId, entries, version);
|
||||
undeletedEntries.forEach(entries::remove);
|
||||
|
||||
var entryEntities = entries.stream().map(e -> {
|
||||
@ -140,7 +144,7 @@ public class EntryPersistenceService {
|
||||
jdbcWriteUtils.saveBatch(entryEntities);
|
||||
}
|
||||
case FALSE_RECOMMENDATION -> {
|
||||
var undeletedEntries = falseRecommendationEntryRepository.undeleteEntries(typeId, entries, version);
|
||||
var undeletedEntries = undeleteFalseRecommendations(typeId, entries, version);
|
||||
undeletedEntries.forEach(entries::remove);
|
||||
|
||||
var entryEntities = entries.stream().map(e -> {
|
||||
@ -159,15 +163,70 @@ public class EntryPersistenceService {
|
||||
|
||||
private List<String> undeleteEntries(String typeId, Set<String> entries, long version) {
|
||||
|
||||
List<DictionaryEntryEntity> entryEntities = entryRepository.findAllByTypeIdAndValueIn(typeId, entries);
|
||||
entryEntities.forEach(e -> {
|
||||
e.setDeleted(false);
|
||||
e.setVersion(version);
|
||||
});
|
||||
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||
var undeleteEntriesQuery = criteriaBuilder.createCriteriaUpdate(DictionaryEntryEntity.class);
|
||||
var entryRoot = undeleteEntriesQuery.from(DictionaryEntryEntity.class);
|
||||
|
||||
entryRepository.saveAll(entryEntities);
|
||||
var typeIdPred = criteriaBuilder.equal(entryRoot.get("typeId"), typeId);
|
||||
var entriesPred = entryRoot.get("value").in(entries);
|
||||
undeleteEntriesQuery.where(typeIdPred, entriesPred);
|
||||
undeleteEntriesQuery.set("version", version);
|
||||
undeleteEntriesQuery.set("deleted", false);
|
||||
|
||||
int updated = entityManager.createQuery(undeleteEntriesQuery).executeUpdate();
|
||||
|
||||
var existingEntriesQuery = criteriaBuilder.createQuery(DictionaryEntryEntity.class);
|
||||
entryRoot = existingEntriesQuery.from(DictionaryEntryEntity.class);
|
||||
existingEntriesQuery.where(typeIdPred, criteriaBuilder.equal(entryRoot.get("deleted"), false));
|
||||
|
||||
return entityManager.createQuery(existingEntriesQuery).getResultStream().map(DictionaryEntryEntity::getValue).toList();
|
||||
|
||||
}
|
||||
|
||||
|
||||
private List<String> undeleteFalsePositives(String typeId, Set<String> entries, long version) {
|
||||
|
||||
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||
var undeleteEntriesQuery = criteriaBuilder.createCriteriaUpdate(DictionaryFalsePositiveEntryEntity.class);
|
||||
var entryRoot = undeleteEntriesQuery.from(DictionaryFalsePositiveEntryEntity.class);
|
||||
|
||||
var typeIdPred = criteriaBuilder.equal(entryRoot.get("typeId"), typeId);
|
||||
var entriesPred = entryRoot.get("value").in(entries);
|
||||
undeleteEntriesQuery.where(typeIdPred, entriesPred);
|
||||
undeleteEntriesQuery.set("version", version);
|
||||
undeleteEntriesQuery.set("deleted", false);
|
||||
|
||||
int updated = entityManager.createQuery(undeleteEntriesQuery).executeUpdate();
|
||||
|
||||
var existingEntriesQuery = criteriaBuilder.createQuery(DictionaryFalsePositiveEntryEntity.class);
|
||||
entryRoot = existingEntriesQuery.from(DictionaryFalsePositiveEntryEntity.class);
|
||||
existingEntriesQuery.where(typeIdPred, criteriaBuilder.equal(entryRoot.get("deleted"), false));
|
||||
|
||||
return entityManager.createQuery(existingEntriesQuery).getResultStream().map(DictionaryFalsePositiveEntryEntity::getValue).toList();
|
||||
|
||||
}
|
||||
|
||||
|
||||
private List<String> undeleteFalseRecommendations(String typeId, Set<String> entries, long version) {
|
||||
|
||||
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||
var undeleteEntriesQuery = criteriaBuilder.createCriteriaUpdate(DictionaryFalseRecommendationEntryEntity.class);
|
||||
var entryRoot = undeleteEntriesQuery.from(DictionaryFalseRecommendationEntryEntity.class);
|
||||
|
||||
var typeIdPred = criteriaBuilder.equal(entryRoot.get("typeId"), typeId);
|
||||
var entriesPred = entryRoot.get("value").in(entries);
|
||||
undeleteEntriesQuery.where(typeIdPred, entriesPred);
|
||||
undeleteEntriesQuery.set("version", version);
|
||||
undeleteEntriesQuery.set("deleted", false);
|
||||
|
||||
int updated = entityManager.createQuery(undeleteEntriesQuery).executeUpdate();
|
||||
|
||||
var existingEntriesQuery = criteriaBuilder.createQuery(DictionaryFalseRecommendationEntryEntity.class);
|
||||
entryRoot = existingEntriesQuery.from(DictionaryFalseRecommendationEntryEntity.class);
|
||||
existingEntriesQuery.where(typeIdPred, criteriaBuilder.equal(entryRoot.get("deleted"), false));
|
||||
|
||||
return entityManager.createQuery(existingEntriesQuery).getResultStream().map(DictionaryFalseRecommendationEntryEntity::getValue).toList();
|
||||
|
||||
return entryEntities.stream().map(DictionaryEntryEntity::getValue).toList();
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user