RED-8702: Explore document databases to store entityLog

* added new repository queries for redaction-service partial entry querying
This commit is contained in:
maverickstuder 2024-03-27 17:35:39 +01:00
parent 45d75dc147
commit d447a08c69
5 changed files with 63 additions and 16 deletions

View File

@ -125,10 +125,10 @@ public class EntityLogMongoServiceTest extends AbstractPersistenceServerServiceT
var latest = entityLogMongoService.findLatestAnalysisNumber(TEST_DOSSIER_ID, TEST_FILE3_ID)
.orElseThrow();
var latestChangedEntries = entityLogMongoService.findAllEntityLogEntriesByAnalysisNumber(TEST_DOSSIER_ID, TEST_FILE3_ID, latest);
var latestChangedEntries = entityLogMongoService.findEntityLogEntriesByAnalysisNumber(TEST_DOSSIER_ID, TEST_FILE3_ID, latest);
assertEquals(latestChangedEntries.size(), 490);
var manuallyChangedEntries = entityLogMongoService.findAllEntityLogEntriesWithManualChanges(TEST_DOSSIER_ID, TEST_FILE3_ID);
var manuallyChangedEntries = entityLogMongoService.findEntityLogEntriesWithManualChanges(TEST_DOSSIER_ID, TEST_FILE3_ID);
assertEquals(manuallyChangedEntries.size(), 1014);
String NEW_ENTITY_LOG_ENTRY = """

View File

@ -18,4 +18,8 @@ public class EntityLogChanges {
private List<EntityLogEntry> newEntityLogEntries = new ArrayList<>();
private List<EntityLogEntry> updatedEntityLogEntries = new ArrayList<>();
public boolean hasChanges() {
return !newEntityLogEntries.isEmpty() || !updatedEntityLogEntries.isEmpty();
}
}

View File

@ -4,7 +4,6 @@ import java.util.Optional;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import com.iqser.red.service.persistence.service.v1.api.shared.mongo.document.EntityLogDocument;
@ -12,11 +11,11 @@ import com.iqser.red.service.persistence.service.v1.api.shared.mongo.document.En
@Repository
public interface EntityLogDocumentRepository extends MongoRepository<EntityLogDocument, String> {
@Query(value = "{ '_id' : ?0 }", fields = "{ 'analysisNumber' : 1 }")
Optional<EntityLogDocument> findAnalysisNumberById(@Param("_id") String id);
@Query(value = "{ 'id' : ?0 }", fields = "{ 'analysisNumber' : 1 }")
Optional<EntityLogDocument> findAnalysisNumberById(String id);
@Query(value = "{ '_id': ?0 }", fields = "{ 'entityLogEntryDocuments': 0 }")
@Query(value = "{ 'id': ?0 }", fields = "{ 'entityLogEntryDocuments': 0 }")
Optional<EntityLogDocument> findEntityLogDocumentWithoutEntriesById(String id);
}

View File

@ -1,6 +1,8 @@
package com.iqser.red.service.persistence.service.v1.api.shared.mongo.repository;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
@ -14,9 +16,11 @@ public interface EntityLogEntryDocumentRepository extends MongoRepository<Entity
@Query("{ 'entityLogId' : ?0, 'manualChanges' : { $exists: true, $not: { $size: 0 } } }")
List<EntityLogEntryDocument> findByEntityLogIdAndManualChangesNotEmpty(String entityLogId);
@Query("{ 'entityLogId' : ?0, 'changes.analysisNumber' : ?1 }")
List<EntityLogEntryDocument> findByEntityLogIdAndChangesAnalysisNumber(String entityLogId, int analysisNumber);
@Query("{ 'entityLogId' : ?0}")
List<EntityLogEntryDocument> findByEntityLogId(String entityLogId);
@ -25,10 +29,18 @@ public interface EntityLogEntryDocumentRepository extends MongoRepository<Entity
List<EntityLogEntryDocument> findByEntityLogIdAndContainingNodeIdNotEmpty(String entityLogId);
@Query("{ 'id' : { $in: ?0 }, 'containingNodeId' : { $exists: true, $not: { $size: 0 } } }")
List<EntityLogEntryDocument> findAllByIdAndContainingNodeIdNotEmpty(List<String> ids);
@Query("{ 'entityLogId' : ?0 , 'containingNodeId' : { $exists: true, $eq: { $size: 0 } } }")
List<EntityLogEntryDocument> findByEntityLogIdAndContainingNodeIdEmpty(String entityLogId);
@Query(value = "{ 'id' : { $in: ?0 }, 'containingNodeId' : { $exists: true, $not: { $size: 0 } } }", fields = "{ 'containingNodeId.0': 1 }")
List<EntityLogEntryDocument> findContainingNodeIdForAllByIdAndContainingNodeIdNotEmpty(List<String> ids);
@Query("{ 'entityLogId' : ?0, $or: [ { 'containingNodeId' : { $exists: true, $eq: { $size: 0 } } }, { 'containingNodeId.0' : { $in: ?1 } } ] }")
List<EntityLogEntryDocument> findByEntityLogIdAndNotContainedOrFirstContainedByElementInList(String entityLogId, Collection<Integer> containingNodeIds);
@Query(value = "{ 'entityLogId' : ?0}", delete = true)
void deleteByEntityLogId(String entityLogId);
}

View File

@ -4,6 +4,8 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;
@ -25,7 +27,8 @@ public class EntityLogMongoService {
private final EntityLogDocumentMapper mapper = EntityLogDocumentMapper.INSTANCE;
public EntityLogMongoService(EntityLogDocumentRepository entityLogDocumentRepository, EntityLogEntryDocumentRepository entityLogEntryDocumentRepository,
public EntityLogMongoService(EntityLogDocumentRepository entityLogDocumentRepository,
EntityLogEntryDocumentRepository entityLogEntryDocumentRepository,
EntityLogDocumentUpdateService entityLogDocumentUpdateService) {
this.entityLogDocumentRepository = entityLogDocumentRepository;
@ -165,8 +168,7 @@ public class EntityLogMongoService {
}
public List<EntityLogEntry> findAllEntityLogEntriesWithManualChanges(String dossierId, String fileId) {
public List<EntityLogEntry> findEntityLogEntriesWithManualChanges(String dossierId, String fileId) {
return entityLogEntryDocumentRepository.findByEntityLogIdAndManualChangesNotEmpty(mapper.getLogId(dossierId, fileId))
.stream()
@ -175,7 +177,7 @@ public class EntityLogMongoService {
}
public List<EntityLogEntry> findAllEntityLogEntriesByAnalysisNumber(String dossierId, String fileId, int analysisNumber) {
public List<EntityLogEntry> findEntityLogEntriesByAnalysisNumber(String dossierId, String fileId, int analysisNumber) {
return entityLogEntryDocumentRepository.findByEntityLogIdAndChangesAnalysisNumber(mapper.getLogId(dossierId, fileId), analysisNumber)
.stream()
@ -184,20 +186,48 @@ public class EntityLogMongoService {
}
public List<EntityLogEntry> findAllEntityLogEntriesWithContainingNodeIdsWithEntryIdsIn(String dossierId, String fileId, Collection<String> entryIds) {
public Set<Integer> findFirstContainingNodeIdForEachEntry(String dossierId, String fileId, Collection<String> entryIds) {
String entityLogId = mapper.getLogId(dossierId, fileId);
List<String> ids = entryIds.stream()
.map(entryId -> mapper.getLogEntryId(entityLogId, entryId))
.toList();
return entityLogEntryDocumentRepository.findAllByIdAndContainingNodeIdNotEmpty(ids)
return entityLogEntryDocumentRepository.findContainingNodeIdForAllByIdAndContainingNodeIdNotEmpty(ids)
.stream()
.filter(entityLogEntryDocument -> !entityLogEntryDocument.getContainingNodeId().isEmpty())
.map((EntityLogEntryDocument entityLogEntryDocument) -> entityLogEntryDocument.getContainingNodeId()
.get(0))
.collect(Collectors.toSet());
}
public List<EntityLogEntry> findEntityLogEntriesNotContainedOrFirstContainedByElementInList(String dossierId, String fileId, Collection<Integer> nodeIds) {
String entityLogId = mapper.getLogId(dossierId, fileId);
return entityLogEntryDocumentRepository.findByEntityLogIdAndNotContainedOrFirstContainedByElementInList(entityLogId, nodeIds)
.stream()
.map(mapper::fromLogEntryDocument)
.toList();
}
public List<EntityLogEntry> findAllEntityLogEntriesWithContainingNodeIds(String dossierId, String fileId) {
public List<EntityLogEntry> findEntityLogEntriesWithEntryIdsIn(String dossierId, String fileId, Collection<String> entryIds) {
String entityLogId = mapper.getLogId(dossierId, fileId);
List<String> ids = entryIds.stream()
.map(entryId -> mapper.getLogEntryId(entityLogId, entryId))
.toList();
return entityLogEntryDocumentRepository.findAllById(ids)
.stream()
.map(mapper::fromLogEntryDocument)
.toList();
}
public List<EntityLogEntry> findEntityLogEntriesWithContainingNodeIds(String dossierId, String fileId) {
return entityLogEntryDocumentRepository.findByEntityLogIdAndContainingNodeIdNotEmpty(mapper.getLogId(dossierId, fileId))
.stream()
@ -205,15 +235,17 @@ public class EntityLogMongoService {
.toList();
}
public Optional<EntityLog> findEntityLogWithoutEntries(String dossierId, String fileId) {
return entityLogDocumentRepository.findEntityLogDocumentWithoutEntriesById(mapper.getLogId(dossierId, fileId))
.map(mapper::fromLogDocument);
}
public void saveEntityLogWithoutEntries(String dossierId, String fileId, EntityLog entityLog) {
entityLogDocumentUpdateService.updateEntityLogDocumentWithoutEntries(dossierId, fileId, entityLog);
}
}