/*
 * Copyright (C) 2017. OpenLattice, Inc
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * You can contact the owner of the copyright at support@openlattice.com
 *
 */

package com.openlattice.postgres;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.function.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Matthew Tamayo-Rios &lt;matthew@openlattice.com&gt;
 */
public class KeyIterator<T> implements Iterator<T> {
    private static final Logger logger = LoggerFactory.getLogger( KeyIterator.class );
    private final ResultSet                 rs;
    private final CountdownConnectionCloser closer;
    private final Function<ResultSet, T>    mapper;
    private       boolean                   next;

    public KeyIterator( ResultSet rs, CountdownConnectionCloser closer, Function<ResultSet, T> mapper ) {
        this.closer = closer;
        this.rs = rs;
        this.mapper = mapper;
        try {
            next = rs.next();
        } catch ( SQLException e ) {
            logger.error( "Unable to execute sql for load all keys for data map store" );
            throw new IllegalStateException( "Unable to execute sql statement", e );
        }
    }

    @Override public boolean hasNext() {
        return next;
    }

    @Override public T next() {
        T key;
        if ( next ) {
            key = mapper.apply( rs );
        } else {
            throw new NoSuchElementException( "No more elements available in iterator" );
        }
        try {
            next = rs.next();
        } catch ( SQLException e ) {
            logger.error( "Unable to retrieve next result from result set." );
            next = false;
        }

        if ( !next ) {
            closer.countDown();
        }

        return key;
    }
}

@@ -1661,32 +1661,68 @@
 xt = rs.next();%0A
+            countDownIfExhausted();%0A
         %7D catch 
@@ -1734,32 +1734,32 @@
 Exception e ) %7B%0A
-
             logg
@@ -2046,24 +2046,25 @@
      T key;%0A
+%0A
         if (
@@ -2224,24 +2224,25 @@
 ;%0A        %7D%0A
+%0A
         try 
@@ -2418,32 +2418,133 @@
 lse;%0A        %7D%0A%0A
+        countDownIfExhausted();%0A%0A        return key;%0A    %7D%0A%0A    public void countDownIfExhausted() %7B%0A
         if ( !ne
@@ -2578,24 +2578,24 @@
 ountDown();%0A
+
         %7D%0A%0A 
@@ -2591,37 +2591,16 @@
        %7D
-%0A%0A        return key;
 %0A    %7D%0A%7D

Fix countdown