use of org.apache.jena.tdb.sys.FileRef in project jena by apache.
the class JournalControl method recoverFromJournal.
/**
* Recovery from a journal.
* Find if there is a commit record; if so, replay the journal to that point.
* Try to see if there is another commit record ...
* Return true if a recovery was attempted; return false if we decided no work needed.
*/
public static boolean recoverFromJournal(StorageConfig sConf, Journal jrnl) {
if (jrnl.isEmpty())
return false;
for (FileRef fileRef : sConf.objectFiles.keySet()) recoverNodeDat(sConf.location, fileRef);
long posn = 0;
for (; ; ) {
// Any errors indicate a partially written journal.
// A commit was not written properly in the prepare phase.
// e.g. JVM died half-way though writing the prepare phase data.
// The valid journal ends at this point. Exit loop and clean up.
long x;
try {
x = scanForCommit(jrnl, posn);
} catch (TDBException ex) {
x = -1;
}
if (x == -1)
break;
recoverSegment(jrnl, posn, x, sConf);
posn = x;
}
// Sync database.
syncAll(sConf);
// We have replayed the journals - clean up.
jrnl.truncate(0);
jrnl.sync();
return true;
}
use of org.apache.jena.tdb.sys.FileRef in project jena by apache.
the class Journal method _read.
// read one entry at the channel position.
// Move position to end of read.
private JournalEntry _read() {
header.clear();
int lenRead = channel.read(header);
if (lenRead == -1) {
// probably broken file.
throw new TDBTransactionException("Read off the end of a journal file");
// return null;
}
header.rewind();
int typeId = header.getInt();
int len = header.getInt();
int ref = header.getInt();
int blockId = header.getInt();
Adler32 adler = new Adler32();
adler.update(header.array());
ByteBuffer bb = ByteBuffer.allocate(len);
lenRead = channel.read(bb);
if (lenRead != len)
throw new TDBTransactionException("Failed to read the journal entry: wanted " + len + " bytes, got " + lenRead);
adler.update(bb.array());
bb.rewind();
// checksum
crcTrailer.clear();
lenRead = channel.read(crcTrailer);
if (lenRead != SizeofCRC)
throw new TDBTransactionException("Failed to read block checksum (got " + lenRead + " bytes, not " + SizeofCRC + ").");
int checksum = Bytes.getInt(crcTrailer.array());
if (checksum != (int) adler.getValue())
throw new TDBTransactionException("Checksum error reading from the Journal.");
JournalEntryType type = JournalEntryType.type(typeId);
FileRef fileRef = FileRef.get(ref);
Block block = new Block(blockId, bb);
return new JournalEntry(type, fileRef, block);
}
use of org.apache.jena.tdb.sys.FileRef in project jena by apache.
the class JournalControl method recovery.
/** Recover a base storage DatasetGraph */
public static void recovery(DatasetGraphTDB dsg) {
if (dsg.getLocation().isMem())
return;
// Do we need to recover?
Journal journal = findJournal(dsg);
if (journal == null || journal.isEmpty())
return;
for (FileRef fileRef : dsg.getConfig().nodeTables.keySet()) recoverNodeDat(dsg, fileRef);
recoverFromJournal(dsg.getConfig(), journal);
journal.close();
// Recovery complete. Tidy up. Node journal files have already been handled.
if (journal.getFilename() != null) {
if (FileOps.exists(journal.getFilename()))
FileOps.delete(journal.getFilename());
}
}
Aggregations