Fixed file not found exception during recovery

Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
Jim Martens 2017-05-31 14:56:11 +02:00
parent 07a2cf16f9
commit 3d51309e0b
1 changed files with 34 additions and 17 deletions

View File

@ -107,45 +107,62 @@ public class PersistenceManager {
/** /**
* Performs the recovery actions. * Performs the recovery actions.
*/ */
public void recovery() { public synchronized void recovery() {
try { try {
BufferedReader reader = new BufferedReader(new FileReader(_dataPath + "log.txt")); BufferedReader reader = new BufferedReader(new FileReader(_dataPath + "log.txt"));
String line; String line;
List<Integer> winner_tas = new ArrayList<>(); List<Integer> winner_tas = new ArrayList<>();
int lsnMax = 0;
int taidMax = 0;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
String[] cols = line.split(","); String[] cols = line.split(",");
if (cols[2].equals("COMMIT")) { if (cols[2].equals("COMMIT")) {
winner_tas.add(Integer.valueOf(cols[1])); winner_tas.add(Integer.valueOf(cols[1]));
} }
if (Integer.valueOf(cols[0]) >= lsnMax) {
lsnMax = Integer.valueOf(cols[0]);
}
if (Integer.valueOf(cols[1]) >= taidMax) {
taidMax = Integer.valueOf(cols[1]);
}
} }
reader.close(); reader.close();
_nextLogSequenceNumber = lsnMax + 1;
_nextTransactionNumber = taidMax + 1;
reader = new BufferedReader(new FileReader(_dataPath + "log.txt")); reader = new BufferedReader(new FileReader(_dataPath + "log.txt"));
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
String[] cols = line.split(","); String[] cols = line.split(",");
if (!cols[2].equals("WRITE") || !winner_tas.contains(Integer.valueOf(cols[1]))) { if (!cols[2].equals("WRITE") || !winner_tas.contains(Integer.valueOf(cols[1]))) {
continue; continue;
} }
int lsn = Integer.valueOf(cols[0]); int lsn = Integer.valueOf(cols[0]);
int pageID = Integer.valueOf(cols[3]); int pageID = Integer.valueOf(cols[3]);
String data = cols[4]; String data = cols[4];
int pageLSN = -1;
BufferedReader readPage = new BufferedReader(new FileReader(_dataPath + pageID + ".txt")); try {
String pageLine = readPage.readLine(); BufferedReader readPage = new BufferedReader(new FileReader(_dataPath + pageID + ".txt"));
readPage.close(); String pageLine = readPage.readLine();
String[] pageCols = pageLine.split(","); readPage.close();
int pageLSN = Integer.parseInt(pageCols[1]); String[] pageCols = pageLine.split(",");
pageLSN = Integer.parseInt(pageCols[1]);
}
catch (FileNotFoundException fne) {
//;
}
catch (IOException ioe) {
ioe.printStackTrace();
}
if (pageLSN >= lsn) { if (pageLSN >= lsn) {
continue; continue;
} }
FileWriter writer = new FileWriter(_dataPath + pageID + ".txt"); FileWriter writer = new FileWriter(_dataPath + pageID + ".txt");
writer.write("" + pageID + "," + lsn + "," + data + "\n"); writer.write("" + pageID + "," + lsn + "," + data + "\n");
writer.close(); writer.close();
} }
reader.close(); reader.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }