📄 basecontainerhandle.java
字号:
/* Derby - Class org.apache.derby.impl.store.raw.data.BaseContainerHandle Copyright 1997, 2004 The Apache Software Foundation or its licensors, as applicable. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */package org.apache.derby.impl.store.raw.data;import org.apache.derby.iapi.reference.SQLState;import org.apache.derby.iapi.services.locks.Lockable;import org.apache.derby.iapi.services.locks.VirtualLockTable;import org.apache.derby.iapi.services.sanity.SanityManager;import org.apache.derby.iapi.error.StandardException;import org.apache.derby.iapi.store.access.SpaceInfo;import org.apache.derby.iapi.store.raw.ContainerHandle;import org.apache.derby.iapi.store.raw.ContainerLock;import org.apache.derby.iapi.store.raw.LockingPolicy;import org.apache.derby.iapi.store.raw.Page;import org.apache.derby.iapi.store.raw.PageKey;import org.apache.derby.iapi.store.raw.PageTimeStamp;import org.apache.derby.iapi.store.raw.RecordHandle;import org.apache.derby.iapi.store.raw.ContainerKey;import org.apache.derby.iapi.store.raw.data.RawContainerHandle;import org.apache.derby.iapi.store.raw.xact.RawTransaction;import org.apache.derby.iapi.store.raw.log.LogInstant;import org.apache.derby.iapi.util.ByteArray;import org.apache.derby.catalog.UUID;import java.util.Hashtable;import java.util.Observable;import java.util.Observer;import java.util.Properties;/** A handle to an open container, implememts RawContainerHandle. <P> This class is an Observer to observe RawTransactions and is also a Observable to handle the list of pages accessed thorough this handle. <BR> This class implements Lockable (defined to be ContainerHandle) and is the object used to logically lock the container. <BR> MT - Mutable - Immutable identity - Thread Aware*/public class BaseContainerHandle extends Observable implements RawContainerHandle, Observer { /* ** Fields */ /** Container identifier <BR> MT - Immutable */ protected /*final*/ ContainerKey identity; /** Is this ContainerHandle active. <BR> MT - Mutable : scoped */ protected boolean active; /** The actual container we are accessing. Only valid when active is true. <BR> MT - Mutable : scoped */ protected BaseContainer container; /** the locking policy we opened the container with. Only valid when active is true. <BR> MT - Mutable : scoped */ private LockingPolicy locking; /** our transaction. Only valid when active is true. <BR> MT - Mutable : scoped */ protected RawTransaction xact; /** are we going to update? <BR> MT - Immutable after container handle becomes active */ private boolean forUpdate; protected int mode; // mode the container was opened in protected PageActions actionsSet; protected AllocationActions allocActionsSet; /* ** Constructor */ /** Create an object that is only used for locking the container. */ public BaseContainerHandle(UUID rawStoreId, RawTransaction xact, ContainerKey identity, LockingPolicy locking, int mode) { this.identity = identity; this.xact = xact; this.locking = locking; this.mode = mode; this.forUpdate = (mode & MODE_FORUPDATE) == MODE_FORUPDATE; } /** Create a container handle that is used to actually access the container. */ public BaseContainerHandle( UUID rawStoreId, RawTransaction xact, PageActions actionsSet, AllocationActions allocActionsSet, LockingPolicy locking, BaseContainer container, int mode) { this(rawStoreId, xact, (ContainerKey) container.getIdentity(), locking, mode); this.actionsSet = actionsSet; this.allocActionsSet = allocActionsSet; this.container = container; // we are inactive until useContainer is called. } /* ** Methods from ContainerHandle */ /** Add a page to the container The page returned will be observing me. @see BaseContainer#addPage @see ContainerHandle#addPage @exception StandardException Standard Cloudscape error policy */ public Page addPage() throws StandardException { checkUpdateOpen(); Page page = container.addPage(this, false /* not an overflow page */); return page; } /** Release free space to the OS. <P> As is possible release any free space to the operating system. This will usually mean releasing any free pages located at the end of the file using the java truncate() interface. @exception StandardException Standard Cloudscape error policy */ public void compressContainer() throws StandardException { checkUpdateOpen(); container.compressContainer(this); } /** Add a page to the container, if flag == ContainerHandle.ADD_PAGE_BULK, tell the container about it. The page returned will be observing me. @see BaseContainer#addPage @see ContainerHandle#addPage @exception StandardException Standard Cloudscape error policy */ public Page addPage(int flag) throws StandardException { if ((flag & ContainerHandle.ADD_PAGE_BULK) != 0 && active && forUpdate) { // start preallocating immediatelly, don't wait for the // preallocation threshold to be crossed. Don't go wild and // preallocate a bunch of pages either, use preAllocate for that. container.clearPreallocThreshold(); } return addPage(); } /** Preallocate numPage if possible. */ public void preAllocate(int numPage) { if (numPage > 0 && active && forUpdate) container.prepareForBulkLoad(this, numPage); } /** * Request the system properties associated with a container. * <p> * Request the value of properties that are associated with a container. * The following properties can be requested: * derby.storage.pageSize * derby.storage.pageReservedSpace * derby.storage.minimumRecordSize * <p> * To get the value of a particular property add it to the property list, * and on return the value of the property will be set to it's current * value. For example: * * get_prop(BaseContainerHandle ch) * { * Properties prop = new Properties(); * prop.put("derby.storage.pageSize", ""); * ch.getContainerProperties(prop); * * System.out.println( * "conatainer's page size = " + * prop.getProperty("derby.storage.pageSize"); * } * * @param prop Property list to fill in. * * @exception StandardException Standard exception policy. **/ public void getContainerProperties(Properties prop) throws StandardException { checkOpen(); container.getContainerProperties(prop); return; } /** Remove a page from the container. @see ContainerHandle#removePage @exception StandardException Standard Cloudscape error policy */ public void removePage(Page page) throws StandardException { if (!active) { if (page != null) page.unlatch(); throw StandardException.newException( SQLState.DATA_CONTAINER_CLOSED); } if (!forUpdate) { if (page != null) page.unlatch(); throw StandardException.newException( SQLState.DATA_CONTAINER_READ_ONLY); } container.removePage(this, (BasePage)page); } public Page getPage(long pageNumber) throws StandardException { checkOpen(); return container.getPage(this, pageNumber, true); } public Page getAllocPage(long pageNumber) throws StandardException { checkOpen(); return container.getAllocPage(this, pageNumber, true); } public Page getUserPageNoWait(long pageNumber) throws StandardException { checkOpen(); return container.getHeadPage(this, pageNumber, false); } public Page getUserPageWait(long pageNumber) throws StandardException { checkOpen(); return container.getHeadPage(this, pageNumber, true); } public Page getPageNoWait(long pageNumber) throws StandardException { checkOpen(); return container.getPage(this, pageNumber, false); } public Page getFirstPage() throws StandardException { checkOpen(); return container.getFirstPage(this); } public Page getNextPage(long pageNumber) throws StandardException { checkOpen(); return container.getNextPage(this, pageNumber); } public Page getPageForInsert(int flag) throws StandardException { checkUpdateOpen(); return container.getPageForInsert(this, flag); } public Page getPageForCompress(int flag, long pageno) throws StandardException { checkUpdateOpen(); return container.getPageForCompress(this, flag, pageno); } /** @see ContainerHandle#isReadOnly() */ public final boolean isReadOnly() { return(!forUpdate); } /** @see ContainerHandle#close */ public void close() { if (xact == null) { // Probably be closed explicitly by a client, after closing // automatically after an abort. if (SanityManager.DEBUG) SanityManager.ASSERT(!active); return; } // notify our observers (Pages) that we are closing ... informObservers(); active = false; getLockingPolicy().unlockContainer(xact, this); // let go of the container if (container != null) { container.letGo(this); container = null; } // and remove ourseleves from this transaction xact.deleteObserver(this); xact = null; } /* cost estimation */ /** @see ContainerHandle#getEstimatedRowCount @exception StandardException Standard Cloudscape error policy */ public long getEstimatedRowCount(int flag) throws StandardException { checkOpen(); return container.getEstimatedRowCount(flag); } /** @see ContainerHandle#setEstimatedRowCount @exception StandardException Standard Cloudscape error policy */ public void setEstimatedRowCount(long count, int flag) throws StandardException { checkOpen(); container.setEstimatedRowCount(count, flag); } /** @see ContainerHandle#getEstimatedPageCount @exception StandardException Standard Cloudscape error policy */ public long getEstimatedPageCount(int flag) throws StandardException { checkOpen(); return container.getEstimatedPageCount(this, flag); } /** @see ContainerHandle#flushContainer @exception StandardException Standard Cloudscape error policy */ public void flushContainer() throws StandardException { checkUpdateOpen(); // raw store may override unlog mode when log is Archived. // if ((mode & MODE_CREATE_UNLOGGED) == 0) // throw StandardException.newException( // SQLState.DATA_NOT_CREATE_UNLOGGED, identity); container.flushAll(); } /** @see ContainerHandle#compactRecord @exception StandardException Standard Cloudscape error policy */ public void compactRecord(RecordHandle record) throws StandardException { if (!forUpdate) { throw StandardException.newException( SQLState.DATA_CONTAINER_READ_ONLY); } PageKey pkey = (PageKey)record.getPageId(); BasePage headPage = (BasePage)getPage(pkey.getPageNumber()); if (headPage != null) { // The page could have been null if it was deallocated after the // row lock is gotten. We are doing all of these post commit so // the record may not even be there and we got a lock for nothing. try { headPage.compactRecord(record); } finally { headPage.unlatch(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -