KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > store > raw > data > OverflowInputStream


1 /*
2
3    Derby - Class org.apache.derby.impl.store.raw.data.OverflowInputStream
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.data;
23
24 import org.apache.derby.iapi.error.StandardException;
25 import org.apache.derby.iapi.reference.SQLState;
26
27 import org.apache.derby.iapi.store.raw.RecordHandle;
28
29 import org.apache.derby.iapi.types.Resetable;
30 import org.apache.derby.iapi.store.raw.LockingPolicy;
31 import org.apache.derby.iapi.store.access.TransactionController;
32
33 import java.io.InputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35
36 /**
37     A OverflowInputStream is used by store to turn a long column
38     into an InputStream.
39 */

40 public class OverflowInputStream
41 extends BufferedByteHolderInputStream
42 implements Resetable
43 {
44     protected BaseContainerHandle owner;
45     protected long overflowPage;
46     protected int overflowId;
47     // remember first page and id for reset
48
protected long firstOverflowPage;
49     protected int firstOverflowId;
50     // the row to lock for Blobs/Clobs
51
protected RecordHandle recordToLock;
52
53     public OverflowInputStream(ByteHolder bh, BaseContainerHandle owner,
54             long overflowPage, int overflowId, RecordHandle recordToLock)
55         throws IOException JavaDoc, StandardException
56     {
57         super(bh);
58         this.owner = owner;
59         this.overflowPage = overflowPage;
60         this.overflowId = overflowId;
61         this.firstOverflowPage = overflowPage;
62         this.firstOverflowId = overflowId;
63         this.recordToLock = recordToLock;
64         fillByteHolder();
65     }
66
67
68     public void fillByteHolder() throws IOException JavaDoc
69     {
70         if ((this.bh.available() == 0) && (this.overflowPage != -1))
71         {
72             this.bh.clear();
73
74             try
75             {
76                 // fill the byte holder with data from the page.
77
BasePage columnOverflowPage =
78                     ((BasePage) this.owner.getPage(overflowPage));
79
80                 if (columnOverflowPage != null)
81                 {
82                     columnOverflowPage.restorePortionLongColumn(this);
83                     columnOverflowPage.unlatch();
84                     columnOverflowPage = null;
85                 }
86             }
87             catch (StandardException se)
88             {
89                 throw new IOException JavaDoc( se.toString() );
90             }
91             this.bh.startReading();
92         }
93     }
94
95
96     public long getOverflowPage() {
97         return this.overflowPage;
98     }
99
100     public int getOverflowId() {
101         return this.overflowId;
102     }
103
104     public void setOverflowPage(long overflowPage) {
105         this.overflowPage = overflowPage;
106     }
107
108     public void setOverflowId(int overflowId) {
109         this.overflowId = overflowId;
110     }
111
112
113     /*
114      Methods of Resetable interface.
115     */

116
117     /*
118      Resets the stream to the beginning.
119      */

120     public void resetStream() throws IOException JavaDoc, StandardException
121     {
122         // check the container is open, this is needed to make sure the
123
// container closed exception is thrown as a StandardException and not
124
// as an IOException
125
owner.checkOpen();
126         // return to the original overflow page and id
127
this.overflowPage = firstOverflowPage;
128         this.overflowId = firstOverflowId;
129         // completely clear the byte holder
130
this.bh.clear();
131         this.bh.startReading();
132         // fill the byte holder
133
fillByteHolder();
134     }
135
136     /*
137       Initialize. Reopen the container. This will have the effect of
138       getting an intent shared lock on the table, which will stay around until
139       the end of the transaction (or until the enclosing blob/clob object is
140       closed). Also get a read lock on the appropriate row.
141     */

142     public void initStream() throws StandardException
143     {
144         // it is possible that the transaction in which the stream was
145
// created is committed and no longer valid
146
// dont want to get NPE but instead throw error that
147
// container was not opened
148
if (owner.getTransaction() == null)
149             throw StandardException.newException(SQLState.DATA_CONTAINER_CLOSED);
150         /*
151         We might want to use the mode and isolation level of the container.
152         This would have the advantage that, if the isolation level
153         is READ_COMMITTED, resources would be freed if blobs/clob finalizers are
154         called (e.g. they are garbage collected) before the end of transaction.
155         If the mode was MODE_CONTAINER, openContainer would get an S lock on the
156         table instead of an IS lock, and lockRecordForRead would have no effect.
157
158         To do this, need to consider:
159         Sometimes the container's locking policy may NOT reflect the correct
160         locking policy. For example, if the container is a table (not an index)
161         and Access handles the locking of the table via an index, the container's
162         locking policy would be set to do no locking.
163         Moreover, if the container is an index, the locking policy would
164         always be set to do no locking.
165         */

166
167         LockingPolicy lp =
168             owner.getTransaction().newLockingPolicy(
169                 LockingPolicy.MODE_RECORD,
170                 TransactionController.ISOLATION_REPEATABLE_READ, true);
171
172         // reopen the container
173
owner = (BaseContainerHandle) owner.getTransaction().openContainer(
174             owner.getId(), lp, owner.getMode());
175
176         // get a read lock on the appropriate row
177
// this will wait until either the lock is granted or an exception is
178
// thrown
179
owner.getLockingPolicy().lockRecordForRead(
180             owner.getTransaction(), owner, recordToLock, true, false);
181     }
182
183
184     /*
185       Close the container associated with this stream. (In the future if we use
186       a read committed isolation mode, this will also free the associated IS
187       table lock and the associated S row lock.)
188     */

189     public void closeStream()
190     {
191         owner.close();
192     }
193
194 }
195
Popular Tags