-
Notifications
You must be signed in to change notification settings - Fork 0
/
Terasort.java
172 lines (140 loc) · 5.71 KB
/
Terasort.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
package pa2;
import java.io.*;
import java.util.*;
public class Terasort {
public static int Chunckdata(String inputfilename) throws NumberFormatException, InterruptedException, IOException
{
int size_of_file = 0;
//To input a character that need to be sorted from disk/memory
File filename = new File(inputfilename);
if(filename.exists())
{
size_of_file = (int) filename.length();
}
long start,end,compute_time;
start = System.currentTimeMillis();
FileReader fread = null;
BufferedReader bread = null;
double chunk_size = 1000000;
int number_of_charperline = 100;
fread = new FileReader(inputfilename); //to read the input file
bread = new BufferedReader(fread); //Pass the fileread object to the bufferedread
int chunck_data = (int) (size_of_file/(chunk_size*number_of_charperline)); //To calculate the data to be chunked
int i = 0;
String current_line;
ArrayList<String> line = new ArrayList<String>();
if(i > chunk_size)
{
System.out.println("The file cannot be sorted \n");
}
if(i < chunk_size)
{
int var = 0;
while((current_line = bread.readLine()) != null)
{
line.add(current_line);
// Mergesort merge = new Mergesort();
Merge_sort(line,0,line.size()-1,var );
var++;
line = new ArrayList<>();
// i++;
}
if (line.size() == chunck_data || line.size() != 0 && line.size() < chunck_data ) {
//Mergesort merge = new Mergesort();
Merge_sort(line,0,line.size()-1,var );
line = new ArrayList<>();
// var++;
}
}
bread.close();
end = System.currentTimeMillis();
compute_time = end - start;
System.out.println("My external sort has taken" + inputfilename + "ms" + compute_time);
return 0;
}
public static void Merge_sort(ArrayList<String> elements, int elements_in_left, int elements_in_right, int var) throws IOException
{
Collections.sort(elements);
String temp_file = "/tmp/intermediate" + var + ".txt";
FileWriter fwrite = new FileWriter(temp_file);
for(int j=0; j < elements.size(); j++)
{
fwrite.write(elements.get(j) + "\n");
}
fwrite.close();
}
public static void files_merge(int noOfFiles,String outputFile) throws Exception
{
String treekey; String treevalue;
String current_file;
ArrayList<String> list = new ArrayList<String>();
FileReader fread = null;
//BufferedWriter[] bwrite = null;
BufferedReader bread[]= null;
TreeMap<String, String> tmap = new TreeMap<String, String>();
int num_file;
for( num_file = 0; num_file < noOfFiles; num_file++)
{
fread = new FileReader("/tmp" + num_file + ".txt");
bread[num_file] = new BufferedReader(fread);
if(tmap.size() > 0)
{
while((current_file = bread[num_file].readLine()) != null)
{
list.add(current_file);
treekey = current_file.substring(1,10);
treevalue = num_file+ current_file.substring(10);
tmap.put(treekey, treevalue);
}
}
}
FileWriter fwrite = new FileWriter(outputFile);
BufferedWriter bwrite = new BufferedWriter(fwrite);
// while(tmap != null)
while(!tmap.isEmpty())
{
String keystr = tmap.keySet().iterator().next();
String val = tmap.get(keystr);
StringTokenizer st =new StringTokenizer(tmap.get(keystr));
int filenum = Integer.parseInt(st.nextToken());
tmap.remove(keystr);
bwrite.write(keystr+st.nextToken());
bwrite.write("\n");
bwrite.flush();
bwrite.close();
keystr = bread[filenum].readLine();
if(keystr == null )
{
System.out.println("The Tree map has no values \n");
}
if(keystr != null )
{
String key = keystr.substring(0,10);
val = filenum+keystr.substring(10);
tmap.put(key, val);
}
}
}
public static String filename;
public static int thread;
public static String threadValue;
public String getfilename() {
return filename;
}
public int getthread() {
return thread;
}
public static void main(String[] args) throws Exception
{
BufferedReader in_file = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter the file to be sorted: ");
filename = in_file.readLine();
String outputfile = "/tmp/output.txt";
long startTime = System.currentTimeMillis();
int noOfFiles= Chunckdata(filename); //Function to divide the files into chunks
files_merge(noOfFiles,outputfile); // Merge the temp files into output file from temp dir
long endTime = System.currentTimeMillis();
int time = (int) ((double)(endTime-startTime)/1000);
System.out.println(thread +" The compute time of mysort = " +time+ " seconds");
}
}