org.mmbase.bridge.util
Class HugeNodeListIterator

java.lang.Object
  extended by org.mmbase.bridge.util.HugeNodeListIterator
All Implemented Interfaces:
Iterator<Node>, ListIterator<Node>, NodeIterator

public class HugeNodeListIterator
extends Object
implements NodeIterator

Iterates the big result of a query. It avoids using a lot of memory (which you would need if you get the complete NodeList first), and pollution of the (node) cache. In this current implementation the Query is 'batched' to avoid reading in all nodes in memory, and the queries are marked with CachePolicy.NEVER.

Since:
MMBase-1.8
Version:
$Id: HugeNodeListIterator.java 41943 2010-04-16 17:18:19Z michiel $
Author:
Michiel Meeuwissen

Field Summary
protected  int batchSize
           
static int DEFAULT_BATCH_SIZE
           
protected  int nextIndex
           
protected  Node nextNode
           
protected  NodeIterator nodeIterator
           
protected  Query originalQuery
           
protected  Node previousNode
           
 
Constructor Summary
HugeNodeListIterator(Query query)
          Constructor for this Iterator.
HugeNodeListIterator(Query query, int batchSize)
          Constructor for this Iterator.
 
Method Summary
 void add(Node o)
           
protected  int compares(Node node1, Node node2)
          Used by nextNode and previousNode.
protected  void executeNextQuery(Query q)
          Executes the given query, and prepares to do 'next', so setting 'nextNode' and 'previousNode'.
protected  void executePreviousQuery(Query q)
          Executes the given query, and prepares to do 'previous', so setting 'nextNode' and 'previousNode', and winds the new nodeIterator to the end.
protected  void executeQuery(Query currentQuery)
          Executes the given query, taking into account the fact wether it is NodeQuery or not, and applying the 'batchSize'.
 boolean hasNext()
          
 boolean hasPrevious()
          
static void main(String[] args)
          Main only for testing.
 Node next()
          
 int nextIndex()
          
 Node nextNode()
          Returns the next element in the iterator as a Node
 Node previous()
          
 int previousIndex()
          
 Node previousNode()
          Returns the previous element in the iterator as a Node
 void remove()
           
 void set(Node o)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_BATCH_SIZE

public static final int DEFAULT_BATCH_SIZE
See Also:
Constant Field Values

nodeIterator

protected NodeIterator nodeIterator

nextNode

protected Node nextNode

previousNode

protected Node previousNode

originalQuery

protected Query originalQuery

batchSize

protected int batchSize

nextIndex

protected int nextIndex
Constructor Detail

HugeNodeListIterator

public HugeNodeListIterator(Query query,
                            int batchSize)
Constructor for this Iterator.

Parameters:
query - The query which is used as a base for the querie(s) to be executed.
batchSize - The (approximate) size of the sub-queries, should be a reasonably large number, like 10000 or so.

HugeNodeListIterator

public HugeNodeListIterator(Query query)
Constructor for this Iterator. The 'batchSize' is taken from the query's 'maxnumber' properties, or, it that is not set, it is defaulted to 10000.

Parameters:
query - The query which is used as a base for the querie(s) to be executed.
Method Detail

executeQuery

protected void executeQuery(Query currentQuery)
Executes the given query, taking into account the fact wether it is NodeQuery or not, and applying the 'batchSize'. The result is available in the 'nodeIterator' member.


executeNextQuery

protected void executeNextQuery(Query q)
Executes the given query, and prepares to do 'next', so setting 'nextNode' and 'previousNode'.


executePreviousQuery

protected void executePreviousQuery(Query q)
Executes the given query, and prepares to do 'previous', so setting 'nextNode' and 'previousNode', and winds the new nodeIterator to the end.


hasNext

public boolean hasNext()

Specified by:
hasNext in interface Iterator<Node>
Specified by:
hasNext in interface ListIterator<Node>

hasPrevious

public boolean hasPrevious()

Specified by:
hasPrevious in interface ListIterator<Node>

next

public Node next()

Specified by:
next in interface Iterator<Node>
Specified by:
next in interface ListIterator<Node>

previous

public Node previous()

Specified by:
previous in interface ListIterator<Node>

previousIndex

public int previousIndex()

Specified by:
previousIndex in interface ListIterator<Node>

nextIndex

public int nextIndex()

Specified by:
nextIndex in interface ListIterator<Node>

compares

protected int compares(Node node1,
                       Node node2)
Used by nextNode and previousNode. Does a field-by-field compare of two Node objects, on the fields used to order the nodes. This is used to determine whether a node comes after or before another - allowing the node iterator to skip nodes it already 'had'.

Returns:
-1 if node1 is smaller than node 2, 0 if both nodes are equals, and +1 is node 1 is greater than node 2.

nextNode

public Node nextNode()
Returns the next element in the iterator as a Node Implementation calculates also the next next Node, and gives back the 'old' next Node, from now on known as 'previousNode'.

Specified by:
nextNode in interface NodeIterator
Returns:
next Node

previousNode

public Node previousNode()
Returns the previous element in the iterator as a Node Implementation is analogous to nextNode.

Specified by:
previousNode in interface NodeIterator
Returns:
previous Node

remove

public void remove()
Specified by:
remove in interface Iterator<Node>
Specified by:
remove in interface ListIterator<Node>
Throws:
UnsupportedOperationException

add

public void add(Node o)
Specified by:
add in interface ListIterator<Node>
Throws:
UnsupportedOperationException

set

public void set(Node o)
Specified by:
set in interface ListIterator<Node>
Throws:
UnsupportedOperationException

main

public static void main(String[] args)
Main only for testing.



MMBase 2.0-SNAPSHOT - null