-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding alternative iterator interface (this commit will be squashed l…
…ater) - Adding iterator interface, and leaving the lower-level interface in place for the moment - Adding test for iterator interface
- Loading branch information
Marco Primi
committed
Jan 8, 2016
1 parent
2994e44
commit 9198fcc
Showing
3 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
src/java/main/org/apache/zookeeper/ChildrenBatchIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.apache.zookeeper; | ||
|
||
import org.apache.zookeeper.data.PathWithStat; | ||
|
||
import javax.naming.OperationNotSupportedException; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
/** | ||
* Iterator over children nodes of a given path. | ||
*/ | ||
class ChildrenBatchIterator implements Iterator<PathWithStat> { | ||
private final ZooKeeper zooKeeper; | ||
private final String path; | ||
private final Watcher watcher; | ||
private final int batchSize; | ||
private Iterator<PathWithStat> currentBatchIterator; | ||
private List<PathWithStat> currentBatch; | ||
private long highestCZkId = -1; | ||
|
||
ChildrenBatchIterator(ZooKeeper zooKeeper, String path, Watcher watcher, int batchSize, int minZkid) | ||
throws KeeperException, InterruptedException { | ||
this.zooKeeper = zooKeeper; | ||
this.path = path; | ||
this.watcher = watcher; | ||
this.batchSize = batchSize; | ||
this.highestCZkId = minZkid; | ||
|
||
this.currentBatch = zooKeeper.getChildren(path, watcher, batchSize, minZkid); | ||
this.currentBatchIterator = currentBatch.iterator(); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return currentBatchIterator.hasNext(); | ||
} | ||
|
||
@Override | ||
public PathWithStat next() { | ||
|
||
if(!hasNext()) { | ||
throw new RuntimeException("No more element"); | ||
} | ||
|
||
PathWithStat returnChildren = currentBatchIterator.next(); | ||
|
||
highestCZkId = returnChildren.getStat().getCzxid(); | ||
|
||
// If we reached the end of the current batch, fetch the next one | ||
|
||
try { | ||
this.currentBatch = zooKeeper.getChildren(path, watcher, batchSize, highestCZkId); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Failed to fetch next batch", e); | ||
} | ||
|
||
this.currentBatchIterator = currentBatch.iterator(); | ||
|
||
return returnChildren; | ||
} | ||
|
||
@Override | ||
public void remove() { | ||
throw new RuntimeException(new OperationNotSupportedException("remove not supported")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters