Search in sources :

Example 16 with Column

use of herddb.model.Column in project herddb by diennea.

the class RecordSerializer method toBean.

@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED")
public static Map<String, Object> toBean(Record record, Table table) {
    try {
        ImmutableMap.Builder<String, Object> res = new ImmutableMap.Builder<>();
        if (table.primaryKey.length == 1) {
            Object key = deserializeSingleColumnPrimaryKey(record.key, table);
            res.put(table.primaryKey[0], key);
        } else {
            deserializeMultiColumnPrimaryKey(record.key, table, res);
        }
        if (record.value != null && record.value.getLength() > 0) {
            try (ByteArrayCursor din = record.value.newCursor()) {
                while (true) {
                    int serialPosition;
                    serialPosition = din.readVIntNoEOFException();
                    if (din.isEof()) {
                        break;
                    }
                    Column col = table.getColumnBySerialPosition(serialPosition);
                    // we have to deserialize or skip always the value, even the column is no more present
                    if (col != null) {
                        Object v = deserializeTypeAndValue(din);
                        res.put(col.name, v);
                    } else {
                        skipTypeAndValue(din);
                    }
                }
            }
        }
        return res.build();
    } catch (IOException err) {
        throw new IllegalArgumentException("malformed record", err);
    }
}
Also used : Column(herddb.model.Column) RawString(herddb.utils.RawString) IOException(java.io.IOException) ImmutableMap(com.google.common.collect.ImmutableMap) ByteArrayCursor(herddb.utils.ByteArrayCursor) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 17 with Column

use of herddb.model.Column in project herddb by diennea.

the class TableManager method tableAltered.

@Override
public void tableAltered(Table table, Transaction transaction) throws DDLException {
    // compute diff, if some column as been dropped we need to remove the value from each record
    List<String> droppedColumns = new ArrayList<>();
    for (Column c : this.table.columns) {
        if (table.getColumn(c.name) == null) {
            droppedColumns.add(c.name);
        }
    }
    this.table = table;
    if (!droppedColumns.isEmpty()) {
        // no lock is necessary
        pages.values().forEach(p -> {
            p.flushRecordsCache();
        });
    }
    if (this.table.auto_increment) {
        rebuildNextPrimaryKeyValue();
    }
    // clear FK caches, foreignkeys may change
    parentForeignKeyQueries.clear();
    childForeignKeyQueries.clear();
}
Also used : Column(herddb.model.Column) ArrayList(java.util.ArrayList)

Example 18 with Column

use of herddb.model.Column in project herddb by diennea.

the class SysindexcolumnsTableManager method buildVirtualRecordList.

@Override
protected Iterable<Record> buildVirtualRecordList(Transaction transaction) {
    List<Table> tables = tableSpaceManager.getAllVisibleTables(transaction);
    List<Record> result = new ArrayList<>();
    tables.forEach(r -> {
        // PK
        int posPk = 0;
        for (String pk : r.getPrimaryKey()) {
            Column column = r.getColumn(pk);
            String indexName = BLinkKeyToPageIndex.deriveIndexName(r.name);
            result.add(RecordSerializer.makeRecord(table, "index_type", "pk", "column_name", column.name, "ordinal_position", posPk++, "clustered", 1, "unique", 1, "tablespace", r.tablespace, "table_name", r.name, "index_name", indexName, // uuid = index_name in BLink !
            "index_uuid", // uuid = index_name in BLink !
            indexName));
        }
        Map<String, AbstractIndexManager> indexesOnTable = tableSpaceManager.getIndexesOnTable(r.name);
        if (indexesOnTable != null) {
            indexesOnTable.values().forEach(indexManager -> {
                Index index = indexManager.getIndex();
                int pos = 0;
                for (Column cc : index.getColumns()) {
                    result.add(RecordSerializer.makeRecord(table, "tablespace", r.tablespace, "table_name", r.name, "index_name", index.name, "index_uuid", index.uuid, "index_type", index.type, "column_name", cc.name, "ordinal_position", pos++, "clustered", 0, "unique", index.unique ? 1 : 0));
                }
            });
        }
    });
    return result;
}
Also used : Table(herddb.model.Table) AbstractIndexManager(herddb.core.AbstractIndexManager) Column(herddb.model.Column) ArrayList(java.util.ArrayList) Record(herddb.model.Record) Index(herddb.model.Index) BLinkKeyToPageIndex(herddb.index.blink.BLinkKeyToPageIndex)

Example 19 with Column

use of herddb.model.Column in project herddb by diennea.

the class RunHerdDB070Test method test.

