Search in sources :

Example 76 with In

use of org.h2.dev.util.BinaryArithmeticStream.In in project h2database by h2database.

the class FileLister method tryUnlockDatabase.

/**
 * Try to lock the database, and then unlock it. If this worked, the
 * .lock.db file will be removed.
 *
 * @param files the database files to check
 * @param message the text to include in the error message
 * @throws SQLException if it failed
 */
public static void tryUnlockDatabase(List<String> files, String message) throws SQLException {
    for (String fileName : files) {
        if (fileName.endsWith(Constants.SUFFIX_LOCK_FILE)) {
            FileLock lock = new FileLock(new TraceSystem(null), fileName, Constants.LOCK_SLEEP);
            try {
                lock.lock(FileLockMethod.FILE);
                lock.unlock();
            } catch (DbException e) {
                throw DbException.get(ErrorCode.CANNOT_CHANGE_SETTING_WHEN_OPEN_1, message).getSQLException();
            }
        } else if (fileName.endsWith(Constants.SUFFIX_MV_FILE)) {
            try (FileChannel f = FilePath.get(fileName).open("r")) {
                java.nio.channels.FileLock lock = f.tryLock(0, Long.MAX_VALUE, true);
                lock.release();
            } catch (Exception e) {
                throw DbException.get(ErrorCode.CANNOT_CHANGE_SETTING_WHEN_OPEN_1, e, message).getSQLException();
            }
        }
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) TraceSystem(org.h2.message.TraceSystem) SQLException(java.sql.SQLException) DbException(org.h2.message.DbException) DbException(org.h2.message.DbException)

Example 77 with In

use of org.h2.dev.util.BinaryArithmeticStream.In in project h2database by h2database.

the class Page method write.

/**
 * Store the page and update the position.
 *
 * @param chunk the chunk
 * @param buff the target buffer
 * @return the position of the buffer just after the type
 */
private int write(Chunk chunk, WriteBuffer buff) {
    int start = buff.position();
    int len = keys.length;
    int type = children != null ? DataUtils.PAGE_TYPE_NODE : DataUtils.PAGE_TYPE_LEAF;
    buff.putInt(0).putShort((byte) 0).putVarInt(map.getId()).putVarInt(len);
    int typePos = buff.position();
    buff.put((byte) type);
    if (type == DataUtils.PAGE_TYPE_NODE) {
        writeChildren(buff);
        for (int i = 0; i <= len; i++) {
            buff.putVarLong(children[i].count);
        }
    }
    int compressStart = buff.position();
    map.getKeyType().write(buff, keys, len, true);
    if (type == DataUtils.PAGE_TYPE_LEAF) {
        map.getValueType().write(buff, values, len, false);
    }
    MVStore store = map.getStore();
    int expLen = buff.position() - compressStart;
    if (expLen > 16) {
        int compressionLevel = store.getCompressionLevel();
        if (compressionLevel > 0) {
            Compressor compressor;
            int compressType;
            if (compressionLevel == 1) {
                compressor = map.getStore().getCompressorFast();
                compressType = DataUtils.PAGE_COMPRESSED;
            } else {
                compressor = map.getStore().getCompressorHigh();
                compressType = DataUtils.PAGE_COMPRESSED_HIGH;
            }
            byte[] exp = new byte[expLen];
            buff.position(compressStart).get(exp);
            byte[] comp = new byte[expLen * 2];
            int compLen = compressor.compress(exp, expLen, comp, 0);
            int plus = DataUtils.getVarIntLen(compLen - expLen);
            if (compLen + plus < expLen) {
                buff.position(typePos).put((byte) (type + compressType));
                buff.position(compressStart).putVarInt(expLen - compLen).put(comp, 0, compLen);
            }
        }
    }
    int pageLength = buff.position() - start;
    int chunkId = chunk.id;
    int check = DataUtils.getCheckValue(chunkId) ^ DataUtils.getCheckValue(start) ^ DataUtils.getCheckValue(pageLength);
    buff.putInt(start, pageLength).putShort(start + 4, (short) check);
    if (pos != 0) {
        throw DataUtils.newIllegalStateException(DataUtils.ERROR_INTERNAL, "Page already stored");
    }
    pos = DataUtils.getPagePos(chunkId, start, pageLength, type);
    store.cachePage(pos, this, getMemory());
    if (type == DataUtils.PAGE_TYPE_NODE) {
        // cache again - this will make sure nodes stays in the cache
        // for a longer time
        store.cachePage(pos, this, getMemory());
    }
    long max = DataUtils.getPageMaxLength(pos);
    chunk.maxLen += max;
    chunk.maxLenLive += max;
    chunk.pageCount++;
    chunk.pageCountLive++;
    if (removedInMemory) {
        // if the page was removed _before_ the position was assigned, we
        // need to mark it removed here, so the fields are updated
        // when the next chunk is stored
        map.removePage(pos, memory);
    }
    return typePos + 1;
}
Also used : Compressor(org.h2.compress.Compressor)

Example 78 with In

use of org.h2.dev.util.BinaryArithmeticStream.In in project h2database by h2database.

the class Page method setValue.

/**
 * Replace the value at an index in this page.
 *
 * @param index the index
 * @param value the new value
 * @return the old value
 */
public Object setValue(int index, Object value) {
    Object old = values[index];
    // this is slightly slower:
    // values = Arrays.copyOf(values, values.length);
    values = values.clone();
    DataType valueType = map.getValueType();
    if (isPersistent()) {
        addMemory(valueType.getMemory(value) - valueType.getMemory(old));
    }
    values[index] = value;
    return old;
}
Also used : DataType(org.h2.mvstore.type.DataType)

Example 79 with In

use of org.h2.dev.util.BinaryArithmeticStream.In in project h2database by h2database.

the class Page method setKey.

/**
 * Replace the key at an index in this page.
 *
 * @param index the index
 * @param key the new key
 */
public void setKey(int index, Object key) {
    // this is slightly slower:
    // keys = Arrays.copyOf(keys, keys.length);
    keys = keys.clone();
    if (isPersistent()) {
        Object old = keys[index];
        DataType keyType = map.getKeyType();
        int mem = keyType.getMemory(key);
        if (old != null) {
            mem -= keyType.getMemory(old);
        }
        addMemory(mem);
    }
    keys[index] = key;
}
Also used : DataType(org.h2.mvstore.type.DataType)

Example 80 with In

use of org.h2.dev.util.BinaryArithmeticStream.In in project h2database by h2database.

the class TcpServer method shutdown.

/**
 * Stop the TCP server with the given URL.
 *
 * @param url the database URL
 * @param password the password
 * @param force if the server should be stopped immediately
 * @param all whether all TCP servers that are running in the JVM should be
 *            stopped
 */
public static synchronized void shutdown(String url, String password, boolean force, boolean all) throws SQLException {
    try {
        int port = Constants.DEFAULT_TCP_PORT;
        int idx = url.lastIndexOf(':');
        if (idx >= 0) {
            String p = url.substring(idx + 1);
            if (StringUtils.isNumber(p)) {
                port = Integer.decode(p);
            }
        }
        String db = getManagementDbName(port);
        try {
            org.h2.Driver.load();
        } catch (Throwable e) {
            throw DbException.convert(e);
        }
        for (int i = 0; i < 2; i++) {
            Connection conn = null;
            PreparedStatement prep = null;
            try {
                conn = DriverManager.getConnection("jdbc:h2:" + url + "/" + db, "", password);
                prep = conn.prepareStatement("CALL STOP_SERVER(?, ?, ?)");
                prep.setInt(1, all ? 0 : port);
                prep.setString(2, password);
                prep.setInt(3, force ? SHUTDOWN_FORCE : SHUTDOWN_NORMAL);
                try {
                    prep.execute();
                } catch (SQLException e) {
                    if (force) {
                    // ignore
                    } else {
                        if (e.getErrorCode() != ErrorCode.CONNECTION_BROKEN_1) {
                            throw e;
                        }
                    }
                }
                break;
            } catch (SQLException e) {
                if (i == 1) {
                    throw e;
                }
            } finally {
                JdbcUtils.closeSilently(prep);
                JdbcUtils.closeSilently(conn);
            }
        }
    } catch (Exception e) {
        throw DbException.toSQLException(e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) DbException(org.h2.message.DbException) UnknownHostException(java.net.UnknownHostException) SQLException(java.sql.SQLException)

Aggregations

SQLException (java.sql.SQLException)63 Connection (java.sql.Connection)59 DbException (org.h2.message.DbException)56 PreparedStatement (java.sql.PreparedStatement)54 ResultSet (java.sql.ResultSet)47 Statement (java.sql.Statement)44 Value (org.h2.value.Value)40 IOException (java.io.IOException)39 ByteArrayInputStream (java.io.ByteArrayInputStream)30 InputStream (java.io.InputStream)29 Column (org.h2.table.Column)24 ArrayList (java.util.ArrayList)23 SimpleResultSet (org.h2.tools.SimpleResultSet)23 Random (java.util.Random)19 Expression (org.h2.expression.Expression)18 JdbcConnection (org.h2.jdbc.JdbcConnection)18 Index (org.h2.index.Index)16 ValueString (org.h2.value.ValueString)16 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)15