comparison src/org/dancres/blitz/remote/MatchSetImpl.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 d3ec5ebc3dba
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
1 package org.dancres.blitz.remote;
2
3 import java.rmi.RemoteException;
4
5 import net.jini.core.entry.Entry;
6 import net.jini.core.entry.UnusableEntryException;
7
8 import net.jini.core.lease.Lease;
9
10 import net.jini.space.MatchSet;
11
12 import org.dancres.blitz.EntryChit;
13
14 import org.dancres.blitz.mangler.EntryMangler;
15
16 import org.dancres.blitz.remote.view.EntryViewUID;
17
18 public class MatchSetImpl implements MatchSet {
19 static final int CHUNK_SIZE = 50;
20
21 private Lease theLease;
22 private EntryViewUID theViewId;
23 private BlitzServer theStub;
24
25 private boolean isDone;
26
27 private EntryChit[] theCurrentBatch;
28 private int theOffset = -1;
29
30 private int theChunkSize;
31
32 private long theLimit;
33
34 MatchSetImpl(BlitzServer aStub, LeaseImpl aLease, long aLimit) {
35 this(aStub, aLease, CHUNK_SIZE, aLimit);
36 }
37
38 MatchSetImpl(BlitzServer aStub, LeaseImpl aLease, long aLimit,
39 EntryChit[] anInitialBatch) {
40 this(aStub, aLease, CHUNK_SIZE, aLimit, anInitialBatch);
41 }
42
43 MatchSetImpl(BlitzServer aStub, LeaseImpl aLease,
44 int aChunkSize, long aLimit) {
45 theStub = aStub;
46 theChunkSize = aChunkSize;
47 theLimit = aLimit;
48 theViewId = (EntryViewUID) aLease.getUID();
49 theLease = aLease;
50 }
51
52 MatchSetImpl(BlitzServer aStub, LeaseImpl aLease,
53 int aChunkSize, long aLimit, EntryChit[] aFirstBatch) {
54 theStub = aStub;
55 theChunkSize = aChunkSize;
56 theLimit = aLimit;
57 theViewId = (EntryViewUID) aLease.getUID();
58 theLease = aLease;
59
60 // Setup index offset if we got a batch
61 if (aFirstBatch != null) {
62 theOffset = 0;
63 theCurrentBatch = aFirstBatch;
64 }
65 }
66
67 public Lease getLease() {
68 return theLease;
69 }
70
71 public Entry next() throws UnusableEntryException, RemoteException {
72 if (amDone())
73 return null;
74
75 if ((theOffset == -1) || (theOffset >= theCurrentBatch.length)) {
76 // Haven't got a batch yet
77 theCurrentBatch = theStub.getNext(theViewId, theChunkSize);
78 theOffset = 0;
79 }
80
81 if (theCurrentBatch == null) {
82 isDone = true;
83 return null;
84 }
85
86 --theLimit;
87
88 return EntryMangler.getMangler().unMangle(theCurrentBatch[theOffset++].getEntry());
89 }
90
91 public Entry getSnapshot() {
92 if (amDone())
93 throw new RuntimeException("No entry to snapshot - iteration is over");
94 if (theCurrentBatch == null)
95 throw new RuntimeException("Call next first!");
96
97 return theCurrentBatch[theOffset - 1].getEntry();
98 }
99
100 private boolean amDone() {
101 return ((isDone) || (theLimit == 0));
102 }
103 }