-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path41.DesignBulkDataImporter.java
More file actions
222 lines (170 loc) · 7.23 KB
/
41.DesignBulkDataImporter.java
File metadata and controls
222 lines (170 loc) · 7.23 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
/* ---------------------------------------------------------------------------- */
/* ( The Authentic JS/JAVA CodeBuff )
___ _ _ _
| _ ) |_ __ _ _ _ __ _ __| |_ __ ____ _ (_)
| _ \ ' \/ _` | '_/ _` / _` \ V V / _` || |
|___/_||_\__,_|_| \__,_\__,_|\_/\_/\__,_|/ |
|__/
*/
/* -------------------------------------------------------------------------- */
/* Youtube: https://youtube.com/@code-with-Bharadwaj */
/* Github : https://github.com/Manu577228 */
/* Portfolio : https://manu-bharadwaj-portfolio.vercel.app/portfolio */
/* ----------------------------------------------------------------------- */
import java.io.*; // For file reading
import java.util.*; // For Lists, Maps
import java.util.concurrent.*; // For ExecutorService & threads
// ---------------- In-Memory DataStore ----------------
// Thread-safe list to simulate database
class DataStore {
private static final List<Map<String, Object>> DB =
Collections.synchronizedList(new ArrayList<>());
// Save a validated record
public static void save(Map<String, Object> record) {
DB.add(record); // synchronized list ensures thread safety
}
// Return all stored records
public static List<Map<String, Object>> listAll() {
return DB;
}
}
// ---------------- BaseImporter (Template Method Pattern) ----------------
// Defines high-level flow: load → validate → process → retry → report
abstract class BaseImporter {
protected String filePath;
protected List<Map<String, Object>> failedRecords = new ArrayList<>();
public BaseImporter(String path) {
this.filePath = path; // Save file path
}
abstract List<Map<String, Object>> load() throws Exception; // Load file data
abstract boolean validate(Map<String, Object> record); // Validate data
// Default process simply stores to DataStore
public void process(Map<String, Object> record) {
DataStore.save(record);
}
// The main template method
public Map<String, Object> run() throws Exception {
List<Map<String, Object>> records = load(); // Load file
ExecutorService service = Executors.newFixedThreadPool(4); // Thread pool
// Submit validation tasks for parallel processing
for (Map<String, Object> rec : records) {
service.submit(() -> {
if (validate(rec)) {
process(rec); // Save if valid
} else {
synchronized (failedRecords) {
failedRecords.add(rec); // Collect failures
}
}
});
}
service.shutdown(); // Stop new tasks
service.awaitTermination(5, TimeUnit.SECONDS); // Wait for threads
// Retry failed records once more
List<Map<String, Object>> retryFails = new ArrayList<>();
for (Map<String, Object> rec : failedRecords) {
if (validate(rec)) {
process(rec);
} else {
retryFails.add(rec);
}
}
// Prepare summary output
Map<String, Object> summary = new LinkedHashMap<>();
summary.put("total", records.size());
summary.put("success", records.size() - retryFails.size());
summary.put("failed", retryFails.size());
summary.put("data", DataStore.listAll());
return summary;
}
}
// ---------------- CSV Importer Strategy ----------------
class CSVImporter extends BaseImporter {
public CSVImporter(String path) {
super(path);
}
@Override
List<Map<String, Object>> load() throws Exception {
List<Map<String, Object>> out = new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader(filePath));
String headerLine = br.readLine(); // Read header
String[] headers = headerLine.split(","); // Split column names
String line;
while ((line = br.readLine()) != null) { // Read each row
String[] parts = line.split(",");
Map<String, Object> row = new HashMap<>();
for (int i = 0; i < headers.length; i++) {
row.put(headers[i], parts.length > i ? parts[i] : "");
}
out.add(row);
}
br.close();
return out; // Return all rows
}
@Override
boolean validate(Map<String, Object> record) {
return record.get("id") != null && !record.get("id").toString().isEmpty();
}
}
// ---------------- JSON Importer Strategy ----------------
class JSONImporter extends BaseImporter {
public JSONImporter(String path) {
super(path);
}
@Override
List<Map<String, Object>> load() throws Exception {
BufferedReader br = new BufferedReader(new FileReader(filePath));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line); // Build JSON string
}
br.close();
// VERY simple JSON array parser (no libs allowed)
// Format expected: [ {"id": "1"}, {"id": "2"} ]
String json = sb.toString().trim();
List<Map<String, Object>> list = new ArrayList<>();
json = json.substring(1, json.length() - 1); // Remove [ ]
String[] objects = json.split("\\},\\s*\\{");
for (String obj : objects) {
String clean = obj.replace("{", "").replace("}", "");
Map<String, Object> map = new HashMap<>();
String[] fields = clean.split(",");
for (String f : fields) {
String[] kv = f.split(":");
map.put(kv[0].replace("\"","").trim(),
kv[1].replace("\"","").trim());
}
list.add(map);
}
return list;
}
@Override
boolean validate(Map<String, Object> record) {
return record.containsKey("id");
}
}
// ---------------- Importer Factory ----------------
class ImporterFactory {
public static BaseImporter getImporter(String filePath) {
if (filePath.endsWith(".csv")) return new CSVImporter(filePath);
if (filePath.endsWith(".json")) return new JSONImporter(filePath);
throw new RuntimeException("Unsupported file format");
}
}
// ---------------- DEMO MAIN ----------------
public class BulkImporter {
public static void main(String[] args) throws Exception {
// Create sample CSV
FileWriter fw = new FileWriter("input.csv");
fw.write("id,name\n1,Alice\n,Invalid\n2,Bob\n");
fw.close();
// Use factory to load correct importer
BaseImporter importer = ImporterFactory.getImporter("input.csv");
// Run import
Map<String, Object> output = importer.run();
// Print final summary
System.out.println("=== Import Summary ===");
System.out.println(output);
}
}