1 | /* |
2 | |
3 | Derby - Class org.apache.derby.impl.io.DirStorageFactory |
4 | |
5 | Copyright 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.impl.io; |
22 | |
23 | import org.apache.derby.iapi.services.sanity.SanityManager; |
24 | |
25 | import org.apache.derby.io.WritableStorageFactory; |
26 | import org.apache.derby.io.StorageFile; |
27 | import org.apache.derby.io.StorageRandomAccessFile; |
28 | |
29 | import java.io.File; |
30 | import java.io.FileNotFoundException; |
31 | import java.io.FileOutputStream; |
32 | import java.io.FileInputStream; |
33 | import java.io.InputStream; |
34 | import java.io.OutputStream; |
35 | import java.io.IOException; |
36 | import java.io.SyncFailedException; |
37 | |
38 | import java.util.Properties; |
39 | |
40 | /** |
41 | * This class provides a disk based implementation of the StorageFactory interface. It is used by the |
42 | * database engine to access persistent data and transaction logs under the directory (default) subsubprotocol. |
43 | */ |
44 | |
45 | public class DirStorageFactory extends BaseStorageFactory |
46 | implements WritableStorageFactory |
47 | { |
48 | /** |
49 | * Construct a StorageFile from a path name. |
50 | * |
51 | * @param path The path name of the file |
52 | * |
53 | * @return A corresponding StorageFile object |
54 | */ |
55 | public final StorageFile newStorageFile( String path) |
56 | { |
57 | return newPersistentFile( path); |
58 | } |
59 | |
60 | /** |
61 | * Construct a StorageFile from a directory and file name. |
62 | * |
63 | * @param directoryName The directory part of the path name. |
64 | * @param fileName The name of the file within the directory. |
65 | * |
66 | * @return A corresponding StorageFile object |
67 | */ |
68 | public final StorageFile newStorageFile( String directoryName, String fileName) |
69 | { |
70 | return newPersistentFile( directoryName, fileName); |
71 | } |
72 | |
73 | /** |
74 | * Construct a StorageFile from a directory and file name. |
75 | * |
76 | * @param directoryName The directory part of the path name. |
77 | * @param fileName The name of the file within the directory. |
78 | * |
79 | * @return A corresponding StorageFile object |
80 | */ |
81 | public final StorageFile newStorageFile( StorageFile directoryName, String fileName) |
82 | { |
83 | return newPersistentFile( directoryName, fileName); |
84 | } |
85 | /** |
86 | * Construct a persistent StorageFile from a path name. |
87 | * |
88 | * @param path The path name of the file. Guaranteed not to be in the temporary file directory. If null |
89 | * then the database directory should be returned. |
90 | * |
91 | * @return A corresponding StorageFile object |
92 | */ |
93 | StorageFile newPersistentFile( String path) |
94 | { |
95 | if( path == null) |
96 | return new DirFile( dataDirectory); |
97 | return new DirFile(dataDirectory, path); |
98 | } |
99 | |
100 | /** |
101 | * Construct a persistent StorageFile from a directory and path name. |
102 | * |
103 | * @param directoryName The path name of the directory. Guaranteed not to be in the temporary file directory. |
104 | * Guaranteed not to be null |
105 | * @param fileName The name of the file within the directory. Guaranteed not to be null. |
106 | * |
107 | * @return A corresponding StorageFile object |
108 | */ |
109 | StorageFile newPersistentFile( String directoryName, String fileName) |
110 | { |
111 | return new DirFile( separatedDataDirectory + directoryName, fileName); |
112 | } |
113 | |
114 | /** |
115 | * Construct a persistent StorageFile from a directory and path name. |
116 | * |
117 | * @param directoryName The path name of the directory. Guaranteed not to be to be null. Guaranteed to be |
118 | * created by a call to one of the newPersistentFile methods. |
119 | * @param fileName The name of the file within the directory. Guaranteed not to be null. |
120 | * |
121 | * @return A corresponding StorageFile object |
122 | */ |
123 | StorageFile newPersistentFile( StorageFile directoryName, String fileName) |
124 | { |
125 | return new DirFile( (DirFile) directoryName, fileName); |
126 | } |
127 | |
128 | /** |
129 | * Force the data of an output stream out to the underlying storage. That is, ensure that |
130 | * it has been made persistent. If the database is to be transient, that is, if the database |
131 | * does not survive a restart, then the sync method implementation need not do anything. |
132 | * |
133 | * @param stream The stream to be synchronized. |
134 | * @param metaData If true then this method must force both changes to the file's |
135 | * contents and metadata to be written to storage; if false, it need only force file content changes |
136 | * to be written. The implementation is allowed to ignore this parameter and always force out |
137 | * metadata changes. |
138 | * |
139 | * @exception IOException if an I/O error occurs. |
140 | * @exception SyncFailedException Thrown when the buffers cannot be flushed, |
141 | * or because the system cannot guarantee that all the buffers have been |
142 | * synchronized with physical media. |
143 | */ |
144 | public void sync( OutputStream stream, boolean metaData) throws IOException, SyncFailedException |
145 | { |
146 | ((FileOutputStream) stream).getFD().sync(); |
147 | } |
148 | |
149 | /** |
150 | * This method tests whether the "rws" and "rwd" modes are implemented. If the "rws" method is supported |
151 | * then the database engine will conclude that the write methods of "rws" mode StorageRandomAccessFiles are |
152 | * slow but the sync method is fast and optimize accordingly. |
153 | * |
154 | * @return <b>true</b> if an StIRandomAccess file opened with "rws" or "rwd" modes immediately writes data to the |
155 | * underlying storage, <b>false</b> if not. |
156 | */ |
157 | public boolean supportsRws() |
158 | { |
159 | return false; |
160 | } |
161 | |
162 | public boolean isReadOnlyDatabase() |
163 | { |
164 | return false; |
165 | } |
166 | |
167 | /** |
168 | * Determine whether the storage supports random access. If random access is not supported then |
169 | * it will only be accessed using InputStreams and OutputStreams (if the database is writable). |
170 | * |
171 | * @return <b>true</b> if the storage supports random access, <b>false</b> if it is writable. |
172 | */ |
173 | public boolean supportsRandomAccess() |
174 | { |
175 | return true; |
176 | } |
177 | |
178 | void doInit() throws IOException |
179 | { |
180 | if( dataDirectory != null) |
181 | { |
182 | File dataDirectoryFile = new File( dataDirectory); |
183 | File databaseRoot = null; |
184 | if( dataDirectoryFile.isAbsolute()) |
185 | databaseRoot = dataDirectoryFile; |
186 | else if( home != null && dataDirectory.startsWith( home)) |
187 | databaseRoot = dataDirectoryFile; |
188 | else |
189 | databaseRoot = new File( home, dataDirectory); |
190 | canonicalName = databaseRoot.getCanonicalPath(); |
191 | createTempDir(); |
192 | separatedDataDirectory = dataDirectory + getSeparator(); |
193 | } |
194 | else if( home != null) |
195 | { |
196 | File root = new File( home); |
197 | dataDirectory = root.getCanonicalPath(); |
198 | separatedDataDirectory = dataDirectory + getSeparator(); |
199 | } |
200 | } // end of doInit |
201 | } |