Search in sources :

Example 76 with DBManager

use of herddb.core.DBManager in project herddb by diennea.

the class BackupRestoreTest method test_backup_restore_with_deletes.

/**
 * Check that restore a dirty delete doesn't revive a record (it is: a phantom deleted record on a dirty page)
 */
@Test
public void test_backup_restore_with_deletes() throws Exception {
    ServerConfiguration serverconfig_1 = newServerConfigurationWithAutoPort(folder.newFolder().toPath());
    serverconfig_1.set(ServerConfiguration.PROPERTY_NODEID, "server1");
    serverconfig_1.set(ServerConfiguration.PROPERTY_MODE, ServerConfiguration.PROPERTY_MODE_CLUSTER);
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_ADDRESS, testEnv.getAddress());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_PATH, testEnv.getPath());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT, testEnv.getTimeout());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ENFORCE_LEADERSHIP, false);
    /* Disable page compaction (avoid compaction of dirty page) */
    serverconfig_1.set(ServerConfiguration.PROPERTY_FILL_PAGE_THRESHOLD, 0.0D);
    ServerConfiguration serverconfig_2 = serverconfig_1.copy().set(ServerConfiguration.PROPERTY_NODEID, "server2").set(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().toPath().toAbsolutePath());
    ClientConfiguration client_configuration = new ClientConfiguration(folder.newFolder().toPath());
    client_configuration.set(ClientConfiguration.PROPERTY_MODE, ServerConfiguration.PROPERTY_MODE_CLUSTER);
    client_configuration.set(ClientConfiguration.PROPERTY_ZOOKEEPER_ADDRESS, testEnv.getAddress());
    client_configuration.set(ClientConfiguration.PROPERTY_ZOOKEEPER_PATH, testEnv.getPath());
    client_configuration.set(ClientConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT, testEnv.getTimeout());
    try (Server server_1 = new Server(serverconfig_1)) {
        server_1.start();
        server_1.waitForStandaloneBoot();
        Table table = Table.builder().name("t1").column("c", ColumnTypes.INTEGER).column("d", ColumnTypes.INTEGER).primaryKey("c").build();
        DBManager manager = server_1.getManager();
        manager.executeStatement(new CreateTableStatement(table), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        manager.executeUpdate(new InsertStatement(TableSpace.DEFAULT, table.name, RecordSerializer.makeRecord(table, "c", 1, "d", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        manager.executeUpdate(new InsertStatement(TableSpace.DEFAULT, table.name, RecordSerializer.makeRecord(table, "c", 2, "d", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        manager.executeUpdate(new InsertStatement(TableSpace.DEFAULT, table.name, RecordSerializer.makeRecord(table, "c", 3, "d", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        manager.executeUpdate(new InsertStatement(TableSpace.DEFAULT, table.name, RecordSerializer.makeRecord(table, "c", 4, "d", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        manager.checkpoint();
        /* Check that new data isn't in a dirty page  */
        assertEquals(0, manager.getTableSpaceManager(TableSpace.DEFAULT).getTableManager(table.name).getStats().getDirtypages());
        manager.executeUpdate(new DeleteStatement(TableSpace.DEFAULT, table.name, Bytes.from_int(1), null), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        manager.executeUpdate(new InsertStatement(TableSpace.DEFAULT, table.name, RecordSerializer.makeRecord(table, "c", 5, "d", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        /* Now we have a dirty page in the checkpoint */
        manager.checkpoint();
        /* Check that a dirty page exists */
        assertEquals(1, manager.getTableSpaceManager(TableSpace.DEFAULT).getTableManager(table.name).getStats().getDirtypages());
        try (Server server_2 = new Server(serverconfig_2)) {
            server_2.start();
            try (HDBClient client = new HDBClient(client_configuration);
                HDBConnection connection = client.openConnection()) {
                assertEquals(4, connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM t1", true, Collections.emptyList(), 0, 0, 10, true).consume().size());
                ByteArrayOutputStream oo = new ByteArrayOutputStream();
                BackupUtils.dumpTableSpace(TableSpace.DEFAULT, 64 * 1024, connection, oo, new ProgressListener() {
                });
                byte[] backupData = oo.toByteArray();
                connection.executeUpdate(TableSpace.DEFAULT, "DELETE FROM t1", 0, false, true, Collections.emptyList());
                assertEquals(0, connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM t1", true, Collections.emptyList(), 0, 0, 10, true).consume().size());
                BackupUtils.restoreTableSpace("newts", server_2.getNodeId(), connection, new ByteArrayInputStream(backupData), new ProgressListener() {
                });
                /* No new insert AND no delete... if 5 it did see the new insert but missed the delete! */
                assertEquals(4, connection.executeScan("newts", "SELECT * FROM newts.t1", true, Collections.emptyList(), 0, 0, 10, true).consume().size());
            }
        }
    }
}
Also used : Table(herddb.model.Table) Server(herddb.server.Server) ServerConfiguration(herddb.server.ServerConfiguration) CreateTableStatement(herddb.model.commands.CreateTableStatement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DeleteStatement(herddb.model.commands.DeleteStatement) InsertStatement(herddb.model.commands.InsertStatement) HDBConnection(herddb.client.HDBConnection) DBManager(herddb.core.DBManager) HDBClient(herddb.client.HDBClient) ProgressListener(herddb.backup.ProgressListener) ByteArrayInputStream(java.io.ByteArrayInputStream) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 77 with DBManager

use of herddb.core.DBManager in project herddb by diennea.

the class SimpleSubqueryTest method deleteWithSubQueryTest.

@Test
public void deleteWithSubQueryTest() throws Exception {
    String nodeId = "localhost";
    try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
        manager.start();
        CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
        manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        manager.waitForTablespace("tblspace1", 10000);
        execute(manager, "CREATE TABLE tblspace1.table1 (k1 string primary key,n1 int)", Collections.emptyList());
        execute(manager, "CREATE TABLE tblspace1.table2 (k2 string primary key,fk string)", Collections.emptyList());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.table1(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1234))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.table1(k1,n1) values(?,?)", Arrays.asList("mykey2", Integer.valueOf(1234))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.table1(k1,n1) values(?,?)", Arrays.asList("mykey3", Integer.valueOf(1234))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.table1(k1,n1) values(?,?)", Arrays.asList("mykey4", Integer.valueOf(1238))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.table2(k2,fk) values(?,?)", Arrays.asList("subkey1", "mykey2")).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.table2(k2,fk) values(?,?)", Arrays.asList("subkey2", "mykey2")).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.table2(k2,fk) values(?,?)", Arrays.asList("subkey3", "mykey3")).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.table2(k2,fk) values(?,?)", Arrays.asList("subkey4", "mykey4")).getUpdateCount());
        assertEquals(2, scan(manager, "SELECT * " + "FROM tblspace1.table1 t1 " + "WHERE t1.k1 in ('mykey','mykey3')" + "", Collections.emptyList()).consumeAndClose().size());
        if (manager.isFullSQLSupportEnabled()) {
            assertEquals(1, scan(manager, "SELECT * " + "FROM tblspace1.table1 t1 " + "WHERE t1.k1 in (SELECT fk FROM tblspace1.table2 WHERE k2='subkey4')" + "", Arrays.asList("mykey4")).consumeAndClose().size());
            assertEquals(1, scan(manager, "SELECT * " + "FROM tblspace1.table1 t1 " + "WHERE t1.n1=1238 and t1.k1 in (SELECT fk FROM tblspace1.table2 WHERE k2='subkey4')" + "", Arrays.asList("mykey4")).consumeAndClose().size());
            assertEquals(1, scan(manager, "SELECT * " + "FROM tblspace1.table1 t1 " + "WHERE t1.n1=? and t1.k1 in (SELECT fk FROM tblspace1.table2 WHERE k2='subkey4')" + "", Arrays.asList(1238, "mykey4")).consumeAndClose().size());
            assertEquals(0, scan(manager, "SELECT * " + "FROM tblspace1.table1 t1 " + "WHERE t1.n1=? and t1.k1 in (SELECT fk FROM tblspace1.table2 WHERE k2='subkey4')" + "", Arrays.asList(124, "mykey4")).consumeAndClose().size());
            assertEquals(1, scan(manager, "SELECT * " + "FROM tblspace1.table1 t1 " + "WHERE t1.k1 in (SELECT fk FROM tblspace1.table2 WHERE k2=?)" + "", Arrays.asList("subkey4")).consumeAndClose().size());
            assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.table1 set n1=1000" + "WHERE k1 in (SELECT fk FROM tblspace1.table2 WHERE k2=?)" + "", Arrays.asList("subkey4")).getUpdateCount());
            assertEquals(1, executeUpdate(manager, "DELETE  " + "FROM tblspace1.table1 " + "WHERE k1 in (SELECT fk FROM tblspace1.table2 WHERE k2=?)" + "", Arrays.asList("subkey4")).getUpdateCount());
            assertEquals(0, scan(manager, "SELECT * " + "FROM tblspace1.table1 t1 " + "WHERE t1.k1 in (SELECT fk FROM tblspace1.table2 WHERE k2=?)" + "", Arrays.asList("subkey4")).consumeAndClose().size());
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DBManager(herddb.core.DBManager) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 78 with DBManager

use of herddb.core.DBManager in project herddb by diennea.

the class SimpleSubqueryTest method tableAliasTest.

@Test
public void tableAliasTest() throws Exception {
    String nodeId = "localhost";
    try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
        manager.start();
        CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
        manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        manager.waitForTablespace("tblspace1", 10000);
        execute(manager, "CREATE TABLE tblspace1.table1 (k1 string primary key,n1 int)", Collections.emptyList());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.table1(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1234))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.table1(k1,n1) values(?,?)", Arrays.asList("mykey2", Integer.valueOf(1234))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.table1(k1,n1) values(?,?)", Arrays.asList("mykey3", Integer.valueOf(1234))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.table1(k1,n1) values(?,?)", Arrays.asList("mykey4", Integer.valueOf(1234))).getUpdateCount());
        assertEquals(1, scan(manager, "SELECT * " + "FROM tblspace1.table1 t1 " + "WHERE t1.k1='mykey2'" + "", Collections.emptyList()).consumeAndClose().size());
        assertEquals(1, scan(manager, "SELECT t1.k1 " + "FROM tblspace1.table1 t1 " + "WHERE t1.k1='mykey2'" + "", Collections.emptyList()).consumeAndClose().size());
        try {
            scan(manager, "SELECT t2.k1 " + "FROM tblspace1.table1 t1", Collections.emptyList());
            fail("query must not work");
        } catch (Exception ok) {
            assertTrue("unexpected error " + ok, "From line 1, column 8 to line 1, column 9: Table 'T2' not found".equals(ok.getMessage()) || ok.getMessage().contains("Column t2.k1 not found"));
        }
        try {
            scan(manager, "SELECT k1 " + "FROM tblspace1.table1 t1 " + "WHERE t2.n1=123", Collections.emptyList());
            fail("query must not work");
        } catch (Exception ok) {
            assertTrue("From line 1, column 42 to line 1, column 43: Table 'T2' not found".equals(ok.getMessage()) || ok.getMessage().contains("Column t2.n1 not found"));
        }
        try {
            scan(manager, "SELECT k1 " + "FROM tblspace1.table1 t1 " + "WHERE t2.k1='aaa'", Collections.emptyList());
            fail("query must not work");
        } catch (Exception ok) {
            assertTrue("From line 1, column 42 to line 1, column 43: Table 'T2' not found".equals(ok.getMessage()) || ok.getMessage().contains("Column t2.k1 not found"));
        }
        try {
            scan(manager, "SELECT * " + "FROM tblspace1.table1 t2 " + "WHERE t1.k1='mykey2'", Collections.emptyList());
            fail("query must not work");
        } catch (Exception ok) {
            assertTrue("From line 1, column 41 to line 1, column 42: Table 'T1' not found".equals(ok.getMessage()) || ok.getMessage().contains("Column t1.k1 not found"));
        }
        try {
            scan(manager, "SELECT * " + "FROM tblspace1.table1 t2 " + "ORDER BY t1.n1", Collections.emptyList());
            fail("query must not work");
        } catch (Exception ok) {
            assertTrue("From line 1, column 44 to line 1, column 45: Table 'T1' not found".equals(ok.getMessage()) || ok.getMessage().contains("Column t1.n1 not found"));
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DBManager(herddb.core.DBManager) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 79 with DBManager

use of herddb.core.DBManager in project herddb by diennea.

the class SystemTablesTest method testSystemTablesForNonNullColumnTypes.

@Test
public void testSystemTablesForNonNullColumnTypes() throws Exception {
    String nodeId = "localhost";
    try (DBManager manager = new DBManager(nodeId, new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
        manager.start();
        CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
        manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        manager.waitForTablespace("tblspace1", 10000);
        execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key auto_increment,n1 int,s1 string not null)", Collections.emptyList());
        execute(manager, "CREATE TABLE tblspace1.tsql2 (k1 string primary key auto_increment,n1 int not null,l1 long not null,s1 string not null)", Collections.emptyList());
        execute(manager, "CREATE BRIN INDEX index1 on tblspace1.tsql2 (s1,n1)", Collections.emptyList());
        execute(manager, "CREATE HASH INDEX index2 on tblspace1.tsql2 (n1)", Collections.emptyList());
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.syscolumns", Collections.emptyList())) {
            List<DataAccessor> records = scan.consume();
            assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql") && t.get("column_name").equals("k1") && t.get("data_type").equals("string") && t.get("auto_increment").equals(1)).findAny().isPresent());
            assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql") && t.get("column_name").equals("n1") && t.get("data_type").equals("integer")).findAny().isPresent());
            assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql") && t.get("column_name").equals("s1") && t.get("data_type").equals("string") && t.get("type_name").equals("string not null") && t.get("is_nullable").equals(DatabaseMetaData.columnNoNulls)).findAny().isPresent());
            assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql2") && t.get("column_name").equals("k1") && t.get("data_type").equals("string") && t.get("auto_increment").equals(1)).findAny().isPresent());
            assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql2") && t.get("column_name").equals("n1") && t.get("data_type").equals("integer") && t.get("type_name").equals("integer not null") && t.get("is_nullable").equals(DatabaseMetaData.columnNoNulls)).findAny().isPresent());
            assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql2") && t.get("column_name").equals("s1") && t.get("data_type").equals("string") && t.get("type_name").equals("string not null") && t.get("is_nullable").equals(DatabaseMetaData.columnNoNulls)).findAny().isPresent());
            assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql2") && t.get("column_name").equals("l1") && t.get("data_type").equals("long") && t.get("type_name").equals("long not null") && t.get("is_nullable").equals(DatabaseMetaData.columnNoNulls)).findAny().isPresent());
        }
    }
}
Also used : DataAccessor(herddb.utils.DataAccessor) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DataScanner(herddb.model.DataScanner) RunningStatementInfo(herddb.core.RunningStatementInfo) Assert.assertTrue(org.junit.Assert.assertTrue) DatabaseMetaData(java.sql.DatabaseMetaData) Test(org.junit.Test) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) DBManager(herddb.core.DBManager) List(java.util.List) TestUtils.scan(herddb.core.TestUtils.scan) TransactionContext(herddb.model.TransactionContext) Assert.assertFalse(org.junit.Assert.assertFalse) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) StatementEvaluationContext(herddb.model.StatementEvaluationContext) TestUtils.execute(herddb.core.TestUtils.execute) RawString(herddb.utils.RawString) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DBManager(herddb.core.DBManager) DataScanner(herddb.model.DataScanner) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) DataAccessor(herddb.utils.DataAccessor) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) RawString(herddb.utils.RawString) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 80 with DBManager

use of herddb.core.DBManager in project herddb by diennea.

the class SystemTablesTest method testSysTables.

@Test
public void testSysTables() throws Exception {
    String nodeId = "localhost";
    try (DBManager manager = new DBManager(nodeId, new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
        manager.start();
        CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
        manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        manager.waitForTablespace("tblspace1", 10000);
        execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key auto_increment,n1 int default 18,s1 string)", Collections.emptyList());
        execute(manager, "CREATE TABLE tblspace1.tsql2 (k1 string primary key,n1 long,s1 timestamp, b1 blob)", Collections.emptyList());
        execute(manager, "CREATE BRIN INDEX index1 on tblspace1.tsql2 (s1,b1)", Collections.emptyList());
        execute(manager, "CREATE HASH INDEX index2 on tblspace1.tsql2 (b1)", Collections.emptyList());
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables", Collections.emptyList())) {
            List<DataAccessor> records = scan.consume();
            assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql")).findAny().isPresent());
            assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql2")).findAny().isPresent());
            assertTrue(records.stream().filter(t -> t.get("table_name").equals("systables")).findAny().isPresent());
        }
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where systemtable=false", Collections.emptyList())) {
            List<DataAccessor> records = scan.consume();
            assertEquals(2, records.size());
            assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql")).findAny().isPresent());
            assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql2")).findAny().isPresent());
        }
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where systemtable='false'", Collections.emptyList())) {
            List<DataAccessor> records = scan.consume();
            assertEquals(2, records.size());
            assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql")).findAny().isPresent());
            assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql2")).findAny().isPresent());
        }
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where systemtable=true", Collections.emptyList())) {
            List<DataAccessor> records = scan.consume();
            assertFalse(records.stream().filter(t -> t.get("table_name").equals("tsql")).findAny().isPresent());
            assertFalse(records.stream().filter(t -> t.get("table_name").equals("tsql2")).findAny().isPresent());
            assertTrue(records.stream().filter(t -> t.get("table_name").equals("systables")).findAny().isPresent());
        }
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where systemtable='true'", Collections.emptyList())) {
            List<DataAccessor> records = scan.consume();
            assertFalse(records.stream().filter(t -> t.get("table_name").equals("tsql")).findAny().isPresent());
            assertFalse(records.stream().filter(t -> t.get("table_name").equals("tsql2")).findAny().isPresent());
            assertTrue(records.stream().filter(t -> t.get("table_name").equals("systables")).findAny().isPresent());
        }
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systablestats", Collections.emptyList())) {
            List<DataAccessor> records = scan.consume();
            assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql")).findAny().isPresent());
            assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql2")).findAny().isPresent());
        }
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.sysindexes order by index_name", Collections.emptyList())) {
            List<DataAccessor> records = scan.consume();
            assertEquals(2, records.size());
            DataAccessor index1 = records.get(0);
            assertEquals(RawString.of("tblspace1"), index1.get("tablespace"));
            assertEquals(RawString.of("brin"), index1.get("index_type"));
            assertEquals(RawString.of("index1"), index1.get("index_name"));
            assertEquals(RawString.of("tsql2"), index1.get("table_name"));
            DataAccessor index2 = records.get(1);
            assertEquals(RawString.of("tblspace1"), index2.get("tablespace"));
            assertEquals(RawString.of("hash"), index2.get("index_type"));
            assertEquals(RawString.of("index2"), index2.get("index_name"));
            assertEquals(RawString.of("tsql2"), index2.get("table_name"));
        }
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.sysindexcolumns order by index_name, column_name", Collections.emptyList())) {
            List<DataAccessor> records = scan.consume();
            for (DataAccessor da : records) {
                System.out.println("rec: " + da.toMap());
            }
            assertTrue(records.stream().map(d -> d.toMap()).filter(d -> {
                return d.get("table_name").equals("tsql2") && d.get("index_type").equals("brin") && d.get("column_name").equals("b1") && d.get("ordinal_position").equals(1) && d.get("index_uuid") != null && d.get("tablespace").equals("tblspace1") && d.get("unique").equals(0) && d.get("index_name").equals("index1");
            }).findAny().isPresent());
            assertTrue(records.stream().map(d -> d.toMap()).filter(d -> {
                return d.get("table_name").equals("tsql2") && d.get("index_type").equals("brin") && d.get("column_name").equals("s1") && d.get("ordinal_position").equals(0) && d.get("index_uuid") != null && d.get("tablespace").equals("tblspace1") && d.get("unique").equals(0) && d.get("index_name").equals("index1");
            }).findAny().isPresent());
            assertTrue(records.stream().map(d -> d.toMap()).filter(d -> {
                return d.get("table_name").equals("tsql2") && d.get("index_type").equals("hash") && d.get("column_name").equals("b1") && d.get("ordinal_position").equals(0) && d.get("index_uuid") != null && d.get("tablespace").equals("tblspace1") && d.get("unique").equals(0) && d.get("index_name").equals("index2");
            }).findAny().isPresent());
            assertTrue(records.stream().map(d -> d.toMap()).filter(d -> {
                return d.get("table_name").equals("tsql2") && d.get("index_type").equals("pk") && d.get("column_name").equals("k1") && d.get("ordinal_position").equals(0) && // index_uuid == index_name for BLink
                d.get("index_uuid").equals("tsql2_primary") && d.get("tablespace").equals("tblspace1") && d.get("unique").equals(1) && d.get("index_name").equals("tsql2_primary");
            }).findAny().isPresent());
            // to be changed every time we add a new system table
            assertEquals(28, records.size());
        }
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.sysindexcolumns where table_name like '%tsql' order by index_name, column_name", Collections.emptyList())) {
            List<DataAccessor> records = scan.consume();
            for (DataAccessor da : records) {
                System.out.println("rec2: " + da.toMap());
            }
            assertTrue(records.stream().map(d -> d.toMap()).filter(d -> {
                System.out.println("filter: " + d);
                return d.get("table_name").equals("tsql") && d.get("index_type").equals("pk") && d.get("column_name").equals("k1") && d.get("ordinal_position").equals(0) && // index_uuid == index_name for BLink
                d.get("index_uuid").equals("tsql_primary") && d.get("tablespace").equals("tblspace1") && d.get("unique").equals(1) && d.get("index_name").equals("tsql_primary");
            }).findAny().isPresent());
            assertEquals(1, records.size());
        }
        try (DataScanner scan = scan(manager, "SELECT sc.* FROM tblspace1.sysindexcolumns sc " + // only non system tables
        "JOIN tblspace1.systables st on sc.table_name=st.table_name and st.systemtable = 'false' " + "order by sc.index_name, sc.column_name", Collections.emptyList())) {
            List<DataAccessor> records = scan.consume();
            for (DataAccessor da : records) {
                System.out.println("rec: " + da.toMap());
            }
            assertTrue(records.stream().map(d -> d.toMap()).filter(d -> {
                return d.get("table_name").equals("tsql2") && d.get("index_type").equals("brin") && d.get("column_name").equals("b1") && d.get("ordinal_position").equals(1) && d.get("index_uuid") != null && d.get("tablespace").equals("tblspace1") && d.get("unique").equals(0) && d.get("index_name").equals("index1");
            }).findAny().isPresent());
            assertTrue(records.stream().map(d -> d.toMap()).filter(d -> {
                return d.get("table_name").equals("tsql2") && d.get("index_type").equals("brin") && d.get("column_name").equals("s1") && d.get("ordinal_position").equals(0) && d.get("index_uuid") != null && d.get("tablespace").equals("tblspace1") && d.get("unique").equals(0) && d.get("index_name").equals("index1");
            }).findAny().isPresent());
            assertTrue(records.stream().map(d -> d.toMap()).filter(d -> {
                return d.get("table_name").equals("tsql2") && d.get("index_type").equals("hash") && d.get("column_name").equals("b1") && d.get("ordinal_position").equals(0) && d.get("index_uuid") != null && d.get("tablespace").equals("tblspace1") && d.get("unique").equals(0) && d.get("index_name").equals("index2");
            }).findAny().isPresent());
            assertTrue(records.stream().map(d -> d.toMap()).filter(d -> {
                return d.get("table_name").equals("tsql2") && d.get("index_type").equals("pk") && d.get("column_name").equals("k1") && d.get("ordinal_position").equals(0) && // index_uuid == index_name for BLink
                d.get("index_uuid").equals("tsql2_primary") && d.get("tablespace").equals("tblspace1") && d.get("unique").equals(1) && d.get("index_name").equals("tsql2_primary");
            }).findAny().isPresent());
            assertEquals(5, records.size());
        }
        execute(manager, "BEGIN TRANSACTION 'tblspace1'", Collections.emptyList());
        long txid;
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systransactions order by txid", Collections.emptyList())) {
            List<DataAccessor> records = scan.consume();
            assertEquals(1, records.size());
            System.out.println("records:" + records);
            txid = (Long) records.get(0).get("txid");
        }
        execute(manager, "COMMIT TRANSACTION 'tblspace1'," + txid, Collections.emptyList());
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systransactions order by txid", Collections.emptyList())) {
            List<DataAccessor> records = scan.consume();
            assertEquals(0, records.size());
        }
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.syscolumns", Collections.emptyList())) {
            List<DataAccessor> records = scan.consume();
            records.forEach(r -> {
                System.out.println("found " + r.toMap());
            });
            assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql") && t.get("column_name").equals("k1") && t.get("data_type").equals("string") && t.get("auto_increment").equals(1)).findAny().isPresent());
            assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql") && t.get("column_name").equals("n1") && t.get("data_type").equals("integer") && t.get("auto_increment").equals(0) && t.get("default_value").equals("18")).findAny().isPresent());
            assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql2") && t.get("column_name").equals("s1") && t.get("data_type").equals("timestamp")).findAny().isPresent());
            assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql2") && t.get("column_name").equals("b1") && t.get("data_type").equals("bytearray")).findAny().isPresent());
            assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql2") && t.get("column_name").equals("n1") && t.get("data_type").equals("long")).findAny().isPresent());
        }
        RunningStatementInfo runningStatementInfo = new RunningStatementInfo("mock query", System.currentTimeMillis(), "tblspace1", "info", 1);
        manager.getRunningStatements().registerRunningStatement(runningStatementInfo);
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.sysstatements ", Collections.emptyList())) {
            List<DataAccessor> records = scan.consume();
            assertEquals(1, records.size());
            records.forEach(s -> {
                System.out.println("STATEMENT: " + s);
            });
        } finally {
            manager.getRunningStatements().unregisterRunningStatement(runningStatementInfo);
        }
    }
}
Also used : DataAccessor(herddb.utils.DataAccessor) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DataScanner(herddb.model.DataScanner) RunningStatementInfo(herddb.core.RunningStatementInfo) Assert.assertTrue(org.junit.Assert.assertTrue) DatabaseMetaData(java.sql.DatabaseMetaData) Test(org.junit.Test) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) DBManager(herddb.core.DBManager) List(java.util.List) TestUtils.scan(herddb.core.TestUtils.scan) TransactionContext(herddb.model.TransactionContext) Assert.assertFalse(org.junit.Assert.assertFalse) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) StatementEvaluationContext(herddb.model.StatementEvaluationContext) TestUtils.execute(herddb.core.TestUtils.execute) RawString(herddb.utils.RawString) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DBManager(herddb.core.DBManager) DataScanner(herddb.model.DataScanner) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) DataAccessor(herddb.utils.DataAccessor) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) RawString(herddb.utils.RawString) RunningStatementInfo(herddb.core.RunningStatementInfo) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Aggregations

DBManager (herddb.core.DBManager)153 Test (org.junit.Test)143 MemoryCommitLogManager (herddb.mem.MemoryCommitLogManager)133 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)133 MemoryDataStorageManager (herddb.mem.MemoryDataStorageManager)132 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)126 DataScanner (herddb.model.DataScanner)92 RawString (herddb.utils.RawString)82 Table (herddb.model.Table)53 DataAccessor (herddb.utils.DataAccessor)53 CreateTableStatement (herddb.model.commands.CreateTableStatement)34 ScanStatement (herddb.model.commands.ScanStatement)34 RuntimeProjectedDataAccessor (herddb.model.planner.ProjectOp.ZeroCopyProjection.RuntimeProjectedDataAccessor)28 List (java.util.List)28 TransactionContext (herddb.model.TransactionContext)27 Index (herddb.model.Index)25 TranslatedQuery (herddb.sql.TranslatedQuery)23 CreateIndexStatement (herddb.model.commands.CreateIndexStatement)21 SecondaryIndexSeek (herddb.index.SecondaryIndexSeek)18 Timestamp (java.sql.Timestamp)15