从JAVA中的文件高性能删除字符串 [英] High Performance deletion of a String from file in JAVA

查看:136
本文介绍了从JAVA中的文件高性能删除字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在电子商务领域工作。我们以极高的速度处理每一笔订单。每个订单都可以取消,并根据用户进行更换。



无论发生什么,订单都不能错过。因此,无论我们收到什么订单,我们都以字符串的形式存储在文件中,当用户取消订单或订单被填充时,我们从文件中删除相应的字符串。



用户也可以一次取消50多个订单。因此,从文件中删除字符串的一般方法是:将所有字符串存储在文件中并通过在列表中搜索来删除它,这会造成非常高的延迟。



对于一个取消请求,这不是一个问题,但是当用户想要一次取消所有的订单,那么这个方法会造成很大的问题。 b
$ b

我的当前代码是从文件中删除一个字符串:

  public void deleteFromFile(String clordID)throws InterruptedException {
//根据文件中的clorid删除
BufferedReader in = null;
String line = null;
尝试{
in = new BufferedReader(new FileReader(dir +\\pendingOrders.txt));
列表< String> listLines = new ArrayList<>(); ((line = in.readLine())!= null){
if(!(line.contains(clordID))){//检查Order ID是唯一的为每个字符串
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 +\\\\\\\\\\\\\\\\\,true));
for(String msg:listLines){
bufferedWriter2.write(msg +\\\
);
}
//in.close();
bufferedWriter2.close();
$ b $ catch(IOException ex){
System.err.println(ex.getMessage());

} //删除

有没有办法提高性能?每当取消请求提出时,将所有字符串存储到列表中,再次清除文件并写入文件肯定会导致高延迟。

div>

尽管我提到了使用文件的缺陷,但是看看你的代码,我可以找到以下改进:
$ b $ ol

  • 如果clorID是唯一的,为什么你检查它的权利,直到文件的结尾?一旦找到包含它的行,你不应该跳出for循环吗?

  • 因为clorID是唯一的,所以它只会在你的文件中出现一次。为什么你需要创建一个列表呢?

  • 如果有一个创建clorID的模式(如果它是一个序列,时间戳记等),那么根据模式创建文件。 (例如:clorid 1 - 10000转到File1,10001转到20000转到File2等等。)这将大大减少所需的时间。

  • 奇怪的是,尽管您在声明中提到你想删除字符串,你的代码不会这样做。你正在做的是再次写同一个字符串!为什么?你应该做的是从pendingOrders.txt
    中删除该字符串,并将其放在cancelledOrders.txt或fulfilledOrders.txt中。

    $ b PS:同样,正如我在评论中指出的那样,还有很多其他人,如果这是真正的电子商务网站并正在投入生产,事情将是一场灾难。


    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.

    解决方案

    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.

    这篇关于从JAVA中的文件高性能删除字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