-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyFileReader.java
76 lines (52 loc) · 1.97 KB
/
MyFileReader.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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
public class MyFileReader {
static FileConfiguration fileConfig = FileConfiguration.getFileConfiguration();
static long numberOfBatches = 1;
static long totalBatches = 1;
ConcurrentHashMap<Long, String> batch = new ConcurrentHashMap<Long, String>();
public static void startFileProcessing(String filePath, long linesCount) throws IOException {
MyFileWriter.initializeFileBuffer();
BufferedReader fileContent = new BufferedReader(new FileReader(filePath));
String line;
long count = 0; // For batch processing, reset purpose
long currentLine = 1;
long batchLinesCount = fileConfig.getBatchLinesCount();
linesCount = fileConfig.isFileHaveHeader() ? linesCount - 1 : linesCount;
HashMap<Long, String> lines = new HashMap<Long, String>();
if (linesCount <= batchLinesCount) {
totalBatches = 1;
} else {
totalBatches = (linesCount % batchLinesCount == 0) ? (linesCount / batchLinesCount)
: (linesCount / batchLinesCount) + 1;
}
// Reading the entire file
while ((line = fileContent.readLine()) != null) {
// Skipping the header, if have any
if (!(fileConfig.isFileHaveHeader() && currentLine == 1)) {
lines.put(fileConfig.isFileHaveHeader() ? currentLine - 1 : currentLine, line);
count++;
}
if (count == batchLinesCount) {
processBatch(lines);
lines.clear();
count = 0;
} else if ((count < batchLinesCount) && (numberOfBatches == totalBatches)
&& (count == linesCount % batchLinesCount)) { // For last batch, if not having exact lines
processBatch(lines);
lines.clear();
count = 0;
}
currentLine++;
}
fileContent.close();
}
public static void processBatch(HashMap<Long, String> batch) {
numberOfBatches++;
Runnable batchObject = new BatchConversion(batch, fileConfig);
ThreadPool.threadPoolExecutor.submit(batchObject);
}
}