diff --git a/src/main/java/li/strolch/runtime/agent/api/ElementMap.java b/src/main/java/li/strolch/runtime/agent/api/ElementMap.java index 39ad53a72..3821a97c6 100644 --- a/src/main/java/li/strolch/runtime/agent/api/ElementMap.java +++ b/src/main/java/li/strolch/runtime/agent/api/ElementMap.java @@ -19,33 +19,36 @@ import java.util.List; import java.util.Set; import li.strolch.model.StrolchElement; +import li.strolch.persistence.api.StrolchTransaction; /** * @author Robert von Burg - * */ public interface ElementMap { - public boolean hasType(String type); + public boolean hasType(StrolchTransaction tx, String type); - public boolean hasElement(String type, String id); + public boolean hasElement(StrolchTransaction tx, String type, String id); - public T getBy(String type, String id); + public T getBy(StrolchTransaction tx, String type, String id); - public List getAllElements(); + public List getAllElements(StrolchTransaction tx); - public List getElementsBy(String type); + public List getElementsBy(StrolchTransaction tx, String type); - public Set getTypes(); + public Set getTypes(StrolchTransaction tx); - public Set getAllKeys(); + public Set getAllKeys(StrolchTransaction tx); - public Set getKeysBy(String type); + public Set getKeysBy(StrolchTransaction tx, String type); - public void add(T element); + public void add(StrolchTransaction tx, T element); - public void update(T element); + public void update(StrolchTransaction tx, T element); - public void remove(T element); + public void remove(StrolchTransaction tx, T element); + public StrolchTransaction openTx(); + + public StrolchTransaction openTx(String realm); } diff --git a/src/main/java/li/strolch/runtime/agent/impl/CachedElementMapHandler.java b/src/main/java/li/strolch/runtime/agent/impl/CachedElementMapHandler.java index 97d3747d5..de4efbdb5 100644 --- a/src/main/java/li/strolch/runtime/agent/impl/CachedElementMapHandler.java +++ b/src/main/java/li/strolch/runtime/agent/impl/CachedElementMapHandler.java @@ -22,7 +22,6 @@ import java.util.Set; import li.strolch.model.Order; import li.strolch.model.Resource; import li.strolch.persistence.api.OrderDao; -import li.strolch.persistence.api.PersistenceHandler; import li.strolch.persistence.api.ResourceDao; import li.strolch.persistence.api.StrolchTransaction; import li.strolch.runtime.agent.api.OrderMap; @@ -54,25 +53,25 @@ public class CachedElementMapHandler extends InMemoryElementMapHandler { OrderMap orderMap = getContainer().getOrderMap(realm); ResourceMap resourceMap = getContainer().getResourceMap(realm); - PersistenceHandler persistenceHandler = getContainer().getComponent(PersistenceHandler.class); - try (StrolchTransaction tx = persistenceHandler.openTx(realm)) { - + try (StrolchTransaction tx = resourceMap.openTx(realm)) { ResourceDao resourceDao = tx.getResourceDao(); Set resourceTypes = resourceDao.queryTypes(); for (String type : resourceTypes) { List resources = resourceDao.queryAll(type); for (Resource resource : resources) { - resourceMap.add(resource); + resourceMap.add(tx, resource); nrOfResources++; } } + } + try (StrolchTransaction tx = orderMap.openTx(realm)) { OrderDao orderDao = tx.getOrderDao(); Set orderTypes = orderDao.queryTypes(); for (String type : orderTypes) { List orders = orderDao.queryAll(type); for (Order order : orders) { - orderMap.add(order); + orderMap.add(tx, order); nrOfOrders++; } } diff --git a/src/main/java/li/strolch/runtime/agent/impl/InMemoryElementListener.java b/src/main/java/li/strolch/runtime/agent/impl/InMemoryElementListener.java index d5fd89b5a..43dfcf1d4 100644 --- a/src/main/java/li/strolch/runtime/agent/impl/InMemoryElementListener.java +++ b/src/main/java/li/strolch/runtime/agent/impl/InMemoryElementListener.java @@ -18,6 +18,7 @@ package li.strolch.runtime.agent.impl; import li.strolch.model.Order; import li.strolch.model.Resource; import li.strolch.model.xml.StrolchElementListener; +import li.strolch.persistence.api.StrolchTransaction; import li.strolch.runtime.agent.api.OrderMap; import li.strolch.runtime.agent.api.ResourceMap; @@ -26,21 +27,23 @@ import li.strolch.runtime.agent.api.ResourceMap; */ public class InMemoryElementListener implements StrolchElementListener { + private StrolchTransaction tx; private ResourceMap resourceMap; private OrderMap orderMap; - public InMemoryElementListener(ResourceMap resourceMap, OrderMap orderMap) { + public InMemoryElementListener(StrolchTransaction tx, ResourceMap resourceMap, OrderMap orderMap) { + this.tx = tx; this.resourceMap = resourceMap; this.orderMap = orderMap; } @Override public void notifyResource(Resource resource) { - this.resourceMap.add(resource); + this.resourceMap.add(this.tx, resource); } @Override public void notifyOrder(Order order) { - this.orderMap.add(order); + this.orderMap.add(this.tx, order); } } diff --git a/src/main/java/li/strolch/runtime/agent/impl/TransactionalElementMap.java b/src/main/java/li/strolch/runtime/agent/impl/TransactionalElementMap.java index 8479de84c..b5f81c5ed 100644 --- a/src/main/java/li/strolch/runtime/agent/impl/TransactionalElementMap.java +++ b/src/main/java/li/strolch/runtime/agent/impl/TransactionalElementMap.java @@ -9,6 +9,11 @@ import li.strolch.persistence.api.StrolchDao; import li.strolch.persistence.api.StrolchTransaction; import li.strolch.runtime.agent.api.ElementMap; +/** + * @author Robert von Burg + * + * @param + */ public abstract class TransactionalElementMap implements ElementMap { private PersistenceHandler persistenceHandler; @@ -19,82 +24,74 @@ public abstract class TransactionalElementMap implemen this.persistenceHandler = persistenceHandler; } + protected String getRealm() { + return this.realm; + } + protected abstract StrolchDao getDao(StrolchTransaction tx); @Override - public boolean hasType(String type) { - try (StrolchTransaction tx = this.persistenceHandler.openTx(this.realm)) { - return getDao(tx).queryTypes().contains(type); - } + public boolean hasType(StrolchTransaction tx, String type) { + return getDao(tx).queryTypes().contains(type); } @Override - public boolean hasElement(String type, String id) { - try (StrolchTransaction tx = this.persistenceHandler.openTx(this.realm)) { - return getDao(tx).queryKeySet(type).contains(id); - } + public boolean hasElement(StrolchTransaction tx, String type, String id) { + return getDao(tx).queryKeySet(type).contains(id); } @Override - public T getBy(String type, String id) { - try (StrolchTransaction tx = this.persistenceHandler.openTx(this.realm)) { - return getDao(tx).queryBy(type, id); - } + public T getBy(StrolchTransaction tx, String type, String id) { + return getDao(tx).queryBy(type, id); } @Override - public List getAllElements() { - try (StrolchTransaction tx = this.persistenceHandler.openTx(this.realm)) { - return getDao(tx).queryAll(); - } + public List getAllElements(StrolchTransaction tx) { + return getDao(tx).queryAll(); } @Override - public List getElementsBy(String type) { - try (StrolchTransaction tx = this.persistenceHandler.openTx(this.realm)) { - return getDao(tx).queryAll(type); - } + public List getElementsBy(StrolchTransaction tx, String type) { + return getDao(tx).queryAll(type); } @Override - public Set getTypes() { - try (StrolchTransaction tx = this.persistenceHandler.openTx(this.realm)) { - return getDao(tx).queryTypes(); - } + public Set getTypes(StrolchTransaction tx) { + return getDao(tx).queryTypes(); } @Override - public Set getAllKeys() { - try (StrolchTransaction tx = this.persistenceHandler.openTx(this.realm)) { - return getDao(tx).queryKeySet(); - } + public Set getAllKeys(StrolchTransaction tx) { + return getDao(tx).queryKeySet(); } @Override - public Set getKeysBy(String type) { - try (StrolchTransaction tx = this.persistenceHandler.openTx(this.realm)) { - return getDao(tx).queryKeySet(type); - } + public Set getKeysBy(StrolchTransaction tx, String type) { + return getDao(tx).queryKeySet(type); } @Override - public void add(T element) { - try (StrolchTransaction tx = this.persistenceHandler.openTx(this.realm)) { - getDao(tx).save(element); - } + public void add(StrolchTransaction tx, T element) { + getDao(tx).save(element); } @Override - public void update(T element) { - try (StrolchTransaction tx = this.persistenceHandler.openTx(this.realm)) { - getDao(tx).update(element); - } + public void update(StrolchTransaction tx, T element) { + getDao(tx).update(element); } @Override - public void remove(T element) { - try (StrolchTransaction tx = this.persistenceHandler.openTx(this.realm)) { - getDao(tx).remove(element); - } + public void remove(StrolchTransaction tx, T element) { + getDao(tx).remove(element); + } + + @Override + public StrolchTransaction openTx() { + return this.persistenceHandler.openTx(); + } + + @Override + public StrolchTransaction openTx(String realm) { + return this.persistenceHandler.openTx(realm); } } diff --git a/src/main/java/li/strolch/runtime/agent/impl/TransactionalElementMapHandler.java b/src/main/java/li/strolch/runtime/agent/impl/TransactionalElementMapHandler.java index 5236323e1..ff3cfaffd 100644 --- a/src/main/java/li/strolch/runtime/agent/impl/TransactionalElementMapHandler.java +++ b/src/main/java/li/strolch/runtime/agent/impl/TransactionalElementMapHandler.java @@ -16,21 +16,16 @@ package li.strolch.runtime.agent.impl; import java.text.MessageFormat; -import java.util.HashMap; -import li.strolch.persistence.api.PersistenceHandler; -import li.strolch.runtime.StrolchConstants; +import li.strolch.persistence.api.StrolchTransaction; import li.strolch.runtime.agent.api.OrderMap; import li.strolch.runtime.agent.api.ResourceMap; -import li.strolch.runtime.agent.api.StrolchAgent; -import li.strolch.runtime.configuration.ComponentConfiguration; -import li.strolch.runtime.configuration.RuntimeConfiguration; import ch.eitchnet.utils.helper.StringHelper; /** * @author Robert von Burg */ -public class TransactionalElementMapHandler extends AbstractElementMapHandler { +public class TransactionalElementMapHandler extends InMemoryElementMapHandler { /** * @param container @@ -40,24 +35,6 @@ public class TransactionalElementMapHandler extends AbstractElementMapHandler { super(container, componentName); } - @Override - public void initialize(ComponentConfiguration configuration) { - - RuntimeConfiguration runtimeConfiguration = configuration.getRuntimeConfiguration(); - String[] realms = runtimeConfiguration.getStringArray(StrolchAgent.PROP_REALMS, StrolchConstants.DEFAULT_REALM); - - this.realms = new HashMap<>(); - for (String realm : realms) { - PersistenceHandler persistenceHandler = getContainer().getComponent(PersistenceHandler.class); - TransactionalResourceMap resourceMap = new TransactionalResourceMap(realm, persistenceHandler); - TransactionalOrderMap orderMap = new TransactionalOrderMap(realm, persistenceHandler); - StrolchRealm strolchRealm = new StrolchRealm(realm, resourceMap, orderMap); - this.realms.put(realm, strolchRealm); - } - - super.initialize(configuration); - } - @Override public void start() { @@ -68,10 +45,14 @@ public class TransactionalElementMapHandler extends AbstractElementMapHandler { int nrOfResources = 0; OrderMap orderMap = getContainer().getOrderMap(realm); - ResourceMap resourceMap = getContainer().getResourceMap(realm); + try (StrolchTransaction tx = orderMap.openTx(realm)) { + nrOfOrders = orderMap.getAllKeys(tx).size(); + } - nrOfOrders = orderMap.getAllKeys().size(); - nrOfResources = resourceMap.getAllKeys().size(); + ResourceMap resourceMap = getContainer().getResourceMap(realm); + try (StrolchTransaction tx = resourceMap.openTx(realm)) { + nrOfResources = resourceMap.getAllKeys(tx).size(); + } long duration = System.nanoTime() - start; String durationS = StringHelper.formatNanoDuration(duration); diff --git a/src/main/java/li/strolch/runtime/agent/impl/TransientElementMapHandler.java b/src/main/java/li/strolch/runtime/agent/impl/TransientElementMapHandler.java index 8bb131580..03d9d8731 100644 --- a/src/main/java/li/strolch/runtime/agent/impl/TransientElementMapHandler.java +++ b/src/main/java/li/strolch/runtime/agent/impl/TransientElementMapHandler.java @@ -22,6 +22,7 @@ import java.util.Map; import li.strolch.model.xml.XmlModelDefaultHandler.XmlModelStatistics; import li.strolch.model.xml.XmlModelFileHandler; +import li.strolch.persistence.api.StrolchTransaction; import li.strolch.runtime.StrolchConstants; import li.strolch.runtime.agent.api.OrderMap; import li.strolch.runtime.agent.api.ResourceMap; @@ -82,10 +83,14 @@ public class TransientElementMapHandler extends InMemoryElementMapHandler { OrderMap orderMap = strolchRealm.getOrderMap(); File modelFile = this.realmModelFiles.get(realm); - InMemoryElementListener elementListener = new InMemoryElementListener(resourceMap, orderMap); - XmlModelFileHandler handler = new XmlModelFileHandler(elementListener, modelFile); - handler.parseFile(); - XmlModelStatistics statistics = handler.getStatistics(); + XmlModelStatistics statistics; + try (StrolchTransaction tx = resourceMap.openTx(realm)) { + InMemoryElementListener elementListener = new InMemoryElementListener(tx, resourceMap, orderMap); + XmlModelFileHandler handler = new XmlModelFileHandler(elementListener, modelFile); + handler.parseFile(); + statistics = handler.getStatistics(); + } + String durationS = StringHelper.formatNanoDuration(statistics.durationNanos); logger.info(MessageFormat.format( "Loading XML Model file {0} for realm {1} took {2}.", modelFile.getName(), realm, durationS)); //$NON-NLS-1$ diff --git a/src/main/java/li/strolch/runtime/query/inmemory/InMemoryQuery.java b/src/main/java/li/strolch/runtime/query/inmemory/InMemoryQuery.java index f014d830a..4b671f5a1 100644 --- a/src/main/java/li/strolch/runtime/query/inmemory/InMemoryQuery.java +++ b/src/main/java/li/strolch/runtime/query/inmemory/InMemoryQuery.java @@ -20,10 +20,10 @@ import java.util.Iterator; import java.util.List; import li.strolch.model.StrolchElement; +import li.strolch.persistence.api.StrolchTransaction; /** * @author Robert von Burg - * */ public class InMemoryQuery { @@ -51,9 +51,9 @@ public class InMemoryQuery { this.selectors.add(selector); } - public List doQuery() { + public List doQuery(StrolchTransaction tx) { - List elements = this.navigator.navigate(); + List elements = this.navigator.navigate(tx); Iterator iter = elements.iterator(); while (iter.hasNext()) { T element = iter.next(); diff --git a/src/main/java/li/strolch/runtime/query/inmemory/ListNavigator.java b/src/main/java/li/strolch/runtime/query/inmemory/ListNavigator.java index fbef6b470..301adc37d 100644 --- a/src/main/java/li/strolch/runtime/query/inmemory/ListNavigator.java +++ b/src/main/java/li/strolch/runtime/query/inmemory/ListNavigator.java @@ -18,10 +18,10 @@ package li.strolch.runtime.query.inmemory; import java.util.List; import li.strolch.model.StrolchElement; +import li.strolch.persistence.api.StrolchTransaction; /** * @author Robert von Burg - * */ public class ListNavigator implements Navigator { @@ -32,7 +32,7 @@ public class ListNavigator implements Navigator { } @Override - public List navigate() { + public List navigate(StrolchTransaction tx) { return this.input; } } diff --git a/src/main/java/li/strolch/runtime/query/inmemory/Navigator.java b/src/main/java/li/strolch/runtime/query/inmemory/Navigator.java index 40b3609ca..8e18658b6 100644 --- a/src/main/java/li/strolch/runtime/query/inmemory/Navigator.java +++ b/src/main/java/li/strolch/runtime/query/inmemory/Navigator.java @@ -18,12 +18,12 @@ package li.strolch.runtime.query.inmemory; import java.util.List; import li.strolch.model.StrolchElement; +import li.strolch.persistence.api.StrolchTransaction; /** * @author Robert von Burg - * */ public interface Navigator{ - public List navigate(); + public List navigate(StrolchTransaction tx); } diff --git a/src/main/java/li/strolch/runtime/query/inmemory/StrolchTypeNavigator.java b/src/main/java/li/strolch/runtime/query/inmemory/StrolchTypeNavigator.java index d0c9ffe68..63a692cad 100644 --- a/src/main/java/li/strolch/runtime/query/inmemory/StrolchTypeNavigator.java +++ b/src/main/java/li/strolch/runtime/query/inmemory/StrolchTypeNavigator.java @@ -18,11 +18,11 @@ package li.strolch.runtime.query.inmemory; import java.util.List; import li.strolch.model.StrolchElement; +import li.strolch.persistence.api.StrolchTransaction; import li.strolch.runtime.agent.api.ElementMap; /** * @author Robert von Burg - * */ public abstract class StrolchTypeNavigator implements Navigator { @@ -32,8 +32,9 @@ public abstract class StrolchTypeNavigator implements this.type = type; } - public List navigate() { - return getElementMap().getElementsBy(this.type); + @Override + public List navigate(StrolchTransaction tx) { + return getElementMap().getElementsBy(tx, this.type); } protected abstract ElementMap getElementMap(); diff --git a/src/test/java/li/strolch/runtime/test/query/inmemory/InMemoryQueryTest.java b/src/test/java/li/strolch/runtime/test/query/inmemory/InMemoryQueryTest.java index cf1fab6d3..432fb1f49 100644 --- a/src/test/java/li/strolch/runtime/test/query/inmemory/InMemoryQueryTest.java +++ b/src/test/java/li/strolch/runtime/test/query/inmemory/InMemoryQueryTest.java @@ -57,7 +57,7 @@ public class InMemoryQueryTest { orderQuery.setNavigator(new ListNavigator<>(orders)); orderQuery.addSelector(new IdSelector("@1")); - List result = orderQuery.doQuery(); + List result = orderQuery.doQuery(null); assertEquals(1, result.size()); assertEquals("@1", result.get(0).getId()); } @@ -71,7 +71,7 @@ public class InMemoryQueryTest { resourceQuery.setNavigator(new ListNavigator<>(resources)); resourceQuery.addSelector(new IdSelector("@1")); - List result = resourceQuery.doQuery(); + List result = resourceQuery.doQuery(null); assertEquals(1, result.size()); assertEquals("@1", result.get(0).getId()); } @@ -87,7 +87,7 @@ public class InMemoryQueryTest { new IdSelector("@4")); resourceQuery.addSelector(andSelector); - List result = resourceQuery.doQuery(); + List result = resourceQuery.doQuery(null); assertEquals(2, result.size()); assertEquals("@3", result.get(0).getId()); assertEquals("@4", result.get(1).getId()); @@ -106,7 +106,7 @@ public class InMemoryQueryTest { BooleanSelector andSelector = new AndSelector(andSelectors); resourceQuery.addSelector(andSelector); - List result = resourceQuery.doQuery(); + List result = resourceQuery.doQuery(null); assertEquals(1, result.size()); assertEquals("@3", result.get(0).getId()); } @@ -124,7 +124,7 @@ public class InMemoryQueryTest { BooleanSelector andSelector = new AndSelector(andSelectors); resourceQuery.addSelector(andSelector); - List result = resourceQuery.doQuery(); + List result = resourceQuery.doQuery(null); assertEquals(0, result.size()); } @@ -140,7 +140,7 @@ public class InMemoryQueryTest { ballQuery.addSelector(ParameterSelector.booleanSelector("parameters", "forChildren", true)); ballQuery.addSelector(ParameterSelector.floatSelector("parameters", "diameter", 22.0)); - List result = ballQuery.doQuery(); + List result = ballQuery.doQuery(null); assertEquals(1, result.size()); } diff --git a/src/test/java/li/strolch/runtime/test/query/inmemory/QueryTest.java b/src/test/java/li/strolch/runtime/test/query/inmemory/QueryTest.java index 1bf6532a2..3cfa19d0c 100644 --- a/src/test/java/li/strolch/runtime/test/query/inmemory/QueryTest.java +++ b/src/test/java/li/strolch/runtime/test/query/inmemory/QueryTest.java @@ -33,6 +33,9 @@ import li.strolch.model.query.ParameterSelection; import li.strolch.model.query.ResourceQuery; import li.strolch.model.query.Selection; import li.strolch.model.query.StrolchTypeNavigation; +import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.runtime.agent.api.OrderMap; +import li.strolch.runtime.agent.api.ResourceMap; import li.strolch.runtime.agent.api.StrolchAgent; import li.strolch.runtime.query.inmemory.InMemoryOrderQueryVisitor; import li.strolch.runtime.query.inmemory.InMemoryQuery; @@ -58,7 +61,10 @@ public class QueryTest { Resource res1 = createResource("@1", "Test Resource", "MyType"); IntegerParameter iP = new IntegerParameter("nbOfBooks", "Number of Books", 33); res1.addParameter(BAG_ID, iP); - agent.getResourceMap().add(res1); + ResourceMap resourceMap = agent.getResourceMap(); + try (StrolchTransaction tx = resourceMap.openTx()) { + resourceMap.add(tx, res1); + } List elementAndSelections = new ArrayList<>(); elementAndSelections.add(new IdSelection("@1")); @@ -69,7 +75,10 @@ public class QueryTest { InMemoryResourceQueryVisitor resourceQuery = new InMemoryResourceQueryVisitor(agent.getContainer()); InMemoryQuery inMemoryQuery = resourceQuery.visit(query); - List result = inMemoryQuery.doQuery(); + List result; + try (StrolchTransaction tx = resourceMap.openTx()) { + result = inMemoryQuery.doQuery(tx); + } assertEquals(1, result.size()); assertEquals("@1", result.get(0).getId()); } @@ -82,7 +91,10 @@ public class QueryTest { Order o1 = createOrder("@1", "Test Order", "MyType"); IntegerParameter iP = new IntegerParameter("nbOfBooks", "Number of Books", 33); o1.addParameter(BAG_ID, iP); - agent.getOrderMap().add(o1); + OrderMap orderMap = agent.getOrderMap(); + try (StrolchTransaction tx = orderMap.openTx()) { + orderMap.add(tx, o1); + } List elementAndSelections = new ArrayList<>(); elementAndSelections.add(new IdSelection("@1")); @@ -93,7 +105,10 @@ public class QueryTest { InMemoryOrderQueryVisitor orderQuery = new InMemoryOrderQueryVisitor(agent.getContainer()); InMemoryQuery inMemoryQuery = orderQuery.visit(query); - List result = inMemoryQuery.doQuery(); + List result; + try (StrolchTransaction tx = orderMap.openTx()) { + result = inMemoryQuery.doQuery(tx); + } assertEquals(1, result.size()); assertEquals("@1", result.get(0).getId()); }