Updated cursor functionality
diff --git a/stack/core/src/main/java/org/usergrid/persistence/cassandra/QueryProcessor.java b/stack/core/src/main/java/org/usergrid/persistence/cassandra/QueryProcessor.java
index b99fe56..b5d77a3 100644
--- a/stack/core/src/main/java/org/usergrid/persistence/cassandra/QueryProcessor.java
+++ b/stack/core/src/main/java/org/usergrid/persistence/cassandra/QueryProcessor.java
@@ -242,6 +242,16 @@
     }
 
 
+    /**
+     * Return the node id from the cursor cache
+     * @param nodeId
+     * @return
+     */
+    public ByteBuffer getCursorCache(int nodeId){
+        return cursorCache.getCursorBytes( nodeId );
+    }
+
+
     private SortPredicate getSort( String propertyName ) {
         for ( SortPredicate sort : sorts ) {
             if ( sort.getPropertyName().equals( propertyName ) ) {
@@ -384,7 +394,7 @@
             QueryNode leftResult = nodes.pop();
 
             // rewrite with the new Or operand
-            OrNode orNode = new OrNode( leftResult, rightResult );
+            OrNode orNode = new OrNode( leftResult, rightResult,  ++contextCount );
 
             nodes.push( orNode );
         }
diff --git a/stack/core/src/main/java/org/usergrid/persistence/query/ir/OrNode.java b/stack/core/src/main/java/org/usergrid/persistence/query/ir/OrNode.java
index 4fefdcf..2597312 100644
--- a/stack/core/src/main/java/org/usergrid/persistence/query/ir/OrNode.java
+++ b/stack/core/src/main/java/org/usergrid/persistence/query/ir/OrNode.java
@@ -16,15 +16,29 @@
 package org.usergrid.persistence.query.ir;
 
 
-/** @author tnine */
+/**
+ * @author tnine
+ */
 public class OrNode extends BooleanNode {
 
+    private final int id;
+
+
     /**
      * @param left
      * @param right
      */
-    public OrNode( QueryNode left, QueryNode right ) {
+    public OrNode( QueryNode left, QueryNode right, int id ) {
         super( left, right );
+        this.id = id;
+    }
+
+
+    /**
+     * Get the context id
+     */
+    public int getId() {
+        return this.id;
     }
 
 
diff --git a/stack/core/src/main/java/org/usergrid/persistence/query/ir/SearchVisitor.java b/stack/core/src/main/java/org/usergrid/persistence/query/ir/SearchVisitor.java
index 20837e3..665ddd2 100644
--- a/stack/core/src/main/java/org/usergrid/persistence/query/ir/SearchVisitor.java
+++ b/stack/core/src/main/java/org/usergrid/persistence/query/ir/SearchVisitor.java
@@ -119,7 +119,9 @@
         ResultIterator right = results.pop();
         ResultIterator left = results.pop();
 
-        UnionIterator union = new UnionIterator( queryProcessor.getPageSizeHint( node ) );
+        final int nodeId = node.getId();
+
+        UnionIterator union = new UnionIterator( queryProcessor.getPageSizeHint( node ), nodeId, queryProcessor.getCursorCache(nodeId  ) );
 
         if ( left != null ) {
             union.addIterator( left );
diff --git a/stack/core/src/main/java/org/usergrid/persistence/query/ir/result/UnionIterator.java b/stack/core/src/main/java/org/usergrid/persistence/query/ir/result/UnionIterator.java
index ec5d1a6..efd4e3d 100644
--- a/stack/core/src/main/java/org/usergrid/persistence/query/ir/result/UnionIterator.java
+++ b/stack/core/src/main/java/org/usergrid/persistence/query/ir/result/UnionIterator.java
@@ -16,6 +16,7 @@
 package org.usergrid.persistence.query.ir.result;
 
 
+import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -28,6 +29,8 @@
 import org.usergrid.persistence.cassandra.CursorCache;
 import org.usergrid.utils.UUIDUtils;
 
+import me.prettyprint.cassandra.serializers.UUIDSerializer;
+
 
 /**
  * Simple iterator to perform Unions
@@ -38,17 +41,24 @@
 
     private static final ScanColumnComparator COMP = new ScanColumnComparator();
 
+    private static final UUIDSerializer UUID_SERIALIZER = UUIDSerializer.get();
 
-     private SortedColumnList list;
 
+    private SortedColumnList list;
+
+    private final int id;
 
 
     /**
-     * @param pageSize
+     * @param pageSize The page size to return
+     * @param id The id assigned to this node
+     * @param minUuid The minimum UUID to return
      */
-    public UnionIterator( int pageSize ) {
+    public UnionIterator( int pageSize, int id, ByteBuffer minUuid ) {
         super( pageSize );
-        list = new SortedColumnList( pageSize );
+
+        this.id = id;
+        list = new SortedColumnList( pageSize, UUID_SERIALIZER.fromByteBuffer( minUuid ) );
     }
 
 
@@ -86,8 +96,6 @@
     }
 
 
-
-
     /*
      * (non-Javadoc)
      *
@@ -98,15 +106,16 @@
     @Override
     public void finalizeCursor( CursorCache cache, UUID lastLoaded ) {
 
+        ByteBuffer buff = UUIDSerializer.get().toByteBuffer( lastLoaded );
+        cache.setNextCursor( id, buff );
         //get our scan column and put them in the cache
-       //we finalize the cursor of the min
+        //we finalize the cursor of the min
     }
 
 
     /**
-     * A Sorted Set with a max size. When a new entry is added, the max is removed.  You can mark the next
-     * "min" by calling the mark method.  Values > min are accepted.  Values > min and that are over size are
-     * discarded
+     * A Sorted Set with a max size. When a new entry is added, the max is removed.  You can mark the next "min" by
+     * calling the mark method.  Values > min are accepted.  Values > min and that are over size are discarded
      */
     public static final class SortedColumnList {
 
@@ -120,34 +129,37 @@
         private ScanColumn min;
 
 
-        public SortedColumnList( int maxSize ) {
+        public SortedColumnList( final int maxSize, final UUID minUuid ) {
             //we need to allocate the extra space if required
-            this.list = new ArrayList<ScanColumn>(maxSize);
+            this.list = new ArrayList<ScanColumn>( maxSize );
             this.maxSize = maxSize;
+
+            if ( minUuid != null ) {
+                min = new AbstractScanColumn( minUuid, null ) {};
+            }
         }
 
 
         /**
          * Add the column to this list
-         * @param col
          */
         public void add( ScanColumn col ) {
             //less than our min, don't add
-            if(COMP.compare( min, col ) >= 0){
+            if ( COMP.compare( min, col ) >= 0 ) {
                 return;
             }
 
             int index = Collections.binarySearch( this.list, col, COMP );
 
             //already present
-            if(index > -1){
-                return ;
+            if ( index > -1 ) {
+                return;
             }
 
-            index = (index * -1) - 1;
+            index = ( index * -1 ) - 1;
 
             //outside the renage
-            if(index >= maxSize){
+            if ( index >= maxSize ) {
                 return;
             }
 
@@ -155,34 +167,31 @@
 
             final int size = this.list.size();
 
-            if(size > maxSize){
+            if ( size > maxSize ) {
                 this.list.subList( maxSize, size ).clear();
             }
-
         }
 
 
         /**
          * Add all the elements to this list
-         * @param cols
          */
         public void addAll( final Collection<? extends ScanColumn> cols ) {
-            for(ScanColumn col:cols){
-               add(col);
+            for ( ScanColumn col : cols ) {
+                add( col );
             }
         }
 
 
         /**
          * Returns a new list.  If no elements are present, returns null
-         * @return
          */
-        public Set<ScanColumn> asSet(){
-            if(this.list.size() == 0){
+        public Set<ScanColumn> asSet() {
+            if ( this.list.size() == 0 ) {
                 return null;
             }
 
-            return new LinkedHashSet<ScanColumn>(this.list);
+            return new LinkedHashSet<ScanColumn>( this.list );
         }
 
 
@@ -194,24 +203,20 @@
             final int size = this.list.size();
 
             //we don't have any elements in the list, and we've never set a min
-            if(size == 0){
+            if ( size == 0 ) {
                 return;
             }
 
-            min = this.list.get( size -1);
+            min = this.list.get( size - 1 );
         }
 
 
         /**
          * Clear the list
          */
-        public void clear(){
+        public void clear() {
             this.list.clear();
         }
-
-
-
-
     }
 
 
diff --git a/stack/core/src/test/java/org/usergrid/persistence/CollectionIT.java b/stack/core/src/test/java/org/usergrid/persistence/CollectionIT.java
index a362358..08c69af 100644
--- a/stack/core/src/test/java/org/usergrid/persistence/CollectionIT.java
+++ b/stack/core/src/test/java/org/usergrid/persistence/CollectionIT.java
@@ -579,11 +579,11 @@
 
         Entity returned = r.getEntities().get( 0 );
 
-        assertEquals( game2.getUuid(), returned.getUuid() );
+        assertEquals( game1.getUuid(), returned.getUuid() );
 
         returned = r.getEntities().get( 1 );
 
-        assertEquals( game1.getUuid(), returned.getUuid() );
+        assertEquals( game2.getUuid(), returned.getUuid() );
 
         query = Query.fromQL( "select * where( keywords contains 'Random' OR keywords contains 'Game')" );
 
@@ -593,11 +593,11 @@
 
         returned = r.getEntities().get( 0 );
 
-        assertEquals( game2.getUuid(), returned.getUuid() );
+        assertEquals( game1.getUuid(), returned.getUuid() );
 
         returned = r.getEntities().get( 1 );
 
-        assertEquals( game1.getUuid(), returned.getUuid() );
+        assertEquals( game2.getUuid(), returned.getUuid() );
 
         // field order shouldn't matter USERGRID-375
         query = Query.fromQL( "select * where keywords contains 'blah' OR title contains 'blah'" );
diff --git a/stack/core/src/test/java/org/usergrid/persistence/query/ir/result/UnionIteratorTest.java b/stack/core/src/test/java/org/usergrid/persistence/query/ir/result/UnionIteratorTest.java
index 79fc8af..683f00d 100644
--- a/stack/core/src/test/java/org/usergrid/persistence/query/ir/result/UnionIteratorTest.java
+++ b/stack/core/src/test/java/org/usergrid/persistence/query/ir/result/UnionIteratorTest.java
@@ -82,7 +82,7 @@
         fourth.add( id9 );
 
 
-        UnionIterator iter = new UnionIterator( 100 );
+        UnionIterator iter = new UnionIterator( 100, 0, null );
         iter.addIterator( first );
         iter.addIterator( second );
         iter.addIterator( third );
@@ -119,7 +119,7 @@
         first.add( id3 );
         first.add( id4 );
 
-        UnionIterator union = new UnionIterator( 100 );
+        UnionIterator union = new UnionIterator( 100, 0, null );
         union.addIterator( first );
 
         Set<ScanColumn> ids = union.next();
@@ -151,7 +151,7 @@
         second.add( id3 );
         second.add( id4 );
 
-        UnionIterator union = new UnionIterator( 100 );
+        UnionIterator union = new UnionIterator( 100, 0, null );
         union.addIterator( first );
         union.addIterator( second );
 
@@ -170,7 +170,7 @@
     @Test
     public void testNoIterator() {
 
-        UnionIterator union = new UnionIterator( 100 );
+        UnionIterator union = new UnionIterator( 100, 0, null );
 
         // now make sure it's right, only 1, 3 and 8 intersect
         assertFalse( union.hasNext() );
@@ -229,7 +229,7 @@
         third.add( thirdSet );
 
         // now intersect them and make sure we get all results in a small set
-        UnionIterator union = new UnionIterator( pageSize );
+        UnionIterator union = new UnionIterator( pageSize, 0, null );
         union.addIterator( first );
         union.addIterator( second );
         union.addIterator( third );
@@ -262,7 +262,7 @@
         UUID id5 = UUIDUtils.minTimeUUID( 5 );
 
 
-        UnionIterator union = new UnionIterator( 5 );
+        UnionIterator union = new UnionIterator( 5, 0, null );
 
         InOrderIterator first = new InOrderIterator( 100 );