Search in sources :

Example 51 with Set

use of org.h2.command.dml.Set in project h2database by h2database.

the class TestIntPerfectHash method test.

private int test(byte[] desc, Set<Integer> set) {
    int max = -1;
    HashSet<Integer> test = new HashSet<>();
    IntPerfectHash hash = new IntPerfectHash(desc);
    for (int x : set) {
        int h = hash.get(x);
        assertTrue(h >= 0);
        assertTrue(h <= set.size() * 3);
        max = Math.max(max, h);
        assertFalse(test.contains(h));
        test.add(h);
    }
    return max;
}
Also used : IntPerfectHash(org.h2.dev.hash.IntPerfectHash) HashSet(java.util.HashSet)

Example 52 with Set

use of org.h2.command.dml.Set in project h2database by h2database.

the class TestNetUtils method testFrequentConnections.

private static void testFrequentConnections(boolean ssl, int count) throws Exception {
    final ServerSocket serverSocket = NetUtils.createServerSocket(PORT, ssl);
    final AtomicInteger counter = new AtomicInteger(count);
    Task serverThread = new Task() {

        @Override
        public void call() {
            while (!stop) {
                try {
                    Socket socket = serverSocket.accept();
                    // System.out.println("opened " + counter);
                    socket.close();
                } catch (Exception e) {
                // ignore
                }
            }
        // System.out.println("stopped ");
        }
    };
    serverThread.execute();
    try {
        Set<ConnectWorker> workers = new HashSet<>();
        for (int i = 0; i < WORKER_COUNT; i++) {
            workers.add(new ConnectWorker(ssl, counter));
        }
        // ensure the server is started
        Thread.sleep(100);
        for (ConnectWorker worker : workers) {
            worker.start();
        }
        for (ConnectWorker worker : workers) {
            worker.join();
            Exception e = worker.getException();
            if (e != null) {
                e.printStackTrace();
            }
        }
    } finally {
        try {
            serverSocket.close();
        } catch (Exception e) {
        // ignore
        }
        serverThread.get();
    }
}
Also used : Task(org.h2.util.Task) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServerSocket(java.net.ServerSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 53 with Set

use of org.h2.command.dml.Set in project h2database by h2database.

the class TestFileLockSerialized method testCheckpointInUpdateRaceCondition.

/**
 * If a checkpoint occurs between beforeWriting and checkWritingAllowed then
 * the result of checkWritingAllowed is READ_ONLY, which is wrong.
 *
 * Also, if a checkpoint started before beforeWriting, and ends between
 * between beforeWriting and checkWritingAllowed, then the same error
 * occurs.
 */
private void testCheckpointInUpdateRaceCondition() throws Exception {
    boolean longRun = false;
    deleteDb("fileLockSerialized");
    String url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized;FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE";
    Connection conn = getConnection(url);
    conn.createStatement().execute("create table test(id int)");
    conn.createStatement().execute("insert into test values(1)");
    for (int i = 0; i < (longRun ? 10000 : 5); i++) {
        Thread.sleep(402);
        conn.createStatement().execute("update test set id = " + i);
    }
    conn.close();
    deleteDb("fileLockSerialized");
}
Also used : Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection)

Example 54 with Set

use of org.h2.command.dml.Set in project h2database by h2database.

the class TestFileLockSerialized method testCache.

/**
 * Caches must be cleared. Session.reconnect only closes the DiskFile (which
 * is associated with the cache) if there is one session
 */
private void testCache() throws Exception {
    deleteDb("fileLockSerialized");
    String urlShared = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized;FILE_LOCK=SERIALIZED";
    Connection connShared1 = getConnection(urlShared);
    Statement statement1 = connShared1.createStatement();
    Connection connShared2 = getConnection(urlShared);
    Statement statement2 = connShared2.createStatement();
    statement1.execute("create table test1(id int)");
    statement1.execute("insert into test1 values(1)");
    ResultSet rs = statement1.executeQuery("select id from test1");
    rs.close();
    rs = statement2.executeQuery("select id from test1");
    rs.close();
    statement1.execute("update test1 set id=2");
    Thread.sleep(500);
    rs = statement2.executeQuery("select id from test1");
    assertTrue(rs.next());
    assertEquals(2, rs.getInt(1));
    rs.close();
    connShared1.close();
    connShared2.close();
    deleteDb("fileLockSerialized");
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet)

Example 55 with Set

use of org.h2.command.dml.Set in project h2database by h2database.

the class CreateScriptFile method openScriptReader.

/**
 * Open a script reader.
 *
 * @param fileName the file name (the file will be overwritten)
 * @param compressionAlgorithm the compression algorithm (uppercase)
 * @param cipher the encryption algorithm or null
 * @param password the encryption password
 * @param charset the character set (for example UTF-8)
 * @return the script reader
 */
public static LineNumberReader openScriptReader(String fileName, String compressionAlgorithm, String cipher, String password, String charset) throws IOException {
    try {
        InputStream in;
        if (cipher != null) {
            byte[] key = SHA256.getKeyPasswordHash("script", password.toCharArray());
            FileStore store = FileStore.open(null, fileName, "rw", cipher, key);
            store.init();
            in = new FileStoreInputStream(store, null, compressionAlgorithm != null, false);
            in = new BufferedInputStream(in, Constants.IO_BUFFER_SIZE_COMPRESS);
        } else {
            in = FileUtils.newInputStream(fileName);
            in = new BufferedInputStream(in, Constants.IO_BUFFER_SIZE);
            in = CompressTool.wrapInputStream(in, compressionAlgorithm, "script.sql");
            if (in == null) {
                throw new IOException("Entry not found: script.sql in " + fileName);
            }
        }
        return new LineNumberReader(new InputStreamReader(in, charset));
    } catch (Exception e) {
        throw new IOException(e.getMessage(), e);
    }
}
Also used : FileStore(org.h2.store.FileStore) InputStreamReader(java.io.InputStreamReader) FileStoreInputStream(org.h2.store.FileStoreInputStream) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileStoreInputStream(org.h2.store.FileStoreInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) IOException(java.io.IOException) LineNumberReader(java.io.LineNumberReader)

Aggregations

SQLException (java.sql.SQLException)107 DbException (org.h2.message.DbException)80 PreparedStatement (java.sql.PreparedStatement)78 Connection (java.sql.Connection)70 Statement (java.sql.Statement)63 ResultSet (java.sql.ResultSet)62 Value (org.h2.value.Value)51 JdbcConnection (org.h2.jdbc.JdbcConnection)48 SimpleResultSet (org.h2.tools.SimpleResultSet)36 HashSet (java.util.HashSet)28 Column (org.h2.table.Column)24 Savepoint (java.sql.Savepoint)23 ValueString (org.h2.value.ValueString)21 IOException (java.io.IOException)20 Random (java.util.Random)17 Expression (org.h2.expression.Expression)16 Task (org.h2.util.Task)16 ExpressionColumn (org.h2.expression.ExpressionColumn)14 ValueExpression (org.h2.expression.ValueExpression)13 IndexColumn (org.h2.table.IndexColumn)13