00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
00044 static final long serialVersionUID = 5521257935120563452L;
00045
00046 public static String SHARE_STRING = new String();
00047
00048
00049
00050
00051 private String[] keys;
00052
00053 private int keyLength;
00054
00055
00056
00057
00058 public StringNode() {
00059 super();
00060 }
00061
00062 public StringNode(int order, int keyLength) {
00063 super(order);
00064
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
00137
00138 public void writeExternal(ObjectOutput out) throws IOException {
00139 super.writeExternal(out);
00140
00141 out.writeInt(keyLength);
00142
00143 for (int n = 0; n < keys.length; ++n) {
00144
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
00153 while (i < keyLength) {
00154 out.writeChar('\0');
00155 ++i;
00156 }
00157 }
00158
00159
00160
00161 }
00162
00163 public void readExternal(ObjectInput in) throws IOException,
00164 ClassNotFoundException {
00165 super.readExternal(in);
00166 keyLength = in.readInt();
00167
00168
00169 int maxKey = super.getMaxNbKey();
00170 int rawLen = maxKey * keyLength;
00171
00172
00173 keys = new String[maxKey];
00174
00175
00176 int n = 0;
00177
00178
00179 StringBuffer newKey = new StringBuffer(keyLength);
00180 for (n = 0; n < maxKey; ++n) {
00181
00182
00183 int i;
00184 char currentread;
00185
00186 for (i = 0; i < keyLength; ++i) {
00187 currentread = in.readChar();
00188 if (currentread == '\0')
00189 break;
00190 newKey.append(currentread);
00191 }
00192
00193 for (i++; i < keyLength; i++) {
00194 in.readChar();
00195 }
00196
00197
00198 keys[n] = newKey.toString();
00199 newKey.setLength(0);
00200 }
00201 }
00202
00206 public int getKeyLength() {
00207 return keyLength;
00208 }
00209
00213 public void setKeyLength(int i) {
00214 keyLength = i;
00215 }
00216
00217 }