comparison src/org/dancres/blitz/meta/MetaIteratorImpl.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 e92d83b192bb
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
1 package org.dancres.blitz.meta;
2
3 import java.io.IOException;
4 import java.io.Serializable;
5
6 import java.util.logging.*;
7
8 import com.sleepycat.je.Database;
9 import com.sleepycat.je.DatabaseEntry;
10 import com.sleepycat.je.Cursor;
11 import com.sleepycat.je.Transaction;
12 import com.sleepycat.je.OperationStatus;
13
14 import org.dancres.blitz.disk.DiskTxn;
15
16 import org.dancres.util.ObjectTransformer;
17
18 class MetaIteratorImpl implements MetaIterator {
19 private Database theDb;
20 private Cursor theCursor;
21 private Transaction theTxn;
22
23 MetaIteratorImpl(Database aDb, DiskTxn aTxn) {
24 theDb = aDb;
25 theTxn = aTxn.getDbTxn();
26 }
27
28 public MetaEntry fetch() throws IOException {
29 OperationStatus myResult = OperationStatus.NOTFOUND;
30
31 DatabaseEntry myKey = new DatabaseEntry();
32 DatabaseEntry myData = new DatabaseEntry();
33
34 try {
35 myResult = getCursor().getNext(myKey, myData, null);
36 } catch (Exception anE) {
37 RegistryImpl.theLogger.log(Level.SEVERE, "Failed to fetch", anE);
38 throw new IOException();
39 }
40
41 if (myResult.equals(OperationStatus.NOTFOUND)) {
42 return null;
43 }
44
45 try {
46 Serializable myObject = (Serializable)
47 ObjectTransformer.toObject(myData.getData());
48
49 return new MetaEntryImpl(myKey.getData(), myObject);
50
51 } catch (Exception anException) {
52 RegistryImpl.theLogger.log(Level.SEVERE,
53 "Failed to read meta data",
54 anException);
55
56 throw new IOException("Couldn't read meta data");
57 }
58 }
59
60 public void release() throws IOException {
61 try {
62 getCursor().close();
63 } catch (Exception anE) {
64 RegistryImpl.theLogger.log(Level.SEVERE, "Failed to release", anE);
65 throw new IOException();
66 }
67 }
68
69 private Cursor getCursor() throws IOException {
70 try {
71 if (theCursor == null) {
72 theCursor = theDb.openCursor(theTxn, null);
73 }
74
75 return theCursor;
76 } catch (Exception anE) {
77 RegistryImpl.theLogger.log(Level.SEVERE,
78 "Failed to getCursor", anE);
79 throw new IOException();
80 }
81 }
82 }