KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.store.raw.data.StreamFileContainerHandle
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.services.locks.Lockable;
25 import org.apache.derby.iapi.services.sanity.SanityManager;
26 import org.apache.derby.iapi.error.StandardException;
27 import org.apache.derby.iapi.store.raw.StreamContainerHandle;
28 import org.apache.derby.iapi.store.raw.ContainerKey;
29 import org.apache.derby.iapi.store.raw.xact.RawTransaction;
30
31 import org.apache.derby.iapi.types.DataValueDescriptor;
32
33 import org.apache.derby.impl.store.raw.data.DropOnCommit;
34
35 import org.apache.derby.catalog.UUID;
36
37 import java.util.Observable JavaDoc;
38 import java.util.Observer JavaDoc;
39 import java.util.Properties JavaDoc;
40
41 /**
42     A handle to an open stream container, implememts StreamContainerHandle.
43     <P>
44     This class is an Observer to observe RawTransactions
45
46     <BR> MT - Mutable - Immutable identity - Thread Aware
47 */

48
49 final class StreamFileContainerHandle
50     implements StreamContainerHandle, Observer JavaDoc
51 {
52
53     /*
54     ** Fields
55     */

56
57     /**
58         Raw Store identifier
59         <BR> MT - Immutable
60     */

61     private final UUID rawStoreId;
62
63     /**
64         Container identifier
65         <BR> MT - Immutable
66     */

67     protected final ContainerKey identity;
68
69
70     /**
71         Is this StreamContainerHandle active.
72
73         <BR> MT - Mutable : scoped
74     */

75     protected boolean active;
76
77     /**
78         The actual container we are accessing. Only valid when active is true.
79
80         <BR> MT - Mutable : scoped
81     */

82     protected StreamFileContainer container;
83
84     /**
85         our transaction. Only valid when active is true.
86
87         <BR> MT - Mutable : scoped
88     */

89     protected RawTransaction xact;
90
91     /**
92         Whether this container should be held open across commit.
93         Only valid when active is true.
94
95         <BR> MT - Mutable : scoped
96     */

97     private boolean hold;
98
99     /*
100     ** Constructor
101     */

102     public StreamFileContainerHandle(
103     UUID rawStoreId,
104     RawTransaction xact,
105     ContainerKey identity,
106     boolean hold)
107     {
108         this.identity = identity;
109         this.xact = xact;
110         this.rawStoreId = rawStoreId;
111         this.hold = hold;
112     }
113
114     public StreamFileContainerHandle(
115     UUID rawStoreId,
116     RawTransaction xact,
117     StreamFileContainer container,
118     boolean hold)
119     {
120
121         this.identity = container.getIdentity();
122         this.xact = xact;
123         this.rawStoreId = rawStoreId;
124         this.hold = hold;
125
126         this.container = container;
127
128         // we are inactive until useContainer is called.
129
}
130
131     /*
132     ** Methods from StreamContainerHandle
133     */

134
135     /**
136      * Request the system properties associated with a container.
137      * @see StreamContainerHandle#getContainerProperties
138      * @param prop Property list to fill in.
139      *
140      * @exception StandardException Standard exception policy.
141      **/

142     public void getContainerProperties(Properties JavaDoc prop)
143         throws StandardException {
144
145         container.getContainerProperties(prop);
146         return;
147     }
148
149     /**
150      * fetch a row from the container.
151     *
152      * @exception StandardException Standard exception policy.
153      **/

154     public boolean fetchNext(DataValueDescriptor[] row)
155         throws StandardException {
156
157         return container.fetchNext(row);
158     }
159
160     /**
161         @see StreamContainerHandle#close
162
163         @exception StandardException Standard exception policy.
164     */

165     public void close()
166     {
167
168         if (xact == null) {
169
170             // Probably be closed explicitly by a client, after closing
171
// automatically after an abort.
172
if (SanityManager.DEBUG)
173                 SanityManager.ASSERT(!active);
174
175             return;
176         }
177
178         active = false;
179
180         // let go of the container
181
container.close();
182         container = null;
183
184         // and remove ourseleves from this transaction
185
xact.deleteObserver(this);
186
187         xact = null;
188     }
189
190     /**
191         remove the stream container
192
193         @exception StandardException Standard Cloudscape error policy
194         @see StreamContainerHandle#removeContainer
195      */

196     public void removeContainer() throws StandardException {
197         container.removeContainer();
198     }
199
200     /**
201         get the container key for the stream container
202      */

203     public ContainerKey getId() {
204         return identity;
205     }
206
207     /*
208     ** Methods of Observer
209     */

210
211     /**
212         Called when the transaction is about to complete.
213
214         @see Observer#update
215     */

216     public void update(Observable JavaDoc obj, Object JavaDoc arg)
217     {
218         if (SanityManager.DEBUG) {
219             if (arg == null)
220                 SanityManager.THROWASSERT("still on observr list " + this);
221         }
222
223         // already been removed from the list
224
if (xact == null) {
225
226             return;
227         }
228
229         if (SanityManager.DEBUG) {
230             // just check reference equality
231

232             if (obj != xact)
233                 SanityManager.THROWASSERT("Observable passed to update is incorrect expected "
234                     + xact + " got " + obj);
235         }
236
237         // close on a commit, abort or drop of this container.
238
if (arg.equals(RawTransaction.COMMIT) ||
239             arg.equals(RawTransaction.ABORT) ||
240             arg.equals(identity))
241         {
242             // close the container
243
close();
244             return;
245
246         }
247         
248         if (arg.equals(RawTransaction.SAVEPOINT_ROLLBACK)) {
249
250             // remain open
251
return;
252         }
253     }
254
255     /*
256     ** Implementation specific methods, these are public so that they can be called
257     ** in other packages that are specific implementations of Data, ie.
258     ** a directory at the level
259     **
260     ** com.ibm.db2j.impl.Database.Storage.RawStore.Data.*
261     */

262
263     /**
264         Attach me to a container. If this method returns false then
265         I cannot be used anymore, and any reference to me must be discarded.
266
267         @exception StandardException Standard Cloudscape error policy
268     */

269     public boolean useContainer() throws StandardException {
270
271         if (SanityManager.DEBUG) {
272             SanityManager.ASSERT(!active);
273             SanityManager.ASSERT(container != null);
274         }
275
276         // always set forUpdate to false
277
if (!container.use(this)) {
278             container = null;
279             return false;
280         }
281
282         active = true;
283
284         // watch transaction and close ourseleves just before it completes.
285
if (!hold)
286         {
287             xact.addObserver(this);
288             xact.addObserver(new DropOnCommit(identity, true));
289         }
290
291         return true;
292     }
293
294     /**
295         Return the RawTransaction this object was opened in.
296     */

297     public final RawTransaction getTransaction() {
298
299         if (SanityManager.DEBUG) {
300             SanityManager.ASSERT(xact != null);
301         }
302
303         return xact;
304     }
305
306     /*
307     ** Implementation specific methods for myself and my sub-classes
308     */

309     public String JavaDoc toString() {
310         if (SanityManager.DEBUG) {
311             String JavaDoc str = new String JavaDoc();
312             str += "StreamContainerHandle:(" + identity.toString() + ")";
313             return(str);
314         } else {
315             return(super.toString());
316         }
317     }
318 }
319
320
Popular Tags