Finished PersistenceManager

Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
2017-05-24 15:07:33 +02:00
parent 90d537f64d
commit 364ecae424

View File

@ -53,6 +53,7 @@ public class PersistenceManager {
*/ */
public int beginTransaction() { public int beginTransaction() {
_transactions.put(_nextTransactionNumber, false); _transactions.put(_nextTransactionNumber, false);
logSimple(_nextTransactionNumber, "BOT");
// return the next transaction number and increase it by one afterwards // return the next transaction number and increase it by one afterwards
return _nextTransactionNumber++; return _nextTransactionNumber++;
} }
@ -70,6 +71,7 @@ public class PersistenceManager {
// only perform commit actions if transaction isn't commited yet // only perform commit actions if transaction isn't commited yet
if (!_transactions.get(taid)) { if (!_transactions.get(taid)) {
_transactions.replace(taid, true); _transactions.replace(taid, true);
logSimple(taid, "COMMIT");
} }
} }
@ -95,14 +97,14 @@ public class PersistenceManager {
_transactionPageBuffer.put(taid, pageIDs); _transactionPageBuffer.put(taid, pageIDs);
_pageBuffer.put(pageid, data); _pageBuffer.put(pageid, data);
// log changes // log changes
int lsn = log(taid, pageid, data); int lsn = logWrite(taid, pageid, data);
_pageLSN.put(pageid, lsn); _pageLSN.put(pageid, lsn);
// call persist // call persist
persist(); persist();
} }
/** /**
* Logs the necessary information to the log file. * Logs a write to the log file.
* *
* @param taid * @param taid
* affected transaction * affected transaction
@ -112,11 +114,32 @@ public class PersistenceManager {
* written data * written data
* @return log sequence number * @return log sequence number
*/ */
private int log(int taid, int pageid, String data) { private int logWrite(int taid, int pageid, String data) {
try { try {
FileWriter fw = new FileWriter("../data/log.txt", true); FileWriter fw = new FileWriter("../data/log.txt", true);
// TODO properly create redo data fw.write("" + _nextLogSequenceNumber + "," + taid + ",WRITE," + pageid + "," + data);
fw.write("" + _nextLogSequenceNumber + "," + taid + "," + pageid + "," + data); fw.write("\n");
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
return _nextLogSequenceNumber++;
}
/**
* Logs a BOT to the log file.
*
* @param taid
* affected transaction
* @param type
* log type
* @return log sequence number
*/
private int logSimple(int taid, String type) {
try {
FileWriter fw = new FileWriter("../data/log.txt", true);
fw.write("" + _nextLogSequenceNumber + "," + taid + "," + type + "\n");
fw.write("\n"); fw.write("\n");
fw.close(); fw.close();
} catch (IOException e) { } catch (IOException e) {
@ -144,7 +167,6 @@ public class PersistenceManager {
} }
} }
// TODO update log file as needed
for (int taid : commitedTransactions) { for (int taid : commitedTransactions) {
Set<Integer> pageIDs = _transactionPageBuffer.get(taid); Set<Integer> pageIDs = _transactionPageBuffer.get(taid);
for (int pageid : pageIDs) { for (int pageid : pageIDs) {