[ 
https://issues.apache.org/jira/browse/LUCENE-8476?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16603154#comment-16603154
 ] 

Namgyu Kim commented on LUCENE-8476:
------------------------------------

Hi, [~jpountz] and [~jim.ferenczi].

Thank you for your reply.

After reviewing the feedback, I realized that I checked wrong. :(

I'm sorry I made the wrong report.

Here's how to check :
{code:java}
public class Test {
    public static void main(String[] args) throws IOException {
        String file = "D:\\test.txt";
        FileOutputStream stream = null;
        try {
            stream = new FileOutputStream(file);
            writeLine(stream, "First");
            writeLine(stream, "Second");
        } finally {
            if (stream != null)
                stream.close();
        }
    }

    private static void writeLine(OutputStream stream, String line) throws 
IOException {
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(stream));
            writer.write(line + "\n");
        } finally {
            if (writer != null)
                writer.close();
        }
    }
}{code}
This code gives me the following results:
=> (Exception in thread "main" java.io.IOException: Stream Closed)

This means that my modification can cause other problems.

So I'll update a new patch that doesn't include "Bug-1)"

>  Bug fix and optimizations in UserDictionary(KoreanAnalyzer)
> ------------------------------------------------------------
>
>                 Key: LUCENE-8476
>                 URL: https://issues.apache.org/jira/browse/LUCENE-8476
>             Project: Lucene - Core
>          Issue Type: Bug
>          Components: modules/analysis
>            Reporter: Namgyu Kim
>            Priority: Major
>              Labels: bugfix, memory-leak, optimization, patch-available
>         Attachments: LUCENE-8476.patch
>
>
> ■ Bug fix
> 1) BufferedReader's close method is not called.
> {code:java}
> // Line 57 method
> public static UserDictionary open(Reader reader) throws IOException {
>   BufferedReader br = new BufferedReader(reader);
>   String line = null;
>   List<String> entries = new ArrayList<>();
>   // text + optional segmentations
>   while ((line = br.readLine()) != null) {
>     ...
>   }
>   if (entries.isEmpty()) {
>     return null;
>   } else {
>     return new UserDictionary(entries);
>   }
> }{code}
> If you look at the code above, there is no close() method for the "br" 
> variable.
>  As I know, BufferedReader can cause a +memory leak+ if the close method is 
> not called.
>  So I changed the code below.
> {code:java}
> // Line 57 method
> public static UserDictionary open(Reader reader) throws IOException {
>   String line = null;
>   List<String> entries = new ArrayList<>();
>   // text + optional segmentations
>   try (BufferedReader br = new BufferedReader(reader)) {
>     while ((line = br.readLine()) != null) {
>       ...
>     }
>   }
>   if (entries.isEmpty()) {
>     return null;
>   } else {
>     return new UserDictionary(entries);
>   }
> }
> {code}
> I solved this problem with 
> "[try-with-resources|https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html]";
>  method available since Java 7.
>  
> ■ Optimizations
> 1) Change from Collections.sort to List.sort (UserDictionary constructor)
> {code:java}
> // Line 82 method
> private UserDictionary(List<String> entries) throws IOException {
>   final CharacterDefinition charDef = CharacterDefinition.getInstance();
>   Collections.sort(entries,
>       Comparator.comparing(e -> e.split("\\s+")[0]));
>   PositiveIntOutputs fstOutput = PositiveIntOutputs.getSingleton();
>   ...
> }{code}
> List.sort in Java 8 is known to be faster than existing Collections.sort. 
> ([http://ankitsambyal.blogspot.com/2014/03/difference-between-listsort-and.html])
>  So I changed the code below.
> {code:java}
> // Line 82 method
> private UserDictionary(List<String> entries) throws IOException {
>   final CharacterDefinition charDef = CharacterDefinition.getInstance();
>   entries.sort(Comparator.comparing(e -> e.split("\\s+")[0]));
>   PositiveIntOutputs fstOutput = PositiveIntOutputs.getSingleton();
>   ...
> }{code}
>  
> 2) Remove unnecessary null check (UserDictionary constructor)
> {code:java}
> // Line 82 method
> private UserDictionary(List<String> entries) throws IOException {
>   ...
>   String lastToken = null;
>   ...
>   for (String entry : entries) {
>     String[] splits = entry.split("\\s+");
>     String token = splits[0];
>     if (lastToken != null && token.equals(lastToken)) {
>       continue;
>     }
>     char lastChar = entry.charAt(entry.length()-1);
>   ...
> }{code}
> Looking at this part of the code,
> {code:java}
> if (lastToken != null && token.equals(lastToken)) {
>   continue;
> }{code}
> A null check for lastToken is unnecessary.
>  Because the equals method of the String class internally performs a null 
> check.
>  So I changed the code as below.
> {code:java}
> // Line 82 method
> private UserDictionary(List<String> entries) throws IOException {
>   ...
>   String lastToken = null;
>   ...
>   for (String entry : entries) {
>     String[] splits = entry.split("\\s+");
>     String token = splits[0];
>     if (token.equals(lastToken)) {
>       continue;
>     }
>     char lastChar = entry.charAt(entry.length()-1);
>   ...
> }{code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to