[Major] Implemented CACHED mode

This lead to refactoring of other code:
 - removed get*Dao() from StrolchTransaction
 - added StrolchTransaction.getPersistenceHandler()
 - removed unused variables in TransactionalElementMap
   - this lead to removal of constructors in subclasses
 - added ComponentContainer.getDataStoreMode()
 - added ElementMap.addAll(), removeAll() and updateAll() methods
   - implemented in all ElementMap implementations
This commit is contained in:
Robert von Burg 2014-01-11 17:56:07 +01:00
parent fa2f927856
commit 5ac8d4f875
7 changed files with 134 additions and 13 deletions

View File

@ -24,7 +24,9 @@ import li.strolch.agent.impl.StrolchRealm;
import li.strolch.model.Order;
import li.strolch.model.Resource;
import li.strolch.model.Tags;
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.persistence.xml.model.OrderContextFactory;
import li.strolch.persistence.xml.model.ResourceContextFactory;
@ -72,10 +74,20 @@ public class XmlPersistenceHandler extends StrolchComponent implements Persisten
@Override
public StrolchTransaction openTx(StrolchRealm realm) {
PersistenceTransaction tx = this.persistenceManager.openTx(realm.getRealm());
XmlStrolchTransaction strolchTx = new XmlStrolchTransaction(realm, tx);
XmlStrolchTransaction strolchTx = new XmlStrolchTransaction(realm, tx, this);
if (getContainer().hasComponent(ObserverHandler.class)) {
strolchTx.setObserverHandler(getContainer().getComponent(ObserverHandler.class));
}
return strolchTx;
}
@Override
public OrderDao getOrderDao(StrolchTransaction tx) {
return new XmlOrderDao(tx);
}
@Override
public ResourceDao getResourceDao(StrolchTransaction tx) {
return new XmlResourceDao(tx);
}
}

View File

@ -20,8 +20,7 @@ import java.util.Set;
import li.strolch.agent.impl.StrolchRealm;
import li.strolch.model.StrolchElement;
import li.strolch.persistence.api.AbstractTransaction;
import li.strolch.persistence.api.OrderDao;
import li.strolch.persistence.api.ResourceDao;
import li.strolch.persistence.api.PersistenceHandler;
import li.strolch.persistence.api.StrolchPersistenceException;
import li.strolch.persistence.api.TransactionCloseStrategy;
import li.strolch.runtime.observer.ObserverHandler;
@ -31,13 +30,15 @@ import ch.eitchnet.xmlpers.api.TransactionResult;
public class XmlStrolchTransaction extends AbstractTransaction {
private XmlPersistenceHandler persistenceHandler;
private ObserverHandler observerHandler;
private boolean suppressUpdates;
private PersistenceTransaction tx;
private TransactionCloseStrategy closeStrategy;
public XmlStrolchTransaction(StrolchRealm realm, PersistenceTransaction tx) {
public XmlStrolchTransaction(StrolchRealm realm, PersistenceTransaction tx, XmlPersistenceHandler persistenceHandler) {
super(realm);
this.persistenceHandler = persistenceHandler;
this.suppressUpdates = false;
this.tx = tx;
this.closeStrategy = TransactionCloseStrategy.COMMIT;
@ -84,6 +85,7 @@ public class XmlStrolchTransaction extends AbstractTransaction {
}
this.tx.autoCloseableCommit();
logger.info(txResult.getLogMessage());
if (!this.suppressUpdates && this.observerHandler != null) {
@ -114,12 +116,7 @@ public class XmlStrolchTransaction extends AbstractTransaction {
}
@Override
public OrderDao getOrderDao() {
return new XmlOrderDao(this);
}
@Override
public ResourceDao getResourceDao() {
return new XmlResourceDao(this);
public PersistenceHandler getPersistenceHandler() {
return this.persistenceHandler;
}
}

View File

@ -0,0 +1,65 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.persistence.impl.dao.test;
import static org.junit.Assert.assertNotNull;
import java.io.File;
import li.strolch.model.Order;
import li.strolch.model.Resource;
import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.testbase.runtime.RuntimeMock;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class ExistingDbTest {
public static final String RUNTIME_PATH = "target/existingDbRuntime/"; //$NON-NLS-1$
public static final String DB_STORE_PATH_DIR = "dbStore"; //$NON-NLS-1$
public static final String CONFIG_SRC = "src/test/resources/existingDbRuntime"; //$NON-NLS-1$
protected static RuntimeMock runtimeMock;
@BeforeClass
public static void beforeClass() {
File rootPath = new File(RUNTIME_PATH);
File configSrc = new File(CONFIG_SRC);
runtimeMock = new RuntimeMock();
runtimeMock.mockRuntime(rootPath, configSrc);
runtimeMock.startContainer(rootPath);
}
@AfterClass
public static void afterClass() {
runtimeMock.destroyRuntime();
}
@Test
public void shouldQueryExistingData() {
try (StrolchTransaction tx = runtimeMock.getDefaultRealm().openTx()) {
Resource resource = tx.getResourceMap().getBy(tx, "MyType", "@1");
assertNotNull("Should be able to read existing element from db", resource);
Order order = tx.getOrderMap().getBy(tx, "MyType", "@1");
assertNotNull("Should be able to read existing element from db", order);
}
}
}

View File

@ -111,13 +111,13 @@ public class ObserverUpdateTest {
// create order
Order newOrder = createOrder("MyTestOrder", "Test Name", "TestType", new Date(), State.CREATED); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
try (StrolchTransaction tx = runtimeMock.getDefaultRealm().openTx()) {
tx.getOrderDao().save(newOrder);
tx.getOrderMap().add(tx, newOrder);
}
// create resource
Resource newResource = createResource("MyTestResource", "Test Name", "TestType"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
try (StrolchTransaction tx = runtimeMock.getDefaultRealm().openTx()) {
tx.getResourceDao().save(newResource);
tx.getResourceMap().add(tx, newResource);
}
assertEquals(2, observer.results.size());

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<StrolchConfiguration>
<Runtime>
<applicationName>StrolchPersistenceTest</applicationName>
<Properties>
<dataStoreMode>CACHED</dataStoreMode>
<verbose>true</verbose>
</Properties>
</Runtime>
<Component>
<name>PersistenceHandler</name>
<api>li.strolch.persistence.api.PersistenceHandler</api>
<impl>li.strolch.persistence.xml.XmlPersistenceHandler</impl>
<Properties>
<verbose>true</verbose>
</Properties>
</Component>
<Component>
<name>ObserverHandler</name>
<api>li.strolch.runtime.observer.ObserverHandler</api>
<impl>li.strolch.runtime.observer.DefaultObserverHandler</impl>
</Component>
</StrolchConfiguration>

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Order Date="2014-01-10T22:13:50.826+01:00" Id="@1" Name="My Order 0" State="CREATED" Type="MyType">
<ParameterBag Id="@bag01" Name="Test Bag" Type="TestBag">
<Parameter Id="@param7" Name="StringList Param" Type="StringList" Value="Hello;World"/>
<Parameter Id="@param6" Name="Date Param" Type="Date" Value="2012-11-30T18:12:05.628+01:00"/>
<Parameter Id="@param5" Name="String Param" Type="String" Value="Strolch"/>
<Parameter Id="@param4" Name="Long Param" Type="Long" Value="4453234566"/>
<Parameter Id="@param3" Name="Integer Param" Type="Integer" Value="77"/>
<Parameter Id="@param2" Name="Float Param" Type="Float" Value="44.3"/>
<Parameter Id="@param1" Name="Boolean Param" Type="Boolean" Value="true"/>
</ParameterBag>
</Order>

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Resource Id="@1" Name="My Resource 0" Type="MyType">
<ParameterBag Id="@bag01" Name="Test Bag" Type="TestBag">
<Parameter Id="@param7" Name="StringList Param" Type="StringList" Value="Hello;World"/>
<Parameter Id="@param6" Name="Date Param" Type="Date" Value="2012-11-30T18:12:05.628+01:00"/>
<Parameter Id="@param5" Name="String Param" Type="String" Value="Strolch"/>
<Parameter Id="@param4" Name="Long Param" Type="Long" Value="4453234566"/>
<Parameter Id="@param3" Name="Integer Param" Type="Integer" Value="77"/>
<Parameter Id="@param2" Name="Float Param" Type="Float" Value="44.3"/>
<Parameter Id="@param1" Name="Boolean Param" Type="Boolean" Value="true"/>
</ParameterBag>
</Resource>