@Test
public void test() throws Exception {
    String file = "herddb.070.joinerror.zip";
    File dbdatadir = folder.newFolder("dbdata070_" + file);
    try (InputStream in = RunHerdDB070Test.class.getResourceAsStream(file)) {
        ZIPUtils.unZip(in, dbdatadir);
    }
    System.out.println("UNZIPPED TO " + dbdatadir);
    final Path dbdata = dbdatadir.toPath().resolve("herddb.070.joinerror").resolve("dbdata");
    Path metadataPath = dbdata.resolve("metadata");
    Path dataPath = dbdata.resolve("data");
    Path logsPath = dbdata.resolve("txlog");
    Path tmoDir = dbdata.resolve("tmp");
    assertTrue(Files.isDirectory(metadataPath));
    assertTrue(Files.isDirectory(dataPath));
    assertTrue(Files.isDirectory(logsPath));
    Path nodeid = dataPath.resolve("nodeid");
    assertTrue(Files.isRegularFile(nodeid));
    String id = new String(Files.readAllBytes(nodeid), StandardCharsets.UTF_8);
    System.out.println("id:" + id);
    String expectedNodeId = "asino";
    assertTrue(id.endsWith("\n" + expectedNodeId));
    try (DBManager manager = new DBManager(expectedNodeId, new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath), tmoDir, null)) {
        manager.start();
        final String tableSpace = "herd";
        final String tableName = "testtable";
        assertEquals(expectedNodeId, manager.getNodeId());
        assertTrue(manager.waitForTablespace(tableSpace, 10000));
        AbstractTableManager tableManagerLicense = manager.getTableSpaceManager("herd").getTableManager("license");
        Table tableLicense = tableManagerLicense.getTable();
        System.out.println("TABLE PK: " + Arrays.toString(tableLicense.primaryKey));
        for (Column c : tableLicense.columns) {
            System.out.println("COL: " + c.name + " serialPos: " + c.serialPosition);
        }
        AbstractTableManager tableManagerCustomer = manager.getTableSpaceManager("herd").getTableManager("customer");
        Table tableCustomer = tableManagerCustomer.getTable();
        System.out.println("TABLE PK: " + Arrays.toString(tableCustomer.primaryKey));
        for (Column c : tableCustomer.columns) {
            System.out.println("COL: " + c.name + " serialPos: " + c.serialPosition);
        }
        {
            TranslatedQuery translated = manager.getPlanner().translate(tableSpace, "SELECT * FROM license", Collections.emptyList(), true, true, false, -1);
            System.out.println("TABLE CONTENTS");
            try (DataScanner scan1 = ((ScanResult) manager.executePlan(translated.plan, translated.context, TransactionContext.NO_TRANSACTION)).dataScanner) {
                List<DataAccessor> consume = scan1.consume();
                System.out.println("NUM " + consume.size());
                assertEquals(15, consume.size());
                for (DataAccessor r : consume) {
                    System.out.println("RECORD " + r.toMap());
                }
            }
        }
        {
            TranslatedQuery translated = manager.getPlanner().translate(tableSpace, "SELECT * FROM customer", Collections.emptyList(), true, true, false, -1);
            System.out.println("TABLE CONTENTS");
            try (DataScanner scan1 = ((ScanResult) manager.executePlan(translated.plan, translated.context, TransactionContext.NO_TRANSACTION)).dataScanner) {
                List<DataAccessor> consume = scan1.consume();
                System.out.println("NUM " + consume.size());
                assertEquals(7, consume.size());
                for (DataAccessor r : consume) {
                    System.out.println("RECORD " + r.toMap());
                }
            }
        }
        {
            TranslatedQuery translated = manager.getPlanner().translate(tableSpace, "SELECT t0.license_id,c.customer_id FROM license t0, customer c WHERE c.customer_id = 3 AND t0.customer_id = 3 AND c.customer_id = t0.customer_id\n" + "            ", Collections.emptyList(), true, true, false, -1);
            System.out.println("TABLE CONTENTS");
            try (DataScanner scan1 = ((ScanResult) manager.executePlan(translated.plan, translated.context, TransactionContext.NO_TRANSACTION)).dataScanner) {
                List<DataAccessor> consume = scan1.consume();
                System.out.println("NUM " + consume.size());
                assertEquals(9, consume.size());
                for (DataAccessor r : consume) {
                    System.out.println("RECORD " + r.toMap());
                }
            }
        }
        {
            TranslatedQuery translated = manager.getPlanner().translate(tableSpace, "SELECT * FROM license t0, customer c WHERE c.customer_id = t0.customer_id", Collections.emptyList(), true, true, false, -1);
            System.out.println("TABLE CONTENTS");
            try (DataScanner scan1 = ((ScanResult) manager.executePlan(translated.plan, translated.context, TransactionContext.NO_TRANSACTION)).dataScanner) {
                List<DataAccessor> consume = scan1.consume();
                System.out.println("NUM " + consume.size());
                assertEquals(15, consume.size());
                for (DataAccessor r : consume) {
                    System.out.println("RECORD " + r.toMap());
                }
            }
        }
    }
}
Also used : Path(java.nio.file.Path) Table(herddb.model.Table) TranslatedQuery(herddb.sql.TranslatedQuery) InputStream(java.io.InputStream) FileMetadataStorageManager(herddb.file.FileMetadataStorageManager) DataAccessor(herddb.utils.DataAccessor) FileCommitLogManager(herddb.file.FileCommitLogManager) DBManager(herddb.core.DBManager) DataScanner(herddb.model.DataScanner) AbstractTableManager(herddb.core.AbstractTableManager) Column(herddb.model.Column) FileDataStorageManager(herddb.file.FileDataStorageManager) List(java.util.List) File(java.io.File) Test(org.junit.Test)

