Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DROOLS-7552: improve updateObjectTypesList() #5552

Merged
merged 2 commits into from
Oct 13, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,20 @@

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

public class SimpleSerializationReliableRefObjectStore extends SimpleSerializationReliableObjectStore {

private Map<String, Long> uniqueObjectTypesInStore; // object type name, occurances
private Set<String> uniqueObjectTypesInStorage;
private transient IdentityHashMap<Object, Long> inverseStorage;

public SimpleSerializationReliableRefObjectStore(Storage<Long, StoredObject> storage) {
super(storage);
uniqueObjectTypesInStore = new HashMap<>();
uniqueObjectTypesInStorage = new HashSet<>();
setInverseStorage(storage);
if (!storage.isEmpty()) {
updateObjectTypesList();
Expand Down Expand Up @@ -119,32 +116,25 @@ private Long fromObjectToFactHandleId(Object object) {

private List<Field> getReferencedObjects(Object object) {
Field[] fields = object.getClass().getDeclaredFields();

return Arrays.stream(fields)
.filter(field -> uniqueObjectTypesInStore.containsKey(field.getType().getName()))
.filter(field -> uniqueObjectTypesInStorage.contains(field.getType().getName()))
.collect(Collectors.toList());
}

private void updateObjectTypesList(Object object) {
uniqueObjectTypesInStore.put(object.getClass().getName(),
storage.values().stream().filter(sObject -> sObject.getObject().getClass().equals(object.getClass())).count());
// if count==0 then remove entry
if (uniqueObjectTypesInStore.get(object.getClass().getName()) == 0) {
uniqueObjectTypesInStore.remove(object.getClass().getName());
if (inverseStorage.keySet().stream().filter(sObject -> sObject.getClass().equals(object.getClass())).findAny()!=null){
Copy link
Contributor

@tkobayas tkobayas Oct 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

findAny() returns Optional.empty() when there is no match, so comparing with null is not right, I think. Please check the execution path when the object class doesn't exist in inverseStorage.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tkobayas your comment is correct, comparing with null is not right, it always returns true. So, I will replace line 125 with
if (!inverseStorage.keySet().stream().filter(sObject -> sObject.getClass().equals(object.getClass())).findAny().isEmpty())

uniqueObjectTypesInStorage.add(object.getClass().getName());
}else{
uniqueObjectTypesInStorage.remove(object.getClass().getName());
}
}

private void updateObjectTypesList() {
// list of unique object types in storage
Set<String> uTypeNames = new HashSet<String>();
uniqueObjectTypesInStorage.clear();
storage.values().forEach(sObject -> {
uTypeNames.add(sObject.getObject().getClass().getName());
uniqueObjectTypesInStorage.add(sObject.getObject().getClass().getName());
});
// add unique object types + their occurrences in the uniqueObjectTypesInStore
uniqueObjectTypesInStore.putAll(storage.values().stream()
.map(sObject -> sObject.getObject().getClass().getName())
.filter(uTypeNames::contains)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting())));
}
}