| 95 |
Kevin |
1 |
/*
|
|
|
2 |
* CS 3114 Project 3
|
|
|
3 |
* Author: Kevin Lee
|
|
|
4 |
* Compiler: Eclipse 3.7.0
|
|
|
5 |
* Operating System: Windows 7 x64
|
|
|
6 |
* Date Completed: 11/1/2011
|
|
|
7 |
*
|
|
|
8 |
* The following program sorts a specified file on disk using
|
|
|
9 |
* heapsort and a buffer pool. The file size must be a multiple
|
|
|
10 |
* of 4096 and comprised of key-value records of 2 bytes each
|
|
|
11 |
* (4 bytes total per record). The buffer pool uses the least
|
|
|
12 |
* recently used scheme and the number of 4096 byte blocks the
|
|
|
13 |
* buffer stores is specified in the command line for this
|
|
|
14 |
* program. A statistics file is also outputted to show the
|
|
|
15 |
* sorted file, cache hits/misses, and disk reads/writes.
|
|
|
16 |
*
|
|
|
17 |
* On my honor:
|
|
|
18 |
*
|
|
|
19 |
* - I have not used source code obtained from another student,
|
|
|
20 |
* or any other unauthorized source, either modified or
|
|
|
21 |
* unmodified.
|
|
|
22 |
*
|
|
|
23 |
* - All source code and documentation used in my program is
|
|
|
24 |
* either my original work, or was derived by me from the
|
|
|
25 |
* source code published in the textbook for this course.
|
|
|
26 |
*
|
|
|
27 |
* - I have not discussed coding details about this project with
|
|
|
28 |
* anyone other than my partner (in the case of a joint
|
|
|
29 |
* submission), instructor, ACM/UPE tutors or the TAs assigned
|
|
|
30 |
* to this course. I understand that I may discuss the concepts
|
|
|
31 |
* of this program with other students, and that another student
|
|
|
32 |
* may help me debug my program so long as neither of us writes
|
|
|
33 |
* anything during the discussion or modifies any computer file
|
|
|
34 |
* during the discussion. I have violated neither the spirit nor
|
|
|
35 |
* letter of this restriction.
|
|
|
36 |
*/
|
|
|
37 |
|
|
|
38 |
import java.io.FileNotFoundException;
|
|
|
39 |
import java.io.IOException;
|
|
|
40 |
import java.io.RandomAccessFile;
|
|
|
41 |
import java.nio.ByteBuffer;
|
|
|
42 |
|
|
|
43 |
public class heapsort {
|
|
|
44 |
|
|
|
45 |
public static void main(String[] args) {
|
|
|
46 |
// Check if correct arguments are supplied
|
|
|
47 |
if (args.length != 3) {
|
|
|
48 |
System.out.println("Usage: heapsort <data-file-name> <numb-buffers> <stat-file-name>");
|
|
|
49 |
return;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
// Save file paths and buffer size
|
|
|
53 |
String dataFile = args[0];
|
|
|
54 |
String statFile = args[2];
|
|
|
55 |
int buffSize = Integer.parseInt(args[1]);
|
|
|
56 |
long fileSize = 0;
|
|
|
57 |
|
|
|
58 |
RandomAccessFile file = null;
|
|
|
59 |
|
|
|
60 |
// Check if specified buffer size is within acceptable range
|
|
|
61 |
if (buffSize < 1 || buffSize > 20) {
|
|
|
62 |
System.out.println("Specified buffer size must be between 1 and 20");
|
|
|
63 |
return;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
// Get the length of the file to sort
|
|
|
67 |
try {
|
|
|
68 |
file = new RandomAccessFile(dataFile, "r");
|
|
|
69 |
fileSize = file.length();
|
|
|
70 |
} catch (FileNotFoundException e) {
|
|
|
71 |
e.printStackTrace();
|
|
|
72 |
} catch (IOException e) {
|
|
|
73 |
e.printStackTrace();
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
// Check to make sure file is a multiple of 4096
|
|
|
77 |
if (fileSize % 4 != 0) {
|
|
|
78 |
System.out.println("Input file size must be a multiple of 4096");
|
|
|
79 |
return;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
// Create a new buffer and heap sort class
|
|
|
83 |
LRUBuffer buff = new LRUBuffer(dataFile, statFile, buffSize, 4096, 4);
|
|
|
84 |
MaxHeap heap = new MaxHeap(buff, fileSize/4);
|
|
|
85 |
|
|
|
86 |
// Start sort
|
|
|
87 |
long time1 = System.currentTimeMillis();
|
|
|
88 |
heap.buildheap();
|
|
|
89 |
for (int i = 0; i < fileSize/4; i++) {
|
|
|
90 |
heap.removemax();
|
|
|
91 |
}
|
|
|
92 |
buff.flushBuffer();
|
|
|
93 |
long time2 = System.currentTimeMillis();
|
|
|
94 |
|
|
|
95 |
// Write sort statistics
|
|
|
96 |
buff.writeStats(time2 - time1);
|
|
|
97 |
|
|
|
98 |
// Print the first record from each block in the sorted file
|
|
|
99 |
try {
|
|
|
100 |
int column = 1;
|
|
|
101 |
byte[] data = new byte[4];
|
|
|
102 |
ByteBuffer bbuff;
|
|
|
103 |
short key, value;
|
|
|
104 |
for (long i = 0; i < fileSize; i+=4096) {
|
|
|
105 |
file.seek(i);
|
|
|
106 |
file.read(data);
|
|
|
107 |
bbuff = ByteBuffer.wrap(data);
|
|
|
108 |
key = bbuff.getShort();
|
|
|
109 |
value = bbuff.getShort();
|
|
|
110 |
System.out.printf("%05d %05d ", key, value);
|
|
|
111 |
if (column % 8 == 0) {
|
|
|
112 |
System.out.println();
|
|
|
113 |
}
|
|
|
114 |
column++;
|
|
|
115 |
}
|
|
|
116 |
file.close();
|
|
|
117 |
} catch (IOException e) {
|
|
|
118 |
e.printStackTrace();
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
}
|