Solved Efficient File Reading

Discussion in 'Plugin Development' started by ShowbizLocket61, Jan 21, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    ShowbizLocket61

    Consider this:
    I have a considerably large file, composed of more than 1000 lines separated by carriage returns.
    There are a few lines which have certain keywords, for the purposes of this thread I will refer to it as "key".

    I want to retrieve the last line that contains "key". I am currently using a BufferedReader to go line by line until the end of the file, which is has a dismal efficiency of around O(N), ignoring string contains() procedures.

    Trying to retrieve the last line 50 times is already taking an extensive toll on the server, around 5-10 seconds of utter lag.

    My question is: could I read the file with the big O as close to O(1) as possible?
     
  2. Offline

    teej107

    Do it 1 time.
     
  3. Offline

    ShowbizLocket61

    @teej107
    Excuse me? I need to search it an unknown number of times, for different keys each time.
     
  4. Offline

    teej107

    @ShowbizLocket61 ah so it's at different parts of the file? Do it at the beginning in the onEnable.
     
  5. Offline

    ShowbizLocket61

    @teej107
    What do you mean? The file updates at erratic times.

    And who wants to lag out the server on enable?

    The point of this thread is to make the searching more efficient, not to avoid it altogether.
     
  6. Offline

    timtower Administrator Administrator Moderator

  7. Offline

    ShowbizLocket61

    @timtower
    The file has a fixed location, and I have no control over it whatsoever. Only reading.
     
  8. Offline

    timtower Administrator Administrator Moderator

    @ShowbizLocket61 What kind of file is it? There are better ways if it is a log.
     
  9. Offline

    ShowbizLocket61

  10. Offline

    teej107

    If I wanted to make this more efficient, I would tell you to use a different formatting for the file.
    "Lagging" out the server in the onEnable is the best time to do so because rarely are players playing when you restart or reload plugins. Whenever you update the file, update the values you have saved in the code.
     
  11. Offline

    ShowbizLocket61

    @teej107
    The file is not mine; I have no control over it; I can only read it. I also have no way of telling when it updates.
     
  12. Offline

    teej107

    Who's file is it then?
     
  13. Offline

    ShowbizLocket61

  14. Offline

    timtower Administrator Administrator Moderator

    @ShowbizLocket61 Can't you ask the other plugin author to add database support (because CSV is pretty much a table) or some kind of API to hook into?
     
  15. Offline

    ShowbizLocket61

    @timtower
    No, I can't.
    Could we stay on topic, please?

    The point of this thread is to find a way to make the searching more efficient, not to avoid it.
     
  16. Offline

    timtower Administrator Administrator Moderator

    @ShowbizLocket61 There is no way to make this more efficient.
    File reading takes time, do it async then.
     
  17. Offline

    mcdorli

    If you know, when the file updates (I mean, you pretty much can gues, aren't you?), then only do the file reading then, or as suggested above, use an Async task
     
  18. Offline

    teej107

    Have you tried hooking into the plugin?
     
    timtower likes this.
  19. Offline

    Mrs. bwfctower

    @ShowbizLocket61 Apache Commons has a class, ReversedLinesFileReader. It is very similar to a BufferedReader, but it starts with the last line rather than the first. You can get the last line using this class.

    And it's packaged with Bukkit!

    EDIT: Oh you want the last line containing 'key'. Nevermind then.
     
  20. Offline

    mythbusterma

    @ShowbizLocket61

    Why is O(n) dismal? And you made the same mistake again. String searching is still O(n*m), regardless, this isn't "dismal," especially with such a small file.

    Why wouldn't you try to avoid the operation instead of just making it "more efficient?" That seems the most practical method for achieving your flawed idea of "efficient and space saving as possible" or whatever that was.

    There's no reason you can't ask the other author to add an API, or add it yourself by recompiling it or adding introspection.

    And how do you propose that being possible? You still have to read over the entire file, and that is by necessity O(n), there's no way to avoid that. That being said, there's no reason this should be taking 5-10 seconds, it should be taking on that order of time. Post your code, there has to be a pretty egregious error in it for that to be the case.

    How certain are you of this? Because that's not true. In the least.

    https://docs.oracle.com/javase/tutorial/essential/io/notification.html
     
    Mrs. bwfctower likes this.
  21. Offline

    ShowbizLocket61

    @Mrs. bwfctower
    Thanks for that! It'll be useful since the target line would almost always be closer to the end than the beginning.

    @mythbusterma
    I said, O(n) disregarding string operations. I don't want to try to avoid the operation, because that operation is the only way to do it. I was asking for you all to answer the question in the context and factors provided by the question itself.

    Anyhow, I posted this thread for help and ideas, not a heated argument like so many of the threads on this forum have became. If Mrs. bwfctower had not suggested the reversed reader and if mythbusterma had not put out that Watch Service link, I would have definitely regretted posting.

    Even though my problem is not exactly solved, I will mark it as so because I am just tired of these thinly veiled attacks popping up around the forums.
     
  22. Offline

    mythbusterma

    @ShowbizLocket61

    I'm not attacking you, I'm pointing out flaws in your logic. At not point did insult you, your abilities, or your code.
     
  23. Offline

    567legodude

    I know that feeling, I can hardly give good-habit advice without some noob coming along and trying to say it's wrong.
     
Thread Status:
Not open for further replies.

Share This Page