Mercurial > hg > blitz_condensed
comparison src/org/dancres/blitz/entry/ExpiredLocatorImpl.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.entry; | |
2 | |
3 import java.io.IOException; | |
4 | |
5 import java.util.logging.Level; | |
6 | |
7 import com.sleepycat.je.Cursor; | |
8 import com.sleepycat.je.DatabaseEntry; | |
9 import com.sleepycat.je.DatabaseException; | |
10 import com.sleepycat.je.OperationStatus; | |
11 | |
12 import org.dancres.blitz.oid.OID; | |
13 import org.dancres.blitz.oid.OIDFactory; | |
14 | |
15 /** | |
16 TupleLocator implementation to be used on Lease tables. See LeaseTrackerImpl | |
17 for information on the database format. | |
18 */ | |
19 class ExpiredLocatorImpl implements TupleLocator { | |
20 private Cursor theCursor; | |
21 private boolean isFirst = true; | |
22 | |
23 private DatabaseEntry theLeaseRecord; | |
24 private DatabaseEntry theLeaseBucket; | |
25 | |
26 private long theScanTime; | |
27 | |
28 /** | |
29 @param aCursor the cursor to use for iteration | |
30 */ | |
31 ExpiredLocatorImpl(Cursor aCursor) { | |
32 theCursor = aCursor; | |
33 theScanTime = System.currentTimeMillis(); | |
34 theLeaseBucket = new DatabaseEntry(); | |
35 } | |
36 | |
37 /** | |
38 Invoke this to load the next matching Tuple. | |
39 | |
40 @return <code>true</code> if there was a tuple, <code>false</code> | |
41 otherwise | |
42 */ | |
43 public boolean fetchNext() throws IOException { | |
44 /* | |
45 The cursor will have been initialised whilst we decided which | |
46 field was best to search on which means it will already be pointed | |
47 at the first record so we must handle this case specially. | |
48 */ | |
49 if (isFirst) { | |
50 isFirst = false; | |
51 | |
52 return loadNext(); | |
53 } else { | |
54 return loadNextMove(); | |
55 } | |
56 } | |
57 | |
58 private boolean loadNextMove() throws IOException { | |
59 return loadNext(true); | |
60 } | |
61 | |
62 private boolean loadNext() throws IOException { | |
63 return loadNext(false); | |
64 } | |
65 | |
66 private boolean loadNext(boolean advance) throws IOException { | |
67 OperationStatus myResult; | |
68 | |
69 try { | |
70 theLeaseRecord = new DatabaseEntry(); | |
71 | |
72 if (advance) | |
73 myResult = theCursor.getNextDup(theLeaseBucket, theLeaseRecord, | |
74 null); | |
75 else | |
76 myResult = theCursor.getFirst(theLeaseBucket, theLeaseRecord, | |
77 null); | |
78 } catch (DatabaseException aDbe) { | |
79 EntryStorage.theLogger.log(Level.SEVERE, "Got Dbe", aDbe); | |
80 throw new IOException("Dbe"); | |
81 } | |
82 | |
83 if ((myResult.equals(OperationStatus.NOTFOUND)) || | |
84 (theScanTime < | |
85 LeaseRecordUtils.unpackExpiry(theLeaseRecord.getData()))) { | |
86 | |
87 if (myResult != OperationStatus.NOTFOUND) | |
88 // System.err.println("Exceeding Expiry: " + | |
89 // LeaseRecordUtils.unpackExpiry(theLeaseRecord.getData())); | |
90 | |
91 // Try for the next non-dupe | |
92 theLeaseRecord = new DatabaseEntry(); | |
93 | |
94 try { | |
95 // System.err.println("Skipping to next bucket"); | |
96 | |
97 myResult = | |
98 theCursor.getNextNoDup(theLeaseBucket, theLeaseRecord, null); | |
99 | |
100 if (myResult.equals(OperationStatus.SUCCESS)) | |
101 return true; | |
102 } catch (DatabaseException aDbe) { | |
103 EntryStorage.theLogger.log(Level.SEVERE, "Got Dbe", aDbe); | |
104 throw new IOException("Dbe"); | |
105 } | |
106 | |
107 return false; | |
108 } else { | |
109 // System.err.println("Maybe expiring one: " + | |
110 // LeaseRecordUtils.unpackExpiry(theLeaseRecord.getData())); | |
111 return true; | |
112 } | |
113 } | |
114 | |
115 /** | |
116 @return the OID of the tuple just fetched with | |
117 <code>fetchNext</code> | |
118 */ | |
119 public OID getOID() { | |
120 OID myResult = | |
121 OIDFactory.newOID( | |
122 LeaseRecordUtils.unpackId(theLeaseRecord.getData())); | |
123 | |
124 // System.err.println("Time is up for: " + myResult); | |
125 | |
126 return myResult; | |
127 } | |
128 | |
129 /** | |
130 When you've finished with the TupleLocator instance, call release. | |
131 */ | |
132 public void release() throws IOException { | |
133 try { | |
134 theCursor.close(); | |
135 } catch (DatabaseException aDbe) { | |
136 EntryStorage.theLogger.log(Level.SEVERE, "Got Dbe", aDbe); | |
137 throw new IOException("Dbe"); | |
138 } | |
139 } | |
140 } |