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