comparison src/org/dancres/blitz/entry/SortingLocator.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 org.dancres.blitz.oid.OID;
6
7 /**
8 This locator will provide a single FIFO list of tuples created from
9 merging the contents of all the passed locators. It assumes that each
10 individual locator is FIFO sorted.
11 */
12 class SortingLocator implements TupleLocator {
13 private TupleLocator[] theLocators;
14
15 private OID[] theSortBuffer;
16
17 private OID theNext;
18
19 SortingLocator(TupleLocator[] aSetOfLocators) {
20 theLocators = aSetOfLocators;
21 }
22
23 /**
24 Invoke this to load the next matching Tuple.
25
26 @return <code>true</code> if there was a tuple, <code>false</code>
27 otherwise
28 */
29 public boolean fetchNext() throws IOException {
30 if (theSortBuffer == null) {
31 // First-time init
32 theSortBuffer = new OID[theLocators.length];
33
34 for (int i = 0; i < theLocators.length; i++) {
35 boolean hasUID = theLocators[i].fetchNext();
36
37 if (hasUID)
38 theSortBuffer[i] = theLocators[i].getOID();
39 }
40 } else {
41 // This cannot be the first time so if theNext is null, we've
42 // run out of matches
43 if (theNext == null)
44 return false;
45
46 // Refresh slots in the slot buffer which contain the last OID
47 for (int i = 0; i < theSortBuffer.length; i++) {
48 if (theSortBuffer[i] != null) {
49 if (theSortBuffer[i].equals(theNext)) {
50 if (theLocators[i].fetchNext())
51 theSortBuffer[i] = theLocators[i].getOID();
52 else
53 theSortBuffer[i] = null;
54 }
55 }
56 }
57 }
58
59 OID myOldest = null;
60
61 for (int i = 0; i < theSortBuffer.length; i++) {
62 if (theSortBuffer[i] != null) {
63 if (myOldest == null) {
64 myOldest = theSortBuffer[i];
65 } else {
66 if (theSortBuffer[i].compareTo(myOldest) < 0) {
67 myOldest = theSortBuffer[i];
68 }
69 }
70 }
71 }
72
73 theNext = myOldest;
74
75 return (theNext != null);
76 }
77
78 /**
79 @return the OID of the tuple just fetched with
80 <code>fetchNext</code>
81 */
82 public OID getOID() {
83 return theNext;
84 }
85
86 /**
87 When you've finished with the TupleLocator instance, call release.
88 */
89 public void release() throws IOException {
90 for (int i = 0; i < theLocators.length; i++) {
91 theLocators[i].release();
92 }
93 }
94 }