Search in sources :

Example 6 with HDBClient

use of herddb.client.HDBClient in project herddb by diennea.

the class BaseBench method startServer.

@Before
public void startServer() throws Exception {
    makeServerConfiguration();
    threadpool = Executors.newFixedThreadPool(numThreads);
    server = new Server(serverConfiguration);
    server.start();
    server.waitForStandaloneBoot();
    client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()));
    client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
    dataSource = new BasicHerdDBDataSource(client);
}
Also used : StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) Server(herddb.server.Server) BasicHerdDBDataSource(herddb.jdbc.BasicHerdDBDataSource) ClientConfiguration(herddb.client.ClientConfiguration) Before(org.junit.Before)

Example 7 with HDBClient

use of herddb.client.HDBClient in project herddb by diennea.

the class SimpleServerTest method test.

@Test
public void test() throws Exception {
    try {
        File tmpConfFile = folder.newFile("test.server.properties");
        try (InputStream in = SimpleServerTest.class.getResourceAsStream("/conf/test.server.properties")) {
            Properties props = new Properties();
            props.load(in);
            props.put(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().getAbsolutePath());
            try (FileOutputStream oo = new FileOutputStream(tmpConfFile)) {
                props.store(oo, "");
            }
        }
        Thread runner = new Thread(() -> {
            ServerMain.main(tmpConfFile.getAbsolutePath());
        });
        runner.start();
        while (ServerMain.getRunningInstance() == null || !ServerMain.getRunningInstance().isStarted()) {
            Thread.sleep(1000);
            System.out.println("waiting for boot");
        }
        ServerMain.getRunningInstance().getServer().waitForStandaloneBoot();
        try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(ServerMain.getRunningInstance().getServer()));
            try (HDBConnection con = client.openConnection()) {
                try (ScanResultSet scan = con.executeScan(TableSpace.DEFAULT, "SELECT * FROM SYSTABLES", false, Collections.emptyList(), 0, 10, 10, false)) {
                    scan.consume();
                }
            }
        }
        URL url = new URL(ServerMain.getRunningInstance().getUiurl());
        try {
            url.getContent();
            fail();
        } catch (FileNotFoundException ok) {
        // OK for "file not found", we want just to check that jetty is up
        }
        ServerMain.getRunningInstance().close();
    } finally {
        if (ServerMain.getRunningInstance() != null) {
            ServerMain.getRunningInstance().close();
        }
    }
}
Also used : HDBConnection(herddb.client.HDBConnection) HDBClient(herddb.client.HDBClient) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) ScanResultSet(herddb.client.ScanResultSet) Properties(java.util.Properties) File(java.io.File) ClientConfiguration(herddb.client.ClientConfiguration) URL(java.net.URL) Test(org.junit.Test)

Example 8 with HDBClient

use of herddb.client.HDBClient in project herddb by diennea.

the class MultipleDataSourcesSameJVMJMXTest method test.

@Test
public void test() throws Exception {
    try (Server server = new Server(TestUtils.newServerConfigurationWithAutoPort(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);
                BasicHerdDBDataSource dataSource_2 = new BasicHerdDBDataSource(client);
                Connection con = dataSource.getConnection();
                Connection con_2 = dataSource_2.getConnection();
                Statement statement = con.createStatement();
                Statement statement_2 = 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')"));
                assertEquals(1, statement_2.executeUpdate("INSERT INTO mytable (key,name) values('k2','name2')"));
                assertEquals(1, statement.executeUpdate("INSERT INTO mytable (key,name) values('k3','name3')"));
                try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable a" + " INNER JOIN mytable b ON 1=1")) {
                    int count = 0;
                    while (rs.next()) {
                        count++;
                    }
                    assertEquals(9, count);
                }
                MBeanServer jmxServer = ManagementFactory.getPlatformMBeanServer();
                Object attributeValue = jmxServer.getAttribute(new ObjectName("org.apache.commons.pool2:type=GenericObjectPool,name=HerdDBClient"), "BorrowedCount");
                assertEquals(1L, attributeValue);
                Object attributeValue2 = jmxServer.getAttribute(new ObjectName("org.apache.commons.pool2:type=GenericObjectPool,name=HerdDBClient2"), "BorrowedCount");
                assertEquals(1L, attributeValue2);
            }
        }
    }
}
Also used : StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) Server(herddb.server.Server) MBeanServer(javax.management.MBeanServer) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ClientConfiguration(herddb.client.ClientConfiguration) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 9 with HDBClient

use of herddb.client.HDBClient in project herddb by diennea.

the class ScanHugeTableTest method testBatch.

@Test
public void testBatch() throws Exception {
    try (Server server = new Server(TestUtils.newServerConfigurationWithAutoPort(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 = 10_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 % 6000 == 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 % 1000 == 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) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 10 with HDBClient

use of herddb.client.HDBClient in project herddb by diennea.

the class SimpleDataSourceTest method test.

@Test
public void test() throws Exception {
    try (Server server = new Server(TestUtils.newServerConfigurationWithAutoPort(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) 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)

Aggregations

HDBClient (herddb.client.HDBClient)84 ClientConfiguration (herddb.client.ClientConfiguration)83 Test (org.junit.Test)75 Server (herddb.server.Server)52 HDBConnection (herddb.client.HDBConnection)46 StaticClientSideMetadataProvider (herddb.server.StaticClientSideMetadataProvider)41 Connection (java.sql.Connection)38 Statement (java.sql.Statement)34 PreparedStatement (java.sql.PreparedStatement)30 ResultSet (java.sql.ResultSet)29 Table (herddb.model.Table)20 CreateTableStatement (herddb.model.commands.CreateTableStatement)20 InsertStatement (herddb.model.commands.InsertStatement)19 ServerConfiguration (herddb.server.ServerConfiguration)14 ScanResultSet (herddb.client.ScanResultSet)13 ByteArrayInputStream (java.io.ByteArrayInputStream)13 ProgressListener (herddb.backup.ProgressListener)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 HashSet (java.util.HashSet)12 Index (herddb.model.Index)9