forked from MinecraftTAS/BigArrayList
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheMapping.java
More file actions
502 lines (426 loc) · 11.7 KB
/
CacheMapping.java
File metadata and controls
502 lines (426 loc) · 11.7 KB
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
* BigArrayList
* Copyright (C) 2015 Douglas Selent
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.dselent.bigarraylist;
import java.io.IOException;
import java.io.Serializable;
/**
* Class that manages the mapping from files on disk to elements in memory for the BigArrayList class.
* Uses an LRU policy at the cache block level to determine which cache block should be swapped out next.
*
* @author Douglas Selent
*
* @param <E> Generic type
*/
class CacheMapping<E extends Serializable>
{
/**
* Array storing the next spot to add to for each cache
*/
private int[] cacheTableSpots;
/**
* The file number each ArrayList/Cache block is currently storing
* Index is the same as the ArrayList index
*/
private int[] cacheTableFiles;
/**
* Array of when each block was last used
* Stores a list of block numbers sorted from least recently used to most recently used
* most recent = end of array
*/
private int[] mostRecentlyUsedList;
/**
* Array for each cache block for whether or not it's data has changed
* If clean, then it does not need to be written to disk when swapped out
* If dirty, then it does need to be written to disk when swapped out
*/
private boolean[] dirtyBits;
/**
* Reference to the associated BigArrayList object
*/
private BigArrayList<E> bigArrayList;
/**
* Reference to the associated FileAccessor object
*/
private FileAccessor<E> fileAccessor;
/**
* Constructs a CacheMapping object for the BigArrayList with the following parameters
*
* @param blockSize Size of each cache block
* @param cacheBlocks Number of cache blocks
* @param theList Associated BigArrayList
* @param folderPath The folder path to store contents on disk
*/
protected CacheMapping(BigArrayList<E> theList, int blockSize, int cacheBlocks, String folderPath)
{
cacheTableSpots = new int[cacheBlocks];
cacheTableFiles = new int[cacheBlocks];
mostRecentlyUsedList = new int[cacheBlocks];
dirtyBits = new boolean[cacheBlocks];
for(int i=0; i<cacheBlocks; i++)
{
cacheTableSpots[i] = 0;
cacheTableFiles[i] = -1;
mostRecentlyUsedList[i] = -1;
dirtyBits[i] = false;
}
bigArrayList = theList;
fileAccessor = new FileAccessor<>(folderPath);
}
/**
* Constructs a CacheMapping object for the BigArrayList with the following parameters
*
* @param blockSize Size of each cache block
* @param cacheBlocks Number of cache blocks
* @param theList Associated BigArrayList
*/
protected CacheMapping(BigArrayList<E> theList, int blockSize, int cacheBlocks)
{
cacheTableSpots = new int[cacheBlocks];
cacheTableFiles = new int[cacheBlocks];
mostRecentlyUsedList = new int[cacheBlocks];
dirtyBits = new boolean[cacheBlocks];
for(int i=0; i<cacheBlocks; i++)
{
cacheTableSpots[i] = 0;
cacheTableFiles[i] = -1;
mostRecentlyUsedList[i] = -1;
dirtyBits[i] = false;
}
bigArrayList = theList;
fileAccessor = new FileAccessor<>();
}
////////////////////////////////////////////////////////////////////////////////////////////////
/**
* @return Returns the fileAccessor object
*/
protected FileAccessor<E> getFileAccessor()
{
return fileAccessor;
}
/**
* Sets the index to add the next element to for the given cache block
*
* @param cacheBlockIndex The index for the cache block
* @param indexToAdd The next index to add to for the specified cache block
*/
private void setCacheTableSpots(int cacheBlockIndex, int indexToAdd)
{
cacheTableSpots[cacheBlockIndex] = indexToAdd;
}
/**
* Sets the mapping for cache blocks that are in memory to the file number on disk
*
* @param index The index of the cache block
* @param fileNumber The file number
*/
private void setCacheTableFiles(int index, int fileNumber)
{
cacheTableFiles[index] = fileNumber;
}
/**
* @param block The cache block
* @return Returns if the cache block is full or not
*/
protected boolean isCacheFull(int block)
{
boolean full = false;
if(cacheTableSpots[block] >= bigArrayList.getBlockSize())
{
full = true;
}
return full;
}
/**
* Sets the dirty bit for the given cache block index
*
* @param blockIndex Index of the cache block
* @param dirty Whether or not the block of cache is dirty
*/
protected void setDirtyBit(int blockIndex, boolean dirty)
{
dirtyBits[blockIndex] = dirty;
}
/**
* Called by the add method of BigArrayList
* Updates meta data associated with adding an element
*
* @param cacheBlockIndex Index of the cache block
*/
protected void addEntry(int cacheBlockIndex)
{
cacheTableSpots[cacheBlockIndex]++;
updateUsedList(cacheBlockIndex);
}
/**
* Called by the remove method of BigArrayList
* Updates meta data associated with removing an element
*
* @param cacheBlockIndex Index of the cache block
*/
protected void removeEntry(int cacheBlockIndex)
{
cacheTableSpots[cacheBlockIndex]--;
updateUsedList(cacheBlockIndex);
}
/**
*
* @param index The index of the element
* @return Returns the file number where the element at this index would be
*/
protected int getFileNumber(long index)
{
long blockSizeLong = bigArrayList.getBlockSize();
long longFileNumber = index / blockSizeLong;
//safe cast, I really doubt there will ever be over 2^31 - 1 files
return (int)longFileNumber;
}
/**
* Returns the last element index for the given file number
*
* @param fileNumber Number of the file
* @return Last element index in the file
*/
protected long getLastIndexInFile(int fileNumber)
{
long blockSizeLong = bigArrayList.getBlockSize();
long fileNumberLong = fileNumber;
long index = (blockSizeLong * fileNumberLong) + blockSizeLong - 1;
return index;
}
/**
* Returns the cacheTableFiles spot where the current file/cache block is being held
*
* @param fileNumber The file number
* @return Returns the cacheTableFiles spot where the current file/cache block is being held
*/
protected int getCacheBlockSpot(int fileNumber)
{
int blockSpot = -1;
for(int i=0; i<cacheTableFiles.length && blockSpot == -1; i++)
{
if(cacheTableFiles[i] == fileNumber)
{
blockSpot = i;
}
}
return blockSpot;
}
/**
* Returns the spot in cache where this element would be
*
* @param index The element index
* @return Returns the spot in cache where this element would be
*/
protected int getSpotInCache(long index)
{
long longTableSize = bigArrayList.getBlockSize();
long spotInFile = index % longTableSize;
//safe cast, cannot be > 2^31 - 1 elements in an ArrayList
return (int)spotInFile;
}
/**
* Returns if the file is in cache or not
*
* @param fileNumber The file number index
* @return Returns true if the contents of the file are in cache and false otherwise
*/
protected boolean isFileInCache(int fileNumber)
{
boolean inCache = false;
for(int i=0; i<cacheTableFiles.length && !inCache; i++)
{
if(cacheTableFiles[i] == fileNumber)
{
inCache = true;
}
}
return inCache;
}
/**
* Returns the first open location to swap a cache block into or -1 if there are no open spots
*
* @return Returns the first open location to swap a cache block into or -1 if there are no open spots
*/
protected int getFirstOpenCacheBlock()
{
int firstOpen = -1;
for(int i=0; i<cacheTableFiles.length && firstOpen == -1; i++)
{
if(cacheTableFiles[i] == -1)
{
firstOpen = i;
}
}
return firstOpen;
}
/**
* Updates the list of most recently used blocks
*
* @param blockNumber Block/File number that was just used
*/
protected void updateUsedList(int blockNumber)
{
int oldPosition = -1;
int newPosition = mostRecentlyUsedList.length - 1;
int shiftPosition = 0;
//find old position if exists
for(int i=0; i<mostRecentlyUsedList.length; i++)
{
if(mostRecentlyUsedList[i] == blockNumber)
{
oldPosition = i;
}
}
//set old spot to -1, clear it out
if(oldPosition != -1)
{
mostRecentlyUsedList[oldPosition] = -1;
}
//find spot to shift to
//if open spaces, find first open one
for(int i=0; i<mostRecentlyUsedList.length; i++)
{
if(mostRecentlyUsedList[i] == -1)
{
shiftPosition = i;
}
}
//shift down
for(int i=shiftPosition; i<mostRecentlyUsedList.length-1; i++)
{
mostRecentlyUsedList[i] = mostRecentlyUsedList[i+1];
}
mostRecentlyUsedList[newPosition] = blockNumber;
}
/**
* @param blockIndex Index to remove from the list
*/
private void removeFromUsedList(int blockIndex)
{
for(int i=0; i<mostRecentlyUsedList.length; i++)
{
if(mostRecentlyUsedList[i] == blockIndex)
{
mostRecentlyUsedList[i] = -1;
}
}
}
/**
* Flushes all data in memory to disk
*/
protected void flushCache()
{
for(int i=0; i<cacheTableFiles.length; i++)
{
flushCacheBlock(i);
}
}
/**
* Flushes a single cache block to disk
*
* @param blockIndex The index of the cache block
*/
private void flushCacheBlock(int blockIndex)
{
//write to file
int fileNumber = cacheTableFiles[blockIndex];
if(dirtyBits[blockIndex])
{
try
{
fileAccessor.writeToFileObject(fileNumber, blockIndex, bigArrayList);
setDirtyBit(blockIndex, false);
}
catch(Exception e)
{
e.printStackTrace();
System.exit(-1);
}
}
//clear list
bigArrayList.clearList(blockIndex);
//remove block from used list
//clear cache for this block
//clear table for this block
removeFromUsedList(blockIndex);
clearCacheBlock(blockIndex);
}
/**
* Clears a cache block from memory
* Only a soft clear
* @param blockToClear The block index
*/
private void clearCacheBlock(int blockToClear)
{
cacheTableSpots[blockToClear] = 0;
cacheTableFiles[blockToClear] = -1;
}
/**
* Brings the content of the given file number into an available cache block
*
* @param fileNumber The file number
* @return The index of the spot where the cache block was brought into
*/
protected int bringFileIntoCache(int fileNumber)
{
//clear a spot if there isn't one
int openCacheBlock = getFirstOpenCacheBlock();
if(openCacheBlock == -1)
{
int blockToFlush = mostRecentlyUsedList[0];
flushCacheBlock(blockToFlush);
}
//read into array list
//set cacheTableFiles to fileNumber
//set cacheTableSpots to number of objects read from file
//update usedList
openCacheBlock = getFirstOpenCacheBlock();
readFromFile(fileNumber, openCacheBlock);
setCacheTableFiles(openCacheBlock, fileNumber);
setCacheTableSpots(openCacheBlock, bigArrayList.getArraySize(openCacheBlock));
updateUsedList(openCacheBlock);
return openCacheBlock;
}
/**
* Reads the data from the given file number / cache block into the specified cache index
*
* @param fileNumber The file number / cache block to read in
* @param cacheIndex The cache index to populate with the data from the file
*/
private void readFromFile(int fileNumber, int cacheIndex)
{
try
{
fileAccessor.readFromFileObject(fileNumber, cacheIndex, bigArrayList);
}
catch(Exception e)
{
e.printStackTrace();
System.exit(-1);
}
}
/**
* Deletes all data from disk
*
* @throws IOException If an I/O error occurs
*/
protected void clearMemory() throws IOException
{
fileAccessor.clearMemory();
}
}