[Major] TXs are opened from ElementMap and all methods need a TX now

This commit is contained in:
Robert von Burg 2013-12-26 00:05:06 +01:00
parent 4e6322a6f7
commit 986492854b
12 changed files with 121 additions and 117 deletions

View File

@ -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 <eitch@eitchnet.ch>
*
*/
public interface ElementMap<T extends StrolchElement> {
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<T> getAllElements();
public List<T> getAllElements(StrolchTransaction tx);
public List<T> getElementsBy(String type);
public List<T> getElementsBy(StrolchTransaction tx, String type);
public Set<String> getTypes();
public Set<String> getTypes(StrolchTransaction tx);
public Set<String> getAllKeys();
public Set<String> getAllKeys(StrolchTransaction tx);
public Set<String> getKeysBy(String type);
public Set<String> 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);
}

View File

@ -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<String> resourceTypes = resourceDao.queryTypes();
for (String type : resourceTypes) {
List<Resource> 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<String> orderTypes = orderDao.queryTypes();
for (String type : orderTypes) {
List<Order> orders = orderDao.queryAll(type);
for (Order order : orders) {
orderMap.add(order);
orderMap.add(tx, order);
nrOfOrders++;
}
}

View File

@ -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);
}
}

View File

@ -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 <eitch@eitchnet.ch>
*
* @param <T>
*/
public abstract class TransactionalElementMap<T extends StrolchElement> implements ElementMap<T> {
private PersistenceHandler persistenceHandler;
@ -19,82 +24,74 @@ public abstract class TransactionalElementMap<T extends StrolchElement> implemen
this.persistenceHandler = persistenceHandler;
}
protected String getRealm() {
return this.realm;
}
protected abstract StrolchDao<T> 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<T> getAllElements() {
try (StrolchTransaction tx = this.persistenceHandler.openTx(this.realm)) {
return getDao(tx).queryAll();
}
public List<T> getAllElements(StrolchTransaction tx) {
return getDao(tx).queryAll();
}
@Override
public List<T> getElementsBy(String type) {
try (StrolchTransaction tx = this.persistenceHandler.openTx(this.realm)) {
return getDao(tx).queryAll(type);
}
public List<T> getElementsBy(StrolchTransaction tx, String type) {
return getDao(tx).queryAll(type);
}
@Override
public Set<String> getTypes() {
try (StrolchTransaction tx = this.persistenceHandler.openTx(this.realm)) {
return getDao(tx).queryTypes();
}
public Set<String> getTypes(StrolchTransaction tx) {
return getDao(tx).queryTypes();
}
@Override
public Set<String> getAllKeys() {
try (StrolchTransaction tx = this.persistenceHandler.openTx(this.realm)) {
return getDao(tx).queryKeySet();
}
public Set<String> getAllKeys(StrolchTransaction tx) {
return getDao(tx).queryKeySet();
}
@Override
public Set<String> getKeysBy(String type) {
try (StrolchTransaction tx = this.persistenceHandler.openTx(this.realm)) {
return getDao(tx).queryKeySet(type);
}
public Set<String> 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);
}
}

View File

@ -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 <eitch@eitchnet.ch>
*/
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);

View File

@ -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$

View File

@ -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 <eitch@eitchnet.ch>
*
*/
public class InMemoryQuery<T extends StrolchElement> {
@ -51,9 +51,9 @@ public class InMemoryQuery<T extends StrolchElement> {
this.selectors.add(selector);
}
public List<T> doQuery() {
public List<T> doQuery(StrolchTransaction tx) {
List<T> elements = this.navigator.navigate();
List<T> elements = this.navigator.navigate(tx);
Iterator<T> iter = elements.iterator();
while (iter.hasNext()) {
T element = iter.next();

View File

@ -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 <eitch@eitchnet.ch>
*
*/
public class ListNavigator<T extends StrolchElement> implements Navigator<T> {
@ -32,7 +32,7 @@ public class ListNavigator<T extends StrolchElement> implements Navigator<T> {
}
@Override
public List<T> navigate() {
public List<T> navigate(StrolchTransaction tx) {
return this.input;
}
}

View File

@ -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 <eitch@eitchnet.ch>
*
*/
public interface Navigator<T extends StrolchElement>{
public List<T> navigate();
public List<T> navigate(StrolchTransaction tx);
}

View File

@ -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 <eitch@eitchnet.ch>
*
*/
public abstract class StrolchTypeNavigator<T extends StrolchElement> implements Navigator<T> {
@ -32,8 +32,9 @@ public abstract class StrolchTypeNavigator<T extends StrolchElement> implements
this.type = type;
}
public List<T> navigate() {
return getElementMap().getElementsBy(this.type);
@Override
public List<T> navigate(StrolchTransaction tx) {
return getElementMap().getElementsBy(tx, this.type);
}
protected abstract ElementMap<T> getElementMap();

View File

@ -57,7 +57,7 @@ public class InMemoryQueryTest {
orderQuery.setNavigator(new ListNavigator<>(orders));
orderQuery.addSelector(new IdSelector<Order>("@1"));
List<Order> result = orderQuery.doQuery();
List<Order> 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<Resource>("@1"));
List<Resource> result = resourceQuery.doQuery();
List<Resource> result = resourceQuery.doQuery(null);
assertEquals(1, result.size());
assertEquals("@1", result.get(0).getId());
}
@ -87,7 +87,7 @@ public class InMemoryQueryTest {
new IdSelector<Resource>("@4"));
resourceQuery.addSelector(andSelector);
List<Resource> result = resourceQuery.doQuery();
List<Resource> 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<Resource> andSelector = new AndSelector<Resource>(andSelectors);
resourceQuery.addSelector(andSelector);
List<Resource> result = resourceQuery.doQuery();
List<Resource> result = resourceQuery.doQuery(null);
assertEquals(1, result.size());
assertEquals("@3", result.get(0).getId());
}
@ -124,7 +124,7 @@ public class InMemoryQueryTest {
BooleanSelector<Resource> andSelector = new AndSelector<Resource>(andSelectors);
resourceQuery.addSelector(andSelector);
List<Resource> result = resourceQuery.doQuery();
List<Resource> result = resourceQuery.doQuery(null);
assertEquals(0, result.size());
}
@ -140,7 +140,7 @@ public class InMemoryQueryTest {
ballQuery.addSelector(ParameterSelector.<Resource>booleanSelector("parameters", "forChildren", true));
ballQuery.addSelector(ParameterSelector.<Resource>floatSelector("parameters", "diameter", 22.0));
List<Resource> result = ballQuery.doQuery();
List<Resource> result = ballQuery.doQuery(null);
assertEquals(1, result.size());
}

View File

@ -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<Selection> elementAndSelections = new ArrayList<>();
elementAndSelections.add(new IdSelection("@1"));
@ -69,7 +75,10 @@ public class QueryTest {
InMemoryResourceQueryVisitor resourceQuery = new InMemoryResourceQueryVisitor(agent.getContainer());
InMemoryQuery<Resource> inMemoryQuery = resourceQuery.visit(query);
List<Resource> result = inMemoryQuery.doQuery();
List<Resource> 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<Selection> elementAndSelections = new ArrayList<>();
elementAndSelections.add(new IdSelection("@1"));
@ -93,7 +105,10 @@ public class QueryTest {
InMemoryOrderQueryVisitor orderQuery = new InMemoryOrderQueryVisitor(agent.getContainer());
InMemoryQuery<Order> inMemoryQuery = orderQuery.visit(query);
List<Order> result = inMemoryQuery.doQuery();
List<Order> result;
try (StrolchTransaction tx = orderMap.openTx()) {
result = inMemoryQuery.doQuery(tx);
}
assertEquals(1, result.size());
assertEquals("@1", result.get(0).getId());
}