Search in sources :

Example 6 with Server

use of herddb.server.Server in project herddb by diennea.

the class ConnectionPoolMaxActiveTest method test.

@Test
public void test() throws Exception {
    try (HerdDBEmbeddedDataSource dataSource = new HerdDBEmbeddedDataSource()) {
        dataSource.setMaxActive(20);
        dataSource.getProperties().setProperty(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().getAbsolutePath());
        dataSource.getProperties().setProperty(ClientConfiguration.PROPERTY_BASEDIR, folder.newFolder().getAbsolutePath());
        try (Connection con = dataSource.getConnection();
            Statement statement = con.createStatement()) {
            statement.execute("CREATE TABLE mytable (key string primary key, name string)");
        }
        Server server = dataSource.getServer();
        List<Connection> connections = new ArrayList<>();
        try {
            for (int i = 0; i < 10; i++) {
                Connection con = dataSource.getConnection();
                connections.add(con);
                try (Statement statement = con.createStatement()) {
                    assertEquals(1, statement.executeUpdate("INSERT INTO mytable (key,name) values('k1" + i + "','name1')"));
                }
            }
            assertEquals(10, server.getConnectionCount());
        } finally {
            for (Connection c : connections) {
                c.close();
            }
        }
    }
}
Also used : Server(herddb.server.Server) Statement(java.sql.Statement) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 7 with Server

use of herddb.server.Server in project herddb by diennea.

the class GeneratedKeysTest method testPreparedStatementInt.

