Search in sources :

Example 61 with Server

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

the class MultiDMLOnSameRecordTest method testWithIndexSeek.

@Test
public void testWithIndexSeek() throws Exception {
    Path baseDir = folder.newFolder().toPath();
    ServerConfiguration serverConfiguration = newServerConfigurationWithAutoPort(baseDir);
    serverConfiguration.set(ServerConfiguration.PROPERTY_MAX_LOGICAL_PAGE_SIZE, 10 * 1024);
    serverConfiguration.set(ServerConfiguration.PROPERTY_MAX_DATA_MEMORY, 1024 * 1024 / 4);
    serverConfiguration.set(ServerConfiguration.PROPERTY_MAX_PK_MEMORY, 1024 * 1024);
    serverConfiguration.set(ServerConfiguration.PROPERTY_CHECKPOINT_PERIOD, 0);
    serverConfiguration.set(ServerConfiguration.PROPERTY_DATADIR, folder.newFolder().getAbsolutePath());
    serverConfiguration.set(ServerConfiguration.PROPERTY_LOGDIR, folder.newFolder().getAbsolutePath());
    try (Server server = new Server(serverConfiguration)) {
        server.start();
        server.waitForStandaloneBoot();
        DBManager manager = server.getManager();
        execute(manager, "CREATE TABLE mytable (id string primary key, n1 long, k2 string)", Collections.emptyList());
        execute(manager, "CREATE INDEX tt ON mytable (k2)", Collections.emptyList());
        ExecutorService threadPool = Executors.newFixedThreadPool(THREADPOLSIZE);
        try {
            List<Future> futures = new ArrayList<>();
            AtomicLong updates = new AtomicLong();
            AtomicLong deletes = new AtomicLong();
            AtomicLong inserts = new AtomicLong();
            AtomicLong duplicatePkErrors = new AtomicLong();
            for (int i = 0; i < TESTSIZE; i++) {
                futures.add(threadPool.submit(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            int k = ThreadLocalRandom.current().nextInt(10);
                            long value = ThreadLocalRandom.current().nextInt(100) + 1000;
                            String key = "test_" + k;
                            boolean update = ThreadLocalRandom.current().nextBoolean();
                            boolean insert = ThreadLocalRandom.current().nextBoolean();
                            boolean delete = ThreadLocalRandom.current().nextBoolean();
                            if (update) {
                                updates.incrementAndGet();
                                execute(manager, "UPDATE mytable set n1=? WHERE k2=?", Arrays.asList(value, key));
                            } else if (insert) {
                                inserts.incrementAndGet();
                                try {
                                    execute(manager, "INSERT INTO mytable(n1, id, k2) values(?,?,?)", Arrays.asList(value, key, key));
                                } catch (DuplicatePrimaryKeyException ok) {
                                    duplicatePkErrors.incrementAndGet();
                                }
                            } else if (delete) {
                                deletes.incrementAndGet();
                                execute(manager, "DELETE FROM mytable WHERE k2=?", Arrays.asList(key));
                            }
                        } catch (Exception err) {
                            throw new RuntimeException(err);
                        }
                    }
                }));
            }
            for (Future f : futures) {
                f.get();
            }
            System.out.println("stats::updates:" + updates);
            System.out.println("stats::deletes:" + deletes);
            System.out.println("stats::inserts:" + inserts);
            System.out.println("stats::duplicatePkErrors:" + duplicatePkErrors);
            TableManagerStats stats = server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getTableManager("mytable").getStats();
            System.out.println("stats::tablesize:" + stats.getTablesize());
            System.out.println("stats::dirty records:" + stats.getDirtyrecords());
            System.out.println("stats::unload count:" + stats.getUnloadedPagesCount());
            System.out.println("stats::load count:" + stats.getLoadedPagesCount());
            System.out.println("stats::buffers used mem:" + stats.getBuffersUsedMemory());
        } finally {
            threadPool.shutdown();
            threadPool.awaitTermination(1, TimeUnit.MINUTES);
        }
    }
    // restart and recovery
    try (Server server = new Server(serverConfiguration)) {
        server.start();
        server.waitForTableSpaceBoot(TableSpace.DEFAULT, 300000, true);
    }
}
Also used : Path(java.nio.file.Path) Server(herddb.server.Server) ServerConfiguration(herddb.server.ServerConfiguration) ArrayList(java.util.ArrayList) DuplicatePrimaryKeyException(herddb.model.DuplicatePrimaryKeyException) AtomicLong(java.util.concurrent.atomic.AtomicLong) DBManager(herddb.core.DBManager) TableManagerStats(herddb.core.stats.TableManagerStats) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) DuplicatePrimaryKeyException(herddb.model.DuplicatePrimaryKeyException) Test(org.junit.Test)

