TypeTable.java |
/* 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.ds; import iitb.con.core.Type; import iitb.con.io.BufferedFileAdapter; import iitb.con.io.IOAdapter; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * TypeTable holds the <tt>Type</tt> as value objects. * The super key is the type id and the sub key is the type name. * In the case both super and sub key act at the same level. <p> * Types are managed in the concept-net in the .meta file. * * @author Prathab K * @see Type */ public class TypeTable implements ItemTable<String, Short, Type>{ /** Meta file handle */ private IOAdapter metaFile; /** Type table with type name as the key */ private Map<String, Type> typeNameTable; /** Type table with type id as the key */ private Map<Short, Type> typeIdTable; /** * Initializes the type table * @param fileName .meta file name * @param mode opening mode (r, rw) * @throws FileNotFoundException if file not found * @throws IOException if file operation fails */ public TypeTable(String fileName, String mode) throws FileNotFoundException, IOException { metaFile = new BufferedFileAdapter(fileName, mode); typeNameTable = new HashMap<String, Type>(); typeIdTable = new HashMap<Short, Type>(); } /** * Puts the given <tt>Type</tt> into the Type table * @param type Type * @see Type */ public void put(Type type) { typeNameTable.put(type.name,type); typeIdTable.put(type.id, type); } /** * Puts the given <tt>Type</tt> into the Type table with type name as key * @param name type name * @param type Type * @see Type */ public void put(String name, Type type) { this.put(type); } /** * Puts the given <tt>Type</tt> into the Type table with type name and type id as key. <br> * Since both super and sub key are maintained at same level, it is enough if one is provided. * @param name type name * @param id type id * @param type Type * @see Type */ public void put(String name, Short id, Type type) { this.put(type); } /** * Gets the <tt>Type</tt> for the specified type id * @param id type id * @return {@link iitb.con.core.Type} */ public Type get(Short id) { return typeIdTable.get(id); } /** * @deprecated */ public Map<Short,Type> get(String name) { return typeIdTable; } /** * Gets the <tt>Type</tt> for the specified type name or type id * @param name type name * @param id type id * @return {@link iitb.con.core.Type} */ public Type get(String name, Short id) { if(name != null) return typeNameTable.get(name); else return typeIdTable.get(id); } /** * Return <tt>NULL</tt>, since there is no implementation for this * @return <tt>NULL</tt> */ public Object getObject(Object o) { return null; } /** * Gets all the types from the type table * @return <tt>Type</tt> as List * @see Type */ public List<Type> getAllItems() { List<Type> types = new ArrayList<Type>(); for(Type type : typeIdTable.values()) types.add(type); return types; } /** * Removes the <tt>Type</tt> with specified type id * @param id type id * @return <tt>true</tt> on success */ public boolean remove(Short id) { if(typeNameTable.remove(typeIdTable.get(id).name) != null) return true; else return false; } /** * Removes the <tt>Type</tt> with specified type name or type id * @param name type name * @param id type id * @return <tt>true</tt> on success */ public boolean remove(String name, Short id) { if(name != null) return this.remove(typeNameTable.get(name).id); else return this.remove(id); } /** * Returns true if type present in the table * @param name type name * @return <tt>true</tt> if type present */ public boolean hasType(String name) { return typeNameTable.containsKey(name); } /** * Commits the types to the respective file * @throws IOException if file operations fail */ public void commit() throws IOException { int size = 0; ItemSerializer is = new TypeSerializer(); ByteBuffer[] buf = new ByteBuffer[typeNameTable.size()]; int i = 0; for(Type type : typeNameTable.values()){ buf[i] = is.serialize(type); size += buf[i].capacity(); i++; } ByteBuffer fullBuffer = ByteBuffer.allocate(size); for(ByteBuffer b : buf) { byte[] bytes = b.array(); for(byte byt : bytes) { fullBuffer.put(byt); } } fullBuffer.flip(); metaFile.write(fullBuffer, 0); } /** * Close the file associate with the table * @throws IOException if file operations fail */ public void close() throws IOException { if(metaFile != null) metaFile.close(); } /** * Returns the size of the table i.e. the no. of types * @return table size */ public int size() { return typeIdTable.size(); } }