KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.store.raw.data.TempRAFContainer
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.impl.store.raw.data.BaseContainerHandle;
25 import org.apache.derby.impl.store.raw.data.BasePage;
26
27 import org.apache.derby.iapi.services.cache.Cacheable;
28 import org.apache.derby.iapi.services.sanity.SanityManager;
29
30 import org.apache.derby.iapi.error.StandardException;
31 import org.apache.derby.iapi.store.raw.xact.RawTransaction;
32 import org.apache.derby.iapi.store.raw.ContainerHandle;
33 import org.apache.derby.iapi.store.raw.ContainerKey;
34 import org.apache.derby.iapi.store.raw.Page;
35 import org.apache.derby.iapi.store.raw.log.LogInstant;
36 import org.apache.derby.iapi.store.raw.data.RawContainerHandle;
37
38 import org.apache.derby.io.StorageFactory;
39 import org.apache.derby.io.StorageFile;
40
41 import java.io.IOException JavaDoc;
42
43 /**
44     needsSync is never true - DONE
45     An exception never marks the store as corrupt
46     clean() does not stubbify
47     preAllocate() does nothing - DONE
48     getFileName() returns a file in the tmp directory - DONE
49     flushAll does nothing - DONE
50     file descriptor is never synced
51 */

52 class TempRAFContainer extends RAFContainer {
53
54     protected int inUseCount;
55
56     TempRAFContainer(BaseDataFileFactory factory) {
57         super(factory);
58     }
59
60     /**
61         @exception StandardException Standard Cloudscape error policy
62     */

63     public Cacheable setIdentity(Object JavaDoc key) throws StandardException {
64
65         ContainerKey newIdentity = (ContainerKey) key;
66         if (newIdentity.getSegmentId() != ContainerHandle.TEMPORARY_SEGMENT) {
67
68             RAFContainer realContainer = new RAFContainer(dataFactory);
69             return realContainer.setIdent(newIdentity);
70         }
71
72         return super.setIdentity(newIdentity);
73
74     }
75
76     /**
77         @exception StandardException Standard Cloudscape error policy
78     */

79     public Cacheable createIdentity(Object JavaDoc key, Object JavaDoc createParameter) throws StandardException {
80
81         ContainerKey newIdentity = (ContainerKey) key;
82
83         if (newIdentity.getSegmentId() != ContainerHandle.TEMPORARY_SEGMENT) {
84             RAFContainer realContainer = new RAFContainer(dataFactory);
85             return realContainer.createIdentity(newIdentity, createParameter);
86         }
87
88         return createIdent(newIdentity, createParameter);
89     }
90
91     /**
92         @exception StandardException Standard Cloudscape error policy
93     */

94     public void removeContainer(LogInstant instant, boolean leaveStub) throws StandardException
95     {
96         // discard all of my pages in the cache
97
pageCache.discard(identity);
98         
99         synchronized(this) {
100             // prevent anybody from looking at this container again
101
setDroppedState(true);
102             setCommittedDropState(true);
103             setDirty(false);
104             needsSync = false;
105
106         }
107
108         removeFile(getFileName(identity, false, false, false));
109     }
110
111     /**
112         Preallocate page. Since we don't sync when we write page anyway, no
113         need to preallocate page.
114     */

115     protected int preAllocate(long lastPreallocPagenum, int preAllocSize)
116     {
117         return 0;
118     }
119
120
121     /**
122         Write the page, if it's within range of the current page range of the container.
123         If we do write it then don't request that it be synced.
124
125         @exception StandardException Standard Cloudscape error policy
126     */

127     protected void writePage(long pageNumber, byte[] pageData, boolean syncPage) throws IOException JavaDoc, StandardException {
128         if (!this.getDroppedState()) {
129             super.writePage(pageNumber, pageData, false);
130         }
131         needsSync = false;
132     }
133
134     StorageFile getFileName(ContainerKey identity, boolean stub,
135         boolean errorOK, boolean tryAlternatePath)
136     {
137         return privGetFileName( identity, stub, errorOK, tryAlternatePath);
138     }
139
140     protected StorageFile privGetFileName(ContainerKey identity, boolean stub,
141         boolean errorOK, boolean tryAlternatePath)
142     {
143         return dataFactory.storageFactory.newStorageFile( dataFactory.storageFactory.getTempDir(),
144                                                     "T" + identity.getContainerId() + ".tmp");
145     }
146
147     /**
148         Add a page without locking the container, only one user will be accessing this
149         table at a time.
150
151         @exception StandardException Standard Cloudscape error policy
152     */

153     public Page addPage(BaseContainerHandle handle, boolean isOverflow) throws StandardException {
154
155         BasePage newPage = newPage(handle, (RawTransaction) null, handle, isOverflow);
156
157         if (SanityManager.DEBUG) {
158             SanityManager.ASSERT(newPage.isLatched());
159         }
160
161         return newPage;
162     }
163
164     /**
165         @exception StandardException Standard Cloudscape error policy
166     */

167     public void truncate(BaseContainerHandle handle) throws StandardException {
168
169         // stop anyone from writing any of my pages out
170
synchronized(this)
171         {
172             setDroppedState(true);
173             setCommittedDropState(true);
174             setDirty(false);
175             needsSync = false;
176         }
177
178         // discard all of my pages in the cache
179
while (pageCache.discard(identity) != true)
180             ;
181
182         removeFile(getFileName(identity, false, true, false));
183
184         createIdent(identity, this);
185
186         addPage(handle, false).unlatch();
187     }
188     /**
189         Lock the container and mark the container as in-use by this container handle.
190
191         @param droppedOK if true, use this container even if it is dropped.,
192         @return true if the container can be used, false if it has been dropped
193         since the lock was requested and droppedOK is not true.
194
195         @exception StandardException I cannot be opened for update.
196     */

197     protected boolean use(BaseContainerHandle handle, boolean forUpdate,
198                           boolean droppedOK)
199         throws StandardException {
200
201         if (super.use(handle, forUpdate, droppedOK)) {
202             inUseCount++;
203             return true;
204         }
205
206         return false;
207     }
208
209     /**
210         Discontinue use of this container. Note that the unlockContainer
211         call made from this method may not release any locks. The container
212         lock may be held until the end of the transaction.
213
214     */

215     protected void letGo(BaseContainerHandle handle) {
216
217         inUseCount--;
218         super.letGo(handle);
219     }
220
221
222     /**
223         Returns true if only a single handle is connected to this container.
224     */

225     public boolean isSingleUser() {
226         return inUseCount == 1;
227     }
228 }
229
Popular Tags