--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.impl.store.raw.data.TempRAFContainer\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to you under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.impl.store.raw.data;\r
+\r
+import org.apache.derby.impl.store.raw.data.BaseContainerHandle;\r
+import org.apache.derby.impl.store.raw.data.BasePage;\r
+\r
+import org.apache.derby.iapi.services.cache.Cacheable;\r
+import org.apache.derby.iapi.services.sanity.SanityManager;\r
+\r
+import org.apache.derby.iapi.error.StandardException;\r
+import org.apache.derby.iapi.store.raw.xact.RawTransaction;\r
+import org.apache.derby.iapi.store.raw.ContainerHandle;\r
+import org.apache.derby.iapi.store.raw.ContainerKey;\r
+import org.apache.derby.iapi.store.raw.Page;\r
+import org.apache.derby.iapi.store.raw.log.LogInstant;\r
+import org.apache.derby.iapi.store.raw.data.RawContainerHandle;\r
+\r
+import org.apache.derby.io.StorageFactory;\r
+import org.apache.derby.io.StorageFile;\r
+\r
+import java.io.IOException;\r
+\r
+/**\r
+ needsSync is never true - DONE\r
+ An exception never marks the store as corrupt\r
+ clean() does not stubbify\r
+ preAllocate() does nothing - DONE\r
+ getFileName() returns a file in the tmp directory - DONE\r
+ flushAll does nothing - DONE\r
+ file descriptor is never synced\r
+*/\r
+class TempRAFContainer extends RAFContainer {\r
+\r
+ protected int inUseCount;\r
+\r
+ TempRAFContainer(BaseDataFileFactory factory) {\r
+ super(factory);\r
+ }\r
+\r
+ /**\r
+ @exception StandardException Standard Derby error policy\r
+ */\r
+ public Cacheable setIdentity(Object key) throws StandardException {\r
+\r
+ ContainerKey newIdentity = (ContainerKey) key;\r
+ if (newIdentity.getSegmentId() != ContainerHandle.TEMPORARY_SEGMENT) {\r
+\r
+ RAFContainer realContainer = new RAFContainer(dataFactory);\r
+ return realContainer.setIdent(newIdentity);\r
+ }\r
+\r
+ return super.setIdentity(newIdentity);\r
+\r
+ }\r
+\r
+ /**\r
+ @exception StandardException Standard Derby error policy\r
+ */\r
+ public Cacheable createIdentity(Object key, Object createParameter) throws StandardException {\r
+\r
+ ContainerKey newIdentity = (ContainerKey) key;\r
+\r
+ if (newIdentity.getSegmentId() != ContainerHandle.TEMPORARY_SEGMENT) {\r
+ RAFContainer realContainer = new RAFContainer(dataFactory);\r
+ return realContainer.createIdentity(newIdentity, createParameter);\r
+ }\r
+\r
+ return createIdent(newIdentity, createParameter);\r
+ }\r
+\r
+ /**\r
+ @exception StandardException Standard Derby error policy\r
+ */\r
+ public void removeContainer(LogInstant instant, boolean leaveStub) throws StandardException\r
+ {\r
+ // discard all of my pages in the cache\r
+ pageCache.discard(identity);\r
+ \r
+ synchronized(this) { \r
+ // prevent anybody from looking at this container again\r
+ setDroppedState(true);\r
+ setCommittedDropState(true);\r
+ setDirty(false);\r
+ needsSync = false;\r
+\r
+ }\r
+\r
+ removeFile(getFileName(identity, false, false, false));\r
+ }\r
+\r
+ /**\r
+ Preallocate page. Since we don't sync when we write page anyway, no\r
+ need to preallocate page.\r
+ */\r
+ protected int preAllocate(long lastPreallocPagenum, int preAllocSize)\r
+ {\r
+ return 0;\r
+ }\r
+\r
+\r
+ /**\r
+ Write the page, if it's within range of the current page range of the container.\r
+ If we do write it then don't request that it be synced.\r
+\r
+ @exception StandardException Standard Derby error policy\r
+ */\r
+ protected void writePage(long pageNumber, byte[] pageData, boolean syncPage) throws IOException, StandardException {\r
+ if (!this.getDroppedState()) {\r
+ super.writePage(pageNumber, pageData, false);\r
+ }\r
+ needsSync = false;\r
+ }\r
+\r
+ StorageFile getFileName(ContainerKey identity, boolean stub,\r
+ boolean errorOK, boolean tryAlternatePath)\r
+ {\r
+ return privGetFileName( identity, stub, errorOK, tryAlternatePath);\r
+ }\r
+\r
+ protected StorageFile privGetFileName(ContainerKey identity, boolean stub,\r
+ boolean errorOK, boolean tryAlternatePath)\r
+ {\r
+ return dataFactory.storageFactory.newStorageFile( dataFactory.storageFactory.getTempDir(),\r
+ "T" + identity.getContainerId() + ".tmp");\r
+ }\r
+\r
+ /**\r
+ Add a page without locking the container, only one user will be accessing this\r
+ table at a time.\r
+\r
+ @exception StandardException Standard Derby error policy\r
+ */\r
+ public Page addPage(BaseContainerHandle handle, boolean isOverflow) throws StandardException {\r
+\r
+ BasePage newPage = newPage(handle, (RawTransaction) null, handle, isOverflow);\r
+\r
+ if (SanityManager.DEBUG) {\r
+ SanityManager.ASSERT(newPage.isLatched());\r
+ }\r
+\r
+ return newPage;\r
+ }\r
+\r
+ /**\r
+ @exception StandardException Standard Derby error policy\r
+ */\r
+ public void truncate(BaseContainerHandle handle) throws StandardException {\r
+\r
+ // stop anyone from writing any of my pages out\r
+ synchronized(this)\r
+ {\r
+ setDroppedState(true);\r
+ setCommittedDropState(true);\r
+ setDirty(false);\r
+ needsSync = false;\r
+ }\r
+\r
+ // discard all of my pages in the cache\r
+ while (pageCache.discard(identity) != true)\r
+ ;\r
+\r
+ removeFile(getFileName(identity, false, true, false));\r
+\r
+ createIdent(identity, this);\r
+\r
+ addPage(handle, false).unlatch();\r
+ }\r
+ /**\r
+ Lock the container and mark the container as in-use by this container handle.\r
+\r
+ @param droppedOK if true, use this container even if it is dropped.,\r
+ @return true if the container can be used, false if it has been dropped\r
+ since the lock was requested and droppedOK is not true.\r
+\r
+ @exception StandardException I cannot be opened for update.\r
+ */\r
+ protected boolean use(BaseContainerHandle handle, boolean forUpdate,\r
+ boolean droppedOK) \r
+ throws StandardException {\r
+\r
+ if (super.use(handle, forUpdate, droppedOK)) {\r
+ inUseCount++;\r
+ return true;\r
+ }\r
+\r
+ return false;\r
+ }\r
+\r
+ /**\r
+ Discontinue use of this container. Note that the unlockContainer\r
+ call made from this method may not release any locks. The container\r
+ lock may be held until the end of the transaction.\r
+\r
+ */\r
+ protected void letGo(BaseContainerHandle handle) {\r
+\r
+ inUseCount--;\r
+ super.letGo(handle);\r
+ }\r
+\r
+\r
+ /**\r
+ Returns true if only a single handle is connected to this container.\r
+ */\r
+ public boolean isSingleUser() {\r
+ return inUseCount == 1;\r
+ }\r
+}\r