Mercurial > hg > blitz_condensed
comparison src/org/prevayler/implementation/SnapshotterImpl.java @ 0:3dc0c5604566
Initial checkin of blitz 2.0 fcs - no installer yet.
author | Dan Creswell <dan.creswell@gmail.com> |
---|---|
date | Sat, 21 Mar 2009 11:00:06 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3dc0c5604566 |
---|---|
1 package org.prevayler.implementation; | |
2 | |
3 import java.io.File; | |
4 import java.io.FileOutputStream; | |
5 import java.io.ByteArrayOutputStream; | |
6 import java.io.ObjectOutputStream; | |
7 import java.io.IOException; | |
8 | |
9 import org.prevayler.PrevalentSystem; | |
10 | |
11 /** | |
12 Represents a snapshot waiting to be persisted to disk. Invoke save() to | |
13 place the snapshot on disk - should only be called once all dirty state | |
14 (if any) has been flushed to disk. | |
15 | |
16 @see org.dancres.blitz.txn.TxnManager | |
17 */ | |
18 class SnapshotterImpl implements Snapshotter { | |
19 private File theTemp; | |
20 private File theReal; | |
21 private byte[] theCachedCopy; | |
22 | |
23 private NumberFileCreator theFileCreator; | |
24 | |
25 private boolean shouldClean; | |
26 | |
27 SnapshotterImpl(NumberFileCreator aCreator, boolean doClean) | |
28 throws IOException { | |
29 | |
30 theTemp = aCreator.newTempSnapshot(); | |
31 theReal = aCreator.newSnapshot(); | |
32 | |
33 theFileCreator = aCreator; | |
34 shouldClean = doClean; | |
35 } | |
36 | |
37 void cacheSnapshot(PrevalentSystem aSystem) throws IOException { | |
38 ByteArrayOutputStream myCache = new ByteArrayOutputStream(); | |
39 ObjectOutputStream mySnapper = new ObjectOutputStream(myCache); | |
40 | |
41 mySnapper.writeObject(aSystem); | |
42 mySnapper.close(); | |
43 | |
44 theCachedCopy = myCache.toByteArray(); | |
45 } | |
46 | |
47 public void save() throws IOException { | |
48 FileOutputStream myOutput = new FileOutputStream(theTemp); | |
49 myOutput.write(theCachedCopy); | |
50 myOutput.close(); | |
51 | |
52 if (!theTemp.renameTo(theReal)) | |
53 throw new IOException("Unable to rename " + theTemp + | |
54 " to " + theReal); | |
55 | |
56 if (shouldClean) | |
57 theFileCreator.newCleaner().clean(); | |
58 } | |
59 } |