comparison src/org/dancres/blitz/NewView.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.io.IOException;
4 import java.util.logging.Level;
5 import java.util.logging.Logger;
6
7 import net.jini.core.transaction.TransactionException;
8 import net.jini.space.JavaSpace;
9
10 import org.dancres.blitz.disk.DiskTxn;
11 import org.dancres.blitz.entry.*;
12 import org.dancres.blitz.mangler.MangledEntry;
13 import org.dancres.blitz.oid.OID;
14 import org.dancres.blitz.notify.EventGenerator;
15 import org.dancres.blitz.notify.EventQueue;
16 import org.dancres.blitz.notify.QueueEvent;
17 import org.dancres.blitz.notify.EventGeneratorState;
18 import org.dancres.struct.LinkedInstance;
19
20 /**
21 <p>Reponsible for dispatching the flow of new writes into the UIDSet.</p>
22 */
23 class NewView {
24 private static final Logger theLogger =
25 Logging.newLogger("org.dancres.blitz.NewView");
26
27 private EventGeneratorImpl theEventGenerator;
28
29 private MangledEntry[] theTemplates;
30 private UIDSet theUIDs;
31
32 private EntryViewImpl theView;
33
34 NewView(EntryViewImpl aView, MangledEntry[] aTemplates, UIDSet aSet) {
35 theTemplates = aTemplates;
36 theUIDs = aSet;
37 theEventGenerator = new EventGeneratorImpl();
38 theView = aView;
39 }
40
41 EventGenerator getSearchTask() {
42 return theEventGenerator;
43 }
44
45 /**
46 @return <code>true</code> if this Visitor wishes to perform a take.
47 */
48 public boolean isDeleter() {
49 return false;
50 }
51
52 /************************************************************************
53 * View stuff
54 ************************************************************************/
55
56 private boolean atCapacity() {
57 /*
58 If we're in merge mode, theMergeTuples will tell us how many
59 Entry's we currently have accrued. Otherwise everything is added
60 to theTuples.
61 */
62 return (theUIDs.isFull());
63 }
64
65 private class EventGeneratorImpl implements EventGenerator {
66 private boolean isTainted = false;
67 private OID theOID;
68
69 EventGeneratorImpl() {
70 }
71
72 public void assign(OID anOID) {
73 theOID = anOID;
74 }
75
76 public long getStartSeqNum() {
77 return 0;
78 }
79
80 public OID getId() {
81 return theOID;
82 }
83
84 public boolean isPersistent() {
85 return false;
86 }
87
88 public long getSourceId() {
89 return 0;
90 }
91
92 void taint(boolean signal) {
93
94 /*
95 try {
96 Tasks.queue(new CleanTask(getId()));
97 } catch (InterruptedException anIE) {
98 theLogger.log(Level.WARNING,
99 "Failed to lodge cleanup for: " + getId(), anIE);
100 }
101 */
102 }
103
104 public void taint() {
105 synchronized (this) {
106 // Tainting can only be done once
107 //
108 if (isTainted)
109 return;
110
111 isTainted = true;
112 }
113
114 try {
115 EventQueue.get().kill(getId());
116 } catch (IOException anIOE) {
117 theLogger.log(Level.SEVERE,
118 "Encountered IOException during kill", anIOE);
119 }
120 }
121
122 public boolean canSee(QueueEvent anEvent, long aTime) {
123 synchronized (this) {
124 if (isTainted) {
125 return false;
126 }
127 }
128
129 // Check if it's txn_ended and my txn and call resolved if it is
130 if ((anEvent.getType() == QueueEvent.TRANSACTION_ENDED) &&
131 (theView.getTxn().getId().equals(anEvent.getTxn().getId()))) {
132 theView.resolved();
133 return false;
134 }
135
136 // We want to see new writes from a transaction
137 //
138 return (anEvent.getType() == QueueEvent.ENTRY_WRITE);
139 }
140
141 public boolean matches(MangledEntry anEntry) {
142 synchronized (this) {
143 if (isTainted) {
144 return false;
145 }
146 }
147
148 for (int i = 0; i < theTemplates.length; i++) {
149 MangledEntry myTemplate = theTemplates[i];
150
151 if (Types.isSubtype(myTemplate.getType(), anEntry.getType())) {
152 if (myTemplate.match(anEntry)) {
153 return true;
154 }
155 }
156 }
157
158 return false;
159 }
160
161 public boolean renew(long aTime) {
162 // Nothing to do as we expire by being tainted by the enclosing
163 // class only
164 //
165 return true;
166 }
167
168 public void recover(long aSeqNum) {
169 // Nothing to do
170 }
171
172 public long jumpSequenceNumber() {
173 return 0;
174 }
175
176 public long jumpSequenceNumber(long aMin) {
177 return 0;
178 }
179
180 public void ping(QueueEvent anEvent, JavaSpace aSource) {
181 synchronized (this) {
182 if (isTainted) {
183 return;
184 }
185 }
186
187 if (atCapacity()) {
188 taint();
189 return;
190 }
191
192 QueueEvent.Context myContext = anEvent.getContext();
193
194 SpaceEntryUID myUID =
195 new SpaceEntryUID(myContext.getEntry().getType(),
196 myContext.getOID());
197
198 theUIDs.add(myUID);
199 }
200
201 public EventGeneratorState getMemento() {
202 throw new RuntimeException(
203 "Shouldn't be happening - we're transient");
204 }
205 }
206 }