comparison src/org/dancres/blitz/ExistsFactory.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;
2
3 import java.lang.ref.WeakReference;
4 import java.io.IOException;
5
6 import org.dancres.blitz.txnlock.BaulkedParty;
7 import org.dancres.blitz.entry.SearchVisitor;
8 import org.dancres.blitz.entry.EntryRepository;
9 import org.dancres.blitz.entry.EntryRepositoryFactory;
10 import org.dancres.blitz.mangler.MangledEntry;
11
12 /**
13 * Use this class for *Exists searches return single results, bulk operations
14 * are <b>not</b> currently supported.
15 *
16 * This class maintains a count of transaction conflicts that have occurred
17 * and when it reaches zero, signals the associated <code>MatchTask</code> via
18 * it's <code>SearchVisitor</code>. Note the signal is only permitted after
19 * <code>enableResolutionSignal</code> has been called. This provides a
20 * barrier such that a full disk-search can be completed without false
21 * terminations from resulting conflicts.
22 */
23 public class ExistsFactory implements VisitorBaulkedPartyFactory {
24 private int _conflicts;
25 private boolean _resolvable = false;
26 private WeakReference _task;
27
28 public BaulkedParty newParty(SingleMatchTask aMatchTask) {
29 _task = new WeakReference(aMatchTask);
30 return new BaulkedPartyImpl();
31 }
32
33 public BaulkedParty newParty(BulkTakeVisitor aMatchTask) {
34 throw new RuntimeException
35 ("Exists not currently supported on bulk operations");
36 }
37
38 public void enableResolutionSignal() {
39 synchronized (this) {
40 _resolvable = true;
41 }
42
43 testAndSignal();
44 }
45
46 private SearchVisitor getVisitor() {
47 MatchTask myTask = (MatchTask) _task.get();
48
49 if (myTask == null)
50 return null;
51 else
52 return myTask.getVisitor();
53 }
54
55 private void testAndSignal() {
56 int myConflicts;
57
58 synchronized(this) {
59 if (!_resolvable)
60 return;
61
62 myConflicts = _conflicts;
63 }
64
65 if (myConflicts == 0) {
66 SingleMatchTask myTask = (SingleMatchTask) _task.get();
67
68 if (myTask != null) {
69 myTask.sendEvent(CompletionEvent.COMPLETED);
70 }
71 }
72 }
73
74 private class BaulkedPartyImpl implements BaulkedParty {
75
76 public BaulkedPartyImpl() {
77 }
78
79 public void blocked(Object aHandback) {
80 synchronized(ExistsFactory.this) {
81 ++_conflicts;
82 }
83 }
84
85 public void unblocked(Object aHandback) {
86 Handback myHandback = (Handback) aHandback;
87
88 try {
89 SearchVisitor myVisitor = getVisitor();
90
91 if (myVisitor == null)
92 return;
93
94 EntryRepository myRepos =
95 EntryRepositoryFactory.get().get(myHandback.getType());
96
97 myRepos.find(myVisitor, myHandback.getOID(),
98 myHandback.getEntry());
99
100 } catch (IOException aDbe) {
101 } finally {
102 synchronized(ExistsFactory.this) {
103 --_conflicts;
104 }
105
106 testAndSignal();
107 }
108 }
109 }
110 }