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

📄 log.java

📁 Java写的含有一个jdbc驱动的小型数据库数据库引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	}
    }

    /**
     * Method declaration
     *
     *
     * @param file
     */
    private void renameNewToCurrent(String file) {

	// even if it crashes here, recovering is no problem
	if ((new File(file + ".new")).exists()) {

	    // if we have a new file
	    // delete the old (maybe already deleted)
	    (new File(file)).delete();

	    // rename the new to the current
	    new File(file + ".new").renameTo(new File(file));
	}
    }

    /**
     * Method declaration
     *
     *
     * @throws SQLException
     */
    private void closeProperties() throws SQLException {
	try {
	    if (fProperties != null) {
		if (Trace.TRACE) {
		    Trace.trace();
		}

		fProperties.close();

		fProperties = null;
	    }
	} catch (Exception e) {
	    throw Trace.error(Trace.FILE_IO_ERROR, sFileProperties + " " + e);
	}
    }

    /**
     * Method declaration
     *
     *
     * @throws SQLException
     */
    private void create() throws SQLException {
	if (Trace.TRACE) {
	    Trace.trace(sName);
	}

	pProperties.put("modified", "no");
	pProperties.put("version", jdbcDriver.VERSION);
	saveProperties();
    }

    /**
     * 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;
	}

	// check by trying to delete the properties file
	// this will not work if some application has the file open
	// this is why the properties file is kept open when running ;-)
	// todo: check if this works in all operating systems
	closeProperties();

	if (Trace.TRACE) {
	    Trace.trace();
	}

	if ((new File(sFileProperties)).delete() == false) {
	    return true;
	}

	// the file was deleted, so recreate it now
	saveProperties();

	return false;
    }

    /**
     * Method declaration
     *
     *
     * @throws SQLException
     */
    private void loadProperties() throws SQLException {
	File f = new File(sFileProperties);

	closeProperties();

	if (Trace.TRACE) {
	    Trace.trace();
	}

	try {

	    // the file is closed only when the database is closed
	    fProperties = new FileInputStream(f);

	    pProperties.load(fProperties);
	} catch (Exception e) {
	    throw Trace.error(Trace.FILE_IO_ERROR, sFileProperties);
	}
    }

    /**
     * Method declaration
     *
     *
     * @throws SQLException
     */
    private void saveProperties() throws SQLException {
	File f = new File(sFileProperties);

	closeProperties();

	if (Trace.TRACE) {
	    Trace.trace();
	}

	try {
	    FileOutputStream out = new FileOutputStream(f);

//#ifdef JAVA2
	    pProperties.store(out, "HSQL database");
//#else
/*
	    pProperties.save(out,"HSQL database");
*/
//#endif
	    out.close();

	    // after saving, open the file again
	    loadProperties();
	} catch (Exception e) {
	    throw Trace.error(Trace.FILE_IO_ERROR, sFileProperties);
	}
    }

    /**
     * Method declaration
     *
     *
     * @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 = 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();

	    time = System.currentTimeMillis() - time;

	    if (Trace.TRACE) {
		Trace.trace(time);
	    }
	} 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 = 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();

	    time = System.currentTimeMillis() - time;

	    if (Trace.TRACE) {
		Trace.trace(time);
	    }
	} 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) {
	    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) {
	    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 channel = new Vector();

	channel.addElement(cSystem);

	Channel current = cSystem;
	int     size = 1;

	try {
	    long	     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 >= size) {
			channel.setSize(id + 1);
		    }

		    current = (Channel) channel.elementAt(id);

		    if (current == null) {
			current = new Channel(cSystem, id);

			channel.setElementAt(current, id);
			dDatabase.registerChannel(current);
		    }

		    s = s.substring(s.indexOf('/', 1) + 1);
		}

		if (!s.equals("")) {
		    dDatabase.execute(s, current);
		}

		if (s.equals("DISCONNECT")) {
		    int id = current.getId();

		    current = new Channel(cSystem, id);

		    channel.setElementAt(current, id);
		}
	    }

	    r.close();

	    for (int i = 0; i < size; i++) {
		current = (Channel) channel.elementAt(i);

		if (current != null) {
		    current.rollback();
		}
	    }

	    time = System.currentTimeMillis() - time;

	    if (Trace.TRACE) {
		Trace.trace(time);
	    }
	} 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, cSystem);
    }

    /**
     * Method declaration
     *
     *
     * @param w
     * @param s
     *
     * @throws IOException
     */
    private static void writeLine(Writer w, String s) throws IOException {
	w.write(StringConverter.unicodeToAscii(s) + "\r\n");
    }

    /**
     * Method declaration
     *
     *
     * @param r
     *
     * @return
     *
     * @throws IOException
     */
    private static String readLine(LineNumberReader r) throws IOException {
	String s = r.readLine();

	return StringConverter.asciiToUnicode(s);
    }

}

⌨️ 快捷键说明

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