Search in sources :

Example 11 with AbstractTableManager

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

the class CalcitePlanner method getRootSchema.

private SchemaPlus getRootSchema() {
    if (rootSchema != null) {
        return rootSchema;
    }
    final SchemaPlus _rootSchema = Frameworks.createRootSchema(true);
    for (String tableSpace : manager.getLocalTableSpaces()) {
        TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSpace);
        SchemaPlus schema = _rootSchema.add(tableSpace, new AbstractSchema());
        List<Table> tables = tableSpaceManager.getAllTablesForPlanner();
        for (Table table : tables) {
            AbstractTableManager tableManager = tableSpaceManager.getTableManager(table.name);
            TableImpl tableDef = new TableImpl(tableManager);
            schema.add(table.name, tableDef);
        }
    }
    rootSchema = _rootSchema;
    return _rootSchema;
}
Also used : Table(herddb.model.Table) ShowCreateTableCalculator.calculateShowCreateTable(herddb.sql.functions.ShowCreateTableCalculator.calculateShowCreateTable) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) RelOptTable(org.apache.calcite.plan.RelOptTable) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) ScannableTable(org.apache.calcite.schema.ScannableTable) AbstractTable(org.apache.calcite.schema.impl.AbstractTable) ModifiableTable(org.apache.calcite.schema.ModifiableTable) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) AbstractTableManager(herddb.core.AbstractTableManager) SchemaPlus(org.apache.calcite.schema.SchemaPlus) TableSpaceManager(herddb.core.TableSpaceManager)

Example 12 with AbstractTableManager

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

the class SimpleFollowerTest method testLeaderOnlineLogAvailableMultipleVersionsActivePages.