Example 62 with Server

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

the class MultiDMLOnSameRecordTest method testWithPrimaryKeyIndexSeek.

@Test
public void testWithPrimaryKeyIndexSeek() throws Exception {
    Path baseDir = folder.newFolder().toPath();
    ServerConfiguration serverConfiguration = newServerConfigurationWithAutoPort(baseDir);
    serverConfiguration.set(ServerConfiguration.PROPERTY_MAX_LOGICAL_PAGE_SIZE, 10 * 1024);
    serverConfiguration.set(ServerConfiguration.PROPERTY_MAX_DATA_MEMORY, 1024 * 1024 / 4);
    serverConfiguration.set(ServerConfiguration.PROPERTY_MAX_PK_MEMORY, 1024 * 1024);
    serverConfiguration.set(ServerConfiguration.PROPERTY_CHECKPOINT_PERIOD, 0);
    serverConfiguration.set(ServerConfiguration.PROPERTY_DATADIR, folder.newFolder().getAbsolutePath());
    serverConfiguration.set(ServerConfiguration.PROPERTY_LOGDIR, folder.newFolder().getAbsolutePath());
    try (Server server = new Server(serverConfiguration)) {
        server.start();
        server.waitForStandaloneBoot();
        DBManager manager = server.getManager();
        execute(manager, "CREATE TABLE mytable (id string primary key, n1 long, n2 integer)", Collections.emptyList());
        ExecutorService threadPool = Executors.newFixedThreadPool(THREADPOLSIZE);
        try {
            List<Future> futures = new ArrayList<>();
            AtomicLong updates = new AtomicLong();
            AtomicLong deletes = new AtomicLong();
            AtomicLong inserts = new AtomicLong();
            AtomicLong duplicatePkErrors = new AtomicLong();
            for (int i = 0; i < TESTSIZE; i++) {
                futures.add(threadPool.submit(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            int k = ThreadLocalRandom.current().nextInt(10);
                            long value = ThreadLocalRandom.current().nextInt(100) + 1000;
                            String key = "test_" + k;
                            boolean update = ThreadLocalRandom.current().nextBoolean();
                            boolean insert = ThreadLocalRandom.current().nextBoolean();
                            boolean delete = ThreadLocalRandom.current().nextBoolean();
                            if (update) {
                                System.err.println("do " + Thread.currentThread() + " update on " + Bytes.from_string(key));
                                updates.incrementAndGet();
                                execute(manager, "UPDATE mytable set n1=? WHERE id=?", Arrays.asList(value, key));
                            } else if (insert) {
                                System.err.println("do " + Thread.currentThread() + " insert on " + Bytes.from_string(key));
                                inserts.incrementAndGet();
                                try {
                                    execute(manager, "INSERT INTO mytable(n1, id) values(?,?)", Arrays.asList(value, key));
                                } catch (DuplicatePrimaryKeyException ok) {
                                    duplicatePkErrors.incrementAndGet();
                                }
                            } else if (delete) {
                                System.err.println("do " + Thread.currentThread() + " delete on " + Bytes.from_string(key));
                                deletes.incrementAndGet();
                                execute(manager, "DELETE FROM mytable WHERE id=?", Arrays.asList(key));
                            }
                        } catch (Exception err) {
                            throw new RuntimeException(err);
                        }
                    }
                }));
            }
            for (Future f : futures) {
                f.get();
            }
            System.out.println("stats::updates:" + updates);
            System.out.println("stats::deletes:" + deletes);
            System.out.println("stats::inserts:" + inserts);
            System.out.println("stats::duplicatePkErrors:" + duplicatePkErrors);
            TableManagerStats stats = server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getTableManager("mytable").getStats();
            System.out.println("stats::tablesize:" + stats.getTablesize());
            System.out.println("stats::dirty records:" + stats.getDirtyrecords());
            System.out.println("stats::unload count:" + stats.getUnloadedPagesCount());
            System.out.println("stats::load count:" + stats.getLoadedPagesCount());
            System.out.println("stats::buffers used mem:" + stats.getBuffersUsedMemory());
        } finally {
            threadPool.shutdown();
            threadPool.awaitTermination(1, TimeUnit.MINUTES);
        }
    }
    // restart and recovery
    try (Server server = new Server(serverConfiguration)) {
        server.start();
        server.waitForTableSpaceBoot(TableSpace.DEFAULT, 300000, true);
    }
}
Also used : Path(java.nio.file.Path) Server(herddb.server.Server) ServerConfiguration(herddb.server.ServerConfiguration) ArrayList(java.util.ArrayList) DuplicatePrimaryKeyException(herddb.model.DuplicatePrimaryKeyException) AtomicLong(java.util.concurrent.atomic.AtomicLong) DBManager(herddb.core.DBManager) TableManagerStats(herddb.core.stats.TableManagerStats) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) DuplicatePrimaryKeyException(herddb.model.DuplicatePrimaryKeyException) Test(org.junit.Test)

