-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFinalProject.java
664 lines (618 loc) · 29.4 KB
/
FinalProject.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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
package hadoopApp;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapred.JobConf;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.Vector;
public class FinalProject {
public static int degreeOfTree = 0;
public static JobConf jobConf;
public static boolean isDebugMode = false;
public static FileSystem hdfs;
public static Path workingDir;
public static Path newFolderPath;
public static String currentSize = "0";
public static String root = "0";
public static String currNode = "0";
public static String parent = "0";
public static String next = "0";
public static String dataToBeInserted;
public static String dataKeyToBeInserted;
public static int nodeCount=0;
public static void main(String args[]) throws Exception {
//first programme will start and welcome the user.....
jobConf = new JobConf(FinalProject.class);
hdfs = FileSystem.get(jobConf);
workingDir = hdfs.getWorkingDirectory();
System.out.println("Entering into database.\nHello!!......................\n");
System.out.println("Enter degree of tree:\n");
Scanner reader = new Scanner(System.in);
degreeOfTree = reader.nextInt();
System.out.println("you've enetered: " + degreeOfTree);
int numberofValues;
System.out.println("Enter number of times you'll give input: ");
reader = new Scanner(System.in);
numberofValues = reader.nextInt();
for(int i=0;i<numberofValues;i++){
currNode = root;
System.out.println("Enter Data: ");
reader = new Scanner(System.in);
dataKeyToBeInserted = reader.next();
System.out.println("Enter DataString: ");
reader = new Scanner(System.in);
dataToBeInserted = reader.next();
insertIntoTree();
printTree();
printAllLeaf();
}
reader.close();
}
private static void printAllLeaf() throws IOException {
System.out.println("Printing all the leaves : ");
String nextPageAddress = "1";
String toPrint = "";
while (!nextPageAddress.equals("0")){
System.out.println("Page Address : " + nextPageAddress);
hdfs = FileSystem.get(jobConf);
Path newFilePath = new Path(newFolderPath + "/" + nextPageAddress + ".txt");
BufferedReader bfr = new BufferedReader(new InputStreamReader(hdfs.open(newFilePath)));
String str;
str = bfr.readLine();//-1 line read here.......
String[] v = str.split(",");
nextPageAddress = v[5];
int size = Integer.parseInt(v[3]);
for (int j = 1;j<=size;j++){
str = bfr.readLine();
toPrint = toPrint + str+ "\n";
}
toPrint = toPrint + "\n";
System.out.println(toPrint);
toPrint = "";
}
}
private static void printTree() throws IOException {
int countOfLevel=0;
System.out.println("\nPrinting Tree in level order fashion: \n");
Vector<String> nodeList = new Vector<>();
//firstly the root page is inserted into stack....
nodeList.add(root);
//this boolean will be true only if we reach the leaf pages so that we can stop traversing further....
boolean done = false;
while (!done){
Vector<String> newList = new Vector<>();
String toPrint = "";
countOfLevel++;
System.out.println("Level : " + countOfLevel + "\n");
for (int i = 0; i < nodeList.size() ; i++){
hdfs = FileSystem.get(jobConf);
Path newFilePath = new Path(newFolderPath + "/" + nodeList.elementAt(i) + ".txt");
BufferedReader bfr = new BufferedReader(new InputStreamReader(hdfs.open(newFilePath)));
String str;
str = bfr.readLine();//-1 line read here.......
String[] v = str.split(",");
toPrint = toPrint + "Page Address : " + nodeList.elementAt(i) + "\n";
if(v[1].equals("1")){
done = true;
int size = Integer.parseInt(v[3]);
for (int j = 1;j<=size;j++){
str = bfr.readLine();
toPrint = toPrint + str+ "\n";
}
toPrint = toPrint + "\n";
} else{
int size = Integer.parseInt(v[3]);
str = bfr.readLine();//zeroth ;line of tree node is read here...
toPrint = toPrint + str+ "\n";//zeroth line is added to printString...
v=str.split(",");
newList.add(v[1]);//pointer to its file is aded in node list here..
//this loop will traverse through each line of file and put lines to printString and also push pointer of its child to newList.
for (int j = 1;j<=size;j++){
str = bfr.readLine();
toPrint = toPrint + str + "\n";
v=str.split(",");
newList.add(v[2]);
}
toPrint = toPrint + "\n";
}
}
System.out.println(toPrint);
nodeList = newList;
}
}
private static void insertIntoTree() throws IOException {
if (root.equals("0")) {
createTree();
if(isDebugMode)
System.out.println("created tree...." + currNode + "," + parent +"," +currentSize + "," + next);
}
hdfs = FileSystem.get(jobConf);
Path newFilePath = new Path(newFolderPath + "/" + root + ".txt");
BufferedReader bfr = new BufferedReader(new InputStreamReader(hdfs.open(newFilePath)));
String str;
str = bfr.readLine();
String[] values = str.split(",");
currNode = root;
currentSize = values[3];
parent = values[4];
if (values[1].equals("1")) {
next = values[5];
insertIntoLeafNode(bfr);
} else {
insertIntoNonLeafNode(bfr);
}
}
private static void insertIntoNonLeafNode(BufferedReader bfr) throws IOException {
int i;
String line = bfr.readLine();//zeroth line is rea here....
if(isDebugMode)
System.out.println("inserting into non leaf node line 114 : currentNode, root : " +currNode+ ", "+root );
String[] v =line.split(",");
String leftPointer = v[1];//leftest pointer..
line = bfr.readLine();//first line read here....
v =line.split(",");
for (i = 1; i <= Integer.parseInt(currentSize) && line != null && Integer.parseInt(v[1]) < Integer.parseInt(dataKeyToBeInserted); i++) {
leftPointer = v[2];
line = bfr.readLine();
if(line!=null)
v = line.split(",");
}
hdfs = FileSystem.get(jobConf);
Path newFilePath = new Path(newFolderPath + "/" + leftPointer + ".txt");
bfr = new BufferedReader(new InputStreamReader(hdfs.open(newFilePath)));
String str;
str = bfr.readLine();
String[] values = str.split(",");
//currNode = root;
currNode = leftPointer;
currentSize = values[3];
parent = values[4];
if (values[1].equals("1")) {
next = values[5];
insertIntoLeafNode(bfr);
} else {
insertIntoNonLeafNode(bfr);
}
}
private static void insertIntoLeafNode(BufferedReader bfr) throws IOException {
if(isDebugMode)
System.out.println("inside insert into LeafNode...." + currNode + "," + parent +"," +currentSize + "," + next);
if (Integer.parseInt(currentSize) < degreeOfTree - 1 && Integer.parseInt(currentSize)!=0) {
if(isDebugMode)
System.out.println("currentSize < degree" + currNode + "," + parent +"," +currentSize + "," + next);
String finalFile = "";
int currSize = Integer.parseInt(currentSize)+1;
finalFile = finalFile + -1 + ",1," + degreeOfTree +"," +currSize + "," + parent + "," + next + "\n";
String line = bfr.readLine();
String[] v = line.split(",");
int data = Integer.parseInt(v[1]);
int i;
for (i = 0; i < currSize-1 && data < Integer.parseInt(dataKeyToBeInserted); i++) {
finalFile = finalFile + line + "\n";
line = bfr.readLine();
if(line!=null) {
v = line.split(",");
data = Integer.parseInt(v[1]);
}
}
if (i == currSize-1) {
finalFile = finalFile + i + "," + dataKeyToBeInserted + "," + dataToBeInserted + "\n";
i++;
} else {
finalFile = finalFile + i + "," + dataKeyToBeInserted + "," + dataToBeInserted + "\n";
i++;
for (; i <= currSize-1; i++) {
v = line.split(",");
finalFile = finalFile + i + "," + v[1] + "," + v[2] + "\n";
line = bfr.readLine();
}
}
Path newFilePath = new Path(newFolderPath + "/" + currNode + ".txt");
hdfs.delete(newFilePath, true);
hdfs.createNewFile(newFilePath);
StringBuilder sb = new StringBuilder();
sb.append(finalFile);
byte[] byt = sb.toString().getBytes();
FSDataOutputStream fsOutStream = hdfs.create(newFilePath);
fsOutStream.write(byt);
fsOutStream.close();
}else if(Integer.parseInt(currentSize) == 0){
int currSize = Integer.parseInt(currentSize) + 1;
String finalFile = "" + -1 + ",1," + degreeOfTree + ","+currSize + "," + parent + "," + next + "\n";
finalFile = finalFile + "0" + "," + dataKeyToBeInserted + "," + dataToBeInserted + "\n";
Path newFilePath = new Path(newFolderPath + "/" + currNode + ".txt");
hdfs.delete(newFilePath, true);
hdfs.createNewFile(newFilePath);
StringBuilder sb = new StringBuilder();
sb.append(finalFile);
byte[] byt = sb.toString().getBytes();
FSDataOutputStream fsOutStream = hdfs.create(newFilePath);
fsOutStream.write(byt);
fsOutStream.close();
}
else {
splitLeaf(bfr);
}
}
private static void splitLeaf(BufferedReader bfr) throws IOException {
nodeCount++;
int midData;
int maxSize = degreeOfTree-1;
int splitlocation;
maxSize++;//it contains total size of both leafs....
if ((maxSize+1 % 2 == 0)) {
splitlocation = (maxSize) / 2;
} else {
splitlocation = ((maxSize) + 1) / 2;
}
Path path;
int sizeOfSecondTree = 0;
sizeOfSecondTree = maxSize - splitlocation;
if(isDebugMode)
System.out.println("split leaf....split location,size of second tree = "+splitlocation +","+sizeOfSecondTree+","+ currNode + "," + parent +"," +currentSize + "," + next);
String finalFile = "";
String secondfFile = "-1,1," + degreeOfTree + "," + sizeOfSecondTree + "," + FinalProject.parent +","+ FinalProject.next + "\n";
if (!next.equals("0")) {
if(isDebugMode)
System.out.println("split leaf nex ! = 0...." + currNode + "," + parent +"," +currentSize + "," + next);
path = new Path(newFolderPath + "/" + nodeCount + ".txt");
finalFile = "" + -1 + "," + 1 + "," + degreeOfTree +","+ splitlocation + "," + parent + "," + nodeCount + "\n";
next = "" + nodeCount;
} else {
if(isDebugMode)
System.out.println("split leaf nex = = 0...." + currNode + "," + parent +"," +currentSize + "," + next);
finalFile = "" + -1 + "," + 1 + "," + degreeOfTree +","+ splitlocation + "," + parent + "," + nodeCount + "\n";
path = new Path(newFolderPath + "/" + nodeCount + ".txt");
next = "" + nodeCount;
}
String line = bfr.readLine();//zeroth line rea here....
String[] v = line.split(",");
int data = Integer.parseInt(v[1]);
int i;
for (i = 0; i < splitlocation && line != null && data < Integer.parseInt(dataKeyToBeInserted); i++) {
finalFile = finalFile + line + "\n";
line = bfr.readLine();
if(line!=null) {
v = line.split(",");
data = Integer.parseInt(v[1]);
}
}
if (i < splitlocation) {
finalFile = finalFile + i + "," + dataKeyToBeInserted + "," + dataToBeInserted + "\n";
i++;
for (; i < splitlocation; i++) {
finalFile = finalFile + i + "," + v[1] + "," + v[2] + "\n";
line = bfr.readLine();
v = line.split(",");
}
midData = Integer.parseInt(v[1]);
for (; i < maxSize; i++) {
int lineNo = i - splitlocation;
secondfFile = secondfFile + lineNo + "," + v[1] + "," + v[2] + "\n";
line = bfr.readLine();
if(line!=null)
v = line.split(",");
}
} else {
midData = Integer.parseInt(v[1]);
if(midData>Integer.parseInt(dataKeyToBeInserted)){
midData = Integer.parseInt(dataKeyToBeInserted);
}
for (; (i < maxSize-1 && data < Integer.parseInt(dataKeyToBeInserted) ); i++) {
int lineNo = i - splitlocation;
secondfFile = secondfFile + lineNo + "," + v[1] + "," + v[2] + "\n";
line = bfr.readLine();
if(line!=null)
v = line.split(",");
}
if (i < maxSize-1) {
int lineNo = i-splitlocation;
secondfFile = secondfFile + lineNo + "," + dataKeyToBeInserted + "," + dataToBeInserted + "\n";
i++;
for (; i < maxSize; i++) {
lineNo = i - splitlocation;
secondfFile = secondfFile + lineNo + "," + v[1] + "," + v[2] + "\n";
line = bfr.readLine();
if(line!=null)
v = line.split(",");
}
} else {
int lineNo = i-splitlocation;
secondfFile = secondfFile + lineNo + "," + dataKeyToBeInserted + "," + dataToBeInserted + "\n";
}
}
Path thisFile = new Path(newFolderPath + "/" + currNode + ".txt");
hdfs.delete(thisFile, true);
hdfs.createNewFile(thisFile);
StringBuilder sb = new StringBuilder();
sb.append(finalFile);
byte[] byt = sb.toString().getBytes();
FSDataOutputStream fsOutStream = hdfs.create(thisFile);
fsOutStream.write(byt);
fsOutStream.close();
hdfs.delete(path, true);
hdfs.createNewFile(path);
sb = new StringBuilder();
sb.append(secondfFile);
byte[] bbyt = sb.toString().getBytes();
FSDataOutputStream fssOutStream = hdfs.create(path);
fssOutStream.write(bbyt);
fssOutStream.close();
propogate(String.valueOf(midData));
}
private static void propogate(String midData) throws IOException {
if(isDebugMode)
System.out.println("propogate....midData="+midData + "," + currNode + "," + parent +"," +currentSize + "," + next);
if (parent.equals("0")) {//if parent is not present....create a new tree node and set it as root
if(isDebugMode)
System.out.println("propogate..creating parent..midData="+midData + "," + currNode + "," + parent +"," +currentSize + "," + next);
nodeCount++;
Path path = new Path(newFolderPath + "/" + nodeCount + ".txt");
if(isDebugMode)
System.out.println("propogate..creating parent..midData,currentNode,parent,currentSize,next,path="+midData + "," + currNode + "," + parent +"," +currentSize + "," + next + "," + path.toString());
changeParentTo(nodeCount,currNode);
changeParentTo(nodeCount,next);
root = "" + nodeCount;
if(isDebugMode)
System.out.println("root changed to : " + root);
parent = "0";
String string = "-1,0," + degreeOfTree + ",1,0" + "\n";//TODO:check here..
string = string + "0" + "," + currNode + "\n";
currNode = root;//TODO change its childrens to point it....
string = string + "1," + midData + "," + next + "\n";
hdfs.delete(path, true);
hdfs.createNewFile(path);
StringBuilder sb = new StringBuilder();
sb.append(string);
byte[] bbyt = sb.toString().getBytes();
FSDataOutputStream fssOutStream = hdfs.create(path);
fssOutStream.write(bbyt);
fssOutStream.close();
return;
} else {
if(isDebugMode)
System.out.println("propogate...parent already present.midData="+midData + "," + currNode + "," + parent +"," +currentSize + "," + next);
Path path = new Path(newFolderPath + "/" + parent + ".txt");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(hdfs.open(path)));
String line = bufferedReader.readLine();
String[] v = line.split(",");
int sizeOfParent = Integer.parseInt(v[3]);
currNode = parent;
parent = v[4];
currentSize = String.valueOf(sizeOfParent);
if (sizeOfParent < degreeOfTree-1) {
if(isDebugMode)
System.out.println("propogate...sizeOfPArent < degree.midData="+midData + "," + currNode + "," + parent +"," +currentSize + "," + next);
String finalFile = "";
int newSize = sizeOfParent +1;
finalFile = "-1,0,"+degreeOfTree +","+newSize+","+parent + "\n";
line = bufferedReader.readLine();
finalFile = finalFile + line +"\n";//zeroth line is copied as it is...
line = bufferedReader.readLine();//first line read here...
v = line.split(",");
int i;
for (i = 1; i < newSize && line != null && Integer.parseInt(midData) > Integer.parseInt(v[1]); i++) {
finalFile = finalFile + line + "\n";
line = bufferedReader.readLine();
if(line!=null)
v = line.split(",");
}
if(i < newSize){
finalFile = finalFile + i + "," + midData + "," + next + "\n";
i++;
for (; i <= newSize && line != null; i++) {
finalFile = finalFile + i + "," + v[1] + "," + v[2] + "\n";
line = bufferedReader.readLine();
if(line!=null)
v = line.split(",");
}
}else {
finalFile = finalFile + i + "," + midData + "," + next + "\n";
}/*else{
System.out.println("some error has occured:");
}*/
hdfs.delete(path, true);
hdfs.createNewFile(path);
StringBuilder sb = new StringBuilder();
sb.append(finalFile);
byte[] bbyt = sb.toString().getBytes();
FSDataOutputStream fssOutStream = hdfs.create(path);
fssOutStream.write(bbyt);
fssOutStream.close();
// currNode = parent;
} else {
//current node is already set to this tree node, parent is set to it's parent and current size is size of this tree node..
//midData is midValue of it's child, line is firstLine of this node and bufferReader Points to zeroth line..
//next point to the pointer of child..
splitTreeNode(midData,line,bufferedReader);
}
}
}
private static void changeParentTo(int nodeCount, String currNodee) throws IOException {
Path newFilePath = new Path(newFolderPath + "/" + currNodee + ".txt");
BufferedReader bfr = new BufferedReader(new InputStreamReader(hdfs.open(newFilePath)));
String str;
String finalFile = "";
str = bfr.readLine();
String [] v = str.split(",");
if(v[1].equals("1")){
finalFile = v[0] + "," + v[1] + "," + degreeOfTree + "," + v[3]+"," + nodeCount + "," +v[5] + "\n";
}else{
finalFile = v[0] + "," + v[1] + "," + degreeOfTree + "," + v[3] + "," + nodeCount + "\n";
}
str = bfr.readLine();
while (str!=null){
finalFile = finalFile + str + "\n";
str = bfr.readLine();
}
if(isDebugMode)
System.out.println("finalFile parent changed for child :" + currNodee+ " ,to " +nodeCount );
if(isDebugMode)
System.out.println("finalFile is :\n" + finalFile);
hdfs.delete(newFilePath, true);
hdfs.createNewFile(newFilePath);
StringBuilder sb = new StringBuilder();
sb.append(finalFile);
byte[] bbyt = sb.toString().getBytes();
FSDataOutputStream fssOutStream = hdfs.create(newFilePath);
fssOutStream.write(bbyt);
fssOutStream.close();
return;
}
private static void splitTreeNode(String midData, String line, BufferedReader bufferedReader) throws IOException {
//current node is already set to this tree node, parent is set to it's parent and current size is size of this tree node..
//midData is midValue of it's child, line is -1 line of this node and bufferReader Points to zeroth line..
//next point to the pointer of child..
String[] v;
if(isDebugMode)
System.out.println("split Tree Node" + "," + currNode + "," + parent +"," +currentSize + "," + next);
nodeCount++;
String finalFile = "",secondFile = "";
int maxSize = degreeOfTree - 1;
maxSize++;//for degree = 5, this value will be 5 and it's middle value will be propogated to top..
String newMidData = "";//this will be propogated to top...
String firstPointerForSecondFile;//zeroth line pointer for second file...first file has it already..
int splitlocation;//maxSize for degree 5 is 5 at this line..
//remember splitLocation is indexed from 1....
if ((maxSize) % 2 == 0) {
splitlocation = (maxSize) / 2;
} else {
splitlocation = ((maxSize) + 1) / 2;
}
splitlocation++;
//TODO:cross check here....
int sizeOfSecondTree = maxSize - splitlocation+1;// for 11 elements it should be 11-7 +1;
int sizeOfFirstNode = splitlocation-1;
finalFile = "-1,0," + degreeOfTree +","+ sizeOfFirstNode +","+ parent + "\n";
line = bufferedReader.readLine();//zeroth line read here...
finalFile = finalFile + line +"\n";//zeroth line is copied as it is...
secondFile = secondFile + "-1,0,"+degreeOfTree +","+sizeOfSecondTree + ","+parent+"\n";
line = bufferedReader.readLine();//first line read here....
v=line.split(",");
int i;
firstPointerForSecondFile = v[2];//it's right pointer of first line of first file...
for (i = 1; i < splitlocation && line != null && Integer.parseInt(midData) > Integer.parseInt(v[1]); i++) {
finalFile = finalFile + line + "\n";
firstPointerForSecondFile = v[2];
line = bufferedReader.readLine();
v = line.split(",");
}
if(i < splitlocation){
finalFile = finalFile + i + "," + midData + "," + next + "\n";
firstPointerForSecondFile = next;
i++;
for (; i < splitlocation && line != null; i++) {
finalFile = finalFile + i + "," + v[1] + "," + v[2] + "\n";
firstPointerForSecondFile = v[2];
line = bufferedReader.readLine();
if(line!=null)
v = line.split(",");
}
secondFile =secondFile + "0," + firstPointerForSecondFile + "\n";
v= line.split(",");
newMidData = (v[1]);
for (; i <= maxSize && line!=null; i++) {
int lineNo = i - splitlocation+1;
secondFile = secondFile + lineNo + "," + v[1]+"," + v[2]+ "\n";
changeParentTo(nodeCount,v[2]);
line = bufferedReader.readLine();
if(line!=null)
v = line.split(",");
}
} else{
v = line.split(",");
newMidData = (v[1]);
secondFile = secondFile + "0," + firstPointerForSecondFile + "\n";
if(Integer.parseInt(midData) < Integer.parseInt(newMidData)){
newMidData = midData;
int lineNo = i-splitlocation+1;
secondFile = secondFile + lineNo + "," + midData + "," + next + "\n";
changeParentTo(nodeCount,next);
i++;
for (; i <= maxSize && line!=null; i++) {
lineNo = i - splitlocation+1;
secondFile = secondFile + lineNo + "," + v[1] + "," + v[2] + "\n";
changeParentTo(nodeCount,v[2]);
line = bufferedReader.readLine();
if(line!=null)
v = line.split(",");
}
}else{
//TODO:NULL point exception may come here previously it was <mexSize
for (; (i <= maxSize) && Integer.parseInt(midData) > Integer.parseInt(v[1]); i++) {
int lineNo = i - splitlocation+1;
secondFile = secondFile + lineNo + "," + v[1] + "," + v[2] + "\n";
changeParentTo(nodeCount,v[2]);
line = bufferedReader.readLine();
if(line!=null)
v = line.split(",");
}
if (i <= maxSize) {
int lineNo = i - splitlocation+1;
secondFile = secondFile + lineNo + "," + midData + "," + next + "\n";
changeParentTo(nodeCount,next);
i++;
for (; i <= maxSize && line!=null; i++) {
lineNo = i - splitlocation+1;
secondFile = secondFile + lineNo + "," + v[1] + "," + v[2] + "\n";
changeParentTo(nodeCount,v[2]);
line = bufferedReader.readLine();
if(line!=null)
v = line.split(",");
}
} else {
int lineNo = i - splitlocation+1;
secondFile = secondFile + lineNo + "," + midData + "," + next + "\n";
changeParentTo(nodeCount,next);
}
}
}
//next = String.valueOf(nodeCount);
Path thisFile = new Path(newFolderPath + "/" + currNode + ".txt");
hdfs.delete(thisFile, true);
hdfs.createNewFile(thisFile);
StringBuilder sb = new StringBuilder();
sb.append(finalFile);
byte[] byt = sb.toString().getBytes();
FSDataOutputStream fsOutStream = hdfs.create(thisFile);
fsOutStream.write(byt);
fsOutStream.close();
Path path = new Path(newFolderPath + "/" + nodeCount + ".txt");
next = String.valueOf(nodeCount);
hdfs.delete(path, true);
hdfs.createNewFile(path);
sb = new StringBuilder();
sb.append(secondFile);
byte[] bbyt = sb.toString().getBytes();
FSDataOutputStream fssOutStream = hdfs.create(path);
fssOutStream.write(bbyt);
fssOutStream.close();
propogate(String.valueOf(newMidData));
}
private static void createTree() throws IOException {
if(isDebugMode)
System.out.println("inside create tree" + "," + currNode + "," + parent +"," +currentSize + "," + next);
nodeCount++;
jobConf = new JobConf(FinalProject.class);
workingDir = hdfs.getWorkingDirectory();
newFolderPath = new Path(workingDir, "Tree");
if (hdfs.exists(newFolderPath)) {
hdfs.delete(newFolderPath, true);
}
hdfs.mkdirs(newFolderPath);
Path newFilePath = new Path(newFolderPath + "/"+nodeCount +".txt");
StringBuilder sb = new StringBuilder();
sb.append("-1,1," + degreeOfTree + ",0" + "," + "0," + "0" + "\n");
byte[] byt = sb.toString().getBytes();
FSDataOutputStream fsOutStream = hdfs.create(newFilePath);
fsOutStream.write(byt);
fsOutStream.close();
if(isDebugMode)
System.out.println("Root node created.");
root = String.valueOf(nodeCount);
currNode = root;
}
}