1 21 package com.db4o; 22 23 import java.io.*; 24 25 import com.db4o.config.*; 26 import com.db4o.ext.*; 27 import com.db4o.inside.*; 28 import com.db4o.io.*; 29 30 33 public class YapRandomAccessFile extends YapFile { 34 35 private Session i_session; 36 37 private IoAdapter i_file; 38 private IoAdapter i_timerFile; private volatile IoAdapter i_backupFile; 40 private byte[] i_timerBytes = new byte[YapConst.LONG_BYTES]; 41 42 private Object i_fileLock; 43 44 YapRandomAccessFile(Configuration config,Session a_session) throws Exception { 45 super(config,null); 46 synchronized (i_lock) { 47 i_fileLock = new Object (); 48 i_session = a_session; 49 if (Deploy.debug) { 50 open(); 53 } else { 54 try { 55 open(); 56 } catch (DatabaseFileLockedException e) { 57 stopSession(); 58 throw e; 59 } 60 } 61 initialize3(); 62 } 63 } 64 65 public void backup(String path) throws IOException { 66 synchronized (i_lock) { 67 checkClosed(); 68 if (i_backupFile != null) { 69 Exceptions4.throwRuntimeException(61); 70 } 71 try { 72 i_backupFile = configImpl().ioAdapter().open(path, true, i_file.getLength()); 73 i_backupFile.blockSize(blockSize()); 74 } catch (Exception e) { 75 i_backupFile = null; 76 Exceptions4.throwRuntimeException(12, path); 77 } 78 } 79 long pos = 0; 80 int bufferlength = 8192; 81 byte[] buffer = new byte[bufferlength]; 82 while(true){ 83 synchronized (i_lock) { 84 i_file.seek(pos); 85 int read = i_file.read(buffer); 86 if(read <= 0 ){ 87 break; 88 } 89 i_backupFile.seek(pos); 90 i_backupFile.write(buffer, read); 91 pos += read; 92 } 93 try { 94 Thread.sleep(1); 95 } catch (InterruptedException e) { 96 97 } 98 } 99 100 synchronized (i_lock) { 101 i_backupFile.close(); 102 i_backupFile = null; 103 } 104 } 105 106 public void blockSize(int size){ 107 i_file.blockSize(size); 108 if (i_timerFile != null) { 109 i_timerFile.blockSize(size); 110 } 111 } 112 113 public byte blockSize() { 114 return (byte) i_file.blockSize(); 115 } 116 117 protected boolean close2() { 118 boolean stopSession = true; 119 synchronized (Global4.lock) { 120 stopSession = i_session.closeInstance(); 121 if (stopSession) { 122 freePrefetchedPointers(); 123 if (Deploy.debug) { 124 write(true); 125 } else { 126 try { 127 write(true); 128 } catch (Throwable t) { 129 fatalException(t); 130 } 131 } 132 super.close2(); 133 Db4o.sessionStopped(i_session); 134 synchronized (i_fileLock) { 135 try { 136 i_file.close(); 137 i_file = null; 138 _fileHeader.close(); 139 closeTimerFile(); 140 } catch (Exception e) { 141 i_file = null; 142 Exceptions4.throwRuntimeException(11, e); 143 } 144 i_file = null; 145 } 146 } 147 } 148 return stopSession; 149 } 150 151 public void commit1() { 152 ensureLastSlotWritten(); 153 super.commit1(); 154 } 155 156 public void copy(int oldAddress, int oldAddressOffset, int newAddress, int newAddressOffset, int length) { 157 158 if (Debug.xbytes && Deploy.overwrite) { 159 checkXBytes(newAddress, newAddressOffset, length); 160 } 161 162 try { 163 164 if (i_backupFile == null) { 165 i_file 166 .blockCopy(oldAddress, oldAddressOffset, newAddress, newAddressOffset, length); 167 return; 168 } 169 170 byte[] copyBytes = new byte[length]; 171 i_file.blockSeek(oldAddress, oldAddressOffset); 172 i_file.read(copyBytes); 173 174 i_file.blockSeek(newAddress, newAddressOffset); 175 i_file.write(copyBytes); 176 177 if (i_backupFile != null) { 178 i_backupFile.blockSeek(newAddress, newAddressOffset); 179 i_backupFile.write(copyBytes); 180 } 181 182 } catch (Exception e) { 183 Exceptions4.throwRuntimeException(16, e); 184 } 185 186 } 187 188 private void checkXBytes(int a_newAddress, int newAddressOffset, int a_length) { 189 if (Debug.xbytes && Deploy.overwrite) { 190 try { 191 byte[] checkXBytes = new byte[a_length]; 192 i_file.blockSeek(a_newAddress, newAddressOffset); 193 i_file.read(checkXBytes); 194 for (int i = 0; i < checkXBytes.length; i++) { 195 if (checkXBytes[i] != YapConst.XBYTE) { 196 String msg = "XByte corruption adress:" + a_newAddress + " length:" 197 + a_length; 198 throw new RuntimeException (msg); 199 } 200 } 201 } catch (Exception e) { 202 e.printStackTrace(); 203 } 204 } 205 } 206 207 void emergencyClose() { 208 super.emergencyClose(); 209 try { 210 i_file.close(); 211 } catch (Exception e) { 212 } 213 try { 214 Db4o.sessionStopped(i_session); 215 } catch (Exception e) { 216 } 217 i_file = null; 218 } 219 220 public long fileLength() { 221 try { 222 return i_file.getLength(); 223 } catch (Exception e) { 224 throw new RuntimeException (); 225 } 226 } 227 228 String fileName() { 229 return i_session.fileName(); 230 } 231 232 private void open() throws Exception { 233 boolean isNew = false; 234 IoAdapter ioAdapter = configImpl().ioAdapter(); 235 if (Deploy.debug) { 236 if (Deploy.deleteFile) { 237 System.out.println("Debug option set to DELETE file."); 238 try { 239 ioAdapter.delete(i_session.fileName()); 240 } catch (Exception e) { 241 } 242 } 243 } 244 try { 245 if (fileName().length() > 0) { 246 247 if(! ioAdapter.exists(fileName())){ 248 isNew = true; 249 logMsg(14, fileName()); 250 i_handlers.oldEncryptionOff(); 251 } 252 253 try { 254 boolean lockFile = Debug.lockFile && configImpl().lockFile() 255 && (!configImpl().isReadOnly()); 256 i_file = ioAdapter.open(fileName(), lockFile, 0); 257 if (needsTimerFile()) { 258 i_timerFile = ioAdapter.open(fileName(), false, 0); 259 } 260 } catch (DatabaseFileLockedException de) { 261 throw de; 262 } catch (Exception e) { 263 Exceptions4.throwRuntimeException(12, fileName(), e); 264 } 265 if (isNew) { 266 configureNewFile(); 267 if (configImpl().reservedStorageSpace() > 0) { 268 reserve(configImpl().reservedStorageSpace()); 269 } 270 write(false); 271 writeHeader(false); 272 } else { 273 readThis(); 274 } 275 } else { 276 Exceptions4.throwRuntimeException(21); 277 } 278 } catch (Exception exc) { 279 if (i_references != null) { 280 i_references.stopTimer(); 281 } 282 throw exc; 283 } 284 } 285 286 public void readBytes(byte[] bytes, int address, int length) { 287 readBytes(bytes, address, 0, length); 288 } 289 290 public void readBytes(byte[] bytes, int address, int addressOffset, int length) { 291 292 if (DTrace.enabled) { 293 DTrace.READ_BYTES.logLength(address + addressOffset, length); 294 } 295 296 try{ 297 i_file.blockSeek(address, addressOffset); 298 int bytesRead=i_file.read(bytes, length); 299 if(bytesRead!=length) { 300 Exceptions4.throwRuntimeException(68, address+"/"+addressOffset,null,false); 301 } 302 }catch(IOException ioex){ 303 304 308 312 if(Debug.atHome){ 313 ioex.printStackTrace(); 314 } 315 316 throw new RuntimeException (); 317 } 318 } 319 320 void reserve(int byteCount) { 321 synchronized (i_lock) { 322 int address = getSlot(byteCount); 323 writeBytes(new YapReader(byteCount), address, 0); 324 free(address, byteCount); 325 } 326 } 327 328 public void syncFiles() { 329 try { 330 i_file.sync(); 331 if (i_timerFile != null) { 332 i_timerFile.sync(); 333 } 334 } catch (Exception e) { 335 } 336 } 337 338 private boolean needsTimerFile() { 339 return needsLockFileThread() && Debug.lockFile; 340 } 341 342 public boolean writeAccessTime(int address, int offset, long time) throws IOException { 343 synchronized (i_fileLock) { 344 if(i_timerFile == null){ 345 return false; 346 } 347 i_timerFile.blockSeek(address, offset); 348 if (Deploy.debug) { 349 YapReader lockBytes = new YapWriter(i_systemTrans, YapConst.LONG_LENGTH); 350 lockBytes.writeLong(time); 351 i_timerFile.write(lockBytes._buffer); 352 } else { 353 YLong.writeLong(time, i_timerBytes); 354 i_timerFile.write(i_timerBytes); 355 } 356 if(i_file == null){ 357 closeTimerFile(); 358 return false; 359 } 360 return true; 361 } 362 } 363 364 private void closeTimerFile() throws IOException{ 365 if(i_timerFile == null){ 366 return; 367 } 368 i_timerFile.close(); 369 i_timerFile = null; 370 } 371 372 373 public void writeBytes(YapReader a_bytes, int address, int addressOffset) { 374 if (configImpl().isReadOnly()) { 375 return; 376 } 377 if (Deploy.debug && !Deploy.flush) { 378 return; 379 } 380 381 try { 382 383 if (Debug.xbytes && Deploy.overwrite) { 384 385 boolean doCheck = true; 386 if(a_bytes instanceof YapWriter){ 387 YapWriter writer = (YapWriter)a_bytes; 388 if(writer.getID() == YapConst.IGNORE_ID){ 389 doCheck = false; 390 } 391 } 392 if (doCheck) { 393 checkXBytes(address, addressOffset, a_bytes.getLength()); 394 } 395 } 396 397 if (DTrace.enabled) { 398 DTrace.WRITE_BYTES.logLength(address + addressOffset,a_bytes.getLength()); 399 } 400 401 i_file.blockSeek(address, addressOffset); 402 i_file.write(a_bytes._buffer, a_bytes.getLength()); 403 if (i_backupFile != null) { 404 i_backupFile.blockSeek(address, addressOffset); 405 i_backupFile.write(a_bytes._buffer, a_bytes.getLength()); 406 } 407 408 } catch (Exception e) { 409 Exceptions4.throwRuntimeException(16, e); 410 } 411 } 412 413 public void debugWriteXBytes(int a_address, int a_length) { 414 if (Debug.xbytes) { 415 writeXBytes(a_address, a_length); 416 } 417 } 418 419 public void writeXBytes(int a_address, int a_length) { 420 if (Deploy.flush) { 421 if (!configImpl().isReadOnly()) { 422 if(a_address > 0 && a_length > 0){ 423 try { 424 if(DTrace.enabled){ 425 DTrace.WRITE_XBYTES.logLength(a_address, a_length); 426 } 427 i_file.blockSeek(a_address); 428 i_file.write(xBytes(a_address, a_length)._buffer, a_length); 429 430 } catch (Exception e) { 431 e.printStackTrace(); 432 } 433 } 434 } 435 } 436 } 437 438 }
| Popular Tags
|