Example 20 with Column

use of herddb.model.Column in project herddb by diennea.

the class UpgradeFrom050WithBrinIndexesTest method test.

private void test(String file) throws Exception {
    File dbdatadir = folder.newFolder("dbdata050_" + file);
    try (InputStream in = UpgradeFrom050WithBrinIndexesTest.class.getResourceAsStream(file)) {
        ZIPUtils.unZip(in, dbdatadir);
    }
    final Path dbdata = dbdatadir.toPath().resolve("dbdata");
    Path metadataPath = dbdata.resolve("metadata");
    Path dataPath = dbdata.resolve("data");
    Path logsPath = dbdata.resolve("txlog");
    Path tmoDir = dbdata.resolve("tmp");
    assertTrue(Files.isDirectory(metadataPath));
    assertTrue(Files.isDirectory(dataPath));
    assertTrue(Files.isDirectory(logsPath));
    Path nodeid = dataPath.resolve("nodeid");
    assertTrue(Files.isRegularFile(nodeid));
    String id = new String(Files.readAllBytes(nodeid), StandardCharsets.UTF_8);
    System.out.println("id:" + id);
    String expectedNodeId = "capra";
    assertTrue(id.endsWith("\n" + expectedNodeId));
    try (DBManager manager = new DBManager(expectedNodeId, new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath), tmoDir, null)) {
        manager.start();
        final String tableSpace = "herd";
        final String tableName = "testtable";
        assertEquals(expectedNodeId, manager.getNodeId());
        assertTrue(manager.waitForTablespace(tableSpace, 10000));
        TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSpace);
        AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableName);
        List<Index> indexes = tableManager.getAvailableIndexes();
        for (Index e : indexes) {
            System.out.println("INDEX: " + e);
            assertEquals(e.type, Index.TYPE_BRIN);
        }
        assertEquals(4, indexes.size());
        for (Column c : tableManager.getTable().getColumns()) {
            System.out.println("COLUMN :" + c);
        }
        {
            TranslatedQuery translated = manager.getPlanner().translate(tableSpace, "SELECT * FROM " + tableName + " ORDER BY pk,n1,n2", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
            System.out.println("TABLE CONTENTS");
            try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
                for (DataAccessor r : scan1.consume()) {
                    System.out.println("RECORD " + r.toMap());
                }
            }
        }
        test(new TestCase("SELECT * FROM " + tableSpace + "." + tableName + " WHERE n1=1", SecondaryIndexSeek.class, 4), manager, tableSpace);
        // this could be SecondaryIndexSeek but we have more indexes and the planner is not so smart
        test(new TestCase("SELECT * FROM " + tableSpace + "." + tableName + " WHERE n2=3", SecondaryIndexPrefixScan.class, 2), manager, tableSpace);
        test(new TestCase("SELECT * FROM " + tableSpace + "." + tableName + " WHERE n2>=3", SecondaryIndexRangeScan.class, 3), manager, tableSpace);
        test(new TestCase("SELECT * FROM " + tableSpace + "." + tableName + " WHERE n1=1 and n2=3", SecondaryIndexPrefixScan.class, 1), manager, tableSpace);
    }
}
Also used : Path(java.nio.file.Path) TranslatedQuery(herddb.sql.TranslatedQuery) SecondaryIndexRangeScan(herddb.index.SecondaryIndexRangeScan) InputStream(java.io.InputStream) FileMetadataStorageManager(herddb.file.FileMetadataStorageManager) DataAccessor(herddb.utils.DataAccessor) Index(herddb.model.Index) FileCommitLogManager(herddb.file.FileCommitLogManager) DBManager(herddb.core.DBManager) DataScanner(herddb.model.DataScanner) SecondaryIndexSeek(herddb.index.SecondaryIndexSeek) AbstractTableManager(herddb.core.AbstractTableManager) Column(herddb.model.Column) FileDataStorageManager(herddb.file.FileDataStorageManager) TableSpaceManager(herddb.core.TableSpaceManager) SecondaryIndexPrefixScan(herddb.index.SecondaryIndexPrefixScan) File(java.io.File) ScanStatement(herddb.model.commands.ScanStatement)

Aggregations

Column (herddb.model.Column)68 StatementExecutionException (herddb.model.StatementExecutionException)25 ArrayList (java.util.ArrayList)24 Table (herddb.model.Table)23 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)19 DataAccessor (herddb.utils.DataAccessor)18 HashMap (java.util.HashMap)14 PlannerOp (herddb.model.planner.PlannerOp)12 List (java.util.List)12 Bytes (herddb.utils.Bytes)10 AbstractTableManager (herddb.core.AbstractTableManager)9 RecordFunction (herddb.model.RecordFunction)9 RawString (herddb.utils.RawString)9 IOException (java.io.IOException)9 HashSet (java.util.HashSet)9 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)9 Test (org.junit.Test)9 DataScanner (herddb.model.DataScanner)8 Tuple (herddb.model.Tuple)8 InsertStatement (herddb.model.commands.InsertStatement)8