comparison src/org/dancres/blitz/entry/IndexLocatorImpl.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 secondary indexes which consist
18 of hashcode followed by oid
19 */
20 class IndexLocatorImpl 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 theHashCode;
29
30 /**
31 @param aCursor the cursor to use for iteration
32 */
33 IndexLocatorImpl(Cursor aCursor, DatabaseEntry aHashCode,
34 MangledEntry aTemplate, int aReadahead) {
35 theCursor = aCursor;
36 theHashCode = aHashCode;
37 theTemplate = aTemplate;
38 theReadahead = aReadahead;
39 }
40
41 /**
42 Invoke this to load the next matching Tuple.
43
44 @return <code>true</code> if there was a tuple, <code>false</code>
45 otherwise
46 */
47 public boolean fetchNext() throws IOException {
48 /*
49 The cursor will have been initialised whilst we decided which
50 field was best to search on which means it will already be pointed
51 at the first record so we must handle this case specially. Note
52 that we only get created if the cursor actually contains records so
53 we don't need to do any testing here.
54 */
55 if (isFirst) {
56 loadNext();
57
58 isFirst = false;
59
60 return true;
61 } else {
62 return loadNextMove();
63 }
64 }
65
66 private boolean loadNextMove() throws IOException {
67 return loadNext(true);
68 }
69
70 private boolean loadNext() throws IOException {
71 return loadNext(false);
72 }
73
74 private boolean loadNext(boolean advance) throws IOException {
75 OperationStatus myResult;
76
77 try {
78 theKey = new DatabaseEntry();
79
80 if (advance)
81 myResult = theCursor.getNextDup(theHashCode, theKey, null);
82 else
83 myResult = theCursor.getCurrent(theHashCode, theKey, null);
84 } catch (DatabaseException aDbe) {
85 EntryStorage.theLogger.log(Level.SEVERE, "Got Dbe", aDbe);
86 throw new IOException("Dbe");
87 }
88
89 // BytePacker myPacker = BytePacker.getMSBPacker(theHashCode.getData());
90
91 // System.out.println("Loop: " + this + ", " + myPacker.getInt(0));
92
93
94 return (! myResult.equals(OperationStatus.NOTFOUND));
95 }
96
97 /**
98 @return the OID of the tuple just fetched with
99 <code>fetchNext</code>
100 */
101 public OID getOID() {
102 OID myResult = OIDFactory.newOID(theKey.getData());
103
104 return myResult;
105 }
106
107 /**
108 When you've finished with the TupleLocator instance, call release.
109 */
110
111 public void release() throws IOException {
112 releaseImpl();
113
114 Readahead.get().add(theTemplate, theReadahead);
115 }
116
117 public void releaseImpl() throws IOException {
118 try {
119 theCursor.close();
120 } catch (DatabaseException aDbe) {
121 EntryStorage.theLogger.log(Level.SEVERE, "Got Dbe", aDbe);
122 throw new IOException("Dbe");
123 }
124 }
125 }