hema chandra
hema chandra

Reputation: 420

High Performance deletion of a String from file in JAVA

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

Answers (2)

Nishit
Nishit

Reputation: 1354

Despite my comment where I mention the pitfalls of using files, looking at your code, I can find the following improvements,

  1. If clorID is unique, why are you checking it right till the end of file? Shouldn't you break out of your for loop as soon as you find the line that contains it?
  2. Since clorID is unique, it is going to be there only once in your file. Why do you need to create a list then?
  3. If there is a pattern in which clorID is created (if it is a sequence, timestamp etc.), then create your file based on the pattern. (For ex. clorid 1 - 10000 go to File1, 10001 to 20000 go to File2 and so on.) This will drastically reduce the time taken.
  4. Strangely, though you mention in your statement that you want to delete the string, your code doesn't do that. What you are doing is writing the same string again! Why? What you should be doing is remove the string from pendingOrders.txt and put it in either cancelledOrders.txt or fulfilledOrders.txt

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

Ravi Kumar
Ravi Kumar

Reputation: 993

Well let met try -

Yes you can improve performance with few fine tuning.

  1. Split file into smaller chunks depending on the volume of transaction. Could be 1 file per day or even per hour.
  2. I believe clordID argument in your method is orderid, Maintain date in the order id in it something like ddMMyyyy-orderid.
  3. When you get request for deletion first extract date from order id and then open file that belongs to that day. This will reduce the number of line you need to iterate.

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

Related Questions