@@ -49,7 +49,6 @@ abstract class Entry {
49
49
50
50
private final int VALUE_SIZE ;
51
51
final int ENTRY_SIZE ;
52
- private File tempFile ;
53
52
long fileSize ;
54
53
private long keys ;
55
54
final int KEY_SIZE ;
@@ -70,27 +69,15 @@ abstract class Entry {
70
69
VALUE_SIZE = valueSize ;
71
70
ENTRY_SIZE = KEY_SIZE + VALUE_SIZE ;
72
71
fileSize = keys * ENTRY_SIZE ;
73
- tempFile = cacheDir .createTempFile ("NBProfiler" , ".map" ); // NOI18N
74
-
75
- RandomAccessFile file = new RandomAccessFile (tempFile , "rw" ); // NOI18N
76
- if (Boolean .getBoolean ("org.graalvm.visualvm.lib.jfluid.heap.zerofile" )) { // NOI18N
77
- byte [] zeros = new byte [512 *1024 ];
78
- while (file .length ()<fileSize ) {
79
- file .write (zeros );
80
- }
81
- file .write (zeros ,0 ,(int )(fileSize -file .length ()));
82
- }
83
- file .setLength (fileSize );
84
- setDumpBuffer (file );
85
- file .close ();
86
72
cacheDirectory = cacheDir ;
73
+ dumpBuffer = cacheDir .createDumpBuffer (fileSize , ENTRY_SIZE );
87
74
}
88
75
89
76
//~ Methods ------------------------------------------------------------------------------------------------------------------
90
77
91
78
protected void finalize () throws Throwable {
92
79
if (cacheDirectory .isTemporary ()) {
93
- tempFile . delete ();
80
+ dumpBuffer . deleteFile ();
94
81
}
95
82
super .finalize ();
96
83
}
@@ -129,18 +116,18 @@ Entry put(long key, long value) {
129
116
}
130
117
}
131
118
132
- private void setDumpBuffer ( RandomAccessFile file ) throws IOException {
119
+ static Data getDumpBuffer ( File f , RandomAccessFile file , int entrySize ) throws IOException {
133
120
long length = file .length ();
134
121
135
122
try {
136
123
if (length > Integer .MAX_VALUE ) {
137
- dumpBuffer = new LongMemoryMappedData (file , length , ENTRY_SIZE );
124
+ return new LongMemoryMappedData (f , file , length , entrySize );
138
125
} else {
139
- dumpBuffer = new MemoryMappedData (file , length );
126
+ return new MemoryMappedData (f , file , length );
140
127
}
141
128
} catch (IOException ex ) {
142
129
if (ex .getCause () instanceof OutOfMemoryError ) {
143
- dumpBuffer = new FileData (file , length );
130
+ return new FileData (f , file , length , entrySize );
144
131
} else {
145
132
throw ex ;
146
133
}
@@ -183,23 +170,19 @@ void writeToStream(DataOutputStream out) throws IOException {
183
170
out .writeInt (ID_SIZE );
184
171
out .writeInt (FOFFSET_SIZE );
185
172
out .writeInt (VALUE_SIZE );
186
- out .writeUTF (tempFile .getAbsolutePath ());
187
- dumpBuffer .force (tempFile );
173
+ dumpBuffer .writeToStream (out );
188
174
}
189
175
190
176
AbstractLongMap (DataInputStream dis , CacheDirectory cacheDir ) throws IOException {
191
177
keys = dis .readLong ();
192
178
ID_SIZE = dis .readInt ();
193
179
FOFFSET_SIZE = dis .readInt ();
194
180
VALUE_SIZE = dis .readInt ();
195
- tempFile = cacheDir .getCacheFile (dis .readUTF ());
196
181
197
182
KEY_SIZE = ID_SIZE ;
198
183
ENTRY_SIZE = KEY_SIZE + VALUE_SIZE ;
199
184
fileSize = keys * ENTRY_SIZE ;
200
- RandomAccessFile file = new RandomAccessFile (tempFile , "rw" ); // NOI18N
201
- setDumpBuffer (file );
202
- file .close ();
185
+ dumpBuffer = Data .readFromStream (dis , cacheDir , ENTRY_SIZE );
203
186
cacheDirectory = cacheDir ;
204
187
}
205
188
@@ -228,6 +211,13 @@ private static boolean isLinux() {
228
211
229
212
interface Data {
230
213
//~ Methods --------------------------------------------------------------------------------------------------------------
214
+ static Data readFromStream (DataInputStream dis , CacheDirectory cacheDir , int entrySize ) throws IOException {
215
+ File tempFile = cacheDir .getCacheFile (dis .readUTF ());
216
+ RandomAccessFile file = new RandomAccessFile (tempFile , "rw" ); // NOI18N
217
+ Data dumpBuffer = getDumpBuffer (tempFile , file , entrySize );
218
+ file .close ();
219
+ return dumpBuffer ;
220
+ }
231
221
232
222
byte getByte (long index );
233
223
@@ -241,23 +231,52 @@ interface Data {
241
231
242
232
void putLong (long index , long data );
243
233
244
- void force (File bufferFile ) throws IOException ;
234
+ void force () throws IOException ;
235
+
236
+ void writeToStream (DataOutputStream out ) throws IOException ;
237
+
238
+ void deleteFile ();
245
239
}
246
240
247
- private class FileData implements Data {
241
+ private static abstract class AbstractData implements Data {
242
+
243
+ File bufferFile ;
244
+
245
+ private AbstractData (File file ) {
246
+ bufferFile = file ;
247
+ }
248
+
249
+ //---- Serialization support
250
+ public void writeToStream (DataOutputStream out ) throws IOException {
251
+ out .writeUTF (bufferFile .getAbsolutePath ());
252
+ force ();
253
+ }
254
+
255
+ public void deleteFile () {
256
+ bufferFile .delete ();
257
+ }
258
+
259
+ }
260
+
261
+ private static class FileData extends AbstractData {
248
262
//~ Instance fields ------------------------------------------------------------------------------------------------------
249
263
250
264
RandomAccessFile file ;
251
265
byte [] buf ;
252
266
boolean bufferModified ;
253
267
long offset ;
268
+ int entrySize ;
269
+ long fileSize ;
254
270
final static int BUFFER_SIZE = 128 ;
255
271
256
272
//~ Constructors ---------------------------------------------------------------------------------------------------------
257
273
258
- FileData (RandomAccessFile f , long length ) throws IOException {
274
+ FileData (File fl , RandomAccessFile f , long length , int entry ) throws IOException {
275
+ super (fl );
259
276
file = f ;
260
- buf = new byte [ENTRY_SIZE *BUFFER_SIZE ];
277
+ fileSize = length ;
278
+ entrySize = entry ;
279
+ buf = new byte [entrySize *BUFFER_SIZE ];
261
280
}
262
281
263
282
//~ Methods --------------------------------------------------------------------------------------------------------------
@@ -318,7 +337,7 @@ public synchronized void putLong(long index, long data) {
318
337
}
319
338
320
339
private int loadBufferIfNeeded (long index ) {
321
- int i = (int ) (index % (ENTRY_SIZE * BUFFER_SIZE ));
340
+ int i = (int ) (index % (entrySize * BUFFER_SIZE ));
322
341
long newOffset = index - i ;
323
342
324
343
if (offset != newOffset ) {
@@ -354,12 +373,12 @@ private void flush() throws IOException {
354
373
}
355
374
356
375
@ Override
357
- public void force (File bufferFile ) throws IOException {
376
+ public void force () throws IOException {
358
377
flush ();
359
378
}
360
379
}
361
380
362
- private static class MemoryMappedData implements Data {
381
+ private static class MemoryMappedData extends AbstractData {
363
382
364
383
private static final FileChannel .MapMode MAP_MODE = isLinux () ? FileChannel .MapMode .PRIVATE : FileChannel .MapMode .READ_WRITE ;
365
384
@@ -369,8 +388,9 @@ private static class MemoryMappedData implements Data {
369
388
370
389
//~ Constructors ---------------------------------------------------------------------------------------------------------
371
390
372
- MemoryMappedData (RandomAccessFile file , long length )
391
+ MemoryMappedData (File f , RandomAccessFile file , long length )
373
392
throws IOException {
393
+ super (f );
374
394
buf = createBuffer (file , length );
375
395
}
376
396
@@ -401,7 +421,7 @@ public void putLong(long index, long data) {
401
421
}
402
422
403
423
@ Override
404
- public void force (File bufferFile ) throws IOException {
424
+ public void force () throws IOException {
405
425
if (MAP_MODE == FileChannel .MapMode .PRIVATE ) {
406
426
File newBufferFile = new File (bufferFile .getAbsolutePath ()+".new" ); // NOI18N
407
427
int length = buf .capacity ();
@@ -423,7 +443,7 @@ private static MappedByteBuffer createBuffer(RandomAccessFile file, long length)
423
443
}
424
444
}
425
445
426
- private static class LongMemoryMappedData implements Data {
446
+ private static class LongMemoryMappedData extends AbstractData {
427
447
428
448
private static int BUFFER_SIZE_BITS = 30 ;
429
449
private static long BUFFER_SIZE = 1L << BUFFER_SIZE_BITS ;
@@ -438,8 +458,9 @@ private static class LongMemoryMappedData implements Data {
438
458
439
459
//~ Constructors ---------------------------------------------------------------------------------------------------------
440
460
441
- LongMemoryMappedData (RandomAccessFile file , long length , int entry )
461
+ LongMemoryMappedData (File f , RandomAccessFile file , long length , int entry )
442
462
throws IOException {
463
+ super (f );
443
464
dumpBuffer = createBuffers (file , length );
444
465
entrySize = entry ;
445
466
}
@@ -479,7 +500,7 @@ private int getBufferOffset(long index) {
479
500
}
480
501
481
502
@ Override
482
- public void force (File bufferFile ) throws IOException {
503
+ public void force () throws IOException {
483
504
if (MemoryMappedData .MAP_MODE == FileChannel .MapMode .PRIVATE ) {
484
505
File newBufferFile = new File (bufferFile .getAbsolutePath ()+".new" ); // NOI18N
485
506
long length = bufferFile .length ();
0 commit comments