/* Copyright (C) 2009  CSE,IIT Bombay  http://www.cse.iitb.ac.in

This file is part of the ConStore open source storage facility for concept-nets.

ConStore is free software and distributed under the 
Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License;
you can copy, distribute and transmit the work
with the work attribution in the manner specified by the author or licensor.
You may not use this work for commercial purposes and may not alter, 
transform, or build upon this work.

Please refer the legal code of the license, available at
http://creativecommons.org/licenses/by-nc-nd/3.0/legalcode

ConStore 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.  */

package iitb.con.indexing;

import iitb.con.ds.Node;
import iitb.con.indexing.IndexTree;
import iitb.con.indexing.isam.ISAMTree;
import iitb.con.net.ConceptNet;
import iitb.con.util.FileUtil;

import java.io.IOException;
import java.util.List;

/**
 * Builds the index for the given set of nodes ({@link Node}).
 * The nodes are key-value pair where the key is the attribute of the type 
 * and the value is the attribute's value.
 * 
 * @author Prathab K
 *
 */
public class IndexBuilder {
    
    /**
     * Creates the index structure for the given list of nodes
     * @param <K> data type of the key
     * @param fileName index file name
     * @param mode file mode
     * @param nodes node list
     * @return <tt>true</tt> on success
     * @see Node
     */
    public static <K extends Comparable<K>> boolean createIndex(String fileName, String mode, 
            List<Node<K>> nodes) {
        try {
            createIndexDir();
            IndexTree<K> indexTree = new ISAMTree<K>(fileName,mode);
            indexTree.create(nodes);
            indexTree.close();
            return true;
        }catch (IOException ie) {
            ie.printStackTrace();
            return false;
        }
    }
    
    /**
     * Gets the file name for the index. <p>
     * The index file name is created as the string formed of type id and attribute name
     * @param typeId type id
     * @param attributeName attribute name
     * @return index file name
     */
    public static String getIndexFileName(Short typeId, String attributeName) {
        return new String(ConceptNet.getConceptNetDir() + "index" + FileUtil.FILE_SEPARATOR +
                typeId + "_" + attributeName);
    }
    
    /**
     * Creates the index directory in the concept-net
     */
    private static void createIndexDir(){
        FileUtil.createDir(ConceptNet.getConceptNetDir()+ "index");
    }
    
    /**
     * Returns <tt>true</tt> if index is present for the specified type's attribute
     * @param typeId type id
     * @param attributeName attribute name
     * @return <tt>true</tt> if index present
     */
    public static boolean hasIndex(Short typeId, String attributeName) {
        return FileUtil.isFileExists(getIndexFileName(typeId, attributeName) + IndexTree.NON_LEAF_EXT);
    }
    
    /**
     * Drops the index by deleting the index file.
     * @param fileName index file name
     * @return <tt>true</tt> on success
     */
    public static boolean dropIndex(String fileName) {
        if(FileUtil.deleteFile(fileName + IndexTree.NON_LEAF_EXT))
            return FileUtil.deleteFile(fileName + IndexTree.LEAF_EXT);
        return false;
    }
    
}