/**
 * Copyright (C) 2009-2013 FoundationDB, LLC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.foundationdb.server.store.format.columnkeys;

import com.foundationdb.qp.storeadapter.FDBAdapter;
import com.foundationdb.server.store.FDBStoreData;
import com.foundationdb.server.store.FDBStoreDataIterator;
import com.foundationdb.KeyValue;
import com.foundationdb.async.AsyncIterator;
import com.foundationdb.tuple.Tuple2;

import java.util.HashMap;
import java.util.Map;

public class ColumnKeysStorageIterator extends FDBStoreDataIterator
{
    private final AsyncIterator<KeyValue> underlying;
    private final int limit;
    private KeyValue pending;
    private int count;

    public ColumnKeysStorageIterator(FDBStoreData storeData, 
                                     AsyncIterator<KeyValue> underlying,
                                     int limit) {
        super(storeData);
        this.underlying = underlying;
        this.limit = limit;
    }

    @Override
    public boolean hasNext() {
        try {
        return (((pending != null) || underlying.hasNext()) &&
                ((limit <= 0) || (count < limit)));
        } catch (RuntimeException e) {
            throw FDBAdapter.wrapFDBException(storeData.session, e);
        }
    }
    
    /**
     * Do next processing, but also duplicate key elimination.
     * That is if the underlying iterator over the keys would return:
     * 'a', 'a', 'b', 'b', 'b', 'c'. 
     * this will return 'a', 'b', 'c'. 
     * pending holds the next key in the sequence, so when completed
     * reading the 'a's, pending holds 'b'. 
     */
    @Override
    public Void next() {
        Map<String,Object> value = new HashMap<>();
        Tuple2 lastKey = null;
        while (true) {
            KeyValue kv;
            if (pending != null) {
                kv = pending;
                pending = null;
            } else {
                try {
                    if (underlying.hasNext()) {
                        kv = underlying.next();
                    } else {
                        break;
                    }
                } catch (RuntimeException e) {
                    throw FDBAdapter.wrapFDBException(storeData.session, e);
                }
            }
            
            Tuple2 key = Tuple2.fromBytes(kv.getKey());
            String name = key.getString(key.size() - 1);
            key = key.popBack();
            if (lastKey == null) {
                lastKey = key;
            }
            else if (!tupleEquals(key, lastKey)) {
                pending = kv;
                break;
            }
            value.put(name, Tuple2.fromBytes(kv.getValue()).get(0));
        }
        storeData.rawKey = lastKey.pack();
        storeData.otherValue = value;
        count++;
        return null;
    }

    // TODO: Until fixed version of ByteArrayUtil.compareUnsigned is released.
    public boolean tupleEquals(Tuple2 t1, Tuple2 t2) {
        return java.util.Arrays.equals(t1.pack(), t2.pack());
    }

    @Override
    public void close() {
        underlying.dispose();
    }
}

@@ -1008,32 +1008,77 @@
 .AsyncIterator;%0A
+import com.foundationdb.tuple.ByteArrayUtil;%0A
 import com.found
@@ -3494,87 +3494,8 @@
  %7D%0A%0A
-    // TODO: Until fixed version of ByteArrayUtil.compareUnsigned is released.%0A
     
@@ -3564,31 +3564,37 @@
 urn 
-java.util.Arrays.equals
+ByteArrayUtil.compareUnsigned
 (t1.
@@ -3611,16 +3611,21 @@
 .pack())
+ == 0
 ;%0A    %7D%0A

use ByteArrayUtil function for comparison