StringNode.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 StringNode extends Node {
00043         // serialization constant
00044         static final long serialVersionUID = 5521257935120563452L;
00045 
00046         public static String SHARE_STRING = new String();
00047 
00048         //-------------------------
00049         // fields
00050         //-------------------------
00051         private String[] keys; // array of key values
00052 
00053         private int keyLength;
00054 
00055         //-------------------------
00056         // constructors
00057         //-------------------------
00058         public StringNode() {
00059                 super();
00060         }
00061 
00062         public StringNode(int order, int keyLength) {
00063                 super(order);
00064                 // allocate arrays
00065                 keys = new String[order - 1];
00066 
00067                 for (int n = 0; n < keys.length; ++n)
00068                         keys[n] = StringNode.SHARE_STRING;
00069 
00070                 this.keyLength = keyLength;
00071 
00072         }
00073 
00074         public String getKeyAtPos(int pos) {
00075                 return this.keys[pos];
00076         }
00077 
00078         public void setKeyPtrAtPos(String key, long ptr, int pos) {
00079                 this.keys[pos] = key;
00080                 super.ptr[pos] = ptr;
00081         }
00082 
00083         public SearchResult searchNode(String key, SearchResult search) {
00084                 search.node = this;
00085                 if (key.compareTo(this.getKeyAtPos(0)) < 0) {
00086                         search.pos = 0;
00087                         search.found = false;
00088                 } else {
00089                         int i;
00090                         for (i = nbKey - 1; i >= 0
00091                                         && key.compareTo(this.getKeyAtPos(i)) < 0; i--)
00092                                 ;
00093                         search.found = key.equals(this.getKeyAtPos(i));
00094                         if (search.found)
00095                                 search.pos = i;
00096                         else
00097                                 search.pos = i + 1;
00098                 }
00099                 return search;
00100         }
00101 
00102         public boolean pushInLeaf(String key, int pos, long newPtr) {
00103                 this.branchs[this.nbKey + 1] = Node.NO_NODE;
00104                 for (int j = this.nbKey - 1; j >= pos; j--) {
00105                         this.setKeyPtrAtPos(this.getKeyAtPos(j), this.getNodePtrAtPos(j),
00106                                         j + 1);
00107                 }
00108                 this.setKeyPtrAtPos(key, newPtr, pos);
00109                 this.nbKey++;
00110                 return this.nbKey >= this.getMaxNbKey();
00111         }
00112 
00113         public boolean promote(String newkey, long keyptr, int pos, long nodePtr) {
00114                 for (int j = this.nbKey; j >= pos + 1; j--) {
00115                         this.setKeyPtrAtPos(keys[j - 1], this.getNodePtrAtPos(j - 1), j);
00116                         this.branchs[j + 1] = this.branchs[j];
00117                 }
00118                 this.branchs[pos + 1] = nodePtr;
00119                 this.setKeyPtrAtPos(newkey, keyptr, pos);
00120                 this.nbKey++;
00121                 return this.nbKey >= this.getMaxNbKey();
00122         }
00123 
00124         public void removeKeyAtPpos(int pos) {
00125                 for (int j = pos + 1; j < this.nbKey; j++) {
00126                         this.setKeyPtrAtPos(this.getKeyAtPos(j), this.getNodePtrAtPos(j),
00127                                         j - 1);
00128                         this.branchs[j] = this.branchs[j + 1];
00129                 }
00130                 this.branchs[this.nbKey] = Node.NO_NODE;
00131 
00132                 this.nbKey--;
00133         }
00134 
00135         //---------------------
00136         // serialization
00137         //---------------------
00138         public void writeExternal(ObjectOutput out) throws IOException {
00139                 super.writeExternal(out);
00140                 // create raw key data
00141                 out.writeInt(keyLength);
00142 
00143                 for (int n = 0; n < keys.length; ++n) {
00144                         // copy characters from string to raw data
00145                         int i = 0;
00146 
00147                         while ((i < keys[n].length()) && (i < keyLength)) {
00148                                 out.writeChar(keys[n].charAt(i));
00149                                 ++i;
00150                         }
00151 
00152                         // fill any extra space with nulls
00153                         while (i < keyLength) {
00154                                 out.writeChar('\0');
00155                                 ++i;
00156                         }
00157                 }
00158 
00159                 // write raw keys
00160                 //   out.writeObject(rawKeys);
00161         }
00162 
00163         public void readExternal(ObjectInput in) throws IOException,
00164                         ClassNotFoundException {
00165                 super.readExternal(in);
00166                 keyLength = in.readInt();
00167 
00168                 // get and check raw key length
00169                 int maxKey = super.getMaxNbKey();
00170 
00171                 // allocate key arrays
00172                 keys = new String[maxKey];
00173 
00174                 // extract strings from their raw format
00175                 int n = 0;
00176                 //  int pos = 0;
00177 
00178                 StringBuffer newKey = new StringBuffer(keyLength);
00179                 for (n = 0; n < maxKey; ++n) {
00180                         // create a buffer
00181 
00182                         int i;
00183                         char currentread;
00184                         // copy characters to buffer
00185                         for (i = 0; i < keyLength; ++i) {
00186                                 currentread = in.readChar();
00187                                 if (currentread == '\0')
00188                                         break;
00189                                 newKey.append(currentread);
00190                         }
00191                         //skip remainding char
00192                         for (i++; i < keyLength; i++) {
00193                                 in.readChar();
00194                         }
00195 
00196                         // create string from buffer
00197                         keys[n] = newKey.toString();
00198                         newKey.setLength(0);
00199                 }
00200         }
00201 
00205         public int getKeyLength() {
00206                 return keyLength;
00207         }
00208 
00212         public void setKeyLength(int i) {
00213                 keyLength = i;
00214         }
00215 
00216 }

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