comparison src/org/dancres/blitz/entry/PrimaryLocatorImpl.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 import org.dancres.blitz.mangler.MangledEntry;
15
16 /**
17 TupleLocator implementation to be used on main index file which contains
18 oid and entry.
19 */
20 class PrimaryLocatorImpl implements TupleLocator {
21 private MangledEntry theTemplate;
22 private int theReadahead;
23
24 private Cursor theCursor;
25 private boolean isFirst = true;
26
27 private DatabaseEntry theKey;
28 private DatabaseEntry theData;
29
30 /**
31 @param aCursor the cursor to use for iteration
32 */
33 PrimaryLocatorImpl(Cursor aCursor, MangledEntry aTemplate, int aReadahead) {
34 theCursor = aCursor;
35 theTemplate = aTemplate;
36 theReadahead = aReadahead;
37 }
38
39 /**
40 Invoke this to load the next matching Tuple.
41
42 @return <code>true</code> if there was a tuple, <code>false</code>
43 otherwise
44 */
45 public boolean fetchNext() throws IOException {
46 /*
47 The cursor will have been initialised whilst we decided which
48 field was best to search on which means it will already be pointed
49 at the first record so we must handle this case specially. Note
50 that we only get created if the cursor actually contains records so
51 we don't need to do any testing here.
52 */
53 if (isFirst) {
54 loadNext(false);
55
56 isFirst = false;
57
58 return true;
59 } else {
60 return loadNext(true);
61 }
62 }
63
64 private boolean loadNext(boolean moveForward) throws IOException {
65 OperationStatus myResult;
66
67 try {
68 theKey = new DatabaseEntry();
69 theData = new DatabaseEntry();
70
71 if (moveForward)
72 myResult = theCursor.getNext(theKey, theData, null);
73 else
74 myResult = theCursor.getCurrent(theKey, theData, null);
75
76 } catch (DatabaseException aDbe) {
77 EntryStorage.theLogger.log(Level.SEVERE, "Got Dbe", aDbe);
78 throw new IOException("Dbe");
79 }
80
81 return (! myResult.equals(OperationStatus.NOTFOUND));
82 }
83
84 /**
85 @return the OID of the tuple just fetched with
86 <code>fetchNext</code>
87 */
88 public OID getOID() {
89 OID myResult = OIDFactory.newOID(theKey.getData());
90
91 return myResult;
92 }
93
94 /**
95 When you've finished with the TupleLocator instance, call release.
96 */
97 public void release() throws IOException {
98 releaseImpl();
99
100 Readahead.get().add(theTemplate, theReadahead);
101 }
102
103 private void releaseImpl() throws IOException {
104 try {
105 theCursor.close();
106 } catch (DatabaseException aDbe) {
107 EntryStorage.theLogger.log(Level.SEVERE, "Got Dbe", aDbe);
108 throw new IOException("Dbe");
109 }
110 }
111 }