@Test
public void testPreparedStatementInt() throws Exception {
    try (Server server = new Server(new ServerConfiguration(folder.newFolder().toPath()))) {
        server.start();
        server.waitForStandaloneBoot();
        try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
            try (BasicHerdDBDataSource dataSource = new BasicHerdDBDataSource(client);
                Connection con = dataSource.getConnection();
                Statement statement = con.createStatement()) {
                statement.execute("CREATE TABLE mytable (n1 int primary key auto_increment, name string)");
                try (PreparedStatement prepared = con.prepareStatement("INSERT INTO mytable (name) values('name1')", Statement.RETURN_GENERATED_KEYS)) {
                    assertEquals(1, prepared.executeUpdate());
                    Object key = null;
                    try (ResultSet generatedKeys = prepared.getGeneratedKeys()) {
                        if (generatedKeys.next()) {
                            key = generatedKeys.getObject(1);
                        }
                    }
                    assertNotNull(key);
                    assertEquals(Integer.valueOf(1), key);
                }
                try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable")) {
                    int count = 0;
                    while (rs.next()) {
                        count++;
                    }
                    assertEquals(1, count);
                }
            }
        }
    }
}
Also used : StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) Server(herddb.server.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ServerConfiguration(herddb.server.ServerConfiguration) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 8 with Server

use of herddb.server.Server in project herddb by diennea.

the class ScanHugeTableTest method testBatch.

@Test
public void testBatch() throws Exception {
    try (Server server = new Server(new ServerConfiguration(folder.newFolder().toPath()))) {
        server.getManager().setMaxDataUsedMemory(750 * 1024 * 1024);
        server.start();
        server.waitForStandaloneBoot();
        try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
            try (BasicHerdDBDataSource dataSource = new BasicHerdDBDataSource(client);
                Connection con = dataSource.getConnection();
                Statement s = con.createStatement();
                PreparedStatement ps = con.prepareStatement("INSERT INTO mytable (n1, name) values(?,?)")) {
                s.execute("CREATE TABLE mytable (n1 int primary key, name string)");
                String bigPrefix = StringUtils.repeat("Test", 300);
                // int size = 1_000_000;
                int size = 100_000;
                {
                    long _start = System.currentTimeMillis();
                    con.setAutoCommit(false);
                    for (int i = 0; i < size; i++) {
                        ps.setInt(1, i);
                        ps.setString(2, bigPrefix + i);
                        ps.addBatch();
                        if (i % 60000 == 0) {
                            ps.executeBatch();
                            con.commit();
                            long _stop = System.currentTimeMillis();
                            System.out.println("written " + i + " records_ " + (_stop - _start) + " ms");
                        }
                    }
                    ps.executeBatch();
                    con.commit();
                    long _stop = System.currentTimeMillis();
                    System.out.println("insert: " + (_stop - _start) + " ms");
                }
                server.getManager().checkpoint();
                con.setAutoCommit(true);
                {
                    long _start = System.currentTimeMillis();
                    try (ResultSet rs = s.executeQuery("SELECT COUNT(*) from mytable")) {
                        assertTrue(rs.next());
                        long res = rs.getLong(1);
                        assertEquals(size, res);
                    }
                    long _stop = System.currentTimeMillis();
                    System.out.println("count: " + (_stop - _start) + " ms");
                }
                {
                    long _start = System.currentTimeMillis();
                    try (ResultSet rs = s.executeQuery("SELECT * from mytable")) {
                        int i = 0;
                        while (rs.next()) {
                            if (i % 100000 == 0) {
                                System.out.println("read " + i + " records");
                            }
                            i++;
                        }
                        assertEquals(size, i);
                    }
                    long _stop = System.currentTimeMillis();
                    System.out.println("read: " + (_stop - _start) + " ms");
                }
            }
        }
    }
}
Also used : StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) Server(herddb.server.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ServerConfiguration(herddb.server.ServerConfiguration) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 9 with Server

use of herddb.server.Server in project herddb by diennea.

the class SimpleDataSourceTest method test.

@Test
public void test() throws Exception {
    try (Server server = new Server(new ServerConfiguration(folder.newFolder().toPath()))) {
        server.start();
        server.waitForStandaloneBoot();
        try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
            try (BasicHerdDBDataSource dataSource = new BasicHerdDBDataSource(client);
                Connection con = dataSource.getConnection();
                Statement statement = con.createStatement()) {
                statement.execute("CREATE TABLE mytable (key string primary key, name string)");
                assertEquals(1, statement.executeUpdate("INSERT INTO mytable (key,NamE) values('k1','name1')"));
                try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable")) {
                    boolean found = false;
                    while (rs.next()) {
                        String key = rs.getString("key");
                        String name = rs.getString("name");
                        assertEquals("k1", key);
                        assertEquals("name1", name);
                        found = true;
                    }
                    assertTrue(found);
                }
                assertEquals(1, statement.executeUpdate("UPDATE MYTaBLE set name='name2' where key='k1'"));
                try (ResultSet rs = statement.executeQuery("SELECT KeY,nAme FROM mytable")) {
                    boolean found = false;
                    while (rs.next()) {
                        String key = rs.getString("key");
                        String name = rs.getString("naMe");
                        assertEquals("k1", key);
                        assertEquals("name2", name);
                        found = true;
                    }
                    assertTrue(found);
                }
                try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable")) {
                    boolean found = false;
                    while (rs.next()) {
                        String key = rs.getString("key");
                        String name = rs.getString("name");
                        assertEquals("k1", key);
                        assertEquals("name2", name);
                        found = true;
                    }
                    assertTrue(found);
                }
                assertEquals(1, statement.executeUpdate("DELETE FROM mytable where name='name2' and key='k1'"));
                assertEquals(0, statement.executeUpdate("UPDATE mytable set name='name2' where key='k1'"));
                try (PreparedStatement ps = con.prepareStatement("INSERT INTO mytable (key,name) values(?,?)")) {
                    for (int i = 0; i < 10; i++) {
                        ps.setString(1, "kp_" + i);
                        ps.setString(2, "n2_" + i);
                        assertEquals(1, ps.executeUpdate());
                    }
                }
                try (PreparedStatement ps = con.prepareStatement("SELECT * FROM mytable");
                    ResultSet rs = ps.executeQuery()) {
                    Set<String> foundKeys = new HashSet<>();
                    while (rs.next()) {
                        foundKeys.add(rs.getString("key"));
                    }
                    for (int i = 0; i < 10; i++) {
                        assertTrue(foundKeys.contains("kp_" + i));
                    }
                }
                try (PreparedStatement ps = con.prepareStatement("SELECT COUNT(*) as total FROM mytable");
                    ResultSet rs = ps.executeQuery()) {
                    assertTrue(rs.next());
                    assertEquals(10, rs.getLong("total"));
                }
                try (PreparedStatement ps = con.prepareStatement("SELECT COUNT(*) as cc FROM mytable");
                    ResultSet rs = ps.executeQuery()) {
                    assertTrue(rs.next());
                    assertEquals(10, rs.getLong("cc"));
                    assertEquals(10, rs.getLong(1));
                    assertEquals(Long.valueOf(10), rs.getObject(1));
                    assertEquals(Long.valueOf(10), rs.getObject("cc"));
                }
            }
        }
    }
}
Also used : Server(herddb.server.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ServerConfiguration(herddb.server.ServerConfiguration) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) ResultSet(java.sql.ResultSet) ClientConfiguration(herddb.client.ClientConfiguration) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with Server

use of herddb.server.Server in project herddb by diennea.

the class SimpleExpressionsTest method test.

