[Fix] fixed check for duplicate objects in the cache

This commit is contained in:
Reto Breitenmoser 2017-10-05 10:38:03 +02:00
parent 4087608e1a
commit 8525bbdf6a
1 changed files with 67 additions and 61 deletions

View File

@ -16,12 +16,18 @@
package li.strolch.utils.objectfilter;
import java.text.MessageFormat;
import java.util.*;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import li.strolch.utils.collections.MapOfMaps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import li.strolch.utils.collections.MapOfMaps;
/**
* This class implements a filter where modifications to an object are collected, and only the most recent action and
* version of the object is returned.
@ -130,8 +136,7 @@ public class ObjectFilter {
public void add(String key, Object objectKey, Object objectToAdd) {
if (ObjectFilter.logger.isDebugEnabled())
ObjectFilter.logger
.debug(MessageFormat.format("add object {0} with key {1}", objectToAdd, key)); //$NON-NLS-1$
ObjectFilter.logger.debug(MessageFormat.format("add object {0} with key {1}", objectToAdd, key)); //$NON-NLS-1$
// BEWARE: you fix a bug here, be sure to update BOTH tables on the logic.
ObjectCache cached = this.cache.getElement(key, objectKey);
@ -147,9 +152,8 @@ public class ObjectFilter {
String existingKey = cached.getKey();
if (!existingKey.equals(key)) {
String msg = "Invalid key provided for object with transaction ID {0} and operation {1}: existing key is {2}, new key is {3}. Object may be present in the same filter instance only once, registered using one key only. Object:{4}"; //$NON-NLS-1$
throw new IllegalArgumentException(MessageFormat
.format(msg, Long.toString(id), Operation.ADD.toString(), existingKey, key,
objectKey.toString()));
throw new IllegalArgumentException(MessageFormat.format(msg, Long.toString(id),
Operation.ADD.toString(), existingKey, key, objectKey.toString()));
}
// The object is in cache: update the version as required, keeping in mind that most
@ -207,13 +211,25 @@ public class ObjectFilter {
public void update(String key, Object objectKey, Object objectToUpdate) {
if (ObjectFilter.logger.isDebugEnabled())
ObjectFilter.logger
.debug(MessageFormat.format("update object {0} with key {1}", objectKey, key)); //$NON-NLS-1$
ObjectFilter.logger.debug(MessageFormat.format("update object {0} with key {1}", objectKey, key)); //$NON-NLS-1$
// BEWARE: you fix a bug here, be sure to update BOTH tables on the logic.
ObjectCache cached = this.cache.getElement(key, objectKey);
if (cached == null) {
List<ObjectCache> allElements = this.cache.getAllElements();
// check if the object is already in the cache with an other key
if (allElements.stream().anyMatch(oCache -> oCache.getObject().equals(objectToUpdate))) {
String msg = "Invalid key provided for object with transaction ID {0} and operation {1}: existing key is {2}, new key is {3}. Object may be present in the same filter instance only once, registered using one key only. Object:{4}"; //$NON-NLS-1$
Optional<ObjectCache> duplicateObj = allElements.stream()
.filter(oCache -> oCache.getObject().equals(objectToUpdate)).findFirst();
throw new IllegalArgumentException(MessageFormat.format(msg, Long.toString(id),
Operation.MODIFY.toString(), duplicateObj.get().getKey(), key, objectToUpdate.toString()));
}
// The object got an ID during this run, but was not added to this cache.
// Hence, we add it now, with the current operation.
ObjectCache cacheObj = new ObjectCache(dispenseID(), key, objectKey, objectToUpdate, Operation.MODIFY);
@ -221,14 +237,6 @@ public class ObjectFilter {
} else {
String existingKey = cached.getKey();
if (!existingKey.equals(key)) {
String msg = "Invalid key provided for object with transaction ID {0} and operation {1}: existing key is {2}, new key is {3}. Object may be present in the same filter instance only once, registered using one key only. Object:{4}"; //$NON-NLS-1$
throw new IllegalArgumentException(MessageFormat
.format(msg, Long.toString(id), Operation.MODIFY.toString(), existingKey, key,
objectToUpdate.toString()));
}
// The object is in cache: update the version as required.
Operation op = cached.getOperation();
switch (op) {
@ -281,8 +289,7 @@ public class ObjectFilter {
public void remove(String key, Object objectKey, Object objectToRemove) {
if (ObjectFilter.logger.isDebugEnabled())
ObjectFilter.logger
.debug(MessageFormat.format("remove object {0} with key {1}", objectKey, key)); //$NON-NLS-1$
ObjectFilter.logger.debug(MessageFormat.format("remove object {0} with key {1}", objectKey, key)); //$NON-NLS-1$
// BEWARE: you fix a bug here, be sure to update BOTH tables on the logic.
ObjectCache cached = this.cache.getElement(key, objectKey);
@ -296,9 +303,8 @@ public class ObjectFilter {
String existingKey = cached.getKey();
if (!existingKey.equals(key)) {
String msg = "Invalid key provided for object with transaction ID {0} and operation {1}: existing key is {2}, new key is {3}. Object may be present in the same filter instance only once, registered using one key only. Object:{4}"; //$NON-NLS-1$
throw new IllegalArgumentException(MessageFormat
.format(msg, Long.toString(id), Operation.REMOVE.toString(), existingKey, key,
objectKey.toString()));
throw new IllegalArgumentException(MessageFormat.format(msg, Long.toString(id),
Operation.REMOVE.toString(), existingKey, key, objectKey.toString()));
}
// The object is in cache: update the version as required.