Search in sources :

Example 1 with StaticClientSideMetadataProvider

use of herddb.server.StaticClientSideMetadataProvider 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 2 with StaticClientSideMetadataProvider

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

the class AdvancedInsertSyntaxTest method testInsertFromSelect.

@Test
public void testInsertFromSelect() 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 create = con.createStatement();
                PreparedStatement statement = con.prepareStatement("INSERT INTO mytable (name) values(?)")) {
                create.execute("CREATE TABLE mytable (n1 int primary key auto_increment, name string)");
                create.execute("CREATE TABLE mytable2 (n1 int primary key auto_increment, name string)");
                {
                    for (int i = 0; i < 100; i++) {
                        statement.setString(1, "v" + i);
                        statement.addBatch();
                    }
                    int[] results = statement.executeBatch();
                    for (int i = 0; i < 100; i++) {
                        assertEquals(1, results[i]);
                    }
                    try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable ORDER BY n1")) {
                        int count = 0;
                        while (rs.next()) {
                            assertEquals("v" + count, rs.getString("name"));
                            assertEquals(count + 1, rs.getInt("n1"));
                            count++;
                        }
                        assertEquals(100, count);
                    }
                }
                statement.executeUpdate("INSERT INTO mytable2(n1,name) SELECT n1, name from mytable order by name desc");
                try (ResultSet rs = statement.executeQuery("SELECT COUNT(*) FROM mytable2")) {
                    assertTrue(rs.next());
                    assertEquals(100, rs.getInt(1));
                }
                // leverage auto_increment
                statement.executeUpdate("INSERT INTO mytable2(name) SELECT name from mytable order by name desc");
                try (ResultSet rs = statement.executeQuery("SELECT COUNT(*) FROM mytable2")) {
                    assertTrue(rs.next());
                    assertEquals(200, rs.getInt(1));
                }
                try (ResultSet rs = statement.executeQuery("SELECT n1 FROM mytable2 order by n1")) {
                    Set<Integer> ids = new HashSet<>();
                    while (rs.next()) {
                        int id = rs.getInt(1);
                        assertTrue(ids.add(id));
                    }
                    assertEquals(200, ids.size());
                }
            }
        }
    }
}
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 3 with StaticClientSideMetadataProvider

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

the class CreateTableSpaceWithStaticMetadataProviderTest method test.

@Test
public void test() throws Exception {
    try (HerdDBEmbeddedDataSource dataSource = new HerdDBEmbeddedDataSource()) {
        dataSource.getProperties().setProperty(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().getAbsolutePath());
        dataSource.getProperties().setProperty(ClientConfiguration.PROPERTY_BASEDIR, folder.newFolder().getAbsolutePath());
        dataSource.setMaxActive(20);
        try (Connection con = dataSource.getConnection()) {
        }
        assertTrue(dataSource.getClient().getClientSideMetadataProvider() instanceof StaticClientSideMetadataProvider);
        try (Connection con = dataSource.getConnection();
            Statement statement = con.createStatement()) {
            String leader = dataSource.getClient().getClientSideMetadataProvider().getTableSpaceLeader(TableSpace.DEFAULT);
            statement.execute("CREATE TABLESPACE 'mytablespace','wait:5000','leader:" + leader + "'");
            statement.execute("CREATE TABLE mytablespace.mytable(pk int primary key)");
        }
    }
}
Also used : StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) Statement(java.sql.Statement) Connection(java.sql.Connection) Test(org.junit.Test)

Example 4 with StaticClientSideMetadataProvider

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

the class CreateTableSpaceWithStaticMetadataProviderTest method testStandAloneServer.

@Test
public void testStandAloneServer() throws Exception {
    try (HerdDBEmbeddedDataSource dataSource = new HerdDBEmbeddedDataSource()) {
        dataSource.getProperties().setProperty(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().getAbsolutePath());
        dataSource.getProperties().setProperty(ClientConfiguration.PROPERTY_BASEDIR, folder.newFolder().getAbsolutePath());
        dataSource.getProperties().setProperty(ClientConfiguration.PROPERTY_MODE, ClientConfiguration.PROPERTY_MODE_STANDALONE);
        dataSource.getProperties().setProperty(ServerConfiguration.PROPERTY_MODE, ServerConfiguration.PROPERTY_MODE_STANDALONE);
        dataSource.setStartServer(true);
        dataSource.setMaxActive(20);
        try (Connection con = dataSource.getConnection()) {
        }
        assertTrue(dataSource.getClient().getClientSideMetadataProvider() instanceof StaticClientSideMetadataProvider);
        try (Connection con = dataSource.getConnection();
            Statement statement = con.createStatement()) {
            String leader = dataSource.getClient().getClientSideMetadataProvider().getTableSpaceLeader(TableSpace.DEFAULT);
            System.out.println("leader:" + leader);
            statement.execute("CREATE TABLESPACE 'mytablespace','wait:5000','leader:" + leader + "'");
            statement.execute("CREATE TABLE mytablespace.mytable(pk int primary key)");
        }
    }
}
Also used : StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) Statement(java.sql.Statement) Connection(java.sql.Connection) Test(org.junit.Test)

Example 5 with StaticClientSideMetadataProvider

use of herddb.server.StaticClientSideMetadataProvider 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)

Aggregations

StaticClientSideMetadataProvider (herddb.server.StaticClientSideMetadataProvider)30 Server (herddb.server.Server)27 Connection (java.sql.Connection)27 Test (org.junit.Test)27 ClientConfiguration (herddb.client.ClientConfiguration)26 HDBClient (herddb.client.HDBClient)26 ServerConfiguration (herddb.server.ServerConfiguration)26 Statement (java.sql.Statement)26 PreparedStatement (java.sql.PreparedStatement)22 ResultSet (java.sql.ResultSet)19 SQLException (java.sql.SQLException)4 HashSet (java.util.HashSet)3 HDBException (herddb.client.HDBException)1 BasicHerdDBDataSource (herddb.jdbc.BasicHerdDBDataSource)1 TableSpace (herddb.model.TableSpace)1 SQLPlanner (herddb.sql.SQLPlanner)1 DefaultEventLoopGroup (io.netty.channel.DefaultEventLoopGroup)1 EpollEventLoopGroup (io.netty.channel.epoll.EpollEventLoopGroup)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 DatabaseMetaData (java.sql.DatabaseMetaData)1