import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class QueryBatcher {
private final String configFilePath;
private final Map<String, List<String>> batchConfig;
public QueryBatcher(String configFilePath) {
this.configFilePath = configFilePath;
this.batchConfig = new HashMap<>();
loadConfig();
}
private void loadConfig() {
try (BufferedReader reader = new BufferedReader(new FileReader(configFilePath))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(","); // Assuming comma-separated format: "batch_name,query1,query2,..."
if (parts.length >= 2) {
String batchName = parts[0].trim();
List<String> queries = new ArrayList<>();
for (int i = 1; i < parts.length; i++) {
queries.add(parts[i].trim());
}
batchConfig.put(batchName, queries);
}
}
} catch (IOException e) {
e.printStackTrace(); // Handle file not found or read errors appropriately.
}
}
public List<String> executeBatch(String batchName) {
// Retrieve queries for the specified batch
List<String> queries = batchConfig.get(batchName);
if (queries == null) {
return new ArrayList<>(); // Return empty list if batch not found.
}
return queries;
}
public static void main(String[] args) {
// Example usage
String configFile = "config.txt"; // Replace with your config file path
QueryBatcher batcher = new QueryBatcher(configFile);
// Example: Execute the "batch1" batch
List<String> batch1Queries = batcher.executeBatch("batch1");
System.out.println("Batch 1 Queries: " + batch1Queries);
//Example: Execute a batch that does not exist
List<String> nonExistentBatch = batcher.executeBatch("nonexistent");
System.out.println("Non existent batch: " + nonExistentBatch);
}
}
Add your comment