IntNode.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
00026  *  2004 Modified by Romain Beaugrand
00027  * 
00028  */
00029 
00030 package org.openmobileis.database.fastobjectdb.db.index.node;
00031 
00032 import java.io.*;
00033 
00034 import org.openmobileis.database.fastobjectdb.db.index.SearchResult;
00035 import org.openmobileis.database.fastobjectdb.db.query.soda.SodaIntIndexComparator;
00036 
00043 public class IntNode extends Node {
00044   // serialization constant
00045   static final long serialVersionUID = 5521257935120563452L;
00046 
00047   //-------------------------
00048   // fields
00049   //-------------------------
00050   private int[] keys; // array of key values
00051 
00052   //-------------------------
00053   // constructors
00054   //-------------------------
00055   public IntNode() {
00056     super();
00057   }
00058 
00059   public IntNode(int order) {
00060     super(order);
00061     // allocate arrays
00062     keys = new int[order - 1];
00063 
00064   }
00065 
00066   public int getKeyAtPos(int pos) {
00067     return this.keys[pos];
00068   }
00069 
00070   public void setKeyPtrAtPos(int key, long ptr, int pos) {
00071     this.keys[pos] = key;
00072     super.ptr[pos] = ptr;
00073   }
00074 
00075   public SearchResult searchNode(int key, SearchResult search) {
00076     search.node = this;
00077     if (key < this.getKeyAtPos(0)) {
00078       search.pos = 0;
00079       search.found = false;
00080     } else {
00081       int i;
00082       for (i = nbKey - 1; i >= 0 && key < this.getKeyAtPos(i); i--);
00083       search.found = (key == this.getKeyAtPos(i));
00084       if (search.found)
00085         search.pos = i;
00086       else
00087         search.pos = i + 1;
00088     }
00089     return search;
00090   }
00091 
00092         public SearchResult searchNode(SodaIntIndexComparator comparator, SearchResult search) {
00093                 search.node = this;
00094                 if (comparator.compareTo(this.getKeyAtPos(0))<0) {
00095                         search.pos = 0;
00096                         search.found = false;
00097                 } else {
00098                         int i;
00099                         for (i = nbKey - 1; i >= 0 && comparator.compareTo(this.getKeyAtPos(i))<0; i--);
00100                         search.found = comparator.isSelected(this.getKeyAtPos(i));
00101                         if (search.found)
00102                                 search.pos = i;
00103                         else
00104                                 search.pos = i + 1;
00105                 }
00106                 return search;
00107         }
00108 
00109   public boolean pushInLeaf(int key, int pos, long newPtr) {
00110     this.branchs[this.nbKey + 1] = Node.NO_NODE;
00111     for (int j = this.nbKey - 1; j >= pos; j--) {
00112       this.setKeyPtrAtPos(this.getKeyAtPos(j), this.getNodePtrAtPos(j), j + 1);
00113     }
00114     this.setKeyPtrAtPos(key, newPtr, pos);
00115     this.nbKey++;
00116     return this.nbKey >= this.getMaxNbKey();
00117   }
00118 
00119   public boolean promote(int newkey, long keyptr, int pos, long nodePtr) {
00120     for (int j = this.nbKey; j >= pos + 1; j--) {
00121       this.setKeyPtrAtPos(keys[j - 1], this.getNodePtrAtPos(j - 1), j);
00122       this.branchs[j + 1] = this.branchs[j];
00123     }
00124     this.branchs[pos + 1] = nodePtr;
00125     this.setKeyPtrAtPos(newkey, keyptr, pos);
00126     this.nbKey++;
00127     return this.nbKey >= this.getMaxNbKey();
00128   }
00129 
00130   public void removeKeyAtPpos(int pos) {
00131     for (int j = pos + 1; j < this.nbKey; j++) {
00132       this.setKeyPtrAtPos(this.getKeyAtPos(j), this.getNodePtrAtPos(j), j - 1);
00133       this.branchs[j] = this.branchs[j + 1];
00134     }
00135     this.branchs[this.nbKey] = Node.NO_NODE;
00136 
00137     this.nbKey--;
00138   }
00139 
00140   //---------------------
00141   // serialization
00142   //---------------------
00143   public void writeExternal(ObjectOutput out) throws IOException {
00144     super.writeExternal(out);
00145     // create raw key data
00146     super.serializeInArray(out, keys);
00147   }
00148 
00149   public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
00150     super.readExternal(in);
00151     keys = super.unserializeInArray(in);
00152   }
00153 
00154 }

Generated on Mon Dec 4 11:03:27 2006 for OpenMobileIS by  doxygen 1.5.1-p1