⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datafilecache.java

📁 纯Java的数据库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            deleteFile(wasNio);            renameDataFile();            backupFile();            database.getProperties().setProperty(                HsqlDatabaseProperties.hsqldb_cache_version,                HsqlDatabaseProperties.THIS_CACHE_VERSION);            database.getProperties().save();            cache.clear();            cache = new Cache(this);            open(cacheReadonly);            dfd.updateTableIndexRoots();            dfd.updateTransactionRowIDs();        } catch (Throwable e) {            database.logger.appLog.logContext(e, null);            throw new HsqlException(                e, Trace.getMessage(Trace.GENERAL_IO_ERROR),                Trace.GENERAL_IO_ERROR);        }        database.logger.appLog.logContext(SimpleLog.LOG_NORMAL, "end");    }    /**     * Used when a row is deleted as a result of some DML or DDL command.     * Removes the row from the cache data structures.     * Adds the file space for the row to the list of free positions.     */    public synchronized void remove(int i,                                    PersistentStore store) throws IOException {        CachedObject r = release(i);        // bug fix for 1.8.0.10        // if r is not in the cache, it is not known if the space has already        // been reallocated to multiple rows, therefore we no longer read the        // space size from file        if (r != null) {            int size = r.getStorageSize();            freeBlocks.add(i, size);        }    }    public synchronized void removePersistence(int i,            PersistentStore store) throws IOException {}    /**     * Allocates file space for the row. <p>     *     * Free space is requested from the block manager if it exists.     * Otherwise the file is grown to accommodate it.     */    private int setFilePos(CachedObject r) throws IOException {        int rowSize = r.getStorageSize();        int i       = freeBlocks == null ? -1                                         : freeBlocks.get(rowSize);        if (i == -1) {            i = (int) (fileFreePosition / cacheFileScale);            long newFreePosition = fileFreePosition + rowSize;            if (newFreePosition > maxDataFileSize) {                throw new IOException(                    Trace.getMessage(Trace.DATA_FILE_IS_FULL));            }            fileFreePosition = newFreePosition;        }        r.setPos(i);        return i;    }    public synchronized void add(CachedObject object) throws IOException {        int size = object.getRealSize(rowOut);        size = ((size + cachedRowPadding - 1) / cachedRowPadding)               * cachedRowPadding;        object.setStorageSize(size);        int i = setFilePos(object);        cache.put(i, object);        // was previously used for text tables        if (storeOnInsert) {            saveRow(object);        }    }    /**     * For a CacheObject that had been previously released from the cache.     * A new version is introduced, using the preallocated space for the object.     */    public synchronized void restore(CachedObject object) throws IOException {        int i = object.getPos();        cache.put(i, object);        // was previously used for text tables        if (storeOnInsert) {            saveRow(object);        }    }    public synchronized int getStorageSize(int i) throws IOException {        CachedObject value = cache.get(i);        if (value != null) {            return value.getStorageSize();        }        return readSize(i);    }    public synchronized CachedObject get(int i, PersistentStore store,                                         boolean keep) throws HsqlException {        if (i < 0) {            return null;        }        try {            CachedObject object = cache.get(i);            if (object == null) {                RowInputInterface rowInput = readObject(i);                if (rowInput == null) {                    return null;                }                object = store.get(rowInput);                if (object == null) {                    throw new IOException("cannot build object from file");                }                // for text tables with empty rows at the beginning,                // pos may move forward in readObject                i = object.getPos();                cache.put(i, object);            }            if (keep) {                object.keepInMemory(true);            }            return object;        } catch (IOException e) {            database.logger.appLog.logContext(e, fileName + " get pos: " + i);            throw Trace.error(Trace.DATA_FILE_ERROR,                              Trace.DataFileCache_makeRow, new Object[] {                e, fileName            });        }    }    synchronized RowInputInterface getRaw(int i) throws IOException {        return readObject(i);    }    protected synchronized int readSize(int pos) throws IOException {        dataFile.seek((long) pos * cacheFileScale);        return dataFile.readInt();    }    protected synchronized RowInputInterface readObject(int pos)    throws IOException {        dataFile.seek((long) pos * cacheFileScale);        int size = dataFile.readInt();        rowIn.resetRow(pos, size);        dataFile.read(rowIn.getBuffer(), 4, size - 4);        return rowIn;    }    public synchronized CachedObject release(int i) {        return cache.release(i);    }    protected synchronized void saveRows(CachedObject[] rows, int offset,                                         int count) throws IOException {        if (count == 0) {            return;        }        try {            for (int i = offset; i < offset + count; i++) {                CachedObject r = rows[i];                saveRow(r);                rows[i] = null;            }        } catch (IOException e) {            database.logger.appLog.logContext(e, null);            throw e;        } catch (Throwable e) {            database.logger.appLog.logContext(e, null);            throw new IOException(e.toString());        } finally {            initBuffers();        }    }    /**     * Writes out the specified Row. Will write only the Nodes or both Nodes     * and table row data depending on what is not already persisted to disk.     */    public synchronized void saveRow(CachedObject row) throws IOException {        setFileModified();        rowOut.reset();        row.write(rowOut);        dataFile.seek((long) row.getPos() * cacheFileScale);        dataFile.write(rowOut.getOutputStream().getBuffer(), 0,                       rowOut.getOutputStream().size());    }    /**     *  Saves the *.data file as compressed *.backup.     *     * @throws  HsqlException     */    void backupFile() throws IOException {        try {            if (fa.isStreamElement(fileName)) {                ZipUnzipFile.compressFile(fileName, backupFileName + ".new",                                          database.getFileAccess());            }        } catch (IOException e) {            database.logger.appLog.logContext(e, null);            throw e;        }    }    void renameBackupFile() {        if (fa.isStreamElement(backupFileName + ".new")) {            fa.removeElement(backupFileName);            fa.renameElement(backupFileName + ".new", backupFileName);        }    }    /**     *  Renames the *.data.new file.     *     * @throws  HsqlException     */    void renameDataFile() {        if (fa.isStreamElement(fileName + ".new")) {            fa.removeElement(fileName);            fa.renameElement(fileName + ".new", fileName);        }    }    void deleteFile(boolean wasNio) {        // first attemp to delete        fa.removeElement(fileName);        // OOo related code        if (database.isStoredFileAccess()) {            return;        }        // OOo end        if (fa.isStreamElement(fileName)) {            if (wasNio) {                System.gc();                fa.removeElement(fileName);            }            if (fa.isStreamElement(fileName)) {                fa.renameElement(fileName, fileName + ".old");                File oldfile = new File(fileName + ".old");                FileUtil.getDefaultInstance().deleteOnExit(oldfile);            }        }    }    void deleteBackup() {        fa.removeElement(backupFileName);    }    /**     * This method deletes a data file or resets its free position.     * this is used only for nio files - not OOo files     */    static void deleteOrResetFreePos(Database database, String filename) {        ScaledRAFile raFile = null;        database.getFileAccess().removeElement(filename);        // OOo related code        if (database.isStoredFileAccess()) {            return;        }        // OOo end        if (!database.getFileAccess().isStreamElement(filename)) {            return;        }        try {            raFile = new ScaledRAFile(database, filename, false);            raFile.seek(LONG_FREE_POS_POS);            raFile.writeLong(INITIAL_FREE_POS);        } catch (IOException e) {            database.logger.appLog.logContext(e, null);        } finally {            if (raFile != null) {                try {                    raFile.close();                } catch (IOException e) {                    database.logger.appLog.logContext(e, null);                }            }        }    }    public int capacity() {        return maxCacheSize;    }    public long bytesCapacity() {        return maxCacheBytes;    }    public long getTotalCachedBlockSize() {        return cache.getTotalCachedBlockSize();    }    public int getFreeBlockCount() {        return freeBlocks.size();    }    public int getTotalFreeBlockSize() {        return 0;    }    public long getFileFreePos() {        return fileFreePosition;    }    public int getCachedObjectCount() {        return cache.size();    }    public String getFileName() {        return fileName;    }    public boolean hasRowInfo() {        return hasRowInfo;    }    public boolean isFileModified() {        return fileModified;    }    protected synchronized void setFileModified() throws IOException {        if (!fileModified) {/*            // unset saved flag;            dataFile.seek(FLAGS_POS);            int flag = BitMap.set(0, FLAG_ISSAVED);            if (hasRowInfo) {                flag = BitMap.set(flag, FLAG_ROWINFO);            }            dataFile.writeInt(flag);*/            fileModified = true;        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -