/** * Parameter write indicates either an orderly close, or a fast close without backup. * * <p>When false, just closes the file. * * <p>When true, writes out all cached rows that have been modified and the free position pointer * for the *.data file and then closes the file. */ public void close(boolean write) throws HsqlException { try { if (cacheReadonly) { dataFile.close(); return; } StopWatch sw = new StopWatch(); if (write) { cache.saveAll(); Trace.printSystemOut("saveAll: " + sw.elapsedTime()); // set empty dataFile.seek(LONG_EMPTY_SIZE); dataFile.writeLong(freeBlocks.getLostBlocksSize()); // set end dataFile.seek(LONG_FREE_POS_POS); dataFile.writeLong(fileFreePosition); // set saved flag; dataFile.seek(FLAGS_POS); int flag = BitMap.set(0, FLAG_ISSAVED); if (hasRowInfo) { flag = BitMap.set(flag, FLAG_ROWINFO); } dataFile.writeInt(flag); // dataFile.seek(fileFreePosition); Trace.printSystemOut("pos and flags: " + sw.elapsedTime()); } if (dataFile != null) { dataFile.close(); dataFile = null; Trace.printSystemOut("close: " + sw.elapsedTime()); } boolean empty = fileFreePosition == INITIAL_FREE_POS; if (empty) { fa.removeElement(fileName); fa.removeElement(backupFileName); } } catch (Exception e) { database.logger.appLog.logContext(e); throw Trace.error(Trace.FILE_IO_ERROR, Trace.DataFileCache_close, new Object[] {e, fileName}); } }
/** * Parameter write indicates either an orderly close, or a fast close without backup. * * <p>When false, just closes the file. * * <p>When true, writes out all cached rows that have been modified and the free position pointer * for the *.data file and then closes the file. */ public void close(boolean write) throws HsqlException { SimpleLog appLog = database.logger.appLog; try { if (cacheReadonly) { if (dataFile != null) { dataFile.close(); } return; } StopWatch sw = new StopWatch(); appLog.sendLine(SimpleLog.LOG_NORMAL, "DataFileCache.close(" + write + ") : start"); if (write) { cache.saveAll(); Trace.printSystemOut("saveAll: " + sw.elapsedTime()); appLog.sendLine(SimpleLog.LOG_NORMAL, "DataFileCache.close() : save data"); if (fileModified || freeBlocks.isModified()) { // set empty dataFile.seek(LONG_EMPTY_SIZE); dataFile.writeLong(freeBlocks.getLostBlocksSize()); // set end dataFile.seek(LONG_FREE_POS_POS); dataFile.writeLong(fileFreePosition); // set saved flag; dataFile.seek(FLAGS_POS); int flag = BitMap.set(0, FLAG_ISSAVED); if (hasRowInfo) { flag = BitMap.set(flag, FLAG_ROWINFO); } dataFile.writeInt(flag); appLog.sendLine(SimpleLog.LOG_NORMAL, "DataFileCache.close() : flags"); // if (dataFile.length() != fileFreePosition) { dataFile.seek(fileFreePosition); } appLog.sendLine(SimpleLog.LOG_NORMAL, "DataFileCache.close() : seek end"); Trace.printSystemOut("pos and flags: " + sw.elapsedTime()); } } if (dataFile != null) { dataFile.close(); appLog.sendLine(SimpleLog.LOG_NORMAL, "DataFileCache.close() : close"); dataFile = null; Trace.printSystemOut("close: " + sw.elapsedTime()); } boolean empty = fileFreePosition == INITIAL_FREE_POS; if (empty) { fa.removeElement(fileName); fa.removeElement(backupFileName); } } catch (Throwable e) { appLog.logContext(e); throw Trace.error(Trace.FILE_IO_ERROR, Trace.DataFileCache_close, new Object[] {e, fileName}); } }
void process() throws IOException { boolean complete = false; Error.printSystemOut("Defrag Transfer begins"); transactionRowLookup = database.txManager.getTransactionIDList(); HsqlArrayList allTables = database.schemaManager.getAllTables(); rootsList = new int[allTables.size()][]; Storage dest = null; try { OutputStream fos = database.logger.getFileAccess().openOutputStreamElement(dataFileName + ".new"); fileStreamOut = new BufferedOutputStream(fos, 1 << 12); for (int i = 0; i < DataFileCache.INITIAL_FREE_POS; i++) { fileStreamOut.write(0); } fileOffset = DataFileCache.INITIAL_FREE_POS; for (int i = 0, tSize = allTables.size(); i < tSize; i++) { Table t = (Table) allTables.get(i); if (t.getTableType() == TableBase.CACHED_TABLE) { int[] rootsArray = writeTableToDataFile(t); rootsList[i] = rootsArray; } else { rootsList[i] = null; } Error.printSystemOut(t.getName().name + " complete"); } writeTransactionRows(); fileStreamOut.flush(); fileStreamOut.close(); fileStreamOut = null; // write out the end of file position dest = ScaledRAFile.newScaledRAFile( database, dataFileName + ".new", false, ScaledRAFile.DATA_FILE_RAF, database .getURLProperties() .getProperty(HsqlDatabaseProperties.url_storage_class_name), database.getURLProperties().getProperty(HsqlDatabaseProperties.url_storage_key)); dest.seek(DataFileCache.LONG_FREE_POS_POS); dest.writeLong(fileOffset); // set shadowed flag; int flags = 0; if (database.logger.propIncrementBackup) { flags = BitMap.set(flags, DataFileCache.FLAG_ISSHADOWED); } flags = BitMap.set(flags, DataFileCache.FLAG_190); flags = BitMap.set(flags, DataFileCache.FLAG_ISSAVED); dest.seek(DataFileCache.FLAGS_POS); dest.writeInt(flags); dest.close(); dest = null; for (int i = 0, size = rootsList.length; i < size; i++) { int[] roots = rootsList[i]; if (roots != null) { Error.printSystemOut(org.hsqldb.lib.StringUtil.getList(roots, ",", "")); } } complete = true; } catch (IOException e) { throw Error.error(ErrorCode.FILE_IO_ERROR, dataFileName + ".new"); } catch (OutOfMemoryError e) { throw Error.error(ErrorCode.OUT_OF_MEMORY); } finally { if (fileStreamOut != null) { fileStreamOut.close(); } if (dest != null) { dest.close(); } if (!complete) { database.logger.getFileAccess().removeElement(dataFileName + ".new"); } } // Error.printSystemOut("Transfer complete: ", stopw.elapsedTime()); }