Skip to content

Commit

Permalink
fix iteration bug. it would always return hasNext=false if seekTo was…
Browse files Browse the repository at this point in the history
… called first. fixing this with maybeInit proved difficult, so init is now done in constructor and iterator itself is created lazily.

patch by jbellis; tested by nk11 for CASSANDRA-153

git-svn-id: https://svn.apache.org/repos/asf/incubator/cassandra/trunk@773017 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
jbellis committed May 8, 2009
1 parent f2260f1 commit 61a8150
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/java/org/apache/cassandra/db/FileStruct.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class FileStruct implements Comparable<FileStruct>, Iterator<String>
private DataInputBuffer bufIn;
private DataOutputBuffer bufOut;
private IPartitioner partitioner;
private FileStructIterator iterator = new FileStructIterator();
private FileStructIterator iterator;

public FileStruct(IFileReader reader, IPartitioner partitioner)
{
Expand Down Expand Up @@ -150,12 +150,16 @@ public void advance() throws IOException

public boolean hasNext()
{
if (iterator == null)
iterator = new FileStructIterator();
return iterator.hasNext();
}

/** do not mix with manual calls to advance(). */
public String next()
{
if (iterator == null)
iterator = new FileStructIterator();
return iterator.next();
}

Expand All @@ -168,6 +172,18 @@ private class FileStructIterator
{
String saved;

public FileStructIterator()
{
if (key == null)
{
if (!isExhausted())
{
forward();
}
}
saved = key;
}

private void forward()
{
try
Expand All @@ -181,23 +197,13 @@ private void forward()
saved = isExhausted() ? null : key;
}

private void maybeInit()
{
if (key == null && !isExhausted())
{
forward();
}
}

public boolean hasNext()
{
maybeInit();
return saved != null;
}

public String next()
{
maybeInit();
if (saved == null)
{
throw new IndexOutOfBoundsException();
Expand Down

0 comments on commit 61a8150

Please sign in to comment.