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

📄 textcache.java

📁 hsql是很有名的嵌入式数据库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* Copyright (c) 2001-2005, The HSQL Development Group
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the HSQL Development Group nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


package org.hsqldb.persist;import java.io.IOException;import java.io.UnsupportedEncodingException;import org.hsqldb.HsqlException;import org.hsqldb.Table;import org.hsqldb.Trace;import org.hsqldb.lib.FileUtil;import org.hsqldb.lib.HsqlByteArrayOutputStream;import org.hsqldb.rowio.RowInputInterface;import org.hsqldb.rowio.RowInputText;import org.hsqldb.rowio.RowInputTextQuoted;import org.hsqldb.rowio.RowOutputText;import org.hsqldb.rowio.RowOutputTextQuoted;import org.hsqldb.scriptio.ScriptWriterText;import org.hsqldb.store.ObjectCacheHashMap;// Ito Kazumitsu 20030328 - patch 1.7.2 - character encoding support// Dimitri Maziuk - patch for NL in string support// sqlbob@users - updated for 1.8.0 to allow new-lines in fields// fredt@users - updated for 1.8.0 to allow correct behaviour with transactions/** * Acts as a buffer manager for a single TEXT table with respect its Row data.<p> * * Handles read/write operations on the table's text format data file using a * compatible pair of org.hsqldb.rowio input/output class instances. * * * @author sqlbob@users (RMP) * @version 1.8.0 * @since 1.7.0 */public class TextCache extends DataFileCache {    //state of Cache    public static final String NL = System.getProperty("line.separator");    public String              fs;    public String              vs;    public String              lvs;    public String              stringEncoding;    public boolean             isQuoted;    public boolean             isAllQuoted;    public boolean             ignoreFirst;    protected String           header;    protected Table            table;    private ObjectCacheHashMap uncommittedCache;    //    /**     *  The source string for a cached table is evaluated and the parameters     *  are used to open the source file.<p>     *     *  Settings are used in this order: (1) settings specified in the     *  source string for the table (2) global database settings in     *  *.properties file (3) program defaults     */    TextCache(Table table, String name) throws HsqlException {        super(table.database, name, null);        this.table       = table;        uncommittedCache = new ObjectCacheHashMap(5);    }    protected void initParams() throws HsqlException {        // fredt - this used to write rows as soon as they are inserted        // but now this is subject to session autoCommit / or commit        // storeOnInsert = true;        HsqlProperties tableprops =            HsqlProperties.delimitedArgPairsToProps(fileName, "=", ";", null);        //-- Get file name        switch (tableprops.errorCodes.length) {            case 0 :                throw Trace.error(Trace.TEXT_TABLE_SOURCE,                                  Trace.TEXT_TABLE_SOURCE_FILENAME);            case 1 :                // source file name is the only key without a value                fileName = tableprops.errorKeys[0].trim();                break;            default :                throw Trace.error(Trace.TEXT_TABLE_SOURCE,                                  Trace.TEXT_TABLE_SOURCE_VALUE_MISSING,                                  tableprops.errorKeys[1]);        }        //-- Get separators:        HsqlDatabaseProperties dbProps = database.getProperties();        fs = translateSep(tableprops.getProperty("fs",                dbProps.getProperty(HsqlDatabaseProperties.textdb_fs, ",")));        vs = translateSep(tableprops.getProperty("vs",                dbProps.getProperty(HsqlDatabaseProperties.textdb_vs, fs)));        lvs = translateSep(tableprops.getProperty("lvs",                dbProps.getProperty(HsqlDatabaseProperties.textdb_lvs, fs)));        if (fs.length() == 0 || vs.length() == 0 || lvs.length() == 0) {            throw Trace.error(Trace.TEXT_TABLE_SOURCE,                              Trace.TEXT_TABLE_SOURCE_SEPARATOR);        }        //-- Get booleans        ignoreFirst = tableprops.isPropertyTrue(            "ignore_first",            dbProps.isPropertyTrue(                HsqlDatabaseProperties.textdb_ignore_first, false));        isQuoted = tableprops.isPropertyTrue(            "quoted",            dbProps.isPropertyTrue(                HsqlDatabaseProperties.textdb_quoted, true));        isAllQuoted = tableprops.isPropertyTrue(            "all_quoted",            dbProps.isPropertyTrue(                HsqlDatabaseProperties.textdb_all_quoted, false));        //-- Get encoding        stringEncoding = translateSep(tableprops.getProperty("encoding",                dbProps.getProperty(HsqlDatabaseProperties.textdb_encoding,                                    "ASCII")));        //-- Get size and scale        int cacheScale = tableprops.getIntegerProperty(            "cache_scale",            dbProps.getIntegerProperty(                HsqlDatabaseProperties.textdb_cache_scale, 10, 8, 16));        int cacheSizeScale = tableprops.getIntegerProperty(            "cache_size_scale",            dbProps.getIntegerProperty(                HsqlDatabaseProperties.textdb_cache_size_scale, 10, 8, 20));        int lookupTableLength = 1 << cacheScale;        int avgRowBytes       = 1 << cacheSizeScale;        maxCacheSize     = lookupTableLength * 3;        maxCacheBytes    = maxCacheSize * avgRowBytes;        maxDataFileSize  = Integer.MAX_VALUE;        cachedRowPadding = 1;        cacheFileScale   = 1;    }    protected void initBuffers() {        if (isQuoted || isAllQuoted) {            rowIn = new RowInputTextQuoted(fs, vs, lvs, isAllQuoted);            rowOut = new RowOutputTextQuoted(fs, vs, lvs, isAllQuoted,                                             stringEncoding);        } else {            rowIn  = new RowInputText(fs, vs, lvs, false);            rowOut = new RowOutputText(fs, vs, lvs, false, stringEncoding);        }    }    private String translateSep(String sep) {        return translateSep(sep, false);    }    /**     * Translates the escaped characters in a separator string and returns     * the non-escaped string.     */    private String translateSep(String sep, boolean isProperty) {        if (sep == null) {            return (null);        }        int next = 0;        if ((next = sep.indexOf('\\')) != -1) {            int          start    = 0;            char[]       sepArray = sep.toCharArray();            char         ch       = 0;            int          len      = sep.length();            StringBuffer realSep  = new StringBuffer(len);            do {                realSep.append(sepArray, start, next - start);                start = ++next;                if (next >= len) {                    realSep.append('\\');                    break;                }                if (!isProperty) {                    ch = sepArray[next];                }                if (ch == 'n') {                    realSep.append('\n');                    start++;                } else if (ch == 'r') {                    realSep.append('\r');                    start++;                } else if (ch == 't') {                    realSep.append('\t');                    start++;                } else if (ch == '\\') {                    realSep.append('\\');                    start++;                } else if (ch == 'u') {                    start++;                    realSep.append(                        (char) Integer.parseInt(                            sep.substring(start, start + 4), 16));                    start += 4;                } else if (sep.startsWith("semi", next)) {                    realSep.append(';');                    start += 4;                } else if (sep.startsWith("space", next)) {                    realSep.append(' ');                    start += 5;                } else if (sep.startsWith("quote", next)) {                    realSep.append('\"');                    start += 5;                } else if (sep.startsWith("apos", next)) {                    realSep.append('\'');                    start += 4;                } else {                    realSep.append('\\');                    realSep.append(sepArray[next]);                    start++;                }            } while ((next = sep.indexOf('\\', start)) != -1);            realSep.append(sepArray, start, len - start);            sep = realSep.toString();        }        return sep;    }    /**     *  Opens a data source file.     */    public void open(boolean readonly) throws HsqlException {        fileFreePosition = 0;        try {            dataFile = ScaledRAFile.newScaledRAFile(fileName, readonly,                    ScaledRAFile.DATA_FILE_RAF, null, null);            fileFreePosition = dataFile.length();            if (fileFreePosition > Integer.MAX_VALUE) {                throw new IOException();            }            initBuffers();        } catch (Exception e) {            throw Trace.error(Trace.FILE_IO_ERROR,                              Trace.TextCache_openning_file_error,                              new Object[] {                fileName, e            });        }        cacheReadonly = readonly;    }    void reopen() throws HsqlException {        open(cacheReadonly);    }    /**     *  Writes newly created rows to disk. In the current implentation,     *  such rows have already been saved, so this method just removes a     *  source file that has no rows.     */    public void close(boolean write) throws HsqlException {        if (dataFile == null) {            return;        }        try {            cache.saveAll();            boolean empty = (dataFile.length() <= NL.length());            dataFile.close();            dataFile = null;            if (empty &&!cacheReadonly) {                FileUtil.delete(fileName);            }        } catch (Exception e) {            throw Trace.error(Trace.FILE_IO_ERROR,                              Trace.TextCache_closing_file_error,                              new Object[] {                fileName, e            });        }    }

⌨️ 快捷键说明

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