001: /*
002:
003: Derby - Class org.apache.derby.impl.store.raw.data.TempRAFContainer
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.store.raw.data;
023:
024: import org.apache.derby.impl.store.raw.data.BaseContainerHandle;
025: import org.apache.derby.impl.store.raw.data.BasePage;
026:
027: import org.apache.derby.iapi.services.cache.Cacheable;
028: import org.apache.derby.iapi.services.sanity.SanityManager;
029:
030: import org.apache.derby.iapi.error.StandardException;
031: import org.apache.derby.iapi.store.raw.xact.RawTransaction;
032: import org.apache.derby.iapi.store.raw.ContainerHandle;
033: import org.apache.derby.iapi.store.raw.ContainerKey;
034: import org.apache.derby.iapi.store.raw.Page;
035: import org.apache.derby.iapi.store.raw.log.LogInstant;
036: import org.apache.derby.iapi.store.raw.data.RawContainerHandle;
037:
038: import org.apache.derby.io.StorageFactory;
039: import org.apache.derby.io.StorageFile;
040:
041: import java.io.IOException;
042:
043: /**
044: needsSync is never true - DONE
045: An exception never marks the store as corrupt
046: clean() does not stubbify
047: preAllocate() does nothing - DONE
048: getFileName() returns a file in the tmp directory - DONE
049: flushAll does nothing - DONE
050: file descriptor is never synced
051: */
052: class TempRAFContainer extends RAFContainer {
053:
054: protected int inUseCount;
055:
056: TempRAFContainer(BaseDataFileFactory factory) {
057: super (factory);
058: }
059:
060: /**
061: @exception StandardException Standard Cloudscape error policy
062: */
063: public Cacheable setIdentity(Object key) throws StandardException {
064:
065: ContainerKey newIdentity = (ContainerKey) key;
066: if (newIdentity.getSegmentId() != ContainerHandle.TEMPORARY_SEGMENT) {
067:
068: RAFContainer realContainer = new RAFContainer(dataFactory);
069: return realContainer.setIdent(newIdentity);
070: }
071:
072: return super .setIdentity(newIdentity);
073:
074: }
075:
076: /**
077: @exception StandardException Standard Cloudscape error policy
078: */
079: public Cacheable createIdentity(Object key, Object createParameter)
080: throws StandardException {
081:
082: ContainerKey newIdentity = (ContainerKey) key;
083:
084: if (newIdentity.getSegmentId() != ContainerHandle.TEMPORARY_SEGMENT) {
085: RAFContainer realContainer = new RAFContainer(dataFactory);
086: return realContainer.createIdentity(newIdentity,
087: createParameter);
088: }
089:
090: return createIdent(newIdentity, createParameter);
091: }
092:
093: /**
094: @exception StandardException Standard Cloudscape error policy
095: */
096: public void removeContainer(LogInstant instant, boolean leaveStub)
097: throws StandardException {
098: // discard all of my pages in the cache
099: pageCache.discard(identity);
100:
101: synchronized (this ) {
102: // prevent anybody from looking at this container again
103: setDroppedState(true);
104: setCommittedDropState(true);
105: setDirty(false);
106: needsSync = false;
107:
108: }
109:
110: removeFile(getFileName(identity, false, false, false));
111: }
112:
113: /**
114: Preallocate page. Since we don't sync when we write page anyway, no
115: need to preallocate page.
116: */
117: protected int preAllocate(long lastPreallocPagenum, int preAllocSize) {
118: return 0;
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,
128: boolean syncPage) throws IOException, StandardException {
129: if (!this .getDroppedState()) {
130: super .writePage(pageNumber, pageData, false);
131: }
132: needsSync = false;
133: }
134:
135: StorageFile getFileName(ContainerKey identity, boolean stub,
136: boolean errorOK, boolean tryAlternatePath) {
137: return privGetFileName(identity, stub, errorOK,
138: tryAlternatePath);
139: }
140:
141: protected StorageFile privGetFileName(ContainerKey identity,
142: boolean stub, boolean errorOK, boolean tryAlternatePath) {
143: return dataFactory.storageFactory.newStorageFile(
144: dataFactory.storageFactory.getTempDir(), "T"
145: + identity.getContainerId() + ".tmp");
146: }
147:
148: /**
149: Add a page without locking the container, only one user will be accessing this
150: table at a time.
151:
152: @exception StandardException Standard Cloudscape error policy
153: */
154: public Page addPage(BaseContainerHandle handle, boolean isOverflow)
155: throws StandardException {
156:
157: BasePage newPage = newPage(handle, (RawTransaction) null,
158: handle, isOverflow);
159:
160: if (SanityManager.DEBUG) {
161: SanityManager.ASSERT(newPage.isLatched());
162: }
163:
164: return newPage;
165: }
166:
167: /**
168: @exception StandardException Standard Cloudscape error policy
169: */
170: public void truncate(BaseContainerHandle handle)
171: throws StandardException {
172:
173: // stop anyone from writing any of my pages out
174: synchronized (this ) {
175: setDroppedState(true);
176: setCommittedDropState(true);
177: setDirty(false);
178: needsSync = false;
179: }
180:
181: // discard all of my pages in the cache
182: while (pageCache.discard(identity) != true)
183: ;
184:
185: removeFile(getFileName(identity, false, true, false));
186:
187: createIdent(identity, this );
188:
189: addPage(handle, false).unlatch();
190: }
191:
192: /**
193: Lock the container and mark the container as in-use by this container handle.
194:
195: @param droppedOK if true, use this container even if it is dropped.,
196: @return true if the container can be used, false if it has been dropped
197: since the lock was requested and droppedOK is not true.
198:
199: @exception StandardException I cannot be opened for update.
200: */
201: protected boolean use(BaseContainerHandle handle,
202: boolean forUpdate, boolean droppedOK)
203: throws StandardException {
204:
205: if (super .use(handle, forUpdate, droppedOK)) {
206: inUseCount++;
207: return true;
208: }
209:
210: return false;
211: }
212:
213: /**
214: Discontinue use of this container. Note that the unlockContainer
215: call made from this method may not release any locks. The container
216: lock may be held until the end of the transaction.
217:
218: */
219: protected void letGo(BaseContainerHandle handle) {
220:
221: inUseCount--;
222: super .letGo(handle);
223: }
224:
225: /**
226: Returns true if only a single handle is connected to this container.
227: */
228: public boolean isSingleUser() {
229: return inUseCount == 1;
230: }
231: }
|