1 65 66 67 package org.hsqldb; 68 69 import java.io.IOException ; 70 71 import org.hsqldb.lib.IntLookup; 72 import org.hsqldb.rowio.RowInputInterface; 73 import org.hsqldb.rowio.RowOutputInterface; 74 75 79 94 class DiskNode extends Node { 95 96 protected Row rData; 97 private int iLeft = NO_POS; 98 private int iRight = NO_POS; 99 private int iParent = NO_POS; 100 private int iId; static final int SIZE_IN_BYTE = 4 * 4; 102 103 DiskNode(CachedRow r, RowInputInterface in, 104 int id) throws IOException , HsqlException { 105 106 iId = id; 107 rData = r; 108 iBalance = in.readIntData(); 109 iLeft = in.readIntData(); 110 111 if (iLeft <= 0) { 112 iLeft = NO_POS; 113 } 114 115 iRight = in.readIntData(); 116 117 if (iRight <= 0) { 118 iRight = NO_POS; 119 } 120 121 iParent = in.readIntData(); 122 123 if (iParent <= 0) { 124 iParent = NO_POS; 125 } 126 127 if (Trace.DOASSERT) { 128 129 } 135 } 136 137 DiskNode(CachedRow r, int id) { 138 iId = id; 139 rData = r; 140 } 141 142 void delete() { 143 rData = null; 144 iBalance = -2; 145 } 146 147 int getKey() { 148 149 if (rData != null) { 150 return ((CachedRow) rData).iPos; 151 } 152 153 return NO_POS; 154 } 155 156 Row getRow() throws HsqlException { 157 158 if (Trace.DOASSERT) { 159 Trace.doAssert(rData != null); 160 } 161 162 return rData; 163 } 164 165 private Node findNode(int pos) throws HsqlException { 166 167 Node ret = null; 168 Row r = ((CachedRow) rData).getTable().getRow(pos); 169 170 if (r != null) { 171 ret = r.getNode(iId); 172 } 173 174 return ret; 175 } 176 177 boolean isLeft(Node node) throws HsqlException { 178 179 if (node == null) { 180 return iLeft == NO_POS; 181 } 182 183 return iLeft == ((DiskNode) node).getKey(); 184 } 185 186 boolean isRight(Node node) throws HsqlException { 187 188 if (node == null) { 189 return iRight == NO_POS; 190 } 191 192 return iRight == ((DiskNode) node).getKey(); 193 } 194 195 Node getLeft() throws HsqlException { 196 197 if (Trace.DOASSERT) { 198 Trace.doAssert(iBalance != -2); 199 } 200 201 if (iLeft == NO_POS) { 202 return null; 203 } 204 205 return findNode(iLeft); 206 } 207 208 Node getRight() throws HsqlException { 209 210 if (Trace.DOASSERT) { 211 Trace.doAssert(iBalance != -2); 212 } 213 214 if (iRight == NO_POS) { 215 return null; 216 } 217 218 return findNode(iRight); 219 } 220 221 Node getParent() throws HsqlException { 222 223 if (Trace.DOASSERT) { 224 Trace.doAssert(iBalance != -2); 225 } 226 227 if (iParent == NO_POS) { 228 return null; 229 } 230 231 return findNode(iParent); 232 } 233 234 boolean isRoot() { 235 return iParent == Node.NO_POS; 236 } 237 238 boolean isFromLeft() throws HsqlException { 239 240 if (this.isRoot()) { 241 return true; 242 } 243 244 if (Trace.DOASSERT) { 245 Trace.doAssert(getParent() != null); 246 } 247 248 DiskNode parent = (DiskNode) getParent(); 249 250 return getKey() == parent.iLeft; 251 } 252 253 Object [] getData() throws HsqlException { 254 255 if (Trace.DOASSERT) { 256 Trace.doAssert(iBalance != -2); 257 } 258 259 return rData.getData(); 260 } 261 262 void setParent(Node n) throws HsqlException { 263 264 if (Trace.DOASSERT) { 265 Trace.doAssert(iBalance != -2); 266 } 267 268 ((CachedRow) rData).setChanged(); 269 270 iParent = n == null ? NO_POS 271 : n.getKey(); 272 } 273 274 void setBalance(int b) throws HsqlException { 275 276 if (Trace.DOASSERT) { 277 Trace.doAssert(iBalance != -2); 278 } 279 280 if (iBalance != b) { 281 ((CachedRow) rData).setChanged(); 282 283 iBalance = b; 284 } 285 } 286 287 void setLeft(Node n) throws HsqlException { 288 289 if (Trace.DOASSERT) { 290 Trace.doAssert(iBalance != -2); 291 } 292 293 ((CachedRow) rData).setChanged(); 294 295 iLeft = n == null ? NO_POS 296 : n.getKey(); 297 } 298 299 void setRight(Node n) throws HsqlException { 300 301 if (Trace.DOASSERT) { 302 Trace.doAssert(iBalance != -2); 303 } 304 305 ((CachedRow) rData).setChanged(); 306 307 iRight = n == null ? NO_POS 308 : n.getKey(); 309 } 310 311 boolean equals(Node n) { 312 313 341 return this == n 342 || (n != null && getKey() == ((DiskNode) n).getKey()); 343 } 344 345 void write(RowOutputInterface out) throws IOException { 346 347 if (Trace.DOASSERT) { 348 349 } 355 356 out.writeIntData(iBalance); 357 out.writeIntData((iLeft == NO_POS) ? 0 358 : iLeft); 359 out.writeIntData((iRight == NO_POS) ? 0 360 : iRight); 361 out.writeIntData((iParent == NO_POS) ? 0 362 : iParent); 363 } 364 365 Node getUpdatedNode() throws HsqlException { 366 367 Row row = rData.getUpdatedRow(); 368 369 return row == null ? null 370 : row.getNode(iId); 371 } 372 373 void writeTranslate(RowOutputInterface out, IntLookup lookup) { 374 375 out.writeIntData(iBalance); 376 writeTranslatePointer(iLeft, out, lookup); 377 writeTranslatePointer(iRight, out, lookup); 378 writeTranslatePointer(iParent, out, lookup); 379 } 380 381 private void writeTranslatePointer(int pointer, RowOutputInterface out, 382 IntLookup lookup) { 383 384 int newPointer = 0; 385 386 if (pointer != Node.NO_POS) { 387 newPointer = lookup.lookupFirstEqual(pointer); 388 } 389 390 out.writeIntData(newPointer); 391 } 392 } 393 | Popular Tags |