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 import org.openmobileis.database.fastobjectdb.db.query.soda.SodaIntIndexComparator;
00036
00043 public class IntNode extends Node {
00044
00045 static final long serialVersionUID = 5521257935120563452L;
00046
00047
00048
00049
00050 private int[] keys;
00051
00052
00053
00054
00055 public IntNode() {
00056 super();
00057 }
00058
00059 public IntNode(int order) {
00060 super(order);
00061
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
00142
00143 public void writeExternal(ObjectOutput out) throws IOException {
00144 super.writeExternal(out);
00145
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 }