@Test
public void test() throws Exception {
    try (Server server = new Server(new ServerConfiguration(folder.newFolder().toPath()))) {
        server.start();
        server.waitForStandaloneBoot();
        try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
            try (BasicHerdDBDataSource dataSource = new BasicHerdDBDataSource(client)) {
                try (Connection con = dataSource.getConnection();
                    Statement statement = con.createStatement()) {
                    statement.execute("CREATE TABLE mytable (k1 string primary key, n1 int, l1 long, t1 timestamp, nu string, b1 bool, d1 double)");
                }
                try (Connection con = dataSource.getConnection();
                    PreparedStatement statement = con.prepareStatement("INSERT INTO mytable(k1,n1,l1,t1,nu,b1,d1) values(?,?,?,?,?,?,?)")) {
                    int i = 1;
                    statement.setString(i++, "mykey");
                    statement.setInt(i++, 1);
                    statement.setLong(i++, 2);
                    statement.setTimestamp(i++, new java.sql.Timestamp(System.currentTimeMillis()));
                    statement.setString(i++, null);
                    statement.setBoolean(i++, true);
                    statement.setDouble(i++, 1.5);
                    int rows = statement.executeUpdate();
                    Assert.assertEquals(1, rows);
                }
                // Types
                try (Connection con = dataSource.getConnection()) {
                    // String
                    try (PreparedStatement statement = con.prepareStatement("SELECT k1 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals("mykey", rs.getObject(1));
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT upper(k1) FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals("MYKEY", rs.getObject(1));
                        }
                    }
                    // Number
                    try (PreparedStatement statement = con.prepareStatement("SELECT n1 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(1, rs.getObject(1));
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT n1*5 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(5.0, rs.getObject(1));
                            assertEquals(5, rs.getLong(1));
                        }
                    }
                    // Long
                    try (PreparedStatement statement = con.prepareStatement("SELECT l1 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(2L, rs.getObject(1));
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT l1*5 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(10.0, rs.getObject(1));
                            assertEquals(10, rs.getLong(1));
                        }
                    }
                    // Null
                    try (PreparedStatement statement = con.prepareStatement("SELECT nu FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(null, rs.getObject(1));
                        }
                    }
                    // Double
                    try (PreparedStatement statement = con.prepareStatement("SELECT 3/2 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(1.5, rs.getObject(1));
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT d1 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(1.5, rs.getObject(1));
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT d1*5/2 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(3.75, rs.getObject(1));
                            assertEquals(3.75, rs.getDouble(1), 0);
                            assertEquals(3, rs.getLong(1));
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT -n1 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(-1, rs.getObject(1));
                            assertEquals(-1, rs.getDouble(1), 0);
                            assertEquals(-1, rs.getInt(1), 0);
                            assertEquals(-1, rs.getLong(1), 0);
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT -n1-n1 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(-2L, rs.getObject(1));
                            assertEquals(-2, rs.getDouble(1), 0);
                            assertEquals(-2, rs.getInt(1), 0);
                            assertEquals(-2, rs.getLong(1), 0);
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT +n1+n1 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(2L, rs.getObject(1));
                            assertEquals(2, rs.getDouble(1), 0);
                            assertEquals(2, rs.getInt(1), 0);
                            assertEquals(2, rs.getLong(1), 0);
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT n1*n1 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(1L, rs.getObject(1));
                            assertEquals(1, rs.getDouble(1), 0);
                            assertEquals(1, rs.getInt(1), 0);
                            assertEquals(1, rs.getLong(1), 0);
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT -d1 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(-1.5, rs.getObject(1));
                            assertEquals(-1.5, rs.getDouble(1), 0);
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT +d1 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(1.5, rs.getObject(1));
                            assertEquals(1.5, rs.getDouble(1), 0);
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT +d1+d1 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(3, rs.getDouble(1), 0);
                            assertEquals(3.0, rs.getObject(1));
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT +d1*d1 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(2.25, rs.getDouble(1), 0);
                            assertEquals(2.25, rs.getObject(1));
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT -d1+d1 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(0, rs.getDouble(1), 0);
                            assertEquals(0.0, rs.getObject(1));
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT +d1+4 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(5.5, rs.getDouble(1), 0);
                            assertEquals(5.5, rs.getObject(1));
                        }
                    }
                    // Boolean
                    try (PreparedStatement statement = con.prepareStatement("SELECT true FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(true, rs.getObject(1));
                            assertEquals(true, rs.getBoolean(1));
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT false FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(false, rs.getObject(1));
                            assertEquals(false, rs.getBoolean(1));
                        }
                    }
                }
            }
        }
    }
}
Also used : Server(herddb.server.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ServerConfiguration(herddb.server.ServerConfiguration) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) ResultSet(java.sql.ResultSet) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Aggregations

Server (herddb.server.Server)36 Connection (java.sql.Connection)33 Test (org.junit.Test)33 ServerConfiguration (herddb.server.ServerConfiguration)32 Statement (java.sql.Statement)32 StaticClientSideMetadataProvider (herddb.server.StaticClientSideMetadataProvider)27 ClientConfiguration (herddb.client.ClientConfiguration)26 HDBClient (herddb.client.HDBClient)26 ResultSet (java.sql.ResultSet)25 PreparedStatement (java.sql.PreparedStatement)22 SQLException (java.sql.SQLException)6 HashSet (java.util.HashSet)3 ArrayList (java.util.ArrayList)2 HDBException (herddb.client.HDBException)1 BasicHerdDBDataSource (herddb.jdbc.BasicHerdDBDataSource)1 TableSpace (herddb.model.TableSpace)1 SQLPlanner (herddb.sql.SQLPlanner)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 List (java.util.List)1 Set (java.util.Set)1