1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. public class QueryBatcher {
  7. private final String configFilePath;
  8. private final Map<String, List<String>> batchConfig;
  9. public QueryBatcher(String configFilePath) {
  10. this.configFilePath = configFilePath;
  11. this.batchConfig = new HashMap<>();
  12. loadConfig();
  13. }
  14. private void loadConfig() {
  15. try (BufferedReader reader = new BufferedReader(new FileReader(configFilePath))) {
  16. String line;
  17. while ((line = reader.readLine()) != null) {
  18. String[] parts = line.split(","); // Assuming comma-separated format: "batch_name,query1,query2,..."
  19. if (parts.length >= 2) {
  20. String batchName = parts[0].trim();
  21. List<String> queries = new ArrayList<>();
  22. for (int i = 1; i < parts.length; i++) {
  23. queries.add(parts[i].trim());
  24. }
  25. batchConfig.put(batchName, queries);
  26. }
  27. }
  28. } catch (IOException e) {
  29. e.printStackTrace(); // Handle file not found or read errors appropriately.
  30. }
  31. }
  32. public List<String> executeBatch(String batchName) {
  33. // Retrieve queries for the specified batch
  34. List<String> queries = batchConfig.get(batchName);
  35. if (queries == null) {
  36. return new ArrayList<>(); // Return empty list if batch not found.
  37. }
  38. return queries;
  39. }
  40. public static void main(String[] args) {
  41. // Example usage
  42. String configFile = "config.txt"; // Replace with your config file path
  43. QueryBatcher batcher = new QueryBatcher(configFile);
  44. // Example: Execute the "batch1" batch
  45. List<String> batch1Queries = batcher.executeBatch("batch1");
  46. System.out.println("Batch 1 Queries: " + batch1Queries);
  47. //Example: Execute a batch that does not exist
  48. List<String> nonExistentBatch = batcher.executeBatch("nonexistent");
  49. System.out.println("Non existent batch: " + nonExistentBatch);
  50. }
  51. }

Add your comment