⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rafcontainer.java

📁 derby database source code.good for you.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*   Derby - Class org.apache.derby.impl.store.raw.data.RAFContainer   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.impl.store.raw.data.BaseContainer;import org.apache.derby.impl.store.raw.data.BaseContainerHandle;import org.apache.derby.impl.store.raw.data.BasePage;import org.apache.derby.iapi.services.cache.Cacheable;import org.apache.derby.iapi.services.context.ContextService;import org.apache.derby.iapi.services.monitor.Monitor;import org.apache.derby.iapi.services.diag.Performance;import org.apache.derby.iapi.services.sanity.SanityManager;import org.apache.derby.iapi.services.io.FormatIdUtil;import org.apache.derby.iapi.error.StandardException;import org.apache.derby.iapi.store.raw.ContainerHandle;import org.apache.derby.iapi.store.raw.ContainerKey;import org.apache.derby.iapi.store.raw.Loggable;import org.apache.derby.iapi.store.raw.log.LogInstant;import org.apache.derby.iapi.store.raw.xact.RawTransaction;import org.apache.derby.io.StorageFactory;import org.apache.derby.io.WritableStorageFactory;import org.apache.derby.io.StorageFile;import org.apache.derby.io.StorageRandomAccessFile;import java.util.Vector;import java.io.DataInput;import java.io.IOException;import java.security.AccessController;import java.security.PrivilegedExceptionAction;import java.security.PrivilegedActionException;/**	RAFContainer (short for RandomAccessFileContainer) is a concrete subclass of FileContainer	for FileContainers which are implemented on java.io.RandomAccessFile.*/class RAFContainer extends FileContainer implements PrivilegedExceptionAction{	/*	 * Immutable fields	 */	protected StorageRandomAccessFile fileData;	/* 	** Mutable fields, only valid when the identity is valid.	*/	protected boolean			needsSync;    /* privileged actions */    private int actionCode;    private static final int GET_FILE_NAME_ACTION = 1;    private static final int CREATE_CONTAINER_ACTION = 2;    private static final int REMOVE_FILE_ACTION = 3;    private static final int OPEN_CONTAINER_ACTION = 4;    private static final int STUBBIFY_ACTION = 5;    private ContainerKey actionIdentity;    private boolean actionStub;    private boolean actionErrorOK;    private boolean actionTryAlternatePath;    private StorageFile actionFile;    private LogInstant actionInstant;    	/*	 * Constructors	 */	RAFContainer(BaseDataFileFactory factory) {		super(factory);	}	/*	** Methods overriding super-class	*/	synchronized public boolean isDirty() {		return super.isDirty() || needsSync;	}	/*	** Methods of Cacheable	*/	/**		Set container's identity		@exception StandardException Standard Cloudscape error policy	*/	public Cacheable setIdentity(Object key) throws StandardException {		ContainerKey newIdentity = (ContainerKey) key;		// if this is an open for a temp container then return an object of that type		if (newIdentity.getSegmentId() == ContainerHandle.TEMPORARY_SEGMENT) {			TempRAFContainer tmpContainer = new TempRAFContainer(dataFactory);			return tmpContainer.setIdent(newIdentity);		}		return setIdent(newIdentity);	}	/**		@exception StandardException Standard Cloudscape error policy	 */	public Cacheable createIdentity(Object key, Object createParameter) throws StandardException {		ContainerKey newIdentity = (ContainerKey) key;		if (newIdentity.getSegmentId() == ContainerHandle.TEMPORARY_SEGMENT) {			TempRAFContainer tmpContainer = new TempRAFContainer(dataFactory);			return tmpContainer.createIdent(newIdentity, createParameter);		}		return createIdent(newIdentity, createParameter);	}	/*	** Container creation, opening, and closing	*/	/**		Remove the container		@exception StandardException Standard Cloudscape error policy	*/	protected void removeContainer(LogInstant instant, boolean leaveStub)		 throws StandardException	{		// discard all of my pages in the cache		pageCache.discard(identity);		stubbify(instant);		// RESOLVE: leaveStub false	}	final void closeContainer() {		if (fileData != null) {			try {				fileData.close();			} catch (IOException ioe) {			} finally {				fileData = null;			}		}	}	/*	** Methods used solely by StoredPage	*/	/**		Read a page into the supplied array.		<BR> MT - thread safe		@exception IOException exception reading page		@exception StandardException Standard Cloudscape error policy	*/	protected void readPage(long pageNumber, byte[] pageData)		 throws IOException, StandardException	{		if (SanityManager.DEBUG) {			SanityManager.ASSERT(!getCommittedDropState());		}		long pageOffset = pageNumber * pageSize;		synchronized (this) {			fileData.seek(pageOffset);			fileData.readFully(pageData, 0, pageSize);		}		if (dataFactory.databaseEncrypted() &&			pageNumber != FIRST_ALLOC_PAGE_NUMBER)		{			decryptPage(pageData, pageSize);		}	}	/**		Write a page from the supplied array.		<BR> MT - thread safe		@exception StandardException Standard Cloudscape error policy		@exception IOException IO error accessing page	*/	protected void writePage(long pageNumber, byte[] pageData, boolean syncPage)		 throws IOException, StandardException	{		synchronized(this)		{			// committed and dropped, do nothing.			// This file container may only be a stub			if (getCommittedDropState())				return;			if (pageNumber == FIRST_ALLOC_PAGE_NUMBER)			{				// write header into the alloc page array regardless of dirty				// bit because the alloc page have zero'ed out the borrowed				// space				writeHeader(pageData);				if (SanityManager.DEBUG)                 {					if (FormatIdUtil.readFormatIdInteger(pageData) !=                             AllocPage.FORMAT_NUMBER)                    {						SanityManager.THROWASSERT(							"expect " +							AllocPage.FORMAT_NUMBER +							"got " +							FormatIdUtil.readFormatIdInteger(pageData));                    }				}			}            // set dataToWrite to actual page buffer or encrypt buffer for            // use by normal write code, and retry catch block write code.            byte[] dataToWrite;            if (dataFactory.databaseEncrypted()                 && pageNumber != FIRST_ALLOC_PAGE_NUMBER)            {                // We cannot encrypt the page in place because pageData is                // still being accessed as clear text.  The encryption                // buffer is shared by all who access this container and can                // only be used within the synchronized block.                dataToWrite = encryptPage(pageData, pageSize);            }             else             {                dataToWrite = pageData;            }            ///////////////////////////////////////////////////            //            // RESOLVE: right now, no logical -> physical mapping.            // We can calculate the offset.  In the future, we may need to            // look at the allocation page or the in memory translation table            // to figure out where the page should go            //            /////////////////////////////////////////////////			long pageOffset = pageNumber * pageSize;			try			{				fileData.seek(pageOffset);				/**					On EPOC (www.symbian.com) a seek beyond the end of					a file just moves the file pointer to the end of the file.				*/				if (fileData.getFilePointer() != pageOffset)					padFile(fileData, pageOffset);				dataFactory.writeInProgress();				try				{					fileData.write(dataToWrite, 0, pageSize);				}				finally				{					dataFactory.writeFinished();				}			}			catch (IOException ioe)			{				// On some platforms, if we seek beyond the end of file, or try				// to write beyond the end of file (not appending to it, but				// skipping some bytes), it will give IOException.				// Try writing zeros from the current end of file to pageOffset				// and see if we can then do the seek/write.  The difference				// between pageOffset and current end of file is almost always				// going to be the multiple of pageSize				if (!padFile(fileData, pageOffset))					throw ioe;	// not writing beyond EOF, rethrow exception				if (SanityManager.DEBUG)                {					SanityManager.ASSERT(                        fileData.length() >= pageOffset,                        "failed to blank filled missing pages");                }				fileData.seek(pageOffset);				dataFactory.writeInProgress();				try				{					fileData.write(dataToWrite, 0, pageSize);				}				finally				{					dataFactory.writeFinished();				}			}			if (syncPage)			{				dataFactory.writeInProgress();				try				{                    if (!dataFactory.dataNotSyncedAtAllocation)                        fileData.sync( false);				}				finally				{					dataFactory.writeFinished();				}			}			else            {				needsSync = true;            }		}	}	/**		Pad the file upto the passed in page offset.		Returns true if the file needed padding.	*/	private boolean padFile(StorageRandomAccessFile file, long pageOffset)		throws IOException, StandardException {		long currentEOF = file.length();		if (currentEOF >= pageOffset)			return false;		// all objects in java are by definition initialized		byte zero[] = new byte[pageSize];		file.seek(currentEOF);		while(currentEOF < pageOffset)		{			dataFactory.writeInProgress();			try

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -