/**
 * Copyright (C) 2009-2013 Akiban Technologies, Inc.
 *
 * 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.akiban.server;

import com.akiban.server.error.PersistitAdapterException;
import com.akiban.server.service.tree.TreeService;
import com.persistit.Accumulator;
import com.persistit.Exchange;
import com.persistit.Transaction;
import com.persistit.Tree;
import com.persistit.exception.PersistitException;
import com.persistit.exception.PersistitInterruptedException;

public class AccumulatorAdapter {

    public static long getSnapshot(AccumInfo accumInfo, TreeService treeService, Tree tree)
    throws PersistitInterruptedException
    {
        Accumulator accumulator = getAccumulator(accumInfo, tree);
        Transaction txn = getCurrentTrx(treeService);
        return accumulator.getSnapshotValue(txn);
    }

    public static long updateAndGet(AccumInfo accumInfo, Exchange exchange, long value) {
        Accumulator accumulator = getAccumulator(accumInfo, exchange.getTree());
        return accumulator.update(value, exchange.getTransaction());
    }
    
    public static long getLiveValue(AccumInfo accumInfo, TreeService treeService, Tree tree)
    {
        Accumulator accumulator = getAccumulator(accumInfo, tree);
        return accumulator.getLiveValue();
    }

    public long getSnapshot() throws PersistitInterruptedException {
        return accumulator.getSnapshotValue(getCurrentTrx());
    }

    public long updateAndGet(long value) {
        return accumulator.update(value, getCurrentTrx());
    }

    public long getLiveValue() {
        return accumulator.getLiveValue();
    }

    public void set(long value) throws PersistitInterruptedException {
        set(value, true);
    }

    void set(long value, boolean evenIfLess) throws PersistitInterruptedException {
        long current = getSnapshot();
        if(evenIfLess || value > current) {
            long diff = value - current;
            this.updateAndGet(diff);
        }
    }

    public AccumulatorAdapter(AccumInfo accumInfo, TreeService treeService, Tree tree) {
        this.treeService = treeService;
        this.accumulator = getAccumulator(accumInfo, tree);
    }

    private static Accumulator getAccumulator(AccumInfo accumInfo, Tree tree) {
        try {
            return tree.getAccumulator(accumInfo.getType(), accumInfo.getIndex());
        } catch (PersistitException e) {
            throw new PersistitAdapterException(e);
        }
    }

    private Transaction getCurrentTrx() {
        return getCurrentTrx(treeService);
    }

    private static Transaction getCurrentTrx(TreeService treeService) {
        return treeService.getDb().getTransaction();
    }

    private final TreeService treeService;
    private final Accumulator accumulator;

    /**
     * Mapping of indexes and types for the Accumulators used by the table status.
     * <p>
     * Note: Remember that <i>any</i> modification to existing values is an
     * <b>incompatible</b> data format change. It is only safe to stop using
     * an index position or add new ones at the end of the range.
     * </p>
     */
    public static enum AccumInfo {
        ORDINAL(0, Accumulator.Type.SUM),
        ROW_COUNT(1, Accumulator.Type.SUM),
        UNIQUE_ID(2, Accumulator.Type.SEQ),
        AUTO_INC(3, Accumulator.Type.SUM),
        SEQUENCE(4, Accumulator.Type.SEQ)
        ;
    
        AccumInfo(int index, Accumulator.Type type) {
            this.index = index;
            this.type = type;
        }
    
        private int getIndex() {
            return index;
        }
    
        private Accumulator.Type getType() {
            return type;
        }
    
        private final int index;
        private final Accumulator.Type type;
    }
}

@@ -3798,172 +3798,616 @@
     
-ORDINAL(0, Accumulator.Type.SUM),%0A        ROW_COUNT(1, Accumulator.Type.SUM),%0A        UNIQUE_ID(2, Accumulator.Type.SEQ),%0A        AUTO_INC(3, Accumulator.Type.SUM),
+/** Ordinal value as used in the hkey. Write once. Attached to the PK tree. */%0A        ORDINAL(0, Accumulator.Type.SUM),%0A        /** Size of a table or group index. Attached to PK tree and GI tree, respectively */%0A        ROW_COUNT(1, Accumulator.Type.SUM),%0A        /** Source of values for hidden primary keys. Attached to the PK tree. */%0A        UNIQUE_ID(2, Accumulator.Type.SEQ),%0A        /** Saves values from the MySQL adapter AUTO INCREMENT columns. Attached to the PK tree. */%0A        AUTO_INC(3, Accumulator.Type.SUM),%0A        /** Source of values for SQL sequences. Attached to the sequence tree. */
 %0A   

Document AccumInfo usage