/**
 * Copyright (C) 2011 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, version 3,
 * as published by the Free Software Foundation.
 *
 * 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.ais.model.Index;
import com.akiban.ais.model.IndexColumn;
import com.akiban.server.service.tree.TreeCache;
import com.akiban.server.service.tree.TreeLink;

import java.util.Arrays;
import java.util.concurrent.atomic.AtomicReference;

public class IndexDef implements TreeLink {
    private final Index index;
    private final String treeName;
    // Identifies fields within the row that form the key part of the index entry.
    private final int[] fields;
    private final RowDef rowDef;
    private AtomicReference<TreeCache> treeCache = new AtomicReference<TreeCache>();


    public IndexDef(String treeName, RowDef rowDef, Index index)
    {
        this.index = index;
        index.indexDef(this);
        this.treeName = treeName;
        this.rowDef = rowDef;
        this.fields = new int[index.getColumns().size()];
        for (IndexColumn indexColumn : index.getColumns()) {
            int positionInRow = indexColumn.getColumn().getPosition();
            int positionInIndex = indexColumn.getPosition();
            this.fields[positionInIndex] = positionInRow;
        }
    }

    public int[] getFields() {
        return fields;
    }

    public RowDef getRowDef() {
        return rowDef;
    }

    public int getIndexKeySegmentCount() {
        return fields.length;
    }

    @Override
    public String toString() {
        return index.toString() + "[" + treeName + "]";
    }

    @Override
    public boolean equals(Object o) {
        if(this == o) {
            return true;
        }
        if(!(o instanceof IndexDef)) {
            return false;
        }
        IndexDef rhs = (IndexDef) o;
        return Arrays.equals(fields, rhs.fields)
               && index.equals(rhs.index)
               && treeName.equals(rhs.treeName);
    }

    @Override
    public int hashCode() {
        return index.hashCode() ^ treeName.hashCode() ^ Arrays.hashCode(fields);
    }

    // TreeLink interface

    @Override
    public String getSchemaName() {
        return index.getIndexName().getSchemaName();
    }

    @Override
    public String getTreeName() {
        return treeName;
    }

    @Override
    public void setTreeCache(TreeCache cache) {
       treeCache.set(cache);
    }

    @Override
    public TreeCache getTreeCache() {
        return treeCache.get();
    }
}

@@ -2169,438 +2169,212 @@
     
-if(this == o) %7B%0A            return true;%0A        %7D%0A        if(!(o instanceof IndexDef)) %7B%0A            return false;%0A        %7D%0A        IndexDef rhs = (IndexDef) o;%0A        return Arrays.equals(fields, rhs.fields)%0A               && index.equals(rhs.index)%0A               && treeName.equals(rhs.treeName);%0A    %7D%0A%0A    @Override%0A    public int hashCode() %7B%0A        return index.hashCode() %5E treeName.hashCode() %5E Arrays.hashCode(fields
+throw new UnsupportedOperationException(%22IndexDef deprecated, use Index%22);%0A    %7D%0A%0A    @Override%0A    public int hashCode() %7B%0A        throw new UnsupportedOperationException(%22IndexDef deprecated, use Index%22
 );%0A 

Throw exceptions from IndexDef equals and hashcode to ensure they aren't used.