1 21 22 package org.apache.derby.impl.store.access.heap; 23 24 31 32 import org.apache.derby.iapi.services.io.ArrayInputStream; 33 import org.apache.derby.iapi.services.io.CompressedNumber; 34 35 import org.apache.derby.iapi.error.StandardException; 36 import org.apache.derby.iapi.store.access.conglomerate.TransactionManager; 37 import org.apache.derby.iapi.types.CloneableObject; 38 import org.apache.derby.iapi.types.Orderable; 39 import org.apache.derby.iapi.types.RowLocation; 40 import org.apache.derby.iapi.store.raw.RecordHandle; 41 import org.apache.derby.iapi.store.raw.ContainerHandle; 42 import org.apache.derby.iapi.store.raw.Transaction; 43 44 import org.apache.derby.iapi.services.io.FormatIdUtil; 45 import org.apache.derby.iapi.services.io.StoredFormatIds; 46 47 import org.apache.derby.iapi.services.sanity.SanityManager; 48 49 import org.apache.derby.iapi.types.DataValueDescriptor; 50 51 import org.apache.derby.iapi.services.cache.ClassSize; 52 53 import org.apache.derby.iapi.types.DataType; 54 55 56 import java.io.ObjectOutput ; 57 import java.io.ObjectInput ; 58 import java.io.IOException ; 59 60 76 77 public class HeapRowLocation extends DataType implements RowLocation 78 { 79 82 private long pageno; 83 private int recid; 84 private RecordHandle rh; 85 86 private static final int BASE_MEMORY_USAGE = ClassSize.estimateBaseFromCatalog( HeapRowLocation.class); 87 private static final int RECORD_HANDLE_MEMORY_USAGE 88 = ClassSize.estimateBaseFromCatalog( org.apache.derby.impl.store.raw.data.RecordId.class); 89 90 public int estimateMemoryUsage() 91 { 92 int sz = BASE_MEMORY_USAGE; 93 94 if( null != rh) 95 sz += RECORD_HANDLE_MEMORY_USAGE; 96 return sz; 97 } 99 public String getTypeName() { 100 return "RowLocation"; 101 } 102 103 public void setValueFromResultSet(java.sql.ResultSet resultSet, int colNumber, 104 boolean isNullable) { 105 } 106 107 public DataValueDescriptor getNewNull() { 108 return new HeapRowLocation(); 109 } 110 111 public Object getObject() { 112 return null; 113 } 114 115 118 public Object cloneObject() 119 { 120 return getClone(); 121 122 } 123 124 public DataValueDescriptor getClone() { 125 return new HeapRowLocation(this); 126 } 127 128 public int getLength() { 129 return 10; 130 } 131 132 public String getString() { 133 return toString(); 134 } 135 136 143 144 public boolean compare(int op, 145 DataValueDescriptor other, 146 boolean orderedNulls, 147 boolean unknownRV) 148 { 149 int result = compare(other); 151 152 switch(op) 153 { 154 case ORDER_OP_LESSTHAN: 155 return (result < 0); case ORDER_OP_EQUALS: 157 return (result == 0); case ORDER_OP_LESSOREQUALS: 159 return (result <= 0); default: 161 162 if (SanityManager.DEBUG) 163 SanityManager.THROWASSERT("Unexpected operation"); 164 return false; 165 } 166 } 167 168 public int compare(DataValueDescriptor other) 169 { 170 if (SanityManager.DEBUG) 172 SanityManager.ASSERT(other instanceof HeapRowLocation); 173 174 HeapRowLocation arg = (HeapRowLocation) other; 175 176 187 188 long myPage = this.pageno; 189 long otherPage = arg.pageno; 190 191 if (myPage < otherPage) 192 return -1; 193 else if (myPage > otherPage) 194 return 1; 195 196 int myRecordId = this.recid; 197 int otherRecordId = arg.recid; 198 199 if (myRecordId == otherRecordId) 200 return 0; 201 else if (myRecordId < otherRecordId) 202 return -1; 203 else 204 return 1; 205 } 206 207 210 211 HeapRowLocation(RecordHandle rh) 212 { 213 setFrom(rh); 214 } 215 216 public HeapRowLocation() 217 { 218 this.pageno = 0; 219 this.recid = RecordHandle.INVALID_RECORD_HANDLE; 220 } 221 222 223 private HeapRowLocation(HeapRowLocation other) 224 { 225 this.pageno = other.pageno; 226 this.recid = other.recid; 227 this.rh = other.rh; 228 } 229 230 public RecordHandle getRecordHandle(ContainerHandle ch) 231 throws StandardException 232 { 233 if (rh != null) 234 return rh; 235 236 return rh = ch.makeRecordHandle(this.pageno, this.recid); 237 } 238 239 void setFrom(RecordHandle rh) 240 { 241 this.pageno = rh.getPageNumber(); 242 this.recid = rh.getId(); 243 this.rh = rh; 244 } 245 246 252 255 256 289 300 301 304 305 310 public int getTypeFormatId() { 311 return StoredFormatIds.ACCESS_HEAP_ROW_LOCATION_V1_ID; 312 } 313 314 public boolean isNull() 315 { 316 return false; 317 } 318 319 public void writeExternal(ObjectOutput out) 320 throws IOException 321 { 322 CompressedNumber.writeLong(out, this.pageno); 324 325 CompressedNumber.writeInt(out, this.recid); 327 } 328 329 334 public void readExternal(ObjectInput in) 335 throws IOException , ClassNotFoundException 336 { 337 this.pageno = CompressedNumber.readLong(in); 338 339 this.recid = CompressedNumber.readInt(in); 340 341 rh = null; 342 } 343 public void readExternalFromArray(ArrayInputStream in) 344 throws IOException , ClassNotFoundException 345 { 346 this.pageno = in.readCompressedLong(); 347 348 this.recid = in.readCompressedInt(); 349 350 rh = null; 351 } 352 353 public void restoreToNull() 354 { 355 if (SanityManager.DEBUG) 356 SanityManager.THROWASSERT("HeapRowLocation is never null"); 357 } 358 protected void setFrom(DataValueDescriptor theValue) { 359 if (SanityManager.DEBUG) 360 SanityManager.THROWASSERT("SHOULD NOT BE CALLED"); 361 } 362 365 366 371 public boolean equals(Object ref) 372 { 373 374 if ((ref instanceof HeapRowLocation)) 375 { 376 HeapRowLocation other = (HeapRowLocation) ref; 377 378 return( 379 (this.pageno == other.pageno) && (this.recid == other.recid)); 380 } 381 else 382 { 383 return false; 384 } 385 386 } 387 388 393 public int hashCode() 394 { 395 return ((int) this.pageno) ^ this.recid; 396 } 397 398 401 public String toString() 402 { 403 String string = 404 "(" + this.pageno + "," + this.recid + ")"; 405 return(string); 406 } 407 } 408 | Popular Tags |