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 package org.openmobileis.database.fastobjectdb.db.index;
00030
00031 import java.io.IOException;
00032 import java.lang.reflect.*;
00033 import org.openmobileis.common.util.collection.*;
00034 import org.openmobileis.database.fastobjectdb.FODBStringIndexDescriptor;
00035 import org.openmobileis.database.fastobjectdb.db.exception.FODBException;
00036 import org.openmobileis.database.fastobjectdb.db.index.node.Node;
00037 import org.openmobileis.database.fastobjectdb.db.index.node.StringNode;
00038 import org.openmobileis.database.fastobjectdb.db.query.soda.SodaStringIndexComparator;
00039 import org.openmobileis.database.fastobjectdb.db.store.*;
00040
00043 public class FODBMultipleStringIndex extends FODBStringIndex implements FODBMultipleIndex {
00044
00045 public FODBMultipleStringIndex(FODBIndexHeader newHeader, FODBCollectionIndexFile cFile, AccessibleObject accObj) throws FODBException {
00046 super(newHeader, cFile, accObj);
00047 }
00048
00049 public FODBMultipleStringIndex(FODBStringIndexDescriptor descriptor, FODBCollectionIndexFile cFile, AccessibleObject accObj) throws FODBException {
00050 super(descriptor, cFile, accObj);
00051 }
00052
00053 public int getType() {
00054 return FODBIndex.MULTIPLE;
00055 }
00056
00060 protected void writeKeyPtr(Node node, int pos, long newptr) throws IOException, ClassNotFoundException {
00061 long arrayPtr = node.getNodePtrAtPos(pos);
00062 long[] ptrList = (long[]) colFile.readLongPtr(arrayPtr);
00063 int size = ptrList.length;
00064
00065 int i = 0;
00066 for (i = 0; i < size; i++) {
00067 if (ptrList[i] == Node.NO_NODE) {
00068 break;
00069 }
00070 }
00071 if (i == size) {
00072 long[] newptrlist = new long[size + ((FODBStringIndexDescriptor)header.descriptor).getIncTabSize()];
00073 System.arraycopy(ptrList, 0, newptrlist, 0, ptrList.length);
00074 newptrlist[size] = newptr;
00075 for (i = size + 1; i < newptrlist.length; i++) {
00076 newptrlist[i] = Node.NO_NODE;
00077 }
00078 colFile.deleteLongPtr(arrayPtr);
00079 long modified = colFile.writeLongPtr(newptrlist);
00080 node.ptr[pos] = modified;
00081 colFile.rewriteNode(node, node.filePtr);
00082 return;
00083 } else {
00084 ptrList[i] = newptr;
00085 colFile.rewriteLongPtr(ptrList, arrayPtr);
00086 return;
00087 }
00088 }
00089
00090 protected long createPtrArray(long ptr) throws IOException, ClassNotFoundException {
00091 long[] newptrlist = new long[((FODBStringIndexDescriptor)header.descriptor).getIncTabSize()];
00092 newptrlist[0] = ptr;
00093 for (int i=1; i<newptrlist.length; i++) {
00094 newptrlist[i] = Node.NO_NODE;
00095 }
00096 return colFile.writeLongPtr(newptrlist);
00097 }
00098
00099
00100
00101
00102
00103
00104 protected void addSearchResult(StringNode node, int pos, LongArray array, SodaStringIndexComparator comparator) throws IOException, ClassNotFoundException {
00105 if (comparator.isDistinct()) {
00106 long[] ptrList = this.readPtrAtPos(node, pos);
00107 array.add(ptrList[0]);
00108 } else {
00109 long[] ptrList = this.readPtrAtPos(node, pos);
00110 array.add(ptrList);
00111 }
00112
00113 }
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 private long[] readPtrAtPos(Node pg, int pos) throws IOException, ClassNotFoundException {
00134 long arrayPtr = pg.ptr[pos];
00135
00136 long[] arrayList = (long[])colFile.readLongPtr(arrayPtr);
00137
00138 int i = 0;
00139 for (i=0; i<arrayList.length; i++) {
00140 if (arrayList[i] == Node.NO_NODE) {
00141 break;
00142 }
00143 }
00144 if (i==arrayList.length) {
00145 return arrayList;
00146 }
00147 long[] newArrayList = new long[i];
00148 System.arraycopy(arrayList, 0, newArrayList, 0, newArrayList.length);
00149 return newArrayList;
00150 }
00151
00156 protected boolean removeKeyPtr(Node node, int pos, long pointer) throws IOException, ClassNotFoundException {
00157 long arrayPtr = node.ptr[pos];
00158 long[] ptrList = (long[])colFile.readLongPtr(arrayPtr);
00159
00160
00161 int i=0;
00162 int size = ptrList.length;
00163 for (i=0; i<size; i++) {
00164 if (ptrList[i] == pointer) {
00165 ptrList[i] = Node.NO_NODE;
00166 break;
00167 }
00168 }
00169 if (i == size) {
00170 throw new IOException("Pointer not found !!!");
00171 }
00172 int nbnull = 1;
00173 for (int j=i+1; j<size ; j++) {
00174 if (ptrList[j] == Node.NO_NODE) {
00175 nbnull++;
00176 } else {
00177 ptrList[j-1] = ptrList[j];
00178 ptrList[j] = Node.NO_NODE;
00179 }
00180 }
00181 int inctabsize = ((FODBStringIndexDescriptor)header.descriptor).getIncTabSize();
00182 if (nbnull == inctabsize) {
00183 if (size > inctabsize) {
00184 long[] newptrlist = new long[size - inctabsize];
00185 System.arraycopy(ptrList, 0, newptrlist, 0, newptrlist.length);
00186 colFile.deleteLongPtr(arrayPtr);
00187 node.ptr[pos] = colFile.writeLongPtr(newptrlist);
00188
00189 colFile.rewriteNode(node, node.filePtr);
00190 return false;
00191 } else {
00192 colFile.deleteLongPtr(arrayPtr);
00193 return true;
00194 }
00195 }
00196
00197 colFile.rewriteLongPtr(ptrList, arrayPtr);
00198 return false;
00199 }
00200 }