Example 63 with Server

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

the class PrettySQLExceptionTest method test.

@Test
public void test() throws Exception {
    ServerConfiguration serverConfiguration = herddb.jdbc.TestUtils.newServerConfigurationWithAutoPort(folder.newFolder().toPath());
    try (Server server = new Server(serverConfiguration)) {
        server.start();
        server.waitForStandaloneBoot();
        try (Connection connection = DriverManager.getConnection(server.getJdbcUrl());
            Statement statement = connection.createStatement()) {
            try (ResultSet rs = statement.executeQuery("SELECT * FROM bad_table")) {
                fail();
            } catch (SQLException errorExpected) {
                System.out.println("EXCEPTION MESSAGE: " + errorExpected.getMessage());
                assertTrue("herddb.model.TableDoesNotExistException: no such table bad_table in tablespace default".equals(errorExpected.getMessage()) || errorExpected.getMessage().contains("Object 'BAD_TABLE' not found"));
            }
            statement.executeUpdate("CREATE TABLE mytable(pk string primary key)");
            statement.executeUpdate("INSERT INTO mytable(pk) values('myvalue')");
            // test pretty print errors
            try (ResultSet rs = statement.executeQuery("SELECT pk FROM mytable")) {
                assertTrue(rs.next());
                assertEquals("Value 'myvalue' (class herddb.utils.RawString) cannot be converted to an integer value, call was getInt(1), column names: [pk]", TestUtils.expectThrows(SQLException.class, () -> {
                    rs.getInt("pk");
                }).getMessage());
                assertEquals("Value 'myvalue' (class herddb.utils.RawString) cannot be converted to an integer value, call was getDate(1), column names: [pk]", TestUtils.expectThrows(SQLException.class, () -> {
                    rs.getDate("pk");
                }).getMessage());
                assertEquals("Value 'myvalue' (class herddb.utils.RawString) cannot be converted to an integer value, call was getTimestamp(1), column names: [pk]", TestUtils.expectThrows(SQLException.class, () -> {
                    rs.getTimestamp("pk");
                }).getMessage());
                assertEquals("Value 'myvalue' (class herddb.utils.RawString) cannot be converted to an integer value, call was getLong(1), column names: [pk]", TestUtils.expectThrows(SQLException.class, () -> {
                    rs.getLong("pk");
                }).getMessage());
            }
        }
    }
}
Also used : Server(herddb.server.Server) SQLException(java.sql.SQLException) Statement(java.sql.Statement) ServerConfiguration(herddb.server.ServerConfiguration) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 64 with Server

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

