001: /*
002:
003: Derby - Class org.apache.derby.impl.io.DirStorageFactory
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.io;
023:
024: import org.apache.derby.iapi.services.sanity.SanityManager;
025:
026: import org.apache.derby.io.WritableStorageFactory;
027: import org.apache.derby.io.StorageFile;
028: import org.apache.derby.io.StorageRandomAccessFile;
029:
030: import java.io.File;
031: import java.io.FileNotFoundException;
032: import java.io.FileOutputStream;
033: import java.io.FileInputStream;
034: import java.io.InputStream;
035: import java.io.OutputStream;
036: import java.io.IOException;
037: import java.io.SyncFailedException;
038:
039: import java.util.Properties;
040:
041: /**
042: * This class provides a disk based implementation of the StorageFactory interface. It is used by the
043: * database engine to access persistent data and transaction logs under the directory (default) subsubprotocol.
044: */
045:
046: public class DirStorageFactory extends BaseStorageFactory implements
047: WritableStorageFactory {
048: /**
049: * Construct a StorageFile from a path name.
050: *
051: * @param path The path name of the file
052: *
053: * @return A corresponding StorageFile object
054: */
055: public final StorageFile newStorageFile(String path) {
056: return newPersistentFile(path);
057: }
058:
059: /**
060: * Construct a StorageFile from a directory and file name.
061: *
062: * @param directoryName The directory part of the path name.
063: * @param fileName The name of the file within the directory.
064: *
065: * @return A corresponding StorageFile object
066: */
067: public final StorageFile newStorageFile(String directoryName,
068: String fileName) {
069: return newPersistentFile(directoryName, fileName);
070: }
071:
072: /**
073: * Construct a StorageFile from a directory and file name.
074: *
075: * @param directoryName The directory part of the path name.
076: * @param fileName The name of the file within the directory.
077: *
078: * @return A corresponding StorageFile object
079: */
080: public final StorageFile newStorageFile(StorageFile directoryName,
081: String fileName) {
082: return newPersistentFile(directoryName, fileName);
083: }
084:
085: /**
086: * Construct a persistent StorageFile from a path name.
087: *
088: * @param path The path name of the file. Guaranteed not to be in the temporary file directory. If null
089: * then the database directory should be returned.
090: *
091: * @return A corresponding StorageFile object
092: */
093: StorageFile newPersistentFile(String path) {
094: if (path == null)
095: return new DirFile(dataDirectory);
096: return new DirFile(dataDirectory, path);
097: }
098:
099: /**
100: * Construct a persistent StorageFile from a directory and path name.
101: *
102: * @param directoryName The path name of the directory. Guaranteed not to be in the temporary file directory.
103: * Guaranteed not to be null
104: * @param fileName The name of the file within the directory. Guaranteed not to be null.
105: *
106: * @return A corresponding StorageFile object
107: */
108: StorageFile newPersistentFile(String directoryName, String fileName) {
109: return new DirFile(separatedDataDirectory + directoryName,
110: fileName);
111: }
112:
113: /**
114: * Construct a persistent StorageFile from a directory and path name.
115: *
116: * @param directoryName The path name of the directory. Guaranteed not to be to be null. Guaranteed to be
117: * created by a call to one of the newPersistentFile methods.
118: * @param fileName The name of the file within the directory. Guaranteed not to be null.
119: *
120: * @return A corresponding StorageFile object
121: */
122: StorageFile newPersistentFile(StorageFile directoryName,
123: String fileName) {
124: return new DirFile((DirFile) directoryName, fileName);
125: }
126:
127: /**
128: * Force the data of an output stream out to the underlying storage. That is, ensure that
129: * it has been made persistent. If the database is to be transient, that is, if the database
130: * does not survive a restart, then the sync method implementation need not do anything.
131: *
132: * @param stream The stream to be synchronized.
133: * @param metaData If true then this method must force both changes to the file's
134: * contents and metadata to be written to storage; if false, it need only force file content changes
135: * to be written. The implementation is allowed to ignore this parameter and always force out
136: * metadata changes.
137: *
138: * @exception IOException if an I/O error occurs.
139: * @exception SyncFailedException Thrown when the buffers cannot be flushed,
140: * or because the system cannot guarantee that all the buffers have been
141: * synchronized with physical media.
142: */
143: public void sync(OutputStream stream, boolean metaData)
144: throws IOException, SyncFailedException {
145: ((FileOutputStream) stream).getFD().sync();
146: }
147:
148: /**
149: * This method tests whether the "rws" and "rwd" modes are implemented. If the "rws" method is supported
150: * then the database engine will conclude that the write methods of "rws" mode StorageRandomAccessFiles are
151: * slow but the sync method is fast and optimize accordingly.
152: *
153: * @return <b>true</b> if an StIRandomAccess file opened with "rws" or "rwd" modes immediately writes data to the
154: * underlying storage, <b>false</b> if not.
155: */
156: public boolean supportsRws() {
157: return false;
158: }
159:
160: public boolean isReadOnlyDatabase() {
161: return false;
162: }
163:
164: /**
165: * Determine whether the storage supports random access. If random access is not supported then
166: * it will only be accessed using InputStreams and OutputStreams (if the database is writable).
167: *
168: * @return <b>true</b> if the storage supports random access, <b>false</b> if it is writable.
169: */
170: public boolean supportsRandomAccess() {
171: return true;
172: }
173:
174: void doInit() throws IOException {
175: if (dataDirectory != null) {
176: File dataDirectoryFile = new File(dataDirectory);
177: File databaseRoot = null;
178: if (dataDirectoryFile.isAbsolute())
179: databaseRoot = dataDirectoryFile;
180: else if (home != null && dataDirectory.startsWith(home))
181: databaseRoot = dataDirectoryFile;
182: else {
183: databaseRoot = new File(home, dataDirectory);
184: if (home != null)
185: dataDirectory = home + getSeparator()
186: + dataDirectory;
187: }
188: canonicalName = databaseRoot.getCanonicalPath();
189: createTempDir();
190: separatedDataDirectory = dataDirectory + getSeparator();
191: } else if (home != null) {
192: File root = new File(home);
193: dataDirectory = root.getCanonicalPath();
194: separatedDataDirectory = dataDirectory + getSeparator();
195: }
196: } // end of doInit
197: }
|