I need to parse the data coming from the URL which looks like this:
hasProcess=true
version=1
DATACENTER=abc
TotalNumberOfServers:4
primary:{0=1, 1=2, 2=1, 3=2, 4=1, 5=2, 6=1, 7=2, 8=1, 9=2, 10=1, 11=2, 12=1, 13=2}
secondary:{0=0, 1=0, 2=0, 3=1, 4=0, 5=0, 6=0, 7=1, 8=0, 9=0, 10=0, 11=1, 12=0, 13=0}
hosttomachine:{3=machineA, 2=machineB, 1=machineC, 4=machineD}
DATACENTER=pqr
TotalNumberOfServers:2
primary:{0=1, 1=2, 2=1, 3=2, 4=1, 5=2, 6=1, 7=2, 8=1, 9=2, 10=1, 11=2, 12=1, 13=2, 14=1}
secondary:{0=0, 1=0, 2=0, 3=1, 4=0, 5=0, 6=0, 7=1, 8=0, 9=0, 10=0, 11=1, 12=0, 13=0, 14=0}
hosttomachine:{1=machineP, 4=machineQ}
DATACENTER=tuv
TotalNumberOfServers:0
primary:{}
secondary:{}
hosttomachine:{}
After parsing the data I need to store each datacenter data in a Map
like this:
HashMap<String, Map<Integer, String>> primaryData
For example, the Key of primaryData
is abc
and value is:
{0=1, 1=2, 2=1, 3=2, 4=1, 5=2, 6=1, 7=2, 8=1, 9=2, 10=1, 11=2, 12=1, 13=2}
which is for primary.
Similarly another Map
for secondary for each datacenter:
HashMap<String, Map<Integer, String>> secondaryData
For example, the Key of secondaryData
is abc
and value is:
{0=0, 1=0, 2=0, 3=1, 4=0, 5=0, 6=0, 7=1, 8=0, 9=0, 10=0, 11=1, 12=0, 13=0}
which is for secondary.
And lastly, one more map for hosttomachine
mapping for each datacenter:
HashMap<String, Map<Integer, String>> hostMachineMapping -
For example, the Key of hostMachineMapping
is abc
and value is:
{3=machineA, 2=machineB, 1=machineC, 4=machineD}
which is for hosttomachine
.
And all the above map will have data for its datacenter as I have three datacenter in the above example. So each each map will have three data. And also I will parse the above response only when hasProcess
is equal to true
. If it is not true, then I won't parse anything.
This code takes more than 200 ms to parse the data and store it in its corresponding HashMap
. Is there any way to parse the above data efficiently and store it in particular HashMap
?
private void parseResponse(String response) throws Exception {
if (response != null) {
Map<String, Map<Integer, String>> primaryData = null;
Map<String, Map<Integer, String>> secondaryData = null;
Map<String, Map<Integer, String>> hostMachineMapping = null;
long version = 0L;
boolean changed = false;
String splitResponse[] = response.split("DATACENTER=");
boolean flag = false;
for (String sr : splitResponse) {
if (!flag) {
flag = true;
String[] header = sr.split("\n");
changed = Boolean.parseBoolean(header[0].split("=")[1]);
if (!changed) {
return;
} else {
version = Integer.parseInt(header[1].split("=")[1]);
primaryData = new HashMap<String, Map<Integer, String>>();
secondaryData = new HashMap<String, Map<Integer, String>>();
hostMachineMapping = new HashMap<String, Map<Integer, String>>();
}
} else {
generateDataCenterMapping(sr, primaryData, secondaryData, hostMachineMapping);
}
}
if (changed) {
Mapping.setPrimaryData(primaryData);
Mapping.setSecondaryData(secondaryData);
Mapping.setHostMachineMapping(hostMachineMapping);
Mapping.setVersion(version);
}
}
}
private void generateDataCenterMapping(String sr, Map<String, Map<Integer, String>> primaryData,
Map<String, Map<Integer, String>> secondaryData,
Map<String, Map<Integer, String>> hostMachineMapping) throws Exception {
String[] data = sr.split("\n\t");
String dcName = data[0];
int numOfServers = Integer.parseInt(data[1].split(":")[1]);
if (numOfServers > 0) {
primaryData.put(dcName, generateMap(data[2]));
secondaryData.put(dcName, generateMap(data[3]));
hostMachineMapping.put(dcName, generateMap(data[4]));
}
}
private Map<Integer, String> generateMap(String map) throws Exception {
String tableString = map.split(":")[1];
Map<Integer, String> table = new HashMap<Integer, String>();
tableString = tableString.substring(1, tableString.length() - 1);
String[] entries = tableString.split(", ");
for (String e : entries) {
String[] entryVal = e.split("=");
table.put(Integer.parseInt(entryVal[0]), entryVal[1]);
}
return table;
}
parseResponse
method? Does it not have any public members? \$\endgroup\$