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