LeafNode.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.indexing.isam; import iitb.con.ds.ItemSerializer; import iitb.con.util.Serializer; import java.nio.ByteBuffer; /** * Leaf Node of the index tree. A leaf node consists of the key with respective values (instance ids). * @author Prathab K * */ public class LeafNode<T extends Comparable<T>> implements ItemSerializer<LeafNode<T>> { /** Instance ids for the key */ public int[] instanceId; /** key of the index structure */ public T key; /** Size of the leaf node */ public short dataSize; public LeafNode(){ } /** * Intializes with the key * @param key index key */ public LeafNode(T key){ this.key = key; } /** * Serializes into bytes of the given leaf node.<p> * Format {[Key] : [No. of values] : [value1, value2, ..., valueN]} * @param node leaf node - {@link LeafNode} * @return serialized node as ByteBuffer */ public ByteBuffer serialize(LeafNode<T> node) { ByteBuffer buf = null; //structSize = no. of instanceIds(2) + instanceId (4)y + dataSize (2) + key (x) = 4 + 4y + x bytes (for String) //structSize = no. of instanceIds(2) + instanceId (4)y + dataTypeSize(x) = 2 + 4y + x bytes int instanceIdSize = instanceId.length * 4; //Integer Size = 4 if(key != null) { if(String.class.isInstance(key)) { //if(key instanceof String) { node.dataSize = (short) ((String)(Object)key).length(); buf = ByteBuffer.allocate(4 + instanceIdSize + node.dataSize); buf.putShort(node.dataSize); buf.put(((String)(Object)key).getBytes()); //}else if(key instanceof Integer) { }else if(Integer.class.isInstance(key)) { buf = ByteBuffer.allocate(6 + instanceIdSize ); buf.putInt((Integer)(Object)key); //}else if(key instanceof Float) { }else if(Float.class.isInstance(key)) { buf = ByteBuffer.allocate(6 + instanceIdSize ); buf.putFloat((Float)(Object)key); //}else if(key instanceof Double) { }else if(Double.class.isInstance(key)) { buf = ByteBuffer.allocate(10 + instanceIdSize ); buf.putDouble((Double)(Object)key); //}else if(key instanceof Boolean) { }else if(Boolean.class.isInstance(key)) { buf = ByteBuffer.allocate(3 + instanceIdSize ); if(((Boolean)(Object)key).booleanValue()) buf.put((byte)1); else buf.put((byte)0); } buf.putShort((short)instanceId.length); for(int i = 0 ; i < instanceId.length ; i++) buf.putInt(node.instanceId[i]); } buf.rewind(); return buf; } /** * Deserializes the given bytes into leaf node. * Format {[Key] : [No. of values] : [value1, value2, ..., valueN]} * @param buf bytes as ByteBuffer * @return leaf node - {@link LeafNode} */ public LeafNode<T> deSerialize(ByteBuffer buf) { this.key = (T) Serializer.toObject(buf, key); short numInstanceIds = buf.getShort(); this.instanceId = new int[numInstanceIds]; for(int i = 0 ; i < numInstanceIds ; i++) this.instanceId[i] = buf.getInt(); return this; } /** * Deserializes the specified attribute to object value * @param buf bytes as {@link ByteBuffer} * @param name attribute name * @return deserialized attribute value as <tt>Object</tt> */ public Object attributeDeSerialize(ByteBuffer buf, Object name) { this.key = (T) Serializer.toObject(buf, key); short numInstanceIds = buf.getShort(); if(this.key.compareTo((T)name) == 0) { this.instanceId = new int[numInstanceIds]; for(int i = 0 ; i < numInstanceIds ; i++) this.instanceId[i] = buf.getInt(); return this; } else { buf.position(buf.position() + numInstanceIds * 4); } return null; } }