-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPTree.java
executable file
·523 lines (439 loc) · 16.3 KB
/
PTree.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
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*
* PTree -- persistent tree
*
* You must follow the coding standards distributed
* on the class web page.
*
* (C) 2007 Mike Dahlin
*
*/
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
public class PTree{
public static final int METADATA_SIZE = 64;
public static final int MAX_TREES = 512;
public static final int MAX_BLOCK_ID = Integer.MAX_VALUE;
//
// Arguments to getParam
//
public static final int ASK_FREE_SPACE = 997;
public static final int ASK_MAX_TREES = 13425;
public static final int ASK_FREE_TREES = 23421;
//
// TNode structure
//
public static final int TNODE_POINTERS = 8;
public static final int BLOCK_SIZE_BYTES = 1024;
public static final int POINTERS_PER_INTERNAL_NODE = 256;
public static final int BLOCK_SIZE_SECTORS = numSectors(BLOCK_SIZE_BYTES);
public static final int TNODES_PER_SECTOR = Disk.SECTOR_SIZE / TNode.TNODE_SIZE;
public static final int ROOTS_LOCATION = ADisk.size() - Helper.paddedDiv(MAX_TREES, TNODES_PER_SECTOR) - 1;
public static final int FREE_LIST_LOCATION = ROOTS_LOCATION - 4; //TODO: make dynamic
public static final int AVAILABLE_SECTORS = FREE_LIST_LOCATION;
private ADisk adisk;
private SimpleLock lock;
public PTree(boolean doFormat)
{
this.adisk = new ADisk(doFormat);
this.lock = new SimpleLock();
}
public TransID beginTrans()
{
return this.adisk.beginTransaction();
}
public void commitTrans(TransID xid) throws IOException, IllegalArgumentException {
this.lock.lock();
try {
this.adisk.commitTransaction(xid);
}finally {
this.lock.unlock();
}
}
public void abortTrans(TransID xid)
throws IOException, IllegalArgumentException
{
this.lock.lock();
try {
this.adisk.abortTransaction(xid);
}finally {
this.lock.unlock();
}
}
public int createTree(TransID xid)
throws IOException, IllegalArgumentException, ResourceException
{
int newTNum = -1;
try{
lock.lock();
//get next available TNum
newTNum = getTNum(xid);
//create tree
writeRoot(xid, newTNum, new TNode(newTNum));
}
finally{
lock.unlock();
}
return newTNum;
}
public void deleteTree(TransID xid, int tnum)
throws IOException, IllegalArgumentException
{
TNode root = readRoot(xid, tnum);
for(int n : root.pointers)
if (n != Node.NULL_PTR)
if (root.treeHeight == 1)
freeSectors(xid, n, n+BLOCK_SIZE_SECTORS-1);
else
deleteNode(xid, readNode(xid, root.pointers[n]));
}
public int getMaxDataBlockId(TransID xid, int tnum)
throws IOException, IllegalArgumentException
{
TNode root = readRoot(xid, tnum);
if (root.treeHeight == 0)
throw new IllegalArgumentException();
for (int n = root.pointers.length-1; n >= 0; n--) {
if (root.pointers[n] != Node.NULL_PTR)
if (root.treeHeight == 1)
return n;
else
return getMaxDataBlockId(xid, readNode(xid, root.pointers[n]), root.treeHeight-1, POINTERS_PER_INTERNAL_NODE * n);
}
assert(false);
return Integer.MIN_VALUE;
}
public int getMaxDataBlockId(TransID tid, InternalNode node, int height, int leavesLeft) throws IllegalArgumentException, IndexOutOfBoundsException, IOException {
for (int i = node.pointers.length-1; i >= 0; i--) {//Step through from right to left
if (node.pointers[i] != Node.NULL_PTR)
if (height == 1) //then the pointers are pointing to leaves
return leavesLeft + i;
else
return getMaxDataBlockId(tid, readNode(tid, node.pointers[i]), height-1, leavesLeft + (POINTERS_PER_INTERNAL_NODE * i));
}
assert (false); //This node has no children, so it shouldn't exist.
return Integer.MIN_VALUE;
}
public void readData(TransID xid, int tnum, int blockId, byte buffer[])
throws IOException, IllegalArgumentException
{
TNode root = readRoot(xid, tnum);
readVisit(xid, root, blockId, buffer);
}
public void writeData(TransID xid, int tnum, int blockId, byte buffer[])
throws IOException, IllegalArgumentException
{
TNode root = readRoot(xid, tnum);
if (root.treeHeight == 0) // if there are no written blocks, just expand the height.
while(maxBlocks(root.treeHeight) < blockId)
root.treeHeight++;
else //reroot the tree.
while (maxBlocks(root.treeHeight) < blockId) {
root.treeHeight++;
InternalNode n = new InternalNode(getSectors(xid, BLOCK_SIZE_SECTORS), root);
root.clear();
root.pointers[0] = n.location;
writeNode(xid, n);
}
writeRoot(xid, root.TNum, root);
writeVisit(xid, root, blockId, buffer);
}
//private
//Find the maximum block ID a tree of a given height supports
public static int maxBlocks(int height) {
return TNODE_POINTERS * (int)Math.pow(POINTERS_PER_INTERNAL_NODE, height - 1) - 1;
}
public void readTreeMetadata(TransID xid, int tnum, byte buffer[])
throws IOException, IllegalArgumentException
{
assert (buffer.length <= METADATA_SIZE);
TNode t = readRoot(xid, tnum);
if (t == null)
throw new IllegalArgumentException();
byte[] metadata = t.metadata;
Helper.fill(metadata, buffer);
}
public void writeTreeMetadata(TransID xid, int tnum, byte buffer[])
throws IOException, IllegalArgumentException
{
assert buffer.length <= METADATA_SIZE;
TNode node = readRoot(xid, tnum);
if (node == null)
throw new IllegalArgumentException();
Helper.fill(buffer,node.metadata);
writeRoot(xid, tnum, node);
}
//Do we need a tid? free list depends on transaction.
//We'll just return the committed freelist.
public int getParam(int param)
throws IOException, IllegalArgumentException
{
TransID tid = this.adisk.beginTransaction();
if(param == ASK_MAX_TREES)
return MAX_TREES;
else if (param == ASK_FREE_TREES)
return numFreeTrees(tid);
else if (param == ASK_FREE_SPACE)
return readFreeList(tid).cardinality();
else
throw new IllegalArgumentException();
}
//private
public int numFreeTrees(TransID tid) throws IOException {
int s = 0;
for(int tnum = 0; tnum < MAX_TREES; tnum++)
if(readRoot(tid, tnum) == null)
s++;
return s;
}
//find a tnum that is unallocated.
//ResourceException if there are none.
public int getTNum(TransID tid) throws ResourceException, IOException{
for(int i = 0; i < MAX_TREES; i++)
if (readRoot(tid, i) == null)
return i;
throw new ResourceException();
}
//private
public int[] findRoot(int tnum) throws IndexOutOfBoundsException{
if (tnum >= MAX_TREES)
throw new IndexOutOfBoundsException();
int[] position = new int[2];
position[0] = ROOTS_LOCATION + tnum / TNODES_PER_SECTOR;
position[1] = tnum % TNODES_PER_SECTOR;
if (position[0] >= this.adisk.getNSectors())
throw new IndexOutOfBoundsException();
return position;
}
//private
public TNode readRoot(TransID tid, int tnum) throws IOException, IndexOutOfBoundsException{
int[] position = findRoot(tnum);
int sectornum = position[0];
int offset = position[1] * TNode.TNODE_SIZE;
// ByteBuffer buff = ByteBuffer.allocate(Disk.SECTOR_SIZE);
byte[] buff = readSector(tid, sectornum);
byte[] nodebytes = new byte[TNode.TNODE_SIZE];
// buff.get(nodebytes, offset, TNode.TNODE_SIZE);
for(int i = 0; i < TNode.TNODE_SIZE; i++)
nodebytes[i] = buff[i+offset];
if (Arrays.equals(nodebytes, new byte[TNode.TNODE_SIZE]))
return null;
try {
return new TNode(nodebytes);
} catch (ClassNotFoundException e) {
return null;
}
}
//private
public void writeRoot(TransID tid, int tnum, TNode node) throws IllegalArgumentException, IndexOutOfBoundsException, IOException {
int[] position = findRoot(tnum);
int sectornum = position[0];
int offset = position[1] * TNode.TNODE_SIZE;
byte[] buff = readSector(tid, sectornum);
byte[] nodeBytes = node.getBytes();
assert nodeBytes.length == TNode.TNODE_SIZE;
assert offset + nodeBytes.length <= Disk.SECTOR_SIZE;
for(int i = 0; i < nodeBytes.length; i++)
buff[offset + i] = nodeBytes[i];
writeSectors(tid, sectornum, buff);
}
//private
public void readVisit(TransID tid, TNode root, int blockID, byte[] buffer) throws IllegalArgumentException, IOException {
if (blockID > getMaxDataBlockId(tid, root.TNum)) {
Helper.fill(new byte[BLOCK_SIZE_BYTES], buffer);
return;
}
if (root.treeHeight == 1) {
Helper.fill(readBlock(tid, root.pointers[blockID]), buffer);
return;
}
int leavesBelow = (int) Math.pow(POINTERS_PER_INTERNAL_NODE, root.treeHeight-1);
int index = blockID / leavesBelow;
if(root.pointers[index] == Node.NULL_PTR) {
Helper.fill(readBlock(tid, root.pointers[blockID]), buffer);
return;
}
readVisit(tid, root.treeHeight-1, getChild(tid, root, index), blockID%leavesBelow, buffer);
}
//private
public void readVisit(TransID tid, int height, InternalNode node, int blockID, byte[] buffer) throws IllegalArgumentException, IndexOutOfBoundsException, IOException {
assert (height >= 1);
if (height == 1) {
if (blockID >= POINTERS_PER_INTERNAL_NODE)
Helper.fill(new byte[BLOCK_SIZE_BYTES], buffer);
else if (node.pointers[blockID] == Node.NULL_PTR)
Helper.fill(new byte[BLOCK_SIZE_BYTES], buffer);
else
Helper.fill(readBlock(tid, node.pointers[blockID]), buffer);
return;
}
int leavesBelow = (int) Math.pow(POINTERS_PER_INTERNAL_NODE, height-1);
int index = blockID / leavesBelow;
if(node.pointers[index] == Node.NULL_PTR) {
Helper.fill(new byte[BLOCK_SIZE_BYTES], buffer);
return;
}
readVisit(tid, height-1, getChild(tid, node, index), blockID%leavesBelow, buffer);
}
//private
public void writeVisit(TransID tid, TNode root, int blockID, byte[] buffer) throws IllegalArgumentException, IndexOutOfBoundsException, ResourceException, IOException {
assert(root.treeHeight >= 1);
if (root.treeHeight == 1) {
short block = getSectors(tid, BLOCK_SIZE_SECTORS);
writeBlock(tid, block, buffer);
root.pointers[blockID] = block;
writeRoot(tid, root.TNum, root);
return;
}
int leavesBelow = (int) Math.pow(POINTERS_PER_INTERNAL_NODE, root.treeHeight-1);
int index = (blockID / leavesBelow);
writeVisit(tid, root.treeHeight-1, getChild(tid, root, index), blockID%leavesBelow, buffer);
}
//private
public void writeVisit(TransID tid, int height, InternalNode node, int blockID, byte[] buffer) throws IllegalArgumentException, IndexOutOfBoundsException, ResourceException, IOException {
assert (height >= 1);
if(height == 1) {
assert (blockID < POINTERS_PER_INTERNAL_NODE);
short block;
if (node.pointers[blockID] != Node.NULL_PTR)
block = node.pointers[blockID];
else
block = getSectors(tid, BLOCK_SIZE_SECTORS);
writeBlock(tid, block, buffer);
node.pointers[blockID] = block;
writeNode(tid, node);
return;
}
int leavesBelow = (int) Math.pow(POINTERS_PER_INTERNAL_NODE, height-1);
int index = blockID / leavesBelow;
writeVisit(tid, height-1, getChild(tid, node, index), blockID%leavesBelow, buffer);
}
//private
public void deleteNode(TransID tid, InternalNode node) throws IllegalArgumentException, IndexOutOfBoundsException, IOException {
if (node == null)
return;
for(int i : node.pointers)
if (node.pointers[i] != Node.NULL_PTR) {
deleteNode(tid, readNode(tid, node.pointers[i]));
freeSectors(tid, node.pointers[i], node.pointers[i] + BLOCK_SIZE_SECTORS - 1);
}
freeSectors(tid, node.location, node.location + BLOCK_SIZE_SECTORS - 1);
}
//private
//essentially c malloc, but with sectors, not bytes.
public short getSectors(TransID tid, int numSectors) throws IllegalArgumentException, IndexOutOfBoundsException, IOException, ResourceException{
BitMap freelist = readFreeList(tid);
try {
for(int i = freelist.nextClearBit(0); i < AVAILABLE_SECTORS;i=freelist.nextClearBit(i+1)) {
if(freelist.nextSetBit(i) >= i+numSectors || freelist.nextSetBit(i) < 0) {
freelist.set(i, i+numSectors);
writeFreeList(tid, freelist);
return (short) i;
}
}
} catch (IndexOutOfBoundsException e) {
throw new ResourceException();
}
throw new ResourceException();
}
//private
public void freeSector(TransID tid, int sector) throws IllegalArgumentException, IndexOutOfBoundsException, IOException {
BitMap freelist = readFreeList(tid);
freelist.set(sector);
writeFreeList(tid, freelist);
}
//private
public void freeSectors(TransID tid, int start, int finish) throws IllegalArgumentException, IndexOutOfBoundsException, IOException {
assert start <= finish;
for(int i = start; i <= finish; i++) {
freeSector(tid, i);
}
}
//private
public BitMap readFreeList(TransID tid) throws IllegalArgumentException, IndexOutOfBoundsException, IOException {
return new BitMap(readSectors(tid, FREE_LIST_LOCATION, ROOTS_LOCATION-1));
}
//private
public void writeFreeList(TransID tid, BitMap freelist) throws IllegalArgumentException, IndexOutOfBoundsException{
writeSectors(tid, FREE_LIST_LOCATION, freelist.getBytes());
}
public InternalNode getChild(TransID tid, Node parent, int index) throws IllegalArgumentException, IndexOutOfBoundsException, ResourceException, IOException {
//If the requested node already exists, simpoly return it.
if (parent.pointers[index] != Node.NULL_PTR)
return readNode(tid, parent.pointers[index]);
//otherwise allocate a new node and return that.
short sector = getSectors(tid, BLOCK_SIZE_SECTORS);
InternalNode node = new InternalNode(sector);
parent.pointers[index] = sector;
writeNode(tid, node);
if(parent.getClass() == TNode.class) {
TNode t = (TNode) parent;
writeRoot(tid, t.TNum, t);
}else
writeNode(tid, (InternalNode)parent);
return node;
}
//private
public InternalNode readNode(TransID tid, short sectornum) throws IllegalArgumentException, IndexOutOfBoundsException, IOException {
try {
return new InternalNode(sectornum, readBlock(tid, sectornum));
} catch (ClassNotFoundException e) {
return null;
}
}
//private
public void writeNode(TransID tid, InternalNode node) throws IllegalArgumentException, IndexOutOfBoundsException{
writeBlock(tid, node.location, node.getBytes());
}
//private
public byte[] readBlock(TransID tid, int sectornum) throws IllegalArgumentException, IndexOutOfBoundsException, IOException{
return readSectors(tid, sectornum, sectornum + BLOCK_SIZE_SECTORS - 1);
}
//private
public void writeBlock(TransID tid, int sectornum, byte[] buffer) throws IllegalArgumentException, IndexOutOfBoundsException{
assert (buffer.length == BLOCK_SIZE_BYTES);
writeSectors(tid, sectornum, buffer);
}
//private
public byte[] readSectors(TransID tid, int start, int finish) throws IllegalArgumentException, IndexOutOfBoundsException, IOException{
if (finish < start)
throw new IllegalArgumentException();
byte[] sector = new byte[Disk.SECTOR_SIZE];
byte[] buffer = new byte[(finish + 1 - start) * Disk.SECTOR_SIZE];
for(int i = 0; i <= finish-start; i++) {
this.adisk.readSector(tid, start+i, sector);
for(int index = 0; index < Disk.SECTOR_SIZE; index++)
buffer[i*Disk.SECTOR_SIZE + index] = sector[index];
}
return buffer;
}
//private
public void writeSectors(TransID tid, int start, byte[] buffer) throws IllegalArgumentException, IndexOutOfBoundsException{
ByteBuffer buff = ByteBuffer.allocate(numSectors(buffer)*Disk.SECTOR_SIZE);
buff.put(buffer);
buff.rewind();
byte[] sector = new byte[Disk.SECTOR_SIZE];
int i = 0;
while (buff.hasRemaining()) {
buff.get(sector);
this.adisk.writeSector(tid, start+i, sector);
i++;
}
}
//private
public byte[] readSector(TransID tid, int sectornum) throws IllegalArgumentException, IndexOutOfBoundsException, IOException {
byte[] buff = new byte[Disk.SECTOR_SIZE];
this.adisk.readSector(tid, sectornum, buff);
return buff;
}
//private
public void writeSector(TransID tid, int sectornum, byte[] buffer) throws IllegalArgumentException, IndexOutOfBoundsException, IOException {
this.adisk.writeSector(tid, sectornum, buffer);
}
public static int numSectors(byte[] buffer) {
return numSectors(buffer.length);
}
public static int numSectors(int bytes) {
return (bytes / Disk.SECTOR_SIZE) + (bytes % Disk.SECTOR_SIZE != 0 ? 1 : 0);
}
}