Created
July 25, 2011 04:55
-
-
Save klauswuestefeld/1103582 to your computer and use it in GitHub Desktop.
PrevaylerJr - "To serve the persistence needs of 99% of information systems in the world using 23 semicolons."
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
public class PrevaylerJr { | |
public static interface Command extends Serializable { | |
Object executeOn(Object system); | |
} | |
private final Object _system; | |
private final ObjectOutputStream _journal; | |
public PrevaylerJr(Serializable initialState, File storageFile) throws Exception { | |
File tempFile = new File(storageFile.getAbsolutePath() + ".tmp"); | |
_system = restoreState(initialState, storageFile, tempFile); | |
_journal = new ObjectOutputStream(new FileOutputStream(tempFile)); | |
writeToJournal(_system); | |
if (storageFile.delete() && tempFile.renameTo(storageFile)) | |
return; | |
throw new IOException("Unable to rename " + tempFile + " to " + storageFile); | |
} | |
synchronized | |
public Object executeTransaction(Command transaction) throws Exception { | |
writeToJournal(transaction); | |
return transaction.executeOn(_system); | |
} | |
synchronized | |
public Object executeQuery(Command query) { | |
return query.executeOn(_system); | |
} | |
private Object restoreState(Object initialState, File storageFile, File tempFile) { | |
Object state = initialState; | |
try { | |
File fileToRead = storageFile.exists() ? storageFile : tempFile; | |
ObjectInputStream input = new ObjectInputStream(new FileInputStream(fileToRead)); | |
state = restoreImage(input); | |
restoreCommands(state, input); | |
} catch (Exception endOfStreamReached) {} | |
return state; | |
} | |
private Serializable restoreImage(ObjectInputStream input) throws IOException, ClassNotFoundException { | |
return (Serializable) input.readObject(); | |
} | |
private void restoreCommands(Object state, ObjectInputStream input) throws IOException, ClassNotFoundException { | |
while (true) | |
((Command) input.readObject()).executeOn(state); | |
} | |
private void writeToJournal(Object object) throws IOException { | |
_journal.writeObject(object); | |
_journal.flush(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is there a reason to use Object instead of Serializable on lines 67,41 and 11?