1 65 66 67 package org.hsqldb; 68 69 import java.io.IOException ; 70 71 import org.hsqldb.lib.IntLookup; 72 import org.hsqldb.lib.java.JavaSystem; 73 import org.hsqldb.rowio.RowInputInterface; 74 import org.hsqldb.rowio.RowOutputInterface; 75 76 82 97 public class CachedRow extends Row { 98 99 static final int NO_POS = -1; 100 101 protected Table tTable; 103 int storageSize; 104 105 108 protected boolean hasDataChanged; 109 110 113 boolean hasNodesChanged; 114 115 118 CachedRow() {} 119 120 public static CachedRow newCachedRow(Table t, 121 Object [] o) throws HsqlException { 122 123 if (t.isText) { 124 return new CachedDataRow(t, o); 125 } else { 126 return new CachedRow(t, o); 127 } 128 } 129 130 138 CachedRow(Table t, Object [] o) throws HsqlException { 139 140 tTable = t; 141 142 int indexcount = t.getIndexCount(); 143 144 nPrimaryNode = Node.newNode(this, 0, t); 145 146 Node n = nPrimaryNode; 147 148 for (int i = 1; i < indexcount; i++) { 149 n.nNext = Node.newNode(this, i, t); 150 n = n.nNext; 151 } 152 153 oData = o; 154 hasDataChanged = hasNodesChanged = true; 155 } 156 157 165 public CachedRow(Table t, 166 RowInputInterface in) throws IOException , HsqlException { 167 168 tTable = t; 169 iPos = in.getPos(); 170 storageSize = in.getSize(); 171 172 int indexcount = t.getIndexCount(); 173 174 nPrimaryNode = Node.newNode(this, in, 0, t); 175 176 Node n = nPrimaryNode; 177 178 for (int i = 1; i < indexcount; i++) { 179 n.nNext = Node.newNode(this, in, i, t); 180 n = n.nNext; 181 } 182 183 oData = in.readData(tTable.getColumnTypes()); 184 } 185 186 private void readRowInfo(RowInputInterface in) 187 throws IOException , HsqlException { 188 189 } 191 192 199 public void delete() throws HsqlException { 200 201 super.delete(); 202 203 hasNodesChanged = hasDataChanged = false; 204 tTable = null; 205 } 206 207 public int getStorageSize() { 208 return storageSize; 209 } 210 211 216 public void setPos(int pos) { 217 iPos = pos; 218 } 219 220 223 void setChanged() { 224 hasNodesChanged = true; 225 } 226 227 232 public boolean hasChanged() { 233 return hasNodesChanged; 234 } 235 236 241 public Table getTable() { 242 return tTable; 243 } 244 245 248 public int getRealSize(RowOutputInterface out) { 249 return tTable.getIndexCount() * DiskNode.SIZE_IN_BYTE 250 + out.getSize(this); 251 } 252 253 public void setStorageSize(int size) { 254 storageSize = size; 255 } 256 257 264 synchronized public boolean isKeepInMemory() { 265 266 Node n = nPrimaryNode; 267 268 while (n != null) { 269 if (n.isRoot()) { 270 return true; 271 } 272 273 n = n.nNext; 274 } 275 276 return false; 277 } 278 279 291 synchronized Row getUpdatedRow() throws HsqlException { 292 return tTable == null ? null 293 : (CachedRow) tTable.rowStore.get(iPos); 294 } 295 296 299 void setNewNodes() {} 300 301 312 public void write(RowOutputInterface out) { 313 314 try { 315 writeNodes(out); 316 317 if (hasDataChanged) { 318 out.writeData(oData, tTable); 319 out.writeEnd(); 320 321 hasDataChanged = false; 322 } 323 } catch (IOException e) {} 324 } 325 326 private void writeRowInfo(RowOutputInterface out) { 327 328 } 330 331 public void write(RowOutputInterface out, IntLookup lookup) { 332 333 out.writeSize(storageSize); 334 335 Node rownode = nPrimaryNode; 336 337 while (rownode != null) { 338 ((DiskNode) rownode).writeTranslate(out, lookup); 339 340 rownode = rownode.nNext; 341 } 342 343 out.writeData(getData(), getTable()); 344 out.writeEnd(); 345 } 346 347 355 private void writeNodes(RowOutputInterface out) throws IOException { 356 357 out.writeSize(storageSize); 358 359 Node n = nPrimaryNode; 360 361 while (n != null) { 362 n.write(out); 363 364 n = n.nNext; 365 } 366 367 hasNodesChanged = false; 368 } 369 370 377 public boolean equals(Object obj) { 378 379 if (obj == this) { 380 return true; 381 } 382 383 if (obj instanceof CachedRow) { 384 return ((CachedRow) obj).iPos == iPos; 385 } 386 387 return false; 388 } 389 390 395 public int hashCode() { 396 return iPos; 397 } 398 } 399 | Popular Tags |