Re: [Prevayler-coders] The PrevaylerJr Game
Brought to you by:
jsampson,
klauswuestefeld
From: Klaus W. <kla...@gm...> - 2008-07-03 06:49:56
|
> There's a remaining bug -- while transactions will now be prevented > from executing since the journal has been closed, queries are still > allowed to execute against the corrupted prevalent system. Yup. Fixed below. Anything else? Is that it? Is this what 90% of all apps (that fit in RAM) out there need in terms of persistence? Klaus -------------------- import java.io.EOFException; import java.io.IOException; public class PrevaylerJr { private final Object _system; private final AcidOutputStream _journal; public PrevaylerJr(Object initialState, String storageFile) throws Exception { AcidInputStream input = new AcidInputStream(storageFile); try { initialState = input.readObject(); while (true) ((Command)input.readObject()).executeOn(initialState); } catch (EOFException expected) {} _system = initialState; _journal = new AcidOutputStream(storageFile, _system); } synchronized public Object executeTransaction(Command transaction) throws Exception { checkSystemConsistency(); _journal.append(transaction); return tryToExecute(transaction); } synchronized public Object executeQuery(Command query) { checkSystemConsistency(); return tryToExecute(query); } private Object tryToExecute(Command command) { try { return command.executeOn(_system); } catch (Error err) { _journal.close(); throw err; } } private void checkSystemConsistency() { if (_journal.isClosed()) throw new IllegalStateException("An Error such as OutOfMemoryError or StackOverflowError was thrown while executing some previous transaction or query. The system might be in an inconsistent state."); } public static interface Command { Object executeOn(Object system); } } class AcidInputStream { public AcidInputStream(String fileName) throws IOException {} public Object readObject() throws IOException {return null;} } class AcidOutputStream { /** Atomically and durably replaces the previous journal file with the new entry using file rename manoeuvres.*/ public AcidOutputStream(String fileName, Object firstEntry) throws IOException {} public void append(Object entry) throws IOException {} public void close() {} public boolean isClosed() {return false;} } |