/* 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.query;

import iitb.con.core.ConStoreConstants;
import iitb.con.ds.ItemSerializer;
import iitb.con.io.BufferedFileAdapter;
import iitb.con.io.IOAdapter;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * InstanceReader reads an instance item from the concept-net.
 * 
 * @author Prathab K
 *
 */
public class ItemReader<T> {
    /** Item file that to be read */
    private IOAdapter itemFile;
    
    /** Item serializer to decode the item */
    private ItemSerializer<T> itemSerializer;
    
    /**
     * Constructs the InstanceReader with respective file and item serializer
     * @param fileName file name of the where the item exists
     * @param itemSerializer item serializer to decode the item
     * @throws FileNotFoundException if file does not found
     * @throws IOException if file operation fails
     */
    public ItemReader(String fileName, ItemSerializer<T> itemSerializer)
    throws FileNotFoundException, IOException
    {
        itemFile = new BufferedFileAdapter(fileName, "r", ConStoreConstants.IO_BUFFER_SIZE);
        this.itemSerializer = itemSerializer;
        
    }
    
    /**
     * Retrieves and deserialize the item from the file
     * @param location item location 
     * @param length item length
     * @return item (the type of item depends the Item Serializer)
     * @throws IOException
     */
    public T getItem(long location, int length) throws IOException {
        return (T) itemSerializer.deSerialize(read(location, length));
    }
    
    /**
     * Retrieves and deserialize the item from the file
     * @param location item location 
     * @param length item length
     * @param name attribute name
     * @return attribute's value as <tt>Object</tt>
     * @throws IOException
     */
    public Object getItemAttributeValue(long location, int length, String name) throws IOException {
        return itemSerializer.attributeDeSerialize(read(location, length),name);
    }
    
    /**
     * Reads the item content as bytes
     * @param location item location
     * @param length item length
     * @return item contents as ByteBuffer
     * @throws IOException if file not found
     */
    private ByteBuffer read(long location, int length) throws IOException {
        ByteBuffer buf = ByteBuffer.wrap(itemFile.read(location, length));
        return buf;
    }
    
    /**
     * Closes the item file
     * @throws IOException if file operation fails
     */
    public void close() throws IOException{
        itemFile.close();
    }
}