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

📄 rawstore.java

📁 derby database source code.good for you.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*   Derby - Class org.apache.derby.impl.store.raw.RawStore   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;import org.apache.derby.iapi.services.daemon.DaemonFactory;import org.apache.derby.iapi.services.daemon.DaemonService;import org.apache.derby.iapi.services.context.ContextManager;import org.apache.derby.iapi.services.crypto.CipherFactory;import org.apache.derby.iapi.services.crypto.CipherProvider;import org.apache.derby.iapi.services.locks.LockFactory;import org.apache.derby.iapi.services.monitor.Monitor;import org.apache.derby.iapi.services.monitor.ModuleControl;import org.apache.derby.iapi.services.monitor.ModuleSupportable;import org.apache.derby.iapi.services.monitor.PersistentService;import org.apache.derby.iapi.services.sanity.SanityManager;import org.apache.derby.iapi.error.StandardException;import org.apache.derby.iapi.services.i18n.MessageService;import org.apache.derby.iapi.services.property.PersistentSet;import org.apache.derby.iapi.store.access.TransactionInfo;import org.apache.derby.iapi.store.raw.ScanHandle;import org.apache.derby.iapi.store.raw.RawStoreFactory;import org.apache.derby.iapi.store.raw.Transaction;import org.apache.derby.iapi.store.raw.xact.TransactionFactory;import org.apache.derby.iapi.store.raw.data.DataFactory;import org.apache.derby.iapi.store.raw.log.LogFactory;import org.apache.derby.iapi.store.raw.log.LogInstant;import org.apache.derby.impl.services.monitor.UpdateServiceProperties;import org.apache.derby.io.StorageFactory;import org.apache.derby.io.WritableStorageFactory;import org.apache.derby.io.StorageFile;import org.apache.derby.iapi.store.access.DatabaseInstant;import org.apache.derby.catalog.UUID;import org.apache.derby.iapi.services.property.PropertyUtil;import org.apache.derby.iapi.services.io.FileUtil;import org.apache.derby.iapi.util.ReuseFactory;import org.apache.derby.iapi.util.StringUtil;import org.apache.derby.iapi.reference.Attribute;import org.apache.derby.iapi.reference.SQLState;import org.apache.derby.iapi.reference.MessageId;import org.apache.derby.iapi.reference.Property;import java.security.AccessController;import java.security.PrivilegedActionException;import java.security.PrivilegedExceptionAction;import java.security.SecureRandom;import java.util.Date;import java.util.Properties;import java.io.Serializable;import java.io.File;import java.io.FileOutputStream;import java.io.FileInputStream;import java.io.IOException;import java.io.FileNotFoundException;import java.io.OutputStreamWriter;import java.net.MalformedURLException;import java.net.URL;import java.security.PrivilegedExceptionAction;/**	A Raw store that implements the RawStoreFactory module by delegating all the	work to the lower modules TransactionFactory, LogFactory and DataFactory.	<PRE>	String TransactionFactoryId=<moduleIdentifier>	</PRE>		<P>	Class is final as it has methods with privilege blocks	and implements PrivilegedExceptionAction.*/public final class RawStore implements RawStoreFactory, ModuleControl, ModuleSupportable, PrivilegedExceptionAction{	private static final String BACKUP_HISTORY = "BACKUP.HISTORY";	private static final String[] BACKUP_FILTER =	{ DataFactory.TEMP_SEGMENT_NAME, DataFactory.DB_LOCKFILE_NAME, DataFactory.DB_EX_LOCKFILE_NAME, LogFactory.LOG_DIRECTORY_NAME };	protected TransactionFactory	xactFactory;	protected DataFactory			dataFactory;	protected LogFactory			logFactory;    private StorageFactory storageFactory;	private SecureRandom random;	private boolean databaseEncrypted;	private CipherProvider encryptionEngine;	private CipherProvider decryptionEngine;	private CipherFactory cipherFactory;	private int counter_encrypt;	private int counter_decrypt;	private int encryptionBlockSize = RawStoreFactory.DEFAULT_ENCRYPTION_BLOCKSIZE;	String dataDirectory; 					// where files are stored		// this daemon takes care of all daemon work for this raw store	protected DaemonService			rawStoreDaemon;    private int actionCode;    private static final int FILE_WRITER_ACTION = 1;    private StorageFile actionStorageFile;    private boolean actionAppend;    private static final int REGULAR_FILE_EXISTS_ACTION = 2;    private File actionRegularFile;    private static final int STORAGE_FILE_EXISTS_ACTION = 3;    private static final int REGULAR_FILE_DELETE_ACTION = 4;    private static final int REGULAR_FILE_MKDIRS_ACTION = 5;    private static final int REGULAR_FILE_IS_DIRECTORY_ACTION = 6;    private static final int REGULAR_FILE_REMOVE_DIRECTORY_ACTION = 7;    private static final int REGULAR_FILE_RENAME_TO_ACTION = 8;    private File actionRegularFile2;    private static final int COPY_STORAGE_DIRECTORY_TO_REGULAR_ACTION = 9;    private byte[] actionBuffer;    private String[] actionFilter;    private static final int COPY_REGULAR_DIRECTORY_TO_STORAGE_ACTION = 10;    private static final int COPY_REGULAR_FILE_TO_STORAGE_ACTION = 11;    private static final int REGULAR_FILE_LIST_DIRECTORY_ACTION = 12;    	public RawStore() {	}	/*	** Methods of ModuleControl	*/	/**	  We use this RawStore for all databases.	  */	public boolean canSupport(Properties startParams) {		return true;	}	public void	boot(boolean create, Properties properties)		throws StandardException	{		dataDirectory = properties.getProperty(PersistentService.ROOT);		DaemonFactory daemonFactory =			(DaemonFactory)Monitor.startSystemModule(org.apache.derby.iapi.reference.Module.DaemonFactory);		rawStoreDaemon = daemonFactory.createNewDaemon("rawStoreDaemon");		xactFactory = (TransactionFactory)					Monitor.bootServiceModule(						create, this, getTransactionFactoryModule(), properties);		dataFactory = (DataFactory)					Monitor.bootServiceModule(					  create, this, getDataFactoryModule(), properties);		storageFactory = dataFactory.getStorageFactory();		if (properties != null)		{			/***********************************************			 * encryption			 **********************************************/			String dataEncryption = properties.getProperty(Attribute.DATA_ENCRYPTION);			databaseEncrypted = Boolean.valueOf(dataEncryption).booleanValue();			if (databaseEncrypted)			{					cipherFactory =                        (CipherFactory)Monitor.bootServiceModule(create, this,						org.apache.derby.iapi.reference.Module.CipherFactory, properties);					// The database can be encrypted using an encryption key that is given at					// connection url. For security reasons, this key is not made persistent					// in the database. But it is necessary to verify the encryption key 					// whenever booting the database if it is similar to the key that was used					// during creation time. This needs to happen before we access the data/logs to 					// avoid the risk of corrupting the database because of a wrong encryption key.					// Please note this verification process does not provide any added security				        // but is intended to allow to fail gracefully if a wrong encryption key 					// is used during boot time  					cipherFactory.verifyKey(create,storageFactory,properties);					// Initializes the encryption and decryption engines					encryptionEngine = cipherFactory.						createNewCipher(CipherFactory.ENCRYPT);	                                // At creation time of an encrypted database, store the encryption block size					// for the algorithm. Store this value as property given by  	                                // RawStoreFactory.ENCRYPTION_BLOCKSIZE. This value	                                // is made persistent by storing it in service.properties	                                // To connect to an existing database, retrieve the value and use it for	                                // appropriate padding.	                                // The  default value of encryption block size is 8,					// to allow for downgrade issues					// Before support for AES (beetle6023), default encryption block size supported					// was 8					if(create)					{						encryptionBlockSize = encryptionEngine.getEncryptionBlockSize();						properties.put(RawStoreFactory.ENCRYPTION_BLOCKSIZE,								String.valueOf(encryptionBlockSize));					}					else					{						if(properties.getProperty(RawStoreFactory.ENCRYPTION_BLOCKSIZE) != null)						    encryptionBlockSize = Integer.parseInt(properties.getProperty										(RawStoreFactory.ENCRYPTION_BLOCKSIZE));					}					decryptionEngine = cipherFactory.						createNewCipher(CipherFactory.DECRYPT);					random = cipherFactory.getSecureRandom();			}		}		// let everyone knows who their rawStoreFactory is and they can use it		// to get to other modules		// pass in create and properties to dataFactory so it can boot the log		// factory		dataFactory.setRawStoreFactory(this, create, properties);		xactFactory.setRawStoreFactory(this);        if( properties instanceof UpdateServiceProperties)        {            if( storageFactory instanceof WritableStorageFactory)                ((UpdateServiceProperties)properties).setStorageFactory( (WritableStorageFactory) storageFactory);        }        		// log factory is booted by the data factory		logFactory =(LogFactory) Monitor.findServiceModule(this, getLogFactoryModule());		String restoreFromBackup =null;		restoreFromBackup = properties.getProperty(Attribute.CREATE_FROM);		if(restoreFromBackup == null)			restoreFromBackup = properties.getProperty(Attribute.RESTORE_FROM);		if(restoreFromBackup == null)			restoreFromBackup =				properties.getProperty(Attribute.ROLL_FORWARD_RECOVERY_FROM);		//save the service properties to a file if we are doing a restore from		if(restoreFromBackup !=null)		{			//copy the jar files.etc from backup if they don't exist			restoreRemainingFromBackup(restoreFromBackup);			((UpdateServiceProperties)properties).saveServiceProperties();		}		// If the log is at another location, make sure  service.properties		// file has it.		String logDevice = properties.getProperty(Attribute.LOG_DEVICE);		if (logDevice !=null)		{			if (create || !logDevice.equals(logFactory.getCanonicalLogPath()) ||				restoreFromBackup!=null)			{				// get the real location from the log factory				properties.put(Attribute.LOG_DEVICE, logFactory.getCanonicalLogPath());				//make the log device param stored in backup is same as current log device.				properties.put(Property.LOG_DEVICE_AT_BACKUP, logFactory.getCanonicalLogPath());			}			}else{			//when we restore from a backup logDevice param does not exists 			//in service.properties to support restore using OS commands to work. 			//Instead of logDevice, we user logDeviceWhenBackedUp parameter to			//identify the log location while restoring createFrom/restoreFrom/rollForwardRecoveryFrom			//attribute , following make sures the logDevice parameter gets 			//into service.propertues in such cases.			if(restoreFromBackup!=null && logFactory.getCanonicalLogPath()!=null)			{				//logdevice might have got changed because of backup restore. 				properties.put(Attribute.LOG_DEVICE,  logFactory.getCanonicalLogPath());			}			else{				//might have been OS copy restore. We default log to db home				properties.remove(Property.LOG_DEVICE_AT_BACKUP);			}		}				//save the service properties to a file if we are doing a restore from		if(restoreFromBackup !=null)		{			//copy the jar files.etc from backup if they don't exist			restoreRemainingFromBackup(restoreFromBackup);			((UpdateServiceProperties)properties).saveServiceProperties();		}		/**		 * Note: service.properties file acts as flags to indicate		 * that the copy from backup is successful.		 * If we reached so far while restoring from backup means		 * we copied all the necessary data from backup. Only thing		 * that remains is roll forwarding the logs. Incase if we crash at this		 * point and user re boots the datbase again without any restore flags		 * it shoud boot without any problem.		 **/		// no need to tell log factory which raw store factory it belongs to		// since this is passed into the log factory for recovery		// after the factories are loaded, recover the database		logFactory.recover(this, dataFactory, xactFactory);	}	public void	stop() {		if (SanityManager.DEBUG)		{			if (databaseEncrypted)				SanityManager.DEBUG_PRINT("encryption statistics",						"Encryption called " + counter_encrypt + " times, " +						"decryption called " + counter_decrypt + " times");		}		if (rawStoreDaemon != null)			rawStoreDaemon.stop();		if (logFactory == null)			return;		try {			if (logFactory.checkpoint(this, dataFactory, xactFactory, false))			{				if (dataFactory != null)					dataFactory.removeStubsOK();			}		} catch (StandardException se) {			// checkpoint failed, stop all factory from shutting down normally			markCorrupt(se);		}	}	/*	** Methods of RawStoreFactory	*/	/**		Is the store read-only.		@see RawStoreFactory#isReadOnly	*/	public boolean isReadOnly() {		return dataFactory.isReadOnly();	}	public LockFactory getLockFactory() {		return xactFactory.getLockFactory();	}	/*	 * Return the module providing XAresource interface to the transaction     * table.     *	 * @exception StandardException Standard cloudscape exception policy.	 */	public /* XAResourceManager */ Object getXAResourceManager()        throws StandardException    {        return(xactFactory.getXAResourceManager());    }	public Transaction startGlobalTransaction(    ContextManager  contextMgr,    int             format_id,    byte[]          global_id,    byte[]          branch_id)        throws StandardException    {		return xactFactory.startGlobalTransaction(                    this, contextMgr, format_id, global_id, branch_id);	}	public Transaction startTransaction(ContextManager contextMgr, String transName)        throws StandardException    {		return xactFactory.startTransaction(this, contextMgr, transName);	}	public Transaction startNestedReadOnlyUserTransaction(    Object          compatibilitySpace,    ContextManager  contextMgr,    String          transName)        throws StandardException    {		return(            xactFactory.startNestedReadOnlyUserTransaction(                this, compatibilitySpace, contextMgr, transName));	}	public Transaction startNestedUpdateUserTransaction(    ContextManager  contextMgr,    String          transName)        throws StandardException    {		return(            xactFactory.startNestedUpdateUserTransaction(                this, contextMgr, transName));	}	public Transaction findUserTransaction(        ContextManager contextMgr,        String transName)		 throws StandardException	{		return xactFactory.findUserTransaction(this, contextMgr, transName);	}	public Transaction startInternalTransaction(ContextManager contextMgr) throws StandardException {		return xactFactory.startInternalTransaction(this, contextMgr);	}

⌨️ 快捷键说明

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