/* HeliDB -- A simple database for Java, http://www.helidb.org
* Copyright (C) 2008, 2009 Karl Gustafsson
*
* This file is a part of HeliDB.
*
* HeliDB is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeliDB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.helidb.impl.txn.log;
import static org.junit.Assert.assertEquals;
import org.entityfs.EFile;
import org.entityfs.RandomAccess;
import org.entityfs.support.io.RandomAccessMode;
import org.helidb.impl.txn.log.LoggingReadWriteTxnCollaborator;
import org.helidb.txn.AbstractTransactionalDatabaseOnConstantRecordSizeBackendTest;
import org.helidb.txn.Transaction;
import org.helidb.txn.TransactionCollaborator;
import org.helidb.txn.TransactionalDatabase;
import org.junit.Test;
public abstract class AbstractLoggingTxnDbOnConstantRecordSizeBackendTest<P> extends AbstractTransactionalDatabaseOnConstantRecordSizeBackendTest
{
protected abstract TransactionalDatabase<Integer, Long> createNewDatabaseUsingSameFilesButTruncateRollbackLog(TransactionalDatabase<Integer, Long> otherDb, int howMuchToTruncate);
protected EFile truncate(EFile f, int howMuchToTruncate)
{
RandomAccess ra = f.openForRandomAccess(RandomAccessMode.READ_WRITE);
try
{
ra.setLength(ra.length() - howMuchToTruncate);
}
finally
{
ra.close();
}
return f;
}
@Override
protected void injectCannotCommit(TransactionCollaborator<?, ?, ?> collab)
{
((LoggingReadWriteTxnCollaborator<?, ?, ?>) collab).injectCannotCommit();
}
private void testProcessingOfIncompleteRollbackLog(int howMuchToTruncate)
{
TransactionalDatabase<Integer, Long> db1 = createDatabaseWoTxn();
try
{
populateDb(db1);
Transaction txn = Transaction.startTransaction(false);
try
{
db1.delete(1);
db1.update(123, 10000L);
db1.insert(2, 2L);
TransactionalDatabase<Integer, Long> db2 = createNewDatabaseUsingSameFilesButTruncateRollbackLog(db1, howMuchToTruncate);
try
{
// This should contain the old values.
assertEquals(234L, db2.get(123).longValue());
assertEquals(2L, db2.get(1).longValue());
// ...and this
assertEquals(2L, db2.get(2).longValue());
}
finally
{
tearDownDatabase(db2);
}
}
finally
{
if (!txn.isFinished())
{
txn.rollback();
}
}
}
finally
{
tearDownDatabase(db1);
}
}
@Test
public void testProcessingOfIncompleteRollbackLog()
{
for (int i = 1; i <= 12; i++)
{
testProcessingOfIncompleteRollbackLog(i);
}
}
}