1 19 package org.netbeans.mdr.persistence.btreeimpl.btreeindex; 20 21 import org.netbeans.mdr.persistence.*; 22 import java.io.*; 23 24 30 public class FixedKeyPage extends BtreePage { 31 32 int keyLength; 34 44 public void init(Btree btree, byte[] pageId, byte[] pageBuffer, 45 boolean isNew) throws StorageException { 46 47 super.init(btree, pageId, pageBuffer, isNew); 48 keyLength = btree.keyInfo.getLength(); 49 } 50 51 56 int numEntries() { 57 return (freeStart - headerLength)/(keyLength + dataLength); 58 } 59 60 67 int keyOffset(int entryNum) { 68 return headerLength + (entryNum * (keyLength + dataLength)); 69 } 70 71 78 int keyLength(int entryNum) { 79 return keyLength; 80 } 81 82 89 byte[] getData(int entryNum) { 90 byte[] data = new byte[dataLength]; 91 System.arraycopy(pageBuffer, keyOffset(entryNum) + keyLength, 92 data, 0, dataLength); 93 return data; 94 } 95 96 103 byte[] getKey(int entryNum) { 104 byte[] key = new byte[keyLength]; 105 System.arraycopy(pageBuffer, keyOffset(entryNum), key, 0, keyLength); 106 return key; 107 } 108 109 118 BtreeEntry insert(BtreeEntry entry, int entryNum, SearchResult resultPosition) 119 throws StorageException { 120 121 pageSource.dirtyPage(this); 122 123 if (btree.pageSize - freeStart >= entry.length()) { 124 insertHere(entry, entryNum, resultPosition); 125 return null; 126 } else { 127 return split(entry, entryNum, resultPosition); 128 } 129 } 130 131 140 BtreeEntry replace(BtreeEntry entry, int entryNum, SearchResult resultPosition) throws StorageException { 141 142 pageSource.dirtyPage(this); 143 int offset = keyOffset(entryNum) + keyLength; 144 System.arraycopy(entry.data, 0, pageBuffer, offset, entry.data.length); 145 146 if (resultPosition != null) { 148 resultPosition.matched = true; 149 resultPosition.skipCount = 0; 150 resultPosition.page = this; 151 resultPosition.entryNum = entryNum; 152 } 153 154 return null; 156 } 157 158 private void insertHere(BtreeEntry entry, int entryNum, SearchResult resultPosition) { 159 160 int offset; 161 162 163 164 offset = keyOffset(entryNum); 165 if (offset != freeStart) { 166 167 System.arraycopy(pageBuffer, offset, 168 pageBuffer, offset + entry.length(), 169 freeStart - offset); 170 } 171 System.arraycopy(entry.key, 0, pageBuffer, offset, keyLength); 172 System.arraycopy(entry.data, 0, 173 pageBuffer, offset+keyLength, dataLength); 174 freeStart += keyLength + dataLength; 175 176 if (resultPosition != null) { 177 resultPosition.entryNum = entryNum; 178 resultPosition.matched = true; 179 resultPosition.page = this; 180 resultPosition.skipCount = 0; 181 } 182 } 183 184 190 void delete(int firstEntry, int lastEntry) throws StorageException { 191 192 int startOffset, endOffset; 193 194 pageSource.dirtyPage(this); 195 196 startOffset = keyOffset(firstEntry); 197 endOffset = keyOffset(lastEntry+1); 198 if (freeStart != endOffset) { 199 System.arraycopy(pageBuffer, endOffset, pageBuffer, startOffset, 200 freeStart - endOffset); 201 } 202 freeStart -= endOffset - startOffset; 203 } 204 205 218 byte[] splitEntries(BtreePage left, BtreePage right, BtreeEntry entry, 219 int entryNum, SearchResult resultPosition) { 220 221 int entryLength; 222 int offset; 223 int midpoint; 224 int half; 225 int copyLength; 226 boolean insertLeft; 227 byte[] rightKey; 228 229 offset = keyOffset(entryNum); 230 entryLength = keyLength + dataLength; 231 half = numEntries()/2; 232 midpoint = headerLength + half*entryLength; 233 insertLeft = offset < midpoint; 234 235 if (insertLeft) { 237 copyLength = this.freeStart - midpoint; 238 } else { 239 copyLength = offset - midpoint; 241 } 242 243 if (resultPosition != null) { 245 resultPosition.matched = true; 246 resultPosition.skipCount = 0; 247 if (insertLeft) { 248 resultPosition.page = left; 249 resultPosition.entryNum = entryNum; 250 } else { 251 resultPosition.page = right; 252 resultPosition.entryNum = entryNum - half; 253 } 254 } 255 256 if (copyLength > 0) { 257 System.arraycopy(this.pageBuffer, midpoint, right.pageBuffer, 258 right.freeStart, copyLength); 259 right.freeStart += copyLength; 260 } 261 if (!insertLeft) { 262 System.arraycopy(entry.key, 0, right.pageBuffer, right.freeStart, 263 keyLength); 264 right.freeStart += keyLength; 265 System.arraycopy(entry.data, 0, right.pageBuffer, right.freeStart, 266 dataLength); 267 right.freeStart += dataLength; 268 if (this.freeStart != offset) { 269 System.arraycopy(this.pageBuffer, offset, right.pageBuffer, 270 right.freeStart, this.freeStart - offset); 271 right.freeStart += this.freeStart - offset; 272 } 273 } 274 275 if (left != this) { 278 if (insertLeft) { 279 copyLength = offset - headerLength; 280 } else { 281 copyLength = midpoint - headerLength; 282 } 283 if (copyLength > 0) { 284 System.arraycopy(this.pageBuffer, headerLength, 285 left.pageBuffer, left.freeStart, copyLength); 286 } 287 } 288 289 if (insertLeft) { 290 if (midpoint != offset) { 291 System.arraycopy(this.pageBuffer, offset, left.pageBuffer, 292 offset+entryLength, midpoint - offset); 293 } 294 System.arraycopy(entry.key, 0, left.pageBuffer, offset, keyLength); 295 System.arraycopy(entry.data, 0, left.pageBuffer, offset + keyLength, dataLength); 296 left.freeStart = midpoint + entryLength; 297 } else { 298 left.freeStart = midpoint; 299 } 300 if (left != this) { 301 this.freeStart = headerLength; 303 } 304 rightKey = new byte[keyLength]; 305 System.arraycopy(right.pageBuffer, headerLength, rightKey, 0, keyLength); 306 return rightKey; 307 } 308 } 309 | Popular Tags |