Skip to content
This repository has been archived by the owner on Mar 31, 2022. It is now read-only.

Commit

Permalink
Fix indexing of deleted instances #78
Browse files Browse the repository at this point in the history
  • Loading branch information
Gavrilov-Ivan committed Aug 3, 2021
1 parent aa18324 commit dc52384
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public class EntityIndexerImpl implements EntityIndexer {
protected IdSerialization idSerialization;
@Autowired
protected IndexStateRegistry indexStateRegistry;
@Autowired
protected MetadataTools metadataTools;

protected ObjectMapper objectMapper = new ObjectMapper();

Expand Down Expand Up @@ -184,10 +186,26 @@ protected Map<IndexConfiguration, Collection<Object>> reloadEntityInstances(Map<
if (indexConfigurationOpt.isPresent()) {
IndexConfiguration indexConfiguration = indexConfigurationOpt.get();
FetchPlan fetchPlan = fetchPlanLocalCache.computeIfAbsent(indexConfiguration, this::createFetchPlan);
List<Object> loaded = dataManager.load(metaClass.getJavaClass())
.ids(entityIds)
.fetchPlan(fetchPlan)
.list();
List<Object> loaded;
if (metadataTools.hasCompositePrimaryKey(metaClass)) {
loaded = entityIds.stream()
.map(id -> dataManager
.load(metaClass.getJavaClass())
.id(id)
.fetchPlan(fetchPlan)
.optional())
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
} else {
String primaryKeyName = metadataTools.getPrimaryKeyName(metaClass);
loaded = dataManager
.load(metaClass.getJavaClass())
.query("select e from " + metaClass.getName() + " e where e." + primaryKeyName + " in :ids")
.parameter("ids", entityIds)
.fetchPlan(fetchPlan)
.list();
}
result.put(indexConfiguration, loaded);
}
});
Expand Down
30 changes: 30 additions & 0 deletions search/src/test/java/indexing/EntityIndexingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import test_support.*;
import test_support.entity.TestEmbeddableEntity;
import test_support.entity.TestEnum;
import test_support.entity.TestRootEntity;
import test_support.entity.TestRootEntityHD;
import test_support.entity.indexing.*;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -577,4 +579,32 @@ public void indexWithIndexablePredicate() {
TestBulkRequestValidationResult result = TestBulkRequestValidator.validate(Collections.singletonList(expectedData), bulkRequests);
Assert.assertFalse(result.toString(), result.hasFailures());
}

@Test
@DisplayName("Indexing already deleted instance (Soft Delete)")
public void indexDeletedInstance() {
TestRootEntity instance = metadata.create(TestRootEntity.class);
instance.setName("Deleted");

dataManager.save(instance);
dataManager.remove(instance);

entityIndexer.index(instance);
List<BulkRequest> bulkRequests = bulkRequestsTracker.getBulkRequests();
Assert.assertTrue(bulkRequests.isEmpty());
}

@Test
@DisplayName("Indexing already deleted instance (Hard Delete)")
public void indexDeletedInstanceHardDelete() {
TestRootEntityHD instance = metadata.create(TestRootEntityHD.class);
instance.setName("Deleted");

dataManager.save(instance);
dataManager.remove(instance);

entityIndexer.index(instance);
List<BulkRequest> bulkRequests = bulkRequestsTracker.getBulkRequests();
Assert.assertTrue(bulkRequests.isEmpty());
}
}

0 comments on commit dc52384

Please sign in to comment.