KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > store > raw > log > FlushedScanHandle


1 /*
2
3    Derby - Class org.apache.derby.impl.store.raw.log.FlushedScanHandle
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.store.raw.log;
23
24 import org.apache.derby.iapi.reference.SQLState;
25
26 import org.apache.derby.impl.store.raw.log.LogCounter;
27 import org.apache.derby.impl.store.raw.log.LogRecord;
28 import org.apache.derby.impl.store.raw.log.StreamLogScan;
29 import org.apache.derby.iapi.services.io.ArrayInputStream;
30 import org.apache.derby.iapi.services.sanity.SanityManager;
31 import org.apache.derby.iapi.error.StandardException;
32 import org.apache.derby.iapi.store.raw.Loggable;
33 import org.apache.derby.iapi.store.raw.ScanHandle;
34 import org.apache.derby.iapi.store.raw.ScannedTransactionHandle;
35 import org.apache.derby.iapi.store.raw.log.LogFactory;
36 import org.apache.derby.iapi.store.raw.log.LogInstant;
37 import org.apache.derby.iapi.store.raw.xact.TransactionId;
38 import org.apache.derby.iapi.store.access.DatabaseInstant;
39 import java.io.IOException JavaDoc;
40 import java.io.InputStream JavaDoc;
41 import java.util.Enumeration JavaDoc;
42
43 public class FlushedScanHandle implements ScanHandle
44 {
45     LogFactory lf;
46     StreamLogScan fs;
47     
48     LogRecord lr = null;
49     boolean readOptionalData = false;
50     int groupsIWant;
51     
52     ArrayInputStream rawInput = new ArrayInputStream(new byte[4096]);
53     
54     FlushedScanHandle(LogToFile lf, DatabaseInstant start, int groupsIWant)
55          throws StandardException
56     {
57         this.lf = lf;
58         fs = new FlushedScan(lf,((LogCounter)start).getValueAsLong());
59         this.groupsIWant = groupsIWant;
60     }
61     
62     public boolean next() throws StandardException
63     {
64         readOptionalData = false;
65         lr = null;
66
67         // filter the log stream so that only log records that belong to these
68
// interesting groups will be returned
69

70         try
71         {
72             lr = fs.getNextRecord(rawInput,null, groupsIWant);
73             if (lr==null) return false; //End of flushed log
74
if (SanityManager.DEBUG)
75             {
76                 if ((groupsIWant & lr.group()) == 0)
77                     SanityManager.THROWASSERT(groupsIWant + "/" + lr.group());
78             }
79
80             return true;
81         }
82         catch (IOException JavaDoc ioe)
83         {
84             ioe.printStackTrace();
85             fs.close();
86             fs = null;
87             throw lf.markCorrupt(
88                     StandardException.newException(SQLState.LOG_IO_ERROR, ioe));
89         }
90     }
91
92     /**
93       Get the group for the current log record.
94       @exception StandardException Oops
95       */

96     public int getGroup() throws StandardException
97     {
98         return lr.group();
99     }
100
101     /**
102       Get the Loggable associated with the currentLogRecord
103       @exception StandardException Oops
104       */

105     public Loggable getLoggable() throws StandardException
106     {
107         try {
108             return lr.getLoggable();
109         }
110
111         catch (IOException JavaDoc ioe)
112         {
113             ioe.printStackTrace();
114             fs.close();
115             fs = null;
116             throw lf.markCorrupt(
117                     StandardException.newException(SQLState.LOG_IO_ERROR, ioe));
118         }
119
120         catch (ClassNotFoundException JavaDoc cnfe)
121         {
122             fs.close();
123             fs = null;
124             throw lf.markCorrupt(
125                 StandardException.newException(SQLState.LOG_CORRUPTED, cnfe));
126         }
127     }
128
129     //This may be called only once per log record.
130
public InputStream getOptionalData()
131          throws StandardException
132     {
133         if (SanityManager.DEBUG) SanityManager.ASSERT(!readOptionalData);
134         if (lr == null) return null;
135         try
136         {
137             int dataLength = rawInput.readInt();
138             readOptionalData = true;
139             rawInput.setLimit(rawInput.getPosition(), dataLength);
140             return rawInput;
141         }
142
143         catch (IOException JavaDoc ioe)
144         {
145             fs.close();
146             fs = null;
147             throw lf.markCorrupt(
148                     StandardException.newException(SQLState.LOG_IO_ERROR, ioe));
149         }
150     }
151
152     public DatabaseInstant getInstant()
153          throws StandardException
154     {
155         return fs.getLogInstant();
156     }
157
158     public Object JavaDoc getTransactionId()
159          throws StandardException
160     {
161         try
162         {
163             return lr.getTransactionId();
164         }
165         catch (IOException JavaDoc ioe)
166         {
167             ioe.printStackTrace();
168             fs.close();
169             fs = null;
170             throw lf.markCorrupt(
171                     StandardException.newException(SQLState.LOG_IO_ERROR, ioe));
172         }
173         catch (ClassNotFoundException JavaDoc cnfe)
174         {
175             fs.close();
176             fs = null;
177             throw lf.markCorrupt(
178                 StandardException.newException(SQLState.LOG_CORRUPTED, cnfe));
179         }
180     }
181
182     public void close()
183     {
184         if (fs != null) fs.close();
185         fs = null;
186     }
187 }
188
Popular Tags