LongNode.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 
00042 public class LongNode extends Node {
00043   // serialization constant
00044   static final long serialVersionUID = 5521257935120563452L;
00045 
00046   //-------------------------
00047   // fields
00048   //-------------------------
00049   private long[] keys; // array of key values
00050 
00051   //-------------------------
00052   // constructors
00053   //-------------------------
00054   public LongNode() {
00055     super();
00056   }
00057 
00058   public LongNode(int order) {
00059     super(order);
00060     // allocate arrays
00061     keys = new long[order - 1];
00062   }
00063 
00064   public long getKeyAtPos(int pos) {
00065     return this.keys[pos];
00066   }
00067 
00068   public void setKeyPtrAtPos(long key, long ptr, int pos) {
00069     this.keys[pos] = key;
00070     super.ptr[pos] = ptr;
00071   }
00072 
00073   public SearchResult searchNode(long key, SearchResult search) {
00074     search.node = this;
00075     if (key < this.getKeyAtPos(0)) {
00076       search.pos = 0;
00077       search.found = false;
00078     } else {
00079       int i;
00080       for (i = nbKey - 1; i >= 0 && key < this.getKeyAtPos(i); i--);
00081       search.found = (key == this.getKeyAtPos(i));
00082       if (search.found)
00083         search.pos = i;
00084       else
00085         search.pos = i + 1;
00086     }
00087     return search;
00088   }
00089 
00090   public boolean pushInLeaf(long key, int pos, long newPtr) {
00091     this.branchs[this.nbKey + 1] = Node.NO_NODE;
00092     for (int j = this.nbKey - 1; j >= pos; j--) {
00093       this.setKeyPtrAtPos(this.getKeyAtPos(j), this.getNodePtrAtPos(j), j + 1);
00094     }
00095     this.setKeyPtrAtPos(key, newPtr, pos);
00096     this.nbKey++;
00097     return this.nbKey >= this.getMaxNbKey();
00098   }
00099 
00100   public boolean promote(long newkey, long keyptr, int pos, long nodePtr) {
00101     for (int j = this.nbKey; j >= pos + 1; j--) {
00102       this.setKeyPtrAtPos(keys[j - 1], this.getNodePtrAtPos(j - 1), j);
00103       this.branchs[j + 1] = this.branchs[j];
00104     }
00105     this.branchs[pos + 1] = nodePtr;
00106     this.setKeyPtrAtPos(newkey, keyptr, pos);
00107     this.nbKey++;
00108     return this.nbKey >= this.getMaxNbKey();
00109   }
00110 
00111   public void removeKeyAtPpos(int pos) {
00112     for (int j = pos + 1; j < this.nbKey; j++) {
00113       this.setKeyPtrAtPos(this.getKeyAtPos(j), this.getNodePtrAtPos(j), j - 1);
00114       this.branchs[j] = this.branchs[j + 1];
00115     }
00116     this.branchs[this.nbKey] = Node.NO_NODE;
00117 
00118     this.nbKey--;
00119   }
00120 
00121   //---------------------
00122   // serialization
00123   //---------------------
00124   public void writeExternal(ObjectOutput out) throws IOException {
00125     super.writeExternal(out);
00126     // create raw key data
00127                 super.serializeLongArray(out, keys);
00128   }
00129 
00130   public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
00131     super.readExternal(in);
00132     keys = super.unserializeLongArray(in);
00133   }
00134 }

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