Debian Codegraph @

src/main/org/h2/store/fs/split/FilePathSplit.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.store.fs.split;
7
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.OutputStream;
11 import java.io.SequenceInputStream;
12 import java.nio.channels.FileChannel;
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.h2.engine.SysProperties;
17 import org.h2.message.DbException;
18 import org.h2.store.fs.FilePath;
19 import org.h2.store.fs.FilePathWrapper;
20
21 /**
22 * A file system that may split files into multiple smaller files.
23 * (required for a FAT32 because it only support files up to 2 GB).
24 */
25 public class FilePathSplit extends FilePathWrapper {
26
27 private static final String PART_SUFFIX = ".part";
28
29 @Override
30 protected String getPrefix() {
31 return getScheme() + ":" + parse(name)[0] + ":";
32 }
33
34 @Override
35 public FilePath unwrap(String fileName) {
36 return FilePath.get(parse(fileName)[1]);
37 }
38
39 @Override
40 public boolean setReadOnly() {
41 boolean result = false;
42 for (int i = 0;; i++) {
43 FilePath f = getBase(i);
44 if (f.exists()) {
45 result = f.setReadOnly();
46 } else {
47 break;
48 }
49 }
50 return result;
51 }
52
53 @Override
54 public void delete() {
55 for (int i = 0;; i++) {
56 FilePath f = getBase(i);
57 if (f.exists()) {
58 f.delete();
59 } else {
60 break;
61 }
62 }
63 }
64
65 @Override
66 public long lastModified() {
67 long lastModified = 0;
68 for (int i = 0;; i++) {
69 FilePath f = getBase(i);
70 if (f.exists()) {
71 long l = f.lastModified();
72 lastModified = Math.max(lastModified, l);
73 } else {
74 break;
75 }
76 }
77 return lastModified;
78 }
79
80 @Override
81 public long size() {
82 long length = 0;
83 for (int i = 0;; i++) {
84 FilePath f = getBase(i);
85 if (f.exists()) {
86 length += f.size();
87 } else {
88 break;
89 }
90 }
91 return length;
92 }
93
94 @Override
95 public ArrayList<FilePath> newDirectoryStream() {
96 List<FilePath> list = getBase().newDirectoryStream();
97 ArrayList<FilePath> newList = new ArrayList<>();
98 for (FilePath f : list) {
99 if (!f.getName().endsWith(PART_SUFFIX)) {
100 newList.add(wrap(f));
101 }
102 }
103 return newList;
104 }
105
106 @Override
107 public InputStream newInputStream() throws IOException {
108 InputStream input = getBase().newInputStream();
109 for (int i = 1;; i++) {
110 FilePath f = getBase(i);
111 if (f.exists()) {
112 InputStream i2 = f.newInputStream();
113 input = new SequenceInputStream(input, i2);
114 } else {
115 break;
116 }
117 }
118 return input;
119 }
120
121 @Override
122 public FileChannel open(String mode) throws IOException {
123 ArrayList<FileChannel> list = new ArrayList<>();
124 list.add(getBase().open(mode));
125 for (int i = 1;; i++) {
126 FilePath f = getBase(i);
127 if (f.exists()) {
128 list.add(f.open(mode));
129 } else {
130 break;
131 }
132 }
133 FileChannel[] array = list.toArray(new FileChannel[0]);
134 long maxLength = array[0].size();
135 long length = maxLength;
136 if (array.length == 1) {
137 long defaultMaxLength = getDefaultMaxLength();
138 if (maxLength < defaultMaxLength) {
139 maxLength = defaultMaxLength;
140 }
141 } else {
142 if (maxLength == 0) {
143 closeAndThrow(0, array, array[0], maxLength);
144 }
145 for (int i = 1; i < array.length - 1; i++) {
146 FileChannel c = array[i];
147 long l = c.size();
148 length += l;
149 if (l != maxLength) {
150 closeAndThrow(i, array, c, maxLength);
151 }
152 }
153 FileChannel c = array[array.length - 1];
154 long l = c.size();
155 length += l;
156 if (l > maxLength) {
157 closeAndThrow(array.length - 1, array, c, maxLength);
158 }
159 }
160 return new FileSplit(this, mode, array, length, maxLength);
161 }
162
163 private long getDefaultMaxLength() {
164 return 1L << Integer.decode(parse(name)[0]);
165 }
166
167 private void closeAndThrow(int id, FileChannel[] array, FileChannel o,
168 long maxLength) throws IOException {
169 String message = "Expected file length: " + maxLength + " got: " +
170 o.size() + " for " + getName(id);
171 for (FileChannel f : array) {
172 f.close();
173 }
174 throw new IOException(message);
175 }
176
177 @Override
178 public OutputStream newOutputStream(boolean append) throws IOException {
179 return newFileChannelOutputStream(open("rw"), append);
180 }
181
182 @Override
183 public void moveTo(FilePath path, boolean atomicReplace) {
184 FilePathSplit newName = (FilePathSplit) path;
185 for (int i = 0;; i++) {
186 FilePath o = getBase(i);
187 if (o.exists()) {
188 o.moveTo(newName.getBase(i), atomicReplace);
189 } else if (newName.getBase(i).exists()) {
190 newName.getBase(i).delete();
191 } else {
192 break;
193 }
194 }
195 }
196
197 /**
198 * Split the file name into size and base file name.
199 *
200 * @param fileName the file name
201 * @return an array with size and file name
202 */
203 private String[] parse(String fileName) {
204 if (!fileName.startsWith(getScheme())) {
205 throw DbException.getInternalError(fileName + " doesn't start with " + getScheme());
206 }
207 fileName = fileName.substring(getScheme().length() + 1);
208 String size;
209 if (fileName.length() > 0 && Character.isDigit(fileName.charAt(0))) {
210 int idx = fileName.indexOf(':');
211 size = fileName.substring(0, idx);
212 try {
213 fileName = fileName.substring(idx + 1);
214 } catch (NumberFormatException e) {
215 // ignore
216 }
217 } else {
218 size = Long.toString(SysProperties.SPLIT_FILE_SIZE_SHIFT);
219 }
220 return new String[] { size, fileName };
221 }
222
223 /**
224 * Get the file name of a part file.
225 *
226 * @param id the part id
227 * @return the file name including the part id
228 */
229 FilePath getBase(int id) {
230 return FilePath.get(getName(id));
231 }
232
233 private String getName(int id) {
234 return id > 0 ? getBase().name + "." + id + PART_SUFFIX : getBase().name;
235 }
236
237 @Override
238 public String getScheme() {
239 return "split";
240 }
241
242 }