/
MappedBytes.java
323 lines (297 loc) · 13.7 KB
/
MappedBytes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
* Copyright (c) 2016-2022 chronicle.software
*
* https://chronicle.software
*
* 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 net.openhft.chronicle.bytes;
import net.openhft.chronicle.bytes.internal.ChunkedMappedBytes;
import net.openhft.chronicle.bytes.internal.SingleMappedBytes;
import net.openhft.chronicle.core.Jvm;
import net.openhft.chronicle.core.OS;
import net.openhft.chronicle.core.annotation.NonNegative;
import net.openhft.chronicle.core.io.*;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.FileNotFoundException;
/**
* A specialized implementation of {@link AbstractBytes} that wraps memory-mapped data for efficient random file access.
*
* <p>Memory is grouped in chunks of 64 MB by default. The chunk size can be significantly increased if the
* OS supports sparse files via the {@link OS#isSparseFileSupported()} method, e.g. {@code blockSize(512 << 30)}.
*
* <p>Only the most recently accessed memory chunk is reserved, and the previous chunk is released. For random access,
* a chunk can be manually reserved by obtaining the bytesStore() and using reserve(owner) on it.
* However, it is crucial to call release(owner) on the same BytesStore before closing the file to avoid memory leaks.
*
* <p>Several factory methods are provided to create different types of MappedBytes, including single mapped bytes
* and chunked mapped bytes, with optional settings for read-only mode and chunk overlap size.
*
* <p>Note: MappedBytes, like all Bytes, are single-threaded. Also, it is recommended to ensure the mapped file
* is reserved before using MappedBytes.
*
* @see BytesStore
* @see MappedFile
* @see AbstractBytes
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public abstract class MappedBytes extends AbstractBytes<Void> implements Closeable, ManagedCloseable, Syncable {
protected static final boolean TRACE = Jvm.getBoolean("trace.mapped.bytes");
// assume the mapped file is reserved already.
protected MappedBytes()
throws ClosedIllegalStateException, ThreadingIllegalStateException {
this("");
}
protected MappedBytes(final String name)
throws ClosedIllegalStateException, ThreadingIllegalStateException {
super(BytesStore.empty(),
BytesStore.empty().writePosition(),
BytesStore.empty().writeLimit(),
name);
}
/**
* Creates a MappedBytes instance that wraps a single memory-mapped file.
*
* @param filename The name of the file to be memory-mapped.
* @param capacity The maximum number of bytes that can be read from or written to the mapped file.
* @return A new MappedBytes instance.
* @throws FileNotFoundException If the file does not exist.
* @throws ClosedIllegalStateException If the resource has been released or closed.
* @throws ThreadingIllegalStateException If this resource was accessed by multiple threads in an unsafe way
*/
@NotNull
public static MappedBytes singleMappedBytes(@NotNull final String filename, @NonNegative final long capacity)
throws FileNotFoundException, IllegalStateException {
return singleMappedBytes(new File(filename), capacity);
}
/**
* Creates a MappedBytes instance that wraps a single memory-mapped file.
*
* @param file The name of the file to be memory-mapped.
* @param capacity The maximum number of bytes that can be read from or written to the mapped file.
* @return A new MappedBytes instance.
* @throws FileNotFoundException If the file does not exist.
*/
@NotNull
public static MappedBytes singleMappedBytes(@NotNull final File file, @NonNegative final long capacity)
throws FileNotFoundException {
return singleMappedBytes(file, capacity, false);
}
/**
* Creates a MappedBytes instance that wraps a single memory-mapped file.
*
* @param file The name of the file to be memory-mapped.
* @param capacity The maximum number of bytes that can be read from or written to the mapped file.
* @param readOnly read only is true, read-write if false
* @return A new MappedBytes instance.
* @throws FileNotFoundException If the file does not exist.
*/
@NotNull
public static MappedBytes singleMappedBytes(@NotNull File file, @NonNegative long capacity, boolean readOnly)
throws FileNotFoundException {
final MappedFile rw = MappedFile.ofSingle(file, capacity, readOnly);
try {
return new SingleMappedBytes(rw);
} finally {
rw.release(INIT);
}
}
/**
* Creates a MappedBytes instance that wraps a memory-mapped file divided into chunks of a specified size.
*
* @param filename The name of the file to be memory-mapped.
* @param chunkSize The size of each chunk in bytes.
* @return A new MappedBytes instance.
* @throws FileNotFoundException If the file does not exist.
* @throws ClosedIllegalStateException If the resource has been released or closed.
* @throws ThreadingIllegalStateException If this resource was accessed by multiple threads in an unsafe way
*/
@NotNull
public static MappedBytes mappedBytes(@NotNull final String filename, @NonNegative final long chunkSize)
throws FileNotFoundException, ClosedIllegalStateException {
return mappedBytes(new File(filename), chunkSize);
}
/**
* Creates a MappedBytes instance that wraps a memory-mapped file divided into chunks of a specified size.
*
* @param file The name of the file to be memory-mapped.
* @param chunkSize The size of each chunk in bytes.
* @return A new MappedBytes instance.
* @throws FileNotFoundException If the file does not exist.
* @throws ClosedIllegalStateException If the resource has been released or closed.
* @throws ThreadingIllegalStateException If this resource was accessed by multiple threads in an unsafe way
*/
@NotNull
public static MappedBytes mappedBytes(@NotNull final File file, @NonNegative final long chunkSize)
throws FileNotFoundException, ClosedIllegalStateException {
return mappedBytes(file, chunkSize, OS.pageSize());
}
/**
* Creates a MappedBytes instance that wraps a memory-mapped file divided into chunks of a specified size.
*
* @param file The name of the file to be memory-mapped.
* @param chunkSize The size of each chunk in bytes.
* @param overlapSize The size of overlap of chunks in bytes.
* @return A new MappedBytes instance.
* @throws FileNotFoundException If the file does not exist.
* @throws ClosedIllegalStateException If the resource has been released or closed.
* @throws ThreadingIllegalStateException If this resource was accessed by multiple threads in an unsafe way
*/
@NotNull
public static MappedBytes mappedBytes(@NotNull final File file, @NonNegative final long chunkSize, @NonNegative final long overlapSize)
throws FileNotFoundException, ClosedIllegalStateException {
final MappedFile rw = MappedFile.of(file, chunkSize, overlapSize, false);
try {
return mappedBytes(rw);
} finally {
rw.release(INIT);
}
}
/**
* Creates a MappedBytes instance that wraps a memory-mapped file divided into chunks of a specified size.
*
* @param file The name of the file to be memory-mapped.
* @param chunkSize The size of each chunk in bytes.
* @param overlapSize The size of overlap of chunks in bytes.
* @param pageSize The custom page size in bytes.
* @param readOnly read only is true, read-write if false
* @return A new MappedBytes instance.
* @throws FileNotFoundException If the file does not exist.
* @throws ClosedIllegalStateException If the resource has been released or closed.
* @throws ThreadingIllegalStateException If this resource was accessed by multiple threads in an unsafe way
*/
@NotNull
public static MappedBytes mappedBytes(@NotNull final File file,
@NonNegative final long chunkSize,
@NonNegative final long overlapSize,
@NonNegative final int pageSize,
final boolean readOnly)
throws FileNotFoundException, ClosedIllegalStateException {
final MappedFile rw = MappedFile.of(file, chunkSize, overlapSize, pageSize, readOnly);
try {
return mappedBytes(rw);
} finally {
rw.release(INIT);
}
}
/**
* @see #mappedBytes(File, long, long, int, boolean)
*/
@NotNull
public static MappedBytes mappedBytes(@NotNull final File file,
@NonNegative final long chunkSize,
@NonNegative final long overlapSize,
final boolean readOnly)
throws FileNotFoundException, ClosedIllegalStateException {
return mappedBytes(file, chunkSize, overlapSize, PageUtil.getPageSize(file.getAbsolutePath()), readOnly);
}
/**
* Create a MappedBytes for a MappedFile
*
* @param rw MappedFile to use
* @return the MappedBytes
* @throws ClosedIllegalStateException If the resource has been released or closed.
* @throws ThreadingIllegalStateException If this resource was accessed by multiple threads in an unsafe way.
*/
@NotNull
public static MappedBytes mappedBytes(@NotNull final MappedFile rw)
throws ClosedIllegalStateException {
return rw.createBytesFor();
}
/**
* Creates a MappedBytes instance that wraps a read-only memory-mapped file.
*
* @param file The file to be memory-mapped in read-only mode.
* @return A new MappedBytes instance.
* @throws FileNotFoundException If the file does not exist.
* @throws ClosedIllegalStateException If the resource has been released or closed.
* @throws ThreadingIllegalStateException If this resource was accessed by multiple threads in an unsafe way
*/
@NotNull
public static MappedBytes readOnly(@NotNull final File file)
throws FileNotFoundException, ClosedIllegalStateException, ThreadingIllegalStateException {
final MappedFile mappedFile = MappedFile.readOnly(file);
try {
return new ChunkedMappedBytes(mappedFile);
} finally {
mappedFile.release(INIT);
}
}
/**
* Checks if the backing file is read-only.
*
* @return true if the backing file is read-only, false otherwise.
*/
public abstract boolean isBackingFileReadOnly();
/**
* Checks if the bytes are stored in shared memory.
*
* @return true if the bytes are stored in shared memory, false otherwise.
*/
@Override
public boolean sharedMemory() {
return true;
}
/**
* Updates the number of chunks in the mapped file.
*
* @param chunkCount The new number of chunks.
*/
public abstract void chunkCount(long[] chunkCount);
/**
* Retrieves the mapped file.
*
* @return the MappedFile instance.
*/
public abstract MappedFile mappedFile();
/**
* Ensures that any modifications to this MappedBytes instance are written to the storage device containing the mapped file.
*/
@Override
public void sync() {
final BytesStore bs = bytesStore;
if (bs instanceof MappedBytesStore) {
MappedBytesStore mbs = (MappedBytesStore) bs;
mbs.syncUpTo(writePosition());
}
}
/**
* Provides a bytes object for read operations. This object is backed by the current MappedBytes instance.
*
* @return a Bytes instance for read operations.
* @throws ClosedIllegalStateException If the resource has been released or closed.
* @throws ThreadingIllegalStateException If this resource was accessed by multiple threads in an unsafe way.
*/
@Override
public @NotNull Bytes<Void> bytesForRead() throws ClosedIllegalStateException {
throwExceptionIfReleased();
// MappedBytes don't have a backing BytesStore so we have to give out bytesForRead|Write backed by this
return isClear()
? new VanillaBytes(this, writePosition(), bytesStore.writeLimit())
: new SubBytes<>(this, readPosition(), readLimit() + start());
}
/**
* Provides a bytes object for write operations. This object is backed by the current MappedBytes instance.
*
* @return a Bytes instance for write operations.
* @throws ClosedIllegalStateException If the resource has been released or closed.
* @throws ThreadingIllegalStateException If this resource was accessed by multiple threads in an unsafe way.
*/
@Override
public @NotNull Bytes<Void> bytesForWrite() throws ClosedIllegalStateException {
throwExceptionIfReleased();
// MappedBytes don't have a backing BytesStore so we have to give out bytesForRead|Write backed by this
return new VanillaBytes(this, writePosition(), writeLimit());
}
}