Repository navigation
Expand file tree
/
Copy pathFileHashing.java
More file actions
300 lines (282 loc) · 10.7 KB
/
Copy pathFileHashing.java
File metadata and controls
300 lines (282 loc) · 10.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
import java.io.*;
import java.io.File;
public class FileHashing {
private final int bucketSize = 2, tableSize = 3, strLen = 20;
private final int recordLen = strLen;
private final byte empty = '*', delMarker = '#';
private long[] positions;
private BufferedReader buffer = new BufferedReader(
new InputStreamReader(System.in));
private RandomAccessFile outfile;
private RandomAccessFile sorted;
private RandomAccessFile overflow;
public FileHashing() {
}
private void print(byte[] s) { // print a byte array;
for(int k = 0; k < s.length; k++)
System.out.print((char)s[k]);
}
private long hash(byte[] s) {
long xor = 0, pack;
int i, j, slength; // exclude trailing blanks:
for (slength = s.length; s[slength-1] == ' '; slength--);
for (i = 0; i < slength; ) {
for (pack = j = 0; ; j++, i++) {
pack |= (long) s[i]; // include s[i] in the rightmost
if (j == 3 || i == slength - 1) { // byte of pack;
i++;
break;
}
pack <<= 8;
} // xor at one time 8 bytes from s;
xor ^= pack; // last iteration may put less
} // than 8 bytes in pack;
return (xor % tableSize) * bucketSize * recordLen;
}// return byte position of home bucket for s;
private byte[] getName() throws IOException {
System.out.print("Enter a name & phone#: ");
String s = buffer.readLine();
for (int i = s.length(); i < recordLen; i++)
s += ' ';
return s.getBytes(); // s => line
}
private int comparesTo(byte[] s1, byte[] s2) { // same length
for (int i = 0; i < s1.length; i++) // of s1 and s2
if (s1[i] != s2[i]) // is assumed;
return s1[i] - s2[i];
return 0;
}
void insert() throws IOException {
insertion(getName());
}
void insertion(byte[] line) throws IOException {
byte[] name = new byte[recordLen];
boolean done = false, inserted = false;
int counter = 0;
long address = hash(line);
outfile.seek(address);
while (!done && outfile.read(name) != -1) {
if (name[0] == empty || name[0] == delMarker) {
outfile.seek(address+counter*recordLen);
outfile.write(line);
done = inserted = true;
}
else if (comparesTo(name,line) == 0) {
print(line);
System.out.println(" is already in the file");
return;
}
else counter++;
if (counter == bucketSize)
done = true;
else outfile.seek(address+counter*recordLen);
}
if (!inserted) {
done = false;
counter = 0;
overflow.seek(0);
while (!done && overflow.read(name) != -1) {
if (name[0] == delMarker)
done = true;
else if (comparesTo(name,line) == 0) {
print(line);
System.out.println(" is already in the file");
return;
}
else counter++;
}
if (done)
overflow.seek(counter*recordLen);
else overflow.seek(overflow.length());
overflow.write(line);
}
}
private void delete() throws IOException {
byte[] line = getName();
long address = hash(line);
outfile.seek(address);
int counter = 0;
boolean done = false, deleted = false;
byte[] name = new byte[recordLen];
while (!done && outfile.read(name) != -1) {
if (comparesTo(line,name) == 0) {
outfile.seek(address+counter*recordLen);
outfile.write(delMarker);
done = deleted = true;
}
else counter++;
if (counter == bucketSize)
done = true;
else outfile.seek(address+counter*recordLen);
}
if (!deleted) {
done = false;
counter = 0;
overflow.seek(0);
while (!done && overflow.read(name) != -1) {
if (comparesTo(line,name) == 0) {
overflow.seek(counter*recordLen);
overflow.write(delMarker);
done = deleted = true;
}
else counter++;
overflow.seek(counter*recordLen);
}
}
if (!deleted) {
print(line);
System.out.println(" is not in database");
}
}
private void swap(long[] arr, int i, int j) {
long tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp;
}
private int partition(int low, int high) throws IOException {
byte[] rec = new byte[recordLen];
byte[] pivot = new byte[recordLen];
int i, lastSmall;
swap(positions,low,(low+high)/2);
outfile.seek(positions[low]*recordLen);
outfile.read(pivot);
for (lastSmall = low, i = low+1; i <= high; i++) {
outfile.seek(positions[i]*recordLen);
outfile.read(rec);
if (comparesTo(rec,pivot) < 0) {
lastSmall++;
swap(positions,lastSmall,i);
}
}
swap(positions,low,lastSmall);
return lastSmall;
}
private void sort(int low, int high) throws IOException {
if (low < high) {
int pivotLoc = partition(low, high);
sort(low, pivotLoc-1);
sort(pivotLoc+1, high);
}
}
private void sortFile() throws IOException {
byte[] rec = new byte[recordLen];
sort(1,(int)positions[0]); // positions[0] contains the # of elements;
for (int i = 1; i <= positions[0]; i++) { // put data from
outfile.seek(positions[i]*recordLen); // outfile in sorted order
outfile.read(rec);
sorted.write(rec); // in file sorted;
}
}
// data from overflow file and outfile are all stored in outfile and
// prepared for external sort by loading positions of the data to an array;
private void combineFiles() throws IOException {
byte[] rec = new byte[recordLen];
int counter = bucketSize*tableSize;
outfile.seek(outfile.length());
overflow.seek(0);
while (overflow.read(rec) != -1) { // transfer from
if (rec[0] != delMarker) { // overflow to outfile only
counter++; // valid (non-deleted) items;
outfile.write(rec);
}
}
positions = new long[counter+1];
outfile.seek(0); // load to the array positions
int arrCnt = 1; // of valid data stored in output file;
for (int i = 0; i < counter; i++) {
outfile.seek(i*recordLen);
outfile.read(rec);
if (rec[0] != empty && rec[0] != delMarker)
positions[arrCnt++] = i;
}
positions[0] = --arrCnt; // store the number of data in position 0;
}
public void processFile(String fileName) {
char command = '1';
byte[] line = new byte[recordLen];
String commandLine;
try {
(new File(".\\","outfile")).delete();
(new File(".\\","overflow")).delete();
(new File(".\\","sorted")).delete();
RandomAccessFile fIn = new RandomAccessFile(fileName,"rw");
outfile = new RandomAccessFile("outfile","rw");
sorted = new RandomAccessFile("sorted","rw");
overflow = new RandomAccessFile("overflow","rw");
for (int i = 1; i <= tableSize*bucketSize*recordLen; i++)
outfile.write(empty); // initialize outfile;
while (fIn.read(line) != -1) // load fIn to outfile;
insertion(line);
printFile("outfile",outfile);
printFile("overflow",overflow);
while (command != '3') {
System.out.print("Enter your choice "
+ "(1. insert, 2. delete, 3. exit): ");
commandLine = buffer.readLine();
command = commandLine.charAt(0);
if (command == '1')
insert();
else if (command == '2')
delete();
else if (command != '3')
System.out.println("Wrong command entered, please retry.");
printFile("outfile",outfile);
printFile("overflow",overflow);
}
combineFiles();
printFile("outfile",outfile);
sortFile();
printFile("sorted",sorted);
outfile.close();
sorted.close();
overflow.close();
fIn.close();
(new File(".\\","names")).delete();
(new File(".\\","sorted")).renameTo(new File(".\\","names"));
} catch (IOException ioe) {
}
}
private void printFile(String name, RandomAccessFile f) throws IOException {
byte ch = '1';
RandomAccessFile outf = new RandomAccessFile("hash.out","rw");
outf.seek(outf.length());
System.out.println(name);
outf.writeBytes(name + "\n");
f.seek(0);
while (true) {
for (int i = 1; i <= bucketSize; i++) {
for (int j = 1; j <= recordLen; j++) {
try {
ch = f.readByte();
} catch (EOFException e) {
System.out.println();
outf.write('\n');
outf.close();
return;
}
System.out.print((char)ch);
outf.write(ch);
}
System.out.print('|');
outf.write('|');
}
System.out.println('|');
outf.write('|');
outf.write('\n');
}
}
static public void main(String args[]) {
String fileName = "";
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader buffer = new BufferedReader(isr);
FileHashing fClass = new FileHashing();
try {
if (args.length == 0) {
System.out.print("Enter a file name: ");
fileName = buffer.readLine();
}
else fileName = args[0];
} catch(IOException io) {
System.err.println("Cannot open " + fileName);
}
fClass.processFile(fileName);
}
}