@Test
public void testLeaderOnlineLogAvailableMultipleVersionsActivePages() throws Exception {
    ServerConfiguration serverconfig_1 = newServerConfigurationWithAutoPort(folder.newFolder().toPath());
    serverconfig_1.set(ServerConfiguration.PROPERTY_MAX_LOGICAL_PAGE_SIZE, 1000);
    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);
    serverconfig_1.set(ServerConfiguration.PROPERTY_ENFORCE_LEADERSHIP, false);
    // delete ledgers soon
    serverconfig_1.set(ServerConfiguration.PROPERTY_BOOKKEEPER_LEDGERS_RETENTION_PERIOD, 1);
    serverconfig_1.set(ServerConfiguration.PROPERTY_CHECKPOINT_PERIOD, 0);
    // disabled
    serverconfig_1.set(ServerConfiguration.PROPERTY_BOOKKEEPER_MAX_IDLE_TIME, 0);
    /*
         * 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_BOOT_FORCE_DOWNLOAD_SNAPSHOT, true).set(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().toPath().toAbsolutePath());
    Table table = Table.builder().name("t1").column("c", ColumnTypes.INTEGER).column("s", ColumnTypes.STRING).primaryKey("c").build();
    try (Server server_1 = new Server(serverconfig_1)) {
        server_1.start();
        server_1.waitForStandaloneBoot();
        server_1.getManager().executeStatement(new CreateTableStatement(table), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        TableSpaceManager tableSpaceManager = server_1.getManager().getTableSpaceManager(TableSpace.DEFAULT);
        AbstractTableManager tableManager = tableSpaceManager.getTableManager("t1");
        // fill table
        long tx = herddb.core.TestUtils.beginTransaction(server_1.getManager(), TableSpace.DEFAULT);
        for (int i = 0; i < 1000; i++) {
            server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", i, "s", "1")), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx));
        }
        herddb.core.TestUtils.commitTransaction(server_1.getManager(), TableSpace.DEFAULT, tx);
        // when we send the dump to the follower we must send only the latest version of the record
        for (int i = 0; i < 10; i++) {
            tableManager.flush();
            server_1.getManager().executeUpdate(new UpdateStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 1, "s", "2" + i), null), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        }
        BooleanHolder foundDuplicate = new BooleanHolder(false);
        server_1.getManager().getDataStorageManager().fullTableScan(tableSpaceManager.getTableSpaceUUID(), tableManager.getTable().uuid, new FullTableScanConsumer() {

            Map<Bytes, Long> recordPage = new HashMap<>();

            @Override
            public void acceptPage(long pageId, List<Record> records) {
                for (Record record : records) {
                    Long prev = recordPage.put(record.key, pageId);
                    if (prev != null) {
                        foundDuplicate.value = true;
                    }
                }
            }
        });
        assertTrue(foundDuplicate.value);
        // data will be downloaded from the server_1 (PROPERTY_BOOT_FORCE_DOWNLOAD_SNAPSHOT)
        try (Server server_2 = new Server(serverconfig_2)) {
            server_2.start();
            assertTrue(server_2.getManager().isTableSpaceLocallyRecoverable(server_1.getMetadataStorageManager().describeTableSpace(TableSpace.DEFAULT)));
            server_1.getManager().executeStatement(new AlterTableSpaceStatement(TableSpace.DEFAULT, new HashSet<>(Arrays.asList("server1", "server2")), "server1", 1, 0), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            assertTrue(server_2.getManager().waitForTablespace(TableSpace.DEFAULT, 60000, false));
        }
    }
}
Also used : UpdateStatement(herddb.model.commands.UpdateStatement) AlterTableSpaceStatement(herddb.model.commands.AlterTableSpaceStatement) Table(herddb.model.Table) Server(herddb.server.Server) HashMap(java.util.HashMap) ServerConfiguration(herddb.server.ServerConfiguration) CreateTableStatement(herddb.model.commands.CreateTableStatement) BooleanHolder(herddb.utils.BooleanHolder) InsertStatement(herddb.model.commands.InsertStatement) FullTableScanConsumer(herddb.storage.FullTableScanConsumer) Bytes(herddb.utils.Bytes) AbstractTableManager(herddb.core.AbstractTableManager) TransactionContext(herddb.model.TransactionContext) TableSpaceManager(herddb.core.TableSpaceManager) Record(herddb.model.Record) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 13 with AbstractTableManager

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

the class ShowCreateTableCalculatorTest method test.

@Test
public void test() {
    Table t = Table.builder().uuid("1234").column("k1", ColumnTypes.INTEGER).column("s1", ColumnTypes.STRING).primaryKey("k1").tablespace("ts1").name("test3").build();
    AbstractTableManager tm = new MockTableManager(t, new ArrayList<>());
    assertTrue(ShowCreateTableCalculator.calculate(false, "test3", "ts1", tm).equals("CREATE TABLE ts1.test3(k1 integer,s1 string,PRIMARY KEY(k1))"));
    t = Table.builder().uuid("1234").column("k1", ColumnTypes.INTEGER).column("s1", ColumnTypes.STRING).column("s2", ColumnTypes.NOTNULL_STRING).primaryKey("k1", true).primaryKey("s2").tablespace("ts1").name("test3").build();
    tm = new MockTableManager(t, new ArrayList<>());
    assertTrue(ShowCreateTableCalculator.calculate(false, "test3", "ts1", tm).equals("CREATE TABLE ts1.test3(k1 integer auto_increment,s1 string,s2 string not null,PRIMARY KEY(k1,s2))"));
    t = Table.builder().uuid("1234").column("k1", ColumnTypes.INTEGER).column("s1", ColumnTypes.NOTNULL_STRING).column("l1", ColumnTypes.NOTNULL_LONG).column("i1", ColumnTypes.NOTNULL_INTEGER).primaryKey("k1", true).tablespace("ts1").name("test3").build();
    tm = new MockTableManager(t, new ArrayList<>());
    assertTrue(ShowCreateTableCalculator.calculate(false, "test3", "ts1", tm).equals("CREATE TABLE ts1.test3(k1 integer auto_increment,s1 string not null,l1 long not null,i1 integer not null,PRIMARY KEY(k1))"));
    t = Table.builder().uuid("1234").column("ID", ColumnTypes.INTEGER).column("s1", ColumnTypes.NOTNULL_STRING).column("l1", ColumnTypes.NOTNULL_LONG).column("i1", ColumnTypes.NOTNULL_INTEGER).primaryKey("ID", true).tablespace("ts1").name("test4").build();
    tm = new MockTableManager(t, new ArrayList<>());
    assertTrue(ShowCreateTableCalculator.calculate(false, "test4", "ts1", tm).equals("CREATE TABLE ts1.test4(id integer auto_increment,s1 string not null,l1 long not null,i1 integer not null,PRIMARY KEY(id))"));
}
Also used : Table(herddb.model.Table) AbstractTableManager(herddb.core.AbstractTableManager) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 14 with AbstractTableManager

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

the class SystablestatsTableManager method buildVirtualRecordList.

@Override
protected Iterable<Record> buildVirtualRecordList() {
    List<Table> tables = tableSpaceManager.getAllCommittedTables();
    List<Record> result = new ArrayList<>();
    for (Table r : tables) {
        AbstractTableManager tableManager = tableSpaceManager.getTableManager(r.name);
        if (tableManager != null && !tableManager.isSystemTable()) {
            TableManagerStats stats = tableManager.getStats();
            result.add(RecordSerializer.makeRecord(table, "tablespace", r.tablespace, "table_name", r.name, "systemtable", r.name.startsWith("sys") ? "true" : "false", "tablesize", stats.getTablesize(), "loadedpages", stats.getLoadedpages(), "loadedpagescount", stats.getLoadedPagesCount(), "unloadedpagescount", stats.getUnloadedPagesCount(), "dirtypages", stats.getDirtypages(), "dirtyrecords", stats.getDirtyrecords(), "maxlogicalpagesize", stats.getMaxLogicalPageSize(), "keysmemory", stats.getKeysUsedMemory(), "buffersmemory", stats.getBuffersUsedMemory()));
        }
    }
    return result;
}
Also used : Table(herddb.model.Table) AbstractTableManager(herddb.core.AbstractTableManager) TableManagerStats(herddb.core.stats.TableManagerStats) ArrayList(java.util.ArrayList) Record(herddb.model.Record)

Example 15 with AbstractTableManager

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

the class SQLPlanner method buildCreateIndexStatement.

private Statement buildCreateIndexStatement(String defaultTableSpace, CreateIndex s) throws StatementExecutionException {
    try {
        String tableSpace = s.getTable().getSchemaName();
        if (tableSpace == null) {
            tableSpace = defaultTableSpace;
        }
        String tableName = s.getTable().getName();
        String indexName = s.getIndex().getName().toLowerCase();
        String indexType = convertIndexType(s.getIndex().getType());
        herddb.model.Index.Builder builder = herddb.model.Index.builder().name(indexName).uuid(UUID.randomUUID().toString()).type(indexType).table(tableName).tablespace(tableSpace);
        AbstractTableManager tableDefinition = manager.getTableSpaceManager(tableSpace).getTableManager(tableName);
        if (tableDefinition == null) {
            throw new TableDoesNotExistException("no such table " + tableName + " in tablespace " + tableSpace);
        }
        for (String columnName : s.getIndex().getColumnsNames()) {
            columnName = columnName.toLowerCase();
            Column column = tableDefinition.getTable().getColumn(columnName);
            if (column == null) {
                throw new StatementExecutionException("no such column " + columnName + " on table " + tableName + " in tablespace " + tableSpace);
            }
            builder.column(column.name, column.type);
        }
        CreateIndexStatement statement = new CreateIndexStatement(builder.build());
        return statement;
    } catch (IllegalArgumentException err) {
        throw new StatementExecutionException("bad index definition: " + err.getMessage(), err);
    }
}
Also used : TableDoesNotExistException(herddb.model.TableDoesNotExistException) AbstractTableManager(herddb.core.AbstractTableManager) Column(herddb.model.Column) CreateIndexStatement(herddb.model.commands.CreateIndexStatement) CreateIndex(net.sf.jsqlparser.statement.create.index.CreateIndex) Index(net.sf.jsqlparser.statement.create.table.Index) StatementExecutionException(herddb.model.StatementExecutionException)

Aggregations

AbstractTableManager (herddb.core.AbstractTableManager)24 Table (herddb.model.Table)18 TableSpaceManager (herddb.core.TableSpaceManager)16 StatementExecutionException (herddb.model.StatementExecutionException)12 Column (herddb.model.Column)10 ArrayList (java.util.ArrayList)10 CreateTable (net.sf.jsqlparser.statement.create.table.CreateTable)10 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)8 AlterExpression (net.sf.jsqlparser.statement.alter.AlterExpression)8 TableDoesNotExistException (herddb.model.TableDoesNotExistException)7 Expression (net.sf.jsqlparser.expression.Expression)7 SignedExpression (net.sf.jsqlparser.expression.SignedExpression)7 ShowCreateTableCalculator.calculateShowCreateTable (herddb.sql.functions.ShowCreateTableCalculator.calculateShowCreateTable)6 ScanStatement (herddb.model.commands.ScanStatement)5 OpSchema (herddb.sql.expressions.OpSchema)5 AutoIncrementPrimaryKeyRecordFunction (herddb.model.AutoIncrementPrimaryKeyRecordFunction)4 DataScanner (herddb.model.DataScanner)4 RecordFunction (herddb.model.RecordFunction)4 InsertStatement (herddb.model.commands.InsertStatement)4 PlannerOp (herddb.model.planner.PlannerOp)4