Debian Codegraph @

src/main/org/h2/store/fs/split/FileSplit.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.nio.ByteBuffer;
10 import java.nio.channels.FileChannel;
11 import java.nio.channels.FileLock;
12 import org.h2.message.DbException;
13 import org.h2.mvstore.DataUtils;
14 import org.h2.store.fs.FileBaseDefault;
15 import org.h2.store.fs.FilePath;
16
17 /**
18 * A file that may be split into multiple smaller files.
19 */
20 class FileSplit extends FileBaseDefault {
21
22 private final FilePathSplit filePath;
23 private final String mode;
24 private final long maxLength;
25 private FileChannel[] list;
26 private volatile long length;
27
28 FileSplit(FilePathSplit file, String mode, FileChannel[] list, long length,
29 long maxLength) {
30 this.filePath = file;
31 this.mode = mode;
32 this.list = list;
33 this.length = length;
34 this.maxLength = maxLength;
35 }
36
37 @Override
38 public synchronized void implCloseChannel() throws IOException {
39 for (FileChannel c : list) {
40 c.close();
41 }
42 }
43
44 @Override
45 public long size() {
46 return length;
47 }
48
49 @Override
50 public synchronized int read(ByteBuffer dst, long position)
51 throws IOException {
52 int len = dst.remaining();
53 if (len == 0) {
54 return 0;
55 }
56 len = (int) Math.min(len, length - position);
57 if (len <= 0) {
58 return -1;
59 }
60 long offset = position % maxLength;
61 len = (int) Math.min(len, maxLength - offset);
62 FileChannel channel = getFileChannel(position);
63 return channel.read(dst, offset);
64 }
65
66 private FileChannel getFileChannel(long position) throws IOException {
67 int id = (int) (position / maxLength);
68 while (id >= list.length) {
69 int i = list.length;
70 FileChannel[] newList = new FileChannel[i + 1];
71 System.arraycopy(list, 0, newList, 0, i);
72 FilePath f = filePath.getBase(i);
73 newList[i] = f.open(mode);
74 list = newList;
75 }
76 return list[id];
77 }
78
79 @Override
80 protected void implTruncate(long newLength) throws IOException {
81 if (newLength >= length) {
82 return;
83 }
84 int newFileCount = 1 + (int) (newLength / maxLength);
85 if (newFileCount < list.length) {
86 // delete some of the files
87 FileChannel[] newList = new FileChannel[newFileCount];
88 // delete backwards, so that truncating is somewhat transactional
89 for (int i = list.length - 1; i >= newFileCount; i--) {
90 // verify the file is writable
91 list[i].truncate(0);
92 list[i].close();
93 try {
94 filePath.getBase(i).delete();
95 } catch (DbException e) {
96 throw DataUtils.convertToIOException(e);
97 }
98 }
99 System.arraycopy(list, 0, newList, 0, newList.length);
100 list = newList;
101 }
102 long size = newLength - maxLength * (newFileCount - 1);
103 list[list.length - 1].truncate(size);
104 this.length = newLength;
105 }
106
107 @Override
108 public synchronized void force(boolean metaData) throws IOException {
109 for (FileChannel c : list) {
110 c.force(metaData);
111 }
112 }
113
114 @Override
115 public synchronized int write(ByteBuffer src, long position) throws IOException {
116 if (position >= length && position > maxLength) {
117 // may need to extend and create files
118 long oldFilePointer = position;
119 long x = length - (length % maxLength) + maxLength;
120 for (; x < position; x += maxLength) {
121 if (x > length) {
122 // expand the file size
123 position(x - 1);
124 write(ByteBuffer.wrap(new byte[1]));
125 }
126 position = oldFilePointer;
127 }
128 }
129 long offset = position % maxLength;
130 int len = src.remaining();
131 FileChannel channel = getFileChannel(position);
132 int l = (int) Math.min(len, maxLength - offset);
133 if (l == len) {
134 l = channel.write(src, offset);
135 } else {
136 int oldLimit = src.limit();
137 src.limit(src.position() + l);
138 l = channel.write(src, offset);
139 src.limit(oldLimit);
140 }
141 length = Math.max(length, position + l);
142 return l;
143 }
144
145 @Override
146 public synchronized FileLock tryLock(long position, long size,
147 boolean shared) throws IOException {
148 return list[0].tryLock(position, size, shared);
149 }
150
151 @Override
152 public String toString() {
153 return filePath.toString();
154 }
155
156 }