comparison src/org/dancres/blitz/disk/SyncFinalizer.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.dancres.blitz.disk;
2
3 import java.util.logging.Level;
4
5 import com.sleepycat.je.Environment;
6 import com.sleepycat.je.DatabaseException;
7 import com.sleepycat.je.CheckpointConfig;
8
9 import org.dancres.blitz.task.Task;
10
11 /**
12 <p>We pass one of these to the WriteDaemon which calls back on it once
13 it has "forced" all write requests lodged before the call to force. This
14 ensures we checkpoint Db having flushed the relevant updates.</p>
15
16 <p>Optionally, the SyncFinalizer can callback a user-defined
17 task to perform additional work post sync. If this mechanism is used, the
18 thread which calls <code>waitForCompletion()</code> will not be blocked
19 because all work will be done asynchronously. Otherwise, the calling thread
20 is blocked until completion.</p>
21 */
22 class SyncFinalizer implements Task {
23 private boolean isDone = false;
24 private Runnable theCompletionTask;
25 private Environment theEnv;
26
27 SyncFinalizer(Environment anEnv, Runnable aCompletionTask) {
28 theEnv = anEnv;
29 theCompletionTask = aCompletionTask;
30 }
31
32 public void run() {
33 try {
34 theEnv.sync();
35
36 CheckpointConfig myConfig = new CheckpointConfig();
37 myConfig.setForce(true);
38 theEnv.checkpoint(myConfig);
39
40 } catch (DatabaseException aDbe) {
41 WriteDaemon.theLogger.log(Level.SEVERE,
42 "Warning, failed to checkpoint", aDbe);
43 }
44
45 if (theCompletionTask != null)
46 theCompletionTask.run();
47 else
48 synchronized(this) {
49 isDone = true;
50 notify();
51 }
52 }
53
54 /**
55 Only blocks when there's no completion task. i.e. The completion task
56 is assumed to imply that the caller requires asynchronous completion
57 and does not wish to block.
58 */
59 void waitForCompletion() {
60 // No completion task means block.
61 //
62 if (theCompletionTask == null) {
63 long myStart = System.currentTimeMillis();
64
65 synchronized(this) {
66 while (!isDone) {
67 try {
68 wait();
69 } catch (InterruptedException anIE) {
70 }
71 }
72 }
73
74 long myEnd = System.currentTimeMillis();
75 WriteDaemon.theLogger.log(Level.FINE,
76 "Queue blocked for: " +
77 (myEnd - myStart));
78 }
79 }
80 }