the class SelectColumnTest 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 " + data.keyType + " primary key, val " + data.valueType + ")");
                try (PreparedStatement insert = con.prepareStatement("INSERT INTO mytable (key,val) values(?,?)")) {
                    for (Object[] odata : data.data) {
                        insert.setObject(1, odata[0]);
                        setParameterAccordingToJavaType(insert, 2, odata[1]);
                        assertEquals(1, insert.executeUpdate());
                    }
                }
                if (data.checkKey()) {
                    try (PreparedStatement selectKey = con.prepareStatement("SELECT key FROM mytable WHERE key = ?")) {
                        for (Object[] odata : data.data) {
                            selectKey.setObject(1, odata[0]);
                            try (ResultSet rs = selectKey.executeQuery()) {
                                int count = 0;
                                while (rs.next()) {
                                    data.keyChecker.accept(rs, odata[0]);
                                    count++;
                                }
                                assertEquals(1, count);
                            }
                        }
                    }
                }
                try (PreparedStatement selectKey = con.prepareStatement("SELECT val FROM mytable WHERE key = ?")) {
                    for (Object[] odata : data.data) {
                        selectKey.setObject(1, odata[0]);
                        try (ResultSet rs = selectKey.executeQuery()) {
                            int count = 0;
                            while (rs.next()) {
                                data.valueChecker.accept(rs, odata[1]);
                                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) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 65 with Server

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

the class SimpleJoinTest method testSubQuery.

@Test
public void testSubQuery() 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)");
                statement.execute("CREATE TABLE table1 (k1 string primary key,n1 int,s1 string)");
                statement.execute("CREATE TABLE table2 (k2 string primary key,n2 int,s2 string)");
                statement.execute("INSERT INTO table1 (k1,n1,s1) values('a',1,'A')");
                statement.execute("INSERT INTO table1 (k1,n1,s1) values('b',2,'B')");
                statement.execute("INSERT INTO table2 (k2,n2,s2) values('c',3,'A')");
                statement.execute("INSERT INTO table2 (k2,n2,s2) values('d',4,'A')");
                try (ResultSet rs = statement.executeQuery("SELECT t1.k1, max(n1) as maxn1, max(select n2 from table2 t2 WHERE t1.s1=t2.s2) as maxn2 FROM " + " table1 t1 " + " group by k1")) {
                    int count = 0;
                    while (rs.next()) {
                        count++;
                    }
                    assertEquals(2, count);
                }
            }
        }
    }
}
Also used : StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) Server(herddb.server.Server) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Aggregations

Server (herddb.server.Server)119 Test (org.junit.Test)111 ServerConfiguration (herddb.server.ServerConfiguration)60 ClientConfiguration (herddb.client.ClientConfiguration)56 StaticClientSideMetadataProvider (herddb.server.StaticClientSideMetadataProvider)56 Connection (java.sql.Connection)53 HDBClient (herddb.client.HDBClient)52 Statement (java.sql.Statement)47 ResultSet (java.sql.ResultSet)37 Table (herddb.model.Table)36 CreateTableStatement (herddb.model.commands.CreateTableStatement)35 InsertStatement (herddb.model.commands.InsertStatement)32 PreparedStatement (java.sql.PreparedStatement)30 HashSet (java.util.HashSet)24 AlterTableSpaceStatement (herddb.model.commands.AlterTableSpaceStatement)19 Path (java.nio.file.Path)19 DataScanner (herddb.model.DataScanner)18 TableSpaceManager (herddb.core.TableSpaceManager)15 HDBConnection (herddb.client.HDBConnection)14 Index (herddb.model.Index)14