Reputation: 420
We work on ecommerce domain. We process every order at very high speed. Every order can be cancelled and replaced according to the user.
Whatever it happens the orders must not get missed. So as a result whatever order we receive, we store in a file in the form of a string and when the user cancels the order or the order gets filled then we delete the respective String from the file.
There can also be a situation like user can cancel more than 50 orders at a time. As a result the general method for deleting the String from a file : storing all the strings in a file and delete it by searching in the list is causing a very high latency.
For one cancel request it is not a problem but when the user wants to cancel all the orders at a time then this method causing a big issue.
My current code for deleting a string from the file:
public void deleteFromFile(String clordID) throws InterruptedException {
//delete based on clorid from the file
BufferedReader in = null;
String line = null;
try {
in = new BufferedReader(new FileReader(dir + "\\pendingOrders.txt"));
List<String> listLines = new ArrayList<>();
while ((line = in.readLine()) != null) {
if (!(line.contains(clordID))) { // check for Order ID which is unique for every string
listLines.add(line);
}
}
in.close();
BufferedWriter bufferedWriter1 = new BufferedWriter(new FileWriter(dir + "\\pendingOrders.txt"));
bufferedWriter1.write("");
bufferedWriter1.close();
BufferedWriter bufferedWriter2 = new BufferedWriter(new FileWriter(dir + "\\pendingOrders.txt", true));
for (String msg : listLines) {
bufferedWriter2.write(msg + "\n");
}
//in.close();
bufferedWriter2.close();
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
}//Delete
Is there a way to improve the performance? Every time a cancel request raises, storing all the strings into the list and again clearing the file and writing into the file is definitely causing high latency.
Upvotes: 0
Views: 64
Reputation: 1354
Despite my comment where I mention the pitfalls of using files, looking at your code, I can find the following improvements,
PS : Again, as I have pointed out in my comment and so have many others, if this is real eCommerce site and is going into production, things would be a disaster.
Upvotes: 1
Reputation: 993
Well let met try -
Yes you can improve performance with few fine tuning.
Note:- I believe you are not running any e-commerce site, but doing some school summer project. But in case you are using it for a real time application. Please listen to the people who already have give you very good suggestion.
Upvotes: 1