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

📄 log.java

📁 一个用java写的开源的数据库系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        if ((new File(file)).exists()) {            // there must be no such file; overwriting not allowed for security            throw Trace.error(Trace.FILE_IO_ERROR, file);        }        try {            long time = 0;            if (Trace.TRACE) {                time = System.currentTimeMillis();            }            // only ddl commands; needs not so much memory            Result r;            if (full) {                // no drop, no insert, and no positions for cached tables                r = db.getScript(false, false, false, session);            } else {                // no drop, no insert, but positions for cached tables                r = db.getScript(false, false, true, session);            }            Record     n = r.rRoot;            FileWriter w = new FileWriter(file);            while (n != null) {                writeLine(w, (String) n.data[0]);                n = n.next;            }            // inserts are done separetely to save memory            Vector tables = db.getTables();            for (int i = 0; i < tables.size(); i++) {                Table t = (Table) tables.elementAt(i);// cached tables have the index roots set in the ddl script                if ((full ||!t.isCached()) &&!t.isTemp() &&!t.isView()                        && (!t.isText() ||!t.isDataReadOnly())) {                    Index primary = t.getPrimaryIndex();                    Node  x       = primary.first();                    while (x != null) {                        writeLine(w, t.getInsertStatement(x.getData()));                        x = primary.next(x);                    }                }// fredt@users 20020221 - patch 513005 by sqlbob@users (RMP)                if (t.isDataReadOnly() &&!t.isTemp() &&!t.isText()) {                    StringBuffer a = new StringBuffer("SET TABLE ");                    a.append(t.getName().statementName);                    a.append(" READONLY TRUE");                    writeLine(w, a.toString());                }            }            w.close();            if (Trace.TRACE) {                Trace.trace(time - System.currentTimeMillis());            }        } catch (IOException e) {            throw Trace.error(Trace.FILE_IO_ERROR, file + " " + e);        }    }    /**     *  Method declaration     *     * @param  file     */    private void renameNewToCurrent(String file) {        // even if it crashes here, recovering is no problem        File newFile = new File(file + ".new");        if (newFile.exists()) {            // if we have a new file            // delete the old (maybe already deleted)            File oldFile = new File(file);            oldFile.delete();            // rename the new to the current            newFile.renameTo(oldFile);        }    }    private void create() throws SQLException {        if (Trace.TRACE) {            Trace.trace(sName);        }        pProperties.setProperty("version", jdbcDriver.VERSION);        pProperties.setProperty("sql.strict_fk", true);        pProperties.save();    }    /**     *  Method declaration     *     * @return     * @throws  SQLException     */    private boolean isAlreadyOpen() throws SQLException {        // reading the last modified, wait 3 seconds, read again.        // if the same information was read the file was not changed        // and is probably, except the other process is blocked        if (Trace.TRACE) {            Trace.trace();        }        File f  = new File(sName + ".lock");        long l1 = f.lastModified();        try {            Thread.sleep(3000);        } catch (Exception e) {}        long l2 = f.lastModified();        if (l1 != l2) {            return true;        }        return pProperties.isFileOpen();    }    /**     *  Saves the *.data file as compressed *.backup.     *     * @throws  SQLException     */    private void backup() throws SQLException {        if (Trace.TRACE) {            Trace.trace();            // if there is no cache file then backup is not necessary        }        if (!(new File(sFileCache)).exists()) {            return;        }        try {            long time = 0;            if (Trace.TRACE) {                time = System.currentTimeMillis();            }            // create a '.new' file; rename later            DeflaterOutputStream f = new DeflaterOutputStream(                new FileOutputStream(sFileBackup + ".new"),                new Deflater(Deflater.BEST_SPEED), COPY_BLOCK_SIZE);            byte            b[] = new byte[COPY_BLOCK_SIZE];            FileInputStream in  = new FileInputStream(sFileCache);            while (true) {                int l = in.read(b, 0, COPY_BLOCK_SIZE);                if (l == -1) {                    break;                }                f.write(b, 0, l);            }            f.close();            in.close();            if (Trace.TRACE) {                Trace.trace(time - System.currentTimeMillis());            }        } catch (Exception e) {            throw Trace.error(Trace.FILE_IO_ERROR, sFileBackup);        }    }    /**     *  Method declaration     *     * @throws  SQLException     */    private void restoreBackup() throws SQLException {        if (Trace.TRACE) {            Trace.trace("not closed last time!");        }        if (!(new File(sFileBackup)).exists()) {            // the backup don't exists because it was never made or is empty            // the cache file must be deleted in this case            (new File(sFileCache)).delete();            return;        }        try {            long time = 0;            if (Trace.TRACE) {                time = System.currentTimeMillis();            }            InflaterInputStream f =                new InflaterInputStream(new FileInputStream(sFileBackup),                                        new Inflater());            FileOutputStream cache = new FileOutputStream(sFileCache);            byte             b[]   = new byte[COPY_BLOCK_SIZE];            while (true) {                int l = f.read(b, 0, COPY_BLOCK_SIZE);                if (l == -1) {                    break;                }                cache.write(b, 0, l);            }            cache.close();            f.close();            if (Trace.TRACE) {                Trace.trace(time - System.currentTimeMillis());            }        } catch (Exception e) {            throw Trace.error(Trace.FILE_IO_ERROR, sFileBackup);        }    }    /**     *  Method declaration     *     * @throws  SQLException     */    private void openScript() throws SQLException {        if (Trace.TRACE) {            Trace.trace();        }        try {            // todo: use a compressed stream            wScript = new BufferedWriter(new FileWriter(sFileScript, true),                                         4096);        } catch (Exception e) {            throw Trace.error(Trace.FILE_IO_ERROR, sFileScript);        }    }    /**     *  Method declaration     *     * @throws  SQLException     */    private void closeScript() throws SQLException {        if (Trace.TRACE) {            Trace.trace();        }        try {            if (wScript != null) {                wScript.close();                wScript = null;            }        } catch (Exception e) {            throw Trace.error(Trace.FILE_IO_ERROR, sFileScript);        }    }    /**     *  Method declaration     *     * @throws  SQLException     */    private void runScript() throws SQLException {        if (Trace.TRACE) {            Trace.trace();        }        if (!(new File(sFileScript)).exists()) {            return;        }        bRestoring = true;        dDatabase.setReferentialIntegrity(false);        Vector session = new Vector();        session.addElement(sysSession);        Session current = sysSession;        try {            long time = 0;            if (Trace.TRACE) {                time = System.currentTimeMillis();            }            LineNumberReader r =                new LineNumberReader(new FileReader(sFileScript));            while (true) {                String s = readLine(r);                if (s == null) {                    break;                }                if (s.startsWith("/*C")) {                    int id = Integer.parseInt(s.substring(3, s.indexOf('*',                        4)));                    if (id >= session.size()) {                        session.setSize(id + 1);                    }                    current = (Session) session.elementAt(id);                    if (current == null) {                        current = new Session(sysSession, id);                        session.setElementAt(current, id);                        dDatabase.registerSession(current);                    }                    s = s.substring(s.indexOf('/', 1) + 1);                }                if (s.length() != 0) {                    Result result = dDatabase.execute(s, current);                    if ((result != null) && (result.iMode == Result.ERROR)) {                        throw (Trace.getError(result.errorCode,                                              result.sError));                    }                }                if (s.equals("DISCONNECT")) {                    int id = current.getId();                    current = new Session(sysSession, id);                    session.setElementAt(current, id);                }            }            r.close();            for (int i = 0; i < session.size(); i++) {                current = (Session) session.elementAt(i);                if (current != null) {                    current.rollback();                }            }            if (Trace.TRACE) {                Trace.trace(time - System.currentTimeMillis());            }        } catch (IOException e) {            throw Trace.error(Trace.FILE_IO_ERROR, sFileScript + " " + e);        }        dDatabase.setReferentialIntegrity(true);        bRestoring = false;    }    /**     *  Method declaration     *     * @param  full     * @throws  SQLException     */    private void writeScript(boolean full) throws SQLException {        if (Trace.TRACE) {            Trace.trace();            // create script in '.new' file        }        (new File(sFileScript + ".new")).delete();        // script; but only positions of cached tables, not full        scriptToFile(dDatabase, sFileScript + ".new", full, sysSession);    }    /**     *  Method declaration     *     * @param  w     * @param  s     * @throws  IOException     */// fredt@users 20011120 - patch 450455 by kibu@users - optimised    private static final String lineSep = System.getProperty("line.separator",        "\n");    private static int writeLine(Writer w, String s) throws IOException {        String logLine =            StringConverter.unicodeToAscii(s).append(lineSep).toString();        w.write(logLine);        return logLine.length();    }    /**     *  Method declaration     *     * @param  r     * @return     * @throws  IOException     */    private static String readLine(LineNumberReader r) throws IOException {        String s = r.readLine();        return StringConverter.asciiToUnicode(s);    }    /**     *  Method declaration     *     * @return     */    HsqlDatabaseProperties getProperties() {        return pProperties;    }// fredt@users 20020221 - patch 513005 by sqlbob@users (RMP) - text tables    private Hashtable textCacheList = new Hashtable();    Cache openTextCache(String table, String source, boolean readOnlyData,                        boolean reversed) throws SQLException {        closeTextCache(table);        if (pProperties.getProperty("textdb.allow_full_path",                                    "false").equals("false")) {            if (source.indexOf("..") != -1) {                throw (Trace.error(Trace.ACCESS_IS_DENIED, source));            }            String path =                new File(new File(sName).getAbsolutePath()).getParent();            if (path != null) {                source = path + File.separator + source;            }        }        String    prefix = "textdb." + table.toLowerCase() + ".";        TextCache c;        if (reversed) {            c = new ReverseTextCache(source, prefix, pProperties);        } else {            c = new TextCache(source, prefix, pProperties);        }        c.open(readOnlyData || bReadOnly);        textCacheList.put(table, c);        return (c);    }    void closeTextCache(String table) throws SQLException {        TextCache c = (TextCache) textCacheList.remove(table);        if (c != null) {            c.flush();        }    }    void closeAllTextCaches(boolean compact) throws SQLException {        for (Enumeration e =                textCacheList.elements(); e.hasMoreElements(); ) {            if (compact) {                ((TextCache) e.nextElement()).purge();            } else {                ((TextCache) e.nextElement()).flush();            }        }    }    void reopenAllTextCaches() throws SQLException {        for (Enumeration e =                textCacheList.elements(); e.hasMoreElements(); ) {            ((TextCache) e.nextElement()).reopen();        }    }    void shutdownAllTextCaches() throws SQLException {        for (Enumeration e =                textCacheList.elements(); e.hasMoreElements(); ) {            ((TextCache) e.nextElement()).closeFile();        }        textCacheList = new Hashtable();    }}

⌨️ 快捷键说明

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