You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucene.apache.org by "Aaron Isotton (JIRA)" <ji...@apache.org> on 2007/06/25 15:50:25 UTC

[jira] Created: (LUCENE-942) TopDocCollector.topDocs throws ArrayIndexOutOfBoundsException when called twice

TopDocCollector.topDocs throws ArrayIndexOutOfBoundsException when called twice
-------------------------------------------------------------------------------

                 Key: LUCENE-942
                 URL: https://issues.apache.org/jira/browse/LUCENE-942
             Project: Lucene - Java
          Issue Type: Bug
          Components: Search
    Affects Versions: 2.2
            Reporter: Aaron Isotton
            Priority: Minor


Here's the implementation of TopDocCollector.topDocs():

  public TopDocs topDocs() {
    ScoreDoc[] scoreDocs = new ScoreDoc[hq.size()];
    for (int i = hq.size()-1; i >= 0; i--)      // put docs in array
      scoreDocs[i] = (ScoreDoc)hq.pop();
      
    float maxScore = (totalHits==0)
      ? Float.NEGATIVE_INFINITY
      : scoreDocs[0].score;
    
    return new TopDocs(totalHits, scoreDocs, maxScore);
  }

When you call topDocs(), hq gets emptied. Thus the second time you call it scoreDocs.length will be 0 and scoreDocs[0] will throw an ArrayIndexOutOfBoundsException.

I don't know whether this 'call only once' semantics is intended behavior or not; if not, it should be fixed, if yes it should be documented.

Thanks a lot for an absolutely fantastic product,
Aaron

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: java-dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-dev-help@lucene.apache.org


[jira] Commented: (LUCENE-942) TopDocCollector.topDocs throws ArrayIndexOutOfBoundsException when called twice

Posted by "Hoss Man (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LUCENE-942?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12508018 ] 

Hoss Man commented on LUCENE-942:
---------------------------------

this seems like both a documentation issue, and a bad state checking issue.

the javadoc for topDocs should indicate that it should only be called once all collection is done, and that calling it resets the internal state so that it can no longer be called again.

the internals of topDocs should be changed so that it doesn't rely on "totalHits==0" to indicate if there is a maxScore available and throws a meaninful exception if the method is called more then once.  I would suggest moving the maxScore assignment above the loop using hq.top() as both an indicator of a top score being available and as the top score itself, and wrapping the whole body of the method with something like ...

  if (null == hq) throw new IllegalStateException("topDocs can not be called twice")
  try { 
      ... 
  } finally { hq=null}

> TopDocCollector.topDocs throws ArrayIndexOutOfBoundsException when called twice
> -------------------------------------------------------------------------------
>
>                 Key: LUCENE-942
>                 URL: https://issues.apache.org/jira/browse/LUCENE-942
>             Project: Lucene - Java
>          Issue Type: Bug
>          Components: Search
>    Affects Versions: 2.2
>            Reporter: Aaron Isotton
>            Priority: Minor
>
> Here's the implementation of TopDocCollector.topDocs():
>   public TopDocs topDocs() {
>     ScoreDoc[] scoreDocs = new ScoreDoc[hq.size()];
>     for (int i = hq.size()-1; i >= 0; i--)      // put docs in array
>       scoreDocs[i] = (ScoreDoc)hq.pop();
>       
>     float maxScore = (totalHits==0)
>       ? Float.NEGATIVE_INFINITY
>       : scoreDocs[0].score;
>     
>     return new TopDocs(totalHits, scoreDocs, maxScore);
>   }
> When you call topDocs(), hq gets emptied. Thus the second time you call it scoreDocs.length will be 0 and scoreDocs[0] will throw an ArrayIndexOutOfBoundsException.
> I don't know whether this 'call only once' semantics is intended behavior or not; if not, it should be fixed, if yes it should be documented.
> Thanks a lot for an absolutely fantastic product,
> Aaron

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: java-dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-dev-help@lucene.apache.org


[jira] Commented: (LUCENE-942) TopDocCollector.topDocs throws ArrayIndexOutOfBoundsException when called twice

Posted by "Doron Cohen (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LUCENE-942?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12508025 ] 

Doron Cohen commented on LUCENE-942:
------------------------------------

Perhaps simpler to make the scoreDocs[] array a private data member, which null until topDocs[] is called for the first time, and computed only once - then, repeated calls to topDocs() would use that same array... ?

> TopDocCollector.topDocs throws ArrayIndexOutOfBoundsException when called twice
> -------------------------------------------------------------------------------
>
>                 Key: LUCENE-942
>                 URL: https://issues.apache.org/jira/browse/LUCENE-942
>             Project: Lucene - Java
>          Issue Type: Bug
>          Components: Search
>    Affects Versions: 2.2
>            Reporter: Aaron Isotton
>            Priority: Minor
>
> Here's the implementation of TopDocCollector.topDocs():
>   public TopDocs topDocs() {
>     ScoreDoc[] scoreDocs = new ScoreDoc[hq.size()];
>     for (int i = hq.size()-1; i >= 0; i--)      // put docs in array
>       scoreDocs[i] = (ScoreDoc)hq.pop();
>       
>     float maxScore = (totalHits==0)
>       ? Float.NEGATIVE_INFINITY
>       : scoreDocs[0].score;
>     
>     return new TopDocs(totalHits, scoreDocs, maxScore);
>   }
> When you call topDocs(), hq gets emptied. Thus the second time you call it scoreDocs.length will be 0 and scoreDocs[0] will throw an ArrayIndexOutOfBoundsException.
> I don't know whether this 'call only once' semantics is intended behavior or not; if not, it should be fixed, if yes it should be documented.
> Thanks a lot for an absolutely fantastic product,
> Aaron

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: java-dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-dev-help@lucene.apache.org


[jira] Commented: (LUCENE-942) TopDocCollector.topDocs throws ArrayIndexOutOfBoundsException when called twice

Posted by "Hoss Man (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LUCENE-942?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12508039 ] 

Hoss Man commented on LUCENE-942:
---------------------------------

that makes sense ... but there is still a state issue of "don't call topDocs() until done collecting" .. as is a TopDocCollector can't be reused (for more then one search) because totalHits grows without ever being reset, but hq does get reset when topDocs() is called ... currently this is unintentionally enforced by the ArrayIndexOutOfBoundsException, it would be a good idea to continue to enforce it (with an exception that has a better class/message)

a finer point thta some might nit pick about is that it's generally considered a bad idea to return a refrence a member array since your client can mutate it out from under you ... i don't personally think that's much of a concern in this case since there is no expectation that TopDocCollector will ever touch the array again.

> TopDocCollector.topDocs throws ArrayIndexOutOfBoundsException when called twice
> -------------------------------------------------------------------------------
>
>                 Key: LUCENE-942
>                 URL: https://issues.apache.org/jira/browse/LUCENE-942
>             Project: Lucene - Java
>          Issue Type: Bug
>          Components: Search
>    Affects Versions: 2.2
>            Reporter: Aaron Isotton
>            Priority: Minor
>
> Here's the implementation of TopDocCollector.topDocs():
>   public TopDocs topDocs() {
>     ScoreDoc[] scoreDocs = new ScoreDoc[hq.size()];
>     for (int i = hq.size()-1; i >= 0; i--)      // put docs in array
>       scoreDocs[i] = (ScoreDoc)hq.pop();
>       
>     float maxScore = (totalHits==0)
>       ? Float.NEGATIVE_INFINITY
>       : scoreDocs[0].score;
>     
>     return new TopDocs(totalHits, scoreDocs, maxScore);
>   }
> When you call topDocs(), hq gets emptied. Thus the second time you call it scoreDocs.length will be 0 and scoreDocs[0] will throw an ArrayIndexOutOfBoundsException.
> I don't know whether this 'call only once' semantics is intended behavior or not; if not, it should be fixed, if yes it should be documented.
> Thanks a lot for an absolutely fantastic product,
> Aaron

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: java-dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-dev-help@lucene.apache.org