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