[Major] TXs are opened from ElementMap and all methods need a TX now
This commit is contained in:
parent
4e6322a6f7
commit
986492854b
|
@ -19,33 +19,36 @@ import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import li.strolch.model.StrolchElement;
|
import li.strolch.model.StrolchElement;
|
||||||
|
import li.strolch.persistence.api.StrolchTransaction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public interface ElementMap<T extends StrolchElement> {
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,6 @@ import java.util.Set;
|
||||||
import li.strolch.model.Order;
|
import li.strolch.model.Order;
|
||||||
import li.strolch.model.Resource;
|
import li.strolch.model.Resource;
|
||||||
import li.strolch.persistence.api.OrderDao;
|
import li.strolch.persistence.api.OrderDao;
|
||||||
import li.strolch.persistence.api.PersistenceHandler;
|
|
||||||
import li.strolch.persistence.api.ResourceDao;
|
import li.strolch.persistence.api.ResourceDao;
|
||||||
import li.strolch.persistence.api.StrolchTransaction;
|
import li.strolch.persistence.api.StrolchTransaction;
|
||||||
import li.strolch.runtime.agent.api.OrderMap;
|
import li.strolch.runtime.agent.api.OrderMap;
|
||||||
|
@ -54,25 +53,25 @@ public class CachedElementMapHandler extends InMemoryElementMapHandler {
|
||||||
OrderMap orderMap = getContainer().getOrderMap(realm);
|
OrderMap orderMap = getContainer().getOrderMap(realm);
|
||||||
ResourceMap resourceMap = getContainer().getResourceMap(realm);
|
ResourceMap resourceMap = getContainer().getResourceMap(realm);
|
||||||
|
|
||||||
PersistenceHandler persistenceHandler = getContainer().getComponent(PersistenceHandler.class);
|
try (StrolchTransaction tx = resourceMap.openTx(realm)) {
|
||||||
try (StrolchTransaction tx = persistenceHandler.openTx(realm)) {
|
|
||||||
|
|
||||||
ResourceDao resourceDao = tx.getResourceDao();
|
ResourceDao resourceDao = tx.getResourceDao();
|
||||||
Set<String> resourceTypes = resourceDao.queryTypes();
|
Set<String> resourceTypes = resourceDao.queryTypes();
|
||||||
for (String type : resourceTypes) {
|
for (String type : resourceTypes) {
|
||||||
List<Resource> resources = resourceDao.queryAll(type);
|
List<Resource> resources = resourceDao.queryAll(type);
|
||||||
for (Resource resource : resources) {
|
for (Resource resource : resources) {
|
||||||
resourceMap.add(resource);
|
resourceMap.add(tx, resource);
|
||||||
nrOfResources++;
|
nrOfResources++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try (StrolchTransaction tx = orderMap.openTx(realm)) {
|
||||||
OrderDao orderDao = tx.getOrderDao();
|
OrderDao orderDao = tx.getOrderDao();
|
||||||
Set<String> orderTypes = orderDao.queryTypes();
|
Set<String> orderTypes = orderDao.queryTypes();
|
||||||
for (String type : orderTypes) {
|
for (String type : orderTypes) {
|
||||||
List<Order> orders = orderDao.queryAll(type);
|
List<Order> orders = orderDao.queryAll(type);
|
||||||
for (Order order : orders) {
|
for (Order order : orders) {
|
||||||
orderMap.add(order);
|
orderMap.add(tx, order);
|
||||||
nrOfOrders++;
|
nrOfOrders++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@ package li.strolch.runtime.agent.impl;
|
||||||
import li.strolch.model.Order;
|
import li.strolch.model.Order;
|
||||||
import li.strolch.model.Resource;
|
import li.strolch.model.Resource;
|
||||||
import li.strolch.model.xml.StrolchElementListener;
|
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.OrderMap;
|
||||||
import li.strolch.runtime.agent.api.ResourceMap;
|
import li.strolch.runtime.agent.api.ResourceMap;
|
||||||
|
|
||||||
|
@ -26,21 +27,23 @@ import li.strolch.runtime.agent.api.ResourceMap;
|
||||||
*/
|
*/
|
||||||
public class InMemoryElementListener implements StrolchElementListener {
|
public class InMemoryElementListener implements StrolchElementListener {
|
||||||
|
|
||||||
|
private StrolchTransaction tx;
|
||||||
private ResourceMap resourceMap;
|
private ResourceMap resourceMap;
|
||||||
private OrderMap orderMap;
|
private OrderMap orderMap;
|
||||||
|
|
||||||
public InMemoryElementListener(ResourceMap resourceMap, OrderMap orderMap) {
|
public InMemoryElementListener(StrolchTransaction tx, ResourceMap resourceMap, OrderMap orderMap) {
|
||||||
|
this.tx = tx;
|
||||||
this.resourceMap = resourceMap;
|
this.resourceMap = resourceMap;
|
||||||
this.orderMap = orderMap;
|
this.orderMap = orderMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void notifyResource(Resource resource) {
|
public void notifyResource(Resource resource) {
|
||||||
this.resourceMap.add(resource);
|
this.resourceMap.add(this.tx, resource);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void notifyOrder(Order order) {
|
public void notifyOrder(Order order) {
|
||||||
this.orderMap.add(order);
|
this.orderMap.add(this.tx, order);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,11 @@ import li.strolch.persistence.api.StrolchDao;
|
||||||
import li.strolch.persistence.api.StrolchTransaction;
|
import li.strolch.persistence.api.StrolchTransaction;
|
||||||
import li.strolch.runtime.agent.api.ElementMap;
|
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> {
|
public abstract class TransactionalElementMap<T extends StrolchElement> implements ElementMap<T> {
|
||||||
|
|
||||||
private PersistenceHandler persistenceHandler;
|
private PersistenceHandler persistenceHandler;
|
||||||
|
@ -19,82 +24,74 @@ public abstract class TransactionalElementMap<T extends StrolchElement> implemen
|
||||||
this.persistenceHandler = persistenceHandler;
|
this.persistenceHandler = persistenceHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected String getRealm() {
|
||||||
|
return this.realm;
|
||||||
|
}
|
||||||
|
|
||||||
protected abstract StrolchDao<T> getDao(StrolchTransaction tx);
|
protected abstract StrolchDao<T> getDao(StrolchTransaction tx);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasType(String type) {
|
public boolean hasType(StrolchTransaction tx, String type) {
|
||||||
try (StrolchTransaction tx = this.persistenceHandler.openTx(this.realm)) {
|
|
||||||
return getDao(tx).queryTypes().contains(type);
|
return getDao(tx).queryTypes().contains(type);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasElement(String type, String id) {
|
public boolean hasElement(StrolchTransaction tx, String type, String id) {
|
||||||
try (StrolchTransaction tx = this.persistenceHandler.openTx(this.realm)) {
|
|
||||||
return getDao(tx).queryKeySet(type).contains(id);
|
return getDao(tx).queryKeySet(type).contains(id);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T getBy(String type, String id) {
|
public T getBy(StrolchTransaction tx, String type, String id) {
|
||||||
try (StrolchTransaction tx = this.persistenceHandler.openTx(this.realm)) {
|
|
||||||
return getDao(tx).queryBy(type, id);
|
return getDao(tx).queryBy(type, id);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<T> getAllElements() {
|
public List<T> getAllElements(StrolchTransaction tx) {
|
||||||
try (StrolchTransaction tx = this.persistenceHandler.openTx(this.realm)) {
|
|
||||||
return getDao(tx).queryAll();
|
return getDao(tx).queryAll();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<T> getElementsBy(String type) {
|
public List<T> getElementsBy(StrolchTransaction tx, String type) {
|
||||||
try (StrolchTransaction tx = this.persistenceHandler.openTx(this.realm)) {
|
|
||||||
return getDao(tx).queryAll(type);
|
return getDao(tx).queryAll(type);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<String> getTypes() {
|
public Set<String> getTypes(StrolchTransaction tx) {
|
||||||
try (StrolchTransaction tx = this.persistenceHandler.openTx(this.realm)) {
|
|
||||||
return getDao(tx).queryTypes();
|
return getDao(tx).queryTypes();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<String> getAllKeys() {
|
public Set<String> getAllKeys(StrolchTransaction tx) {
|
||||||
try (StrolchTransaction tx = this.persistenceHandler.openTx(this.realm)) {
|
|
||||||
return getDao(tx).queryKeySet();
|
return getDao(tx).queryKeySet();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<String> getKeysBy(String type) {
|
public Set<String> getKeysBy(StrolchTransaction tx, String type) {
|
||||||
try (StrolchTransaction tx = this.persistenceHandler.openTx(this.realm)) {
|
|
||||||
return getDao(tx).queryKeySet(type);
|
return getDao(tx).queryKeySet(type);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void add(T element) {
|
public void add(StrolchTransaction tx, T element) {
|
||||||
try (StrolchTransaction tx = this.persistenceHandler.openTx(this.realm)) {
|
|
||||||
getDao(tx).save(element);
|
getDao(tx).save(element);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(T element) {
|
public void update(StrolchTransaction tx, T element) {
|
||||||
try (StrolchTransaction tx = this.persistenceHandler.openTx(this.realm)) {
|
|
||||||
getDao(tx).update(element);
|
getDao(tx).update(element);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove(StrolchTransaction tx, T element) {
|
||||||
|
getDao(tx).remove(element);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void remove(T element) {
|
public StrolchTransaction openTx() {
|
||||||
try (StrolchTransaction tx = this.persistenceHandler.openTx(this.realm)) {
|
return this.persistenceHandler.openTx();
|
||||||
getDao(tx).remove(element);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public StrolchTransaction openTx(String realm) {
|
||||||
|
return this.persistenceHandler.openTx(realm);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,21 +16,16 @@
|
||||||
package li.strolch.runtime.agent.impl;
|
package li.strolch.runtime.agent.impl;
|
||||||
|
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
import java.util.HashMap;
|
|
||||||
|
|
||||||
import li.strolch.persistence.api.PersistenceHandler;
|
import li.strolch.persistence.api.StrolchTransaction;
|
||||||
import li.strolch.runtime.StrolchConstants;
|
|
||||||
import li.strolch.runtime.agent.api.OrderMap;
|
import li.strolch.runtime.agent.api.OrderMap;
|
||||||
import li.strolch.runtime.agent.api.ResourceMap;
|
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;
|
import ch.eitchnet.utils.helper.StringHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||||
*/
|
*/
|
||||||
public class TransactionalElementMapHandler extends AbstractElementMapHandler {
|
public class TransactionalElementMapHandler extends InMemoryElementMapHandler {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param container
|
* @param container
|
||||||
|
@ -40,24 +35,6 @@ public class TransactionalElementMapHandler extends AbstractElementMapHandler {
|
||||||
super(container, componentName);
|
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
|
@Override
|
||||||
public void start() {
|
public void start() {
|
||||||
|
|
||||||
|
@ -68,10 +45,14 @@ public class TransactionalElementMapHandler extends AbstractElementMapHandler {
|
||||||
int nrOfResources = 0;
|
int nrOfResources = 0;
|
||||||
|
|
||||||
OrderMap orderMap = getContainer().getOrderMap(realm);
|
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();
|
ResourceMap resourceMap = getContainer().getResourceMap(realm);
|
||||||
nrOfResources = resourceMap.getAllKeys().size();
|
try (StrolchTransaction tx = resourceMap.openTx(realm)) {
|
||||||
|
nrOfResources = resourceMap.getAllKeys(tx).size();
|
||||||
|
}
|
||||||
|
|
||||||
long duration = System.nanoTime() - start;
|
long duration = System.nanoTime() - start;
|
||||||
String durationS = StringHelper.formatNanoDuration(duration);
|
String durationS = StringHelper.formatNanoDuration(duration);
|
||||||
|
|
|
@ -22,6 +22,7 @@ import java.util.Map;
|
||||||
|
|
||||||
import li.strolch.model.xml.XmlModelDefaultHandler.XmlModelStatistics;
|
import li.strolch.model.xml.XmlModelDefaultHandler.XmlModelStatistics;
|
||||||
import li.strolch.model.xml.XmlModelFileHandler;
|
import li.strolch.model.xml.XmlModelFileHandler;
|
||||||
|
import li.strolch.persistence.api.StrolchTransaction;
|
||||||
import li.strolch.runtime.StrolchConstants;
|
import li.strolch.runtime.StrolchConstants;
|
||||||
import li.strolch.runtime.agent.api.OrderMap;
|
import li.strolch.runtime.agent.api.OrderMap;
|
||||||
import li.strolch.runtime.agent.api.ResourceMap;
|
import li.strolch.runtime.agent.api.ResourceMap;
|
||||||
|
@ -82,10 +83,14 @@ public class TransientElementMapHandler extends InMemoryElementMapHandler {
|
||||||
OrderMap orderMap = strolchRealm.getOrderMap();
|
OrderMap orderMap = strolchRealm.getOrderMap();
|
||||||
|
|
||||||
File modelFile = this.realmModelFiles.get(realm);
|
File modelFile = this.realmModelFiles.get(realm);
|
||||||
InMemoryElementListener elementListener = new InMemoryElementListener(resourceMap, orderMap);
|
XmlModelStatistics statistics;
|
||||||
|
try (StrolchTransaction tx = resourceMap.openTx(realm)) {
|
||||||
|
InMemoryElementListener elementListener = new InMemoryElementListener(tx, resourceMap, orderMap);
|
||||||
XmlModelFileHandler handler = new XmlModelFileHandler(elementListener, modelFile);
|
XmlModelFileHandler handler = new XmlModelFileHandler(elementListener, modelFile);
|
||||||
handler.parseFile();
|
handler.parseFile();
|
||||||
XmlModelStatistics statistics = handler.getStatistics();
|
statistics = handler.getStatistics();
|
||||||
|
}
|
||||||
|
|
||||||
String durationS = StringHelper.formatNanoDuration(statistics.durationNanos);
|
String durationS = StringHelper.formatNanoDuration(statistics.durationNanos);
|
||||||
logger.info(MessageFormat.format(
|
logger.info(MessageFormat.format(
|
||||||
"Loading XML Model file {0} for realm {1} took {2}.", modelFile.getName(), realm, durationS)); //$NON-NLS-1$
|
"Loading XML Model file {0} for realm {1} took {2}.", modelFile.getName(), realm, durationS)); //$NON-NLS-1$
|
||||||
|
|
|
@ -20,10 +20,10 @@ import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import li.strolch.model.StrolchElement;
|
import li.strolch.model.StrolchElement;
|
||||||
|
import li.strolch.persistence.api.StrolchTransaction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class InMemoryQuery<T extends StrolchElement> {
|
public class InMemoryQuery<T extends StrolchElement> {
|
||||||
|
|
||||||
|
@ -51,9 +51,9 @@ public class InMemoryQuery<T extends StrolchElement> {
|
||||||
this.selectors.add(selector);
|
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();
|
Iterator<T> iter = elements.iterator();
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
T element = iter.next();
|
T element = iter.next();
|
||||||
|
|
|
@ -18,10 +18,10 @@ package li.strolch.runtime.query.inmemory;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import li.strolch.model.StrolchElement;
|
import li.strolch.model.StrolchElement;
|
||||||
|
import li.strolch.persistence.api.StrolchTransaction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class ListNavigator<T extends StrolchElement> implements Navigator<T> {
|
public class ListNavigator<T extends StrolchElement> implements Navigator<T> {
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ public class ListNavigator<T extends StrolchElement> implements Navigator<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<T> navigate() {
|
public List<T> navigate(StrolchTransaction tx) {
|
||||||
return this.input;
|
return this.input;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,12 +18,12 @@ package li.strolch.runtime.query.inmemory;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import li.strolch.model.StrolchElement;
|
import li.strolch.model.StrolchElement;
|
||||||
|
import li.strolch.persistence.api.StrolchTransaction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public interface Navigator<T extends StrolchElement>{
|
public interface Navigator<T extends StrolchElement>{
|
||||||
|
|
||||||
public List<T> navigate();
|
public List<T> navigate(StrolchTransaction tx);
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,11 +18,11 @@ package li.strolch.runtime.query.inmemory;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import li.strolch.model.StrolchElement;
|
import li.strolch.model.StrolchElement;
|
||||||
|
import li.strolch.persistence.api.StrolchTransaction;
|
||||||
import li.strolch.runtime.agent.api.ElementMap;
|
import li.strolch.runtime.agent.api.ElementMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public abstract class StrolchTypeNavigator<T extends StrolchElement> implements Navigator<T> {
|
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;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<T> navigate() {
|
@Override
|
||||||
return getElementMap().getElementsBy(this.type);
|
public List<T> navigate(StrolchTransaction tx) {
|
||||||
|
return getElementMap().getElementsBy(tx, this.type);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract ElementMap<T> getElementMap();
|
protected abstract ElementMap<T> getElementMap();
|
||||||
|
|
|
@ -57,7 +57,7 @@ public class InMemoryQueryTest {
|
||||||
orderQuery.setNavigator(new ListNavigator<>(orders));
|
orderQuery.setNavigator(new ListNavigator<>(orders));
|
||||||
orderQuery.addSelector(new IdSelector<Order>("@1"));
|
orderQuery.addSelector(new IdSelector<Order>("@1"));
|
||||||
|
|
||||||
List<Order> result = orderQuery.doQuery();
|
List<Order> result = orderQuery.doQuery(null);
|
||||||
assertEquals(1, result.size());
|
assertEquals(1, result.size());
|
||||||
assertEquals("@1", result.get(0).getId());
|
assertEquals("@1", result.get(0).getId());
|
||||||
}
|
}
|
||||||
|
@ -71,7 +71,7 @@ public class InMemoryQueryTest {
|
||||||
resourceQuery.setNavigator(new ListNavigator<>(resources));
|
resourceQuery.setNavigator(new ListNavigator<>(resources));
|
||||||
resourceQuery.addSelector(new IdSelector<Resource>("@1"));
|
resourceQuery.addSelector(new IdSelector<Resource>("@1"));
|
||||||
|
|
||||||
List<Resource> result = resourceQuery.doQuery();
|
List<Resource> result = resourceQuery.doQuery(null);
|
||||||
assertEquals(1, result.size());
|
assertEquals(1, result.size());
|
||||||
assertEquals("@1", result.get(0).getId());
|
assertEquals("@1", result.get(0).getId());
|
||||||
}
|
}
|
||||||
|
@ -87,7 +87,7 @@ public class InMemoryQueryTest {
|
||||||
new IdSelector<Resource>("@4"));
|
new IdSelector<Resource>("@4"));
|
||||||
resourceQuery.addSelector(andSelector);
|
resourceQuery.addSelector(andSelector);
|
||||||
|
|
||||||
List<Resource> result = resourceQuery.doQuery();
|
List<Resource> result = resourceQuery.doQuery(null);
|
||||||
assertEquals(2, result.size());
|
assertEquals(2, result.size());
|
||||||
assertEquals("@3", result.get(0).getId());
|
assertEquals("@3", result.get(0).getId());
|
||||||
assertEquals("@4", result.get(1).getId());
|
assertEquals("@4", result.get(1).getId());
|
||||||
|
@ -106,7 +106,7 @@ public class InMemoryQueryTest {
|
||||||
BooleanSelector<Resource> andSelector = new AndSelector<Resource>(andSelectors);
|
BooleanSelector<Resource> andSelector = new AndSelector<Resource>(andSelectors);
|
||||||
resourceQuery.addSelector(andSelector);
|
resourceQuery.addSelector(andSelector);
|
||||||
|
|
||||||
List<Resource> result = resourceQuery.doQuery();
|
List<Resource> result = resourceQuery.doQuery(null);
|
||||||
assertEquals(1, result.size());
|
assertEquals(1, result.size());
|
||||||
assertEquals("@3", result.get(0).getId());
|
assertEquals("@3", result.get(0).getId());
|
||||||
}
|
}
|
||||||
|
@ -124,7 +124,7 @@ public class InMemoryQueryTest {
|
||||||
BooleanSelector<Resource> andSelector = new AndSelector<Resource>(andSelectors);
|
BooleanSelector<Resource> andSelector = new AndSelector<Resource>(andSelectors);
|
||||||
resourceQuery.addSelector(andSelector);
|
resourceQuery.addSelector(andSelector);
|
||||||
|
|
||||||
List<Resource> result = resourceQuery.doQuery();
|
List<Resource> result = resourceQuery.doQuery(null);
|
||||||
assertEquals(0, result.size());
|
assertEquals(0, result.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,7 +140,7 @@ public class InMemoryQueryTest {
|
||||||
ballQuery.addSelector(ParameterSelector.<Resource>booleanSelector("parameters", "forChildren", true));
|
ballQuery.addSelector(ParameterSelector.<Resource>booleanSelector("parameters", "forChildren", true));
|
||||||
ballQuery.addSelector(ParameterSelector.<Resource>floatSelector("parameters", "diameter", 22.0));
|
ballQuery.addSelector(ParameterSelector.<Resource>floatSelector("parameters", "diameter", 22.0));
|
||||||
|
|
||||||
List<Resource> result = ballQuery.doQuery();
|
List<Resource> result = ballQuery.doQuery(null);
|
||||||
assertEquals(1, result.size());
|
assertEquals(1, result.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,9 @@ import li.strolch.model.query.ParameterSelection;
|
||||||
import li.strolch.model.query.ResourceQuery;
|
import li.strolch.model.query.ResourceQuery;
|
||||||
import li.strolch.model.query.Selection;
|
import li.strolch.model.query.Selection;
|
||||||
import li.strolch.model.query.StrolchTypeNavigation;
|
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.agent.api.StrolchAgent;
|
||||||
import li.strolch.runtime.query.inmemory.InMemoryOrderQueryVisitor;
|
import li.strolch.runtime.query.inmemory.InMemoryOrderQueryVisitor;
|
||||||
import li.strolch.runtime.query.inmemory.InMemoryQuery;
|
import li.strolch.runtime.query.inmemory.InMemoryQuery;
|
||||||
|
@ -58,7 +61,10 @@ public class QueryTest {
|
||||||
Resource res1 = createResource("@1", "Test Resource", "MyType");
|
Resource res1 = createResource("@1", "Test Resource", "MyType");
|
||||||
IntegerParameter iP = new IntegerParameter("nbOfBooks", "Number of Books", 33);
|
IntegerParameter iP = new IntegerParameter("nbOfBooks", "Number of Books", 33);
|
||||||
res1.addParameter(BAG_ID, iP);
|
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<>();
|
List<Selection> elementAndSelections = new ArrayList<>();
|
||||||
elementAndSelections.add(new IdSelection("@1"));
|
elementAndSelections.add(new IdSelection("@1"));
|
||||||
|
@ -69,7 +75,10 @@ public class QueryTest {
|
||||||
|
|
||||||
InMemoryResourceQueryVisitor resourceQuery = new InMemoryResourceQueryVisitor(agent.getContainer());
|
InMemoryResourceQueryVisitor resourceQuery = new InMemoryResourceQueryVisitor(agent.getContainer());
|
||||||
InMemoryQuery<Resource> inMemoryQuery = resourceQuery.visit(query);
|
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.size());
|
||||||
assertEquals("@1", result.get(0).getId());
|
assertEquals("@1", result.get(0).getId());
|
||||||
}
|
}
|
||||||
|
@ -82,7 +91,10 @@ public class QueryTest {
|
||||||
Order o1 = createOrder("@1", "Test Order", "MyType");
|
Order o1 = createOrder("@1", "Test Order", "MyType");
|
||||||
IntegerParameter iP = new IntegerParameter("nbOfBooks", "Number of Books", 33);
|
IntegerParameter iP = new IntegerParameter("nbOfBooks", "Number of Books", 33);
|
||||||
o1.addParameter(BAG_ID, iP);
|
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<>();
|
List<Selection> elementAndSelections = new ArrayList<>();
|
||||||
elementAndSelections.add(new IdSelection("@1"));
|
elementAndSelections.add(new IdSelection("@1"));
|
||||||
|
@ -93,7 +105,10 @@ public class QueryTest {
|
||||||
|
|
||||||
InMemoryOrderQueryVisitor orderQuery = new InMemoryOrderQueryVisitor(agent.getContainer());
|
InMemoryOrderQueryVisitor orderQuery = new InMemoryOrderQueryVisitor(agent.getContainer());
|
||||||
InMemoryQuery<Order> inMemoryQuery = orderQuery.visit(query);
|
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.size());
|
||||||
assertEquals("@1", result.get(0).getId());
|
assertEquals("@1", result.get(0).getId());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue