[Major] added new methods ElementMap.removeAll()
This commit is contained in:
parent
281ce4e826
commit
ddf7c95bb5
|
@ -97,4 +97,8 @@ public interface ElementMap<T extends StrolchElement> {
|
|||
public void remove(StrolchTransaction tx, T element);
|
||||
|
||||
public void removeAll(StrolchTransaction tx, List<T> elements);
|
||||
|
||||
public long removeAll(StrolchTransaction tx);
|
||||
|
||||
public long removeAllBy(StrolchTransaction tx, String type);
|
||||
}
|
||||
|
|
|
@ -24,6 +24,9 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import li.strolch.agent.api.ElementMap;
|
||||
import li.strolch.model.StrolchElement;
|
||||
import li.strolch.persistence.api.StrolchDao;
|
||||
|
@ -37,6 +40,7 @@ import ch.eitchnet.utils.dbc.DBC;
|
|||
*/
|
||||
public abstract class CachedElementMap<T extends StrolchElement> implements ElementMap<T> {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(CachedElementMap.class);
|
||||
private Set<String> allKeys;
|
||||
private Map<String, Map<String, T>> elementMap;
|
||||
|
||||
|
@ -374,4 +378,61 @@ public abstract class CachedElementMap<T extends StrolchElement> implements Elem
|
|||
// last is to perform DB changes
|
||||
getDao(tx).removeAll(elements);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long removeAll(StrolchTransaction tx) {
|
||||
|
||||
if (this.elementMap.isEmpty())
|
||||
return 0;
|
||||
|
||||
long removed = 0;
|
||||
|
||||
synchronized (this.elementMap) {
|
||||
Set<String> types = this.elementMap.keySet();
|
||||
for (String type : types) {
|
||||
|
||||
Map<String, T> byType = this.elementMap.get(type);
|
||||
removed += byType.size();
|
||||
byType.clear();
|
||||
}
|
||||
}
|
||||
|
||||
// last is to perform DB changes
|
||||
long daoRemoved = getDao(tx).removeAll();
|
||||
|
||||
if (removed != daoRemoved) {
|
||||
String msg = "Removed {0} elements from cached map, but dao removed {1} elements!";
|
||||
logger.error(MessageFormat.format(msg, removed, daoRemoved));
|
||||
}
|
||||
|
||||
return removed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long removeAllBy(StrolchTransaction tx, String type) {
|
||||
|
||||
long removed = 0;
|
||||
String msg = "The elements with type {0} can not be removed as they do not exist!"; //$NON-NLS-1$
|
||||
|
||||
synchronized (this.elementMap) {
|
||||
Map<String, T> byType = this.elementMap.get(type);
|
||||
if (byType == null) {
|
||||
msg = MessageFormat.format(msg, type);
|
||||
throw new StrolchPersistenceException(msg);
|
||||
}
|
||||
|
||||
removed = byType.size();
|
||||
byType.clear();
|
||||
}
|
||||
|
||||
// last is to perform DB changes
|
||||
long daoRemoved = getDao(tx).removeAllBy(type);
|
||||
|
||||
if (removed != daoRemoved) {
|
||||
msg = "Removed {0} elements from cached map for type {1}, but dao removed {3} elements!";
|
||||
logger.error(MessageFormat.format(msg, removed, type, daoRemoved));
|
||||
}
|
||||
|
||||
return removed;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -95,14 +95,24 @@ public abstract class TransactionalElementMap<T extends StrolchElement> implemen
|
|||
getDao(tx).saveAll(elements);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> updateAll(StrolchTransaction tx, List<T> elements) {
|
||||
getDao(tx).updateAll(elements);
|
||||
return elements;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeAll(StrolchTransaction tx, List<T> elements) {
|
||||
getDao(tx).removeAll(elements);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> updateAll(StrolchTransaction tx, List<T> elements) {
|
||||
getDao(tx).updateAll(elements);
|
||||
return elements;
|
||||
public long removeAll(StrolchTransaction tx) {
|
||||
return getDao(tx).removeAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long removeAllBy(StrolchTransaction tx, String type) {
|
||||
return getDao(tx).removeAllBy(type);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,8 +24,8 @@ import li.strolch.agent.api.ComponentContainer;
|
|||
import li.strolch.agent.api.OrderMap;
|
||||
import li.strolch.agent.api.ResourceMap;
|
||||
import li.strolch.agent.api.StrolchRealm;
|
||||
import li.strolch.model.ModelStatistics;
|
||||
import li.strolch.model.xml.XmlModelSaxFileReader;
|
||||
import li.strolch.model.xml.XmlModelSaxReader.XmlModelStatistics;
|
||||
import li.strolch.persistence.api.PersistenceHandler;
|
||||
import li.strolch.persistence.api.StrolchTransaction;
|
||||
import li.strolch.persistence.inmemory.InMemoryPersistence;
|
||||
|
@ -95,7 +95,7 @@ public class TransientRealm extends StrolchRealm {
|
|||
@Override
|
||||
public void start() {
|
||||
|
||||
XmlModelStatistics statistics;
|
||||
ModelStatistics statistics;
|
||||
try (StrolchTransaction tx = openTx()) {
|
||||
InMemoryElementListener elementListener = new InMemoryElementListener(tx);
|
||||
XmlModelSaxFileReader handler = new XmlModelSaxFileReader(elementListener, modelFile);
|
||||
|
|
|
@ -54,4 +54,8 @@ public interface StrolchDao<T extends StrolchElement> {
|
|||
public void remove(T element);
|
||||
|
||||
public void removeAll(List<T> elements);
|
||||
|
||||
public long removeAll();
|
||||
|
||||
public long removeAllBy(String type);
|
||||
}
|
||||
|
|
|
@ -142,6 +142,10 @@ public class InMemoryDao<T extends StrolchElement> implements StrolchDao<T> {
|
|||
Map<String, T> byType = this.elementMap.get(element.getType());
|
||||
if (byType != null) {
|
||||
byType.remove(element.getId());
|
||||
|
||||
if (byType.isEmpty()) {
|
||||
this.elementMap.remove(element.getType());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -151,4 +155,27 @@ public class InMemoryDao<T extends StrolchElement> implements StrolchDao<T> {
|
|||
remove(element);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long removeAll() {
|
||||
long removed = 0;
|
||||
|
||||
for (String type : this.elementMap.keySet()) {
|
||||
Map<String, T> byType = this.elementMap.remove(type);
|
||||
removed += byType.size();
|
||||
byType.clear();
|
||||
}
|
||||
|
||||
return removed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long removeAllBy(String type) {
|
||||
Map<String, T> byType = this.elementMap.remove(type);
|
||||
if (byType == null)
|
||||
return 0;
|
||||
long removed = byType.size();
|
||||
byType.clear();
|
||||
return removed;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue