Search in sources :

Example 1 with DRTable

use of org.voltdb.compiler.statements.DRTable in project voltdb by VoltDB.

the class DDLCompiler method addTableToCatalog.

private void addTableToCatalog(Database db, VoltXMLElement node, boolean isXDCR) throws VoltCompilerException {
    assert node.name.equals("table");
    // Construct table-specific maps
    HashMap<String, Column> columnMap = new HashMap<>();
    HashMap<String, Index> indexMap = new HashMap<>();
    final String name = node.attributes.get("name");
    // create a table node in the catalog
    final Table table = db.getTables().add(name);
    // set max value before return for view table
    table.setTuplelimit(Integer.MAX_VALUE);
    // add the original DDL to the table (or null if it's not there)
    TableAnnotation annotation = new TableAnnotation();
    table.setAnnotation(annotation);
    // handle the case where this is a materialized view
    final String query = node.attributes.get("query");
    if (query != null) {
        assert (query.length() > 0);
        m_matViewMap.put(table, query);
    }
    final boolean isStream = (node.attributes.get("stream") != null);
    final String streamTarget = node.attributes.get("export");
    final String streamPartitionColumn = node.attributes.get("partitioncolumn");
    // all tables start replicated
    // if a partition is found in the project file later,
    //  then this is reversed
    table.setIsreplicated(true);
    // map of index replacements for later constraint fixup
    final Map<String, String> indexReplacementMap = new TreeMap<>();
    // Need the columnTypes sorted by column index.
    SortedMap<Integer, VoltType> columnTypes = new TreeMap<>();
    for (VoltXMLElement subNode : node.children) {
        if (subNode.name.equals("columns")) {
            int colIndex = 0;
            for (VoltXMLElement columnNode : subNode.children) {
                if (columnNode.name.equals("column")) {
                    addColumnToCatalog(table, columnNode, columnTypes, columnMap, m_compiler);
                    colIndex++;
                }
            }
            // limit the total number of columns in a table
            if (colIndex > MAX_COLUMNS) {
                String msg = "Table " + name + " has " + colIndex + " columns (max is " + MAX_COLUMNS + ")";
                throw m_compiler.new VoltCompilerException(msg);
            }
        }
        if (subNode.name.equals("indexes")) {
            // that refer to them.
            for (VoltXMLElement indexNode : subNode.children) {
                if (indexNode.name.equals("index") == false)
                    continue;
                String indexName = indexNode.attributes.get("name");
                if (indexName.startsWith(HSQLInterface.AUTO_GEN_IDX_PREFIX) == false) {
                    addIndexToCatalog(db, table, indexNode, indexReplacementMap, indexMap, columnMap, m_compiler);
                }
            }
            for (VoltXMLElement indexNode : subNode.children) {
                if (indexNode.name.equals("index") == false)
                    continue;
                String indexName = indexNode.attributes.get("name");
                if (indexName.startsWith(HSQLInterface.AUTO_GEN_IDX_PREFIX) == true) {
                    addIndexToCatalog(db, table, indexNode, indexReplacementMap, indexMap, columnMap, m_compiler);
                }
            }
        }
        if (subNode.name.equals("constraints")) {
            for (VoltXMLElement constraintNode : subNode.children) {
                if (constraintNode.name.equals("constraint")) {
                    addConstraintToCatalog(table, constraintNode, indexReplacementMap, indexMap);
                }
            }
        }
    }
    // Warn user if DR table don't have any unique index.
    if (isXDCR && node.attributes.get("drTable") != null && node.attributes.get("drTable").equalsIgnoreCase("ENABLE")) {
        boolean hasUniqueIndex = false;
        for (Index index : table.getIndexes()) {
            if (index.getUnique()) {
                hasUniqueIndex = true;
                break;
            }
        }
        if (!hasUniqueIndex) {
            String info = String.format("Table %s doesn't have any unique index, it will cause full table scans to update/delete DR record and may become slower as table grow.", table.getTypeName());
            m_compiler.addWarn(info);
        }
    }
    table.setSignature(CatalogUtil.getSignatureForTable(name, columnTypes));
    /*
         * Validate that each variable-length column is below the max value length,
         * and that the maximum size for the row is below the max row length.
         */
    int maxRowSize = 0;
    for (Column c : columnMap.values()) {
        VoltType t = VoltType.get((byte) c.getType());
        if (t == VoltType.STRING && (!c.getInbytes())) {
            if (c.getSize() * MAX_BYTES_PER_UTF8_CHARACTER > VoltType.MAX_VALUE_LENGTH) {
                throw m_compiler.new VoltCompilerException("Column " + name + "." + c.getName() + " specifies a maximum size of " + c.getSize() + " characters" + " but the maximum supported size is " + VoltType.humanReadableSize(VoltType.MAX_VALUE_LENGTH / MAX_BYTES_PER_UTF8_CHARACTER) + " characters or " + VoltType.humanReadableSize(VoltType.MAX_VALUE_LENGTH) + " bytes");
            }
            maxRowSize += 4 + c.getSize() * MAX_BYTES_PER_UTF8_CHARACTER;
        } else if (t.isVariableLength()) {
            if (c.getSize() > VoltType.MAX_VALUE_LENGTH) {
                throw m_compiler.new VoltCompilerException("Column " + name + "." + c.getName() + " specifies a maximum size of " + c.getSize() + " bytes" + " but the maximum supported size is " + VoltType.humanReadableSize(VoltType.MAX_VALUE_LENGTH));
            }
            maxRowSize += 4 + c.getSize();
        } else {
            maxRowSize += t.getLengthInBytesForFixedTypes();
        }
    }
    if (maxRowSize > MAX_ROW_SIZE) {
        throw m_compiler.new VoltCompilerException("Error: Table " + name + " has a maximum row size of " + maxRowSize + " but the maximum supported row size is " + MAX_ROW_SIZE);
    }
    // the DDL statement for the VIEW
    if (query != null) {
        annotation.ddl = query;
    } else {
        // Get the final DDL for the table rebuilt from the catalog object
        // Don't need a real StringBuilder or export state to get the CREATE for a table
        annotation.ddl = CatalogSchemaTools.toSchema(new StringBuilder(), table, query, isStream, streamPartitionColumn, streamTarget);
    }
}
Also used : DRTable(org.voltdb.compiler.statements.DRTable) Table(org.voltdb.catalog.Table) ReplicateTable(org.voltdb.compiler.statements.ReplicateTable) TableAnnotation(org.voltdb.compilereport.TableAnnotation) HashMap(java.util.HashMap) Index(org.voltdb.catalog.Index) VoltXMLElement(org.hsqldb_voltpatches.VoltXMLElement) TreeMap(java.util.TreeMap) Constraint(org.voltdb.catalog.Constraint) Column(org.voltdb.catalog.Column) VoltType(org.voltdb.VoltType) VoltCompilerException(org.voltdb.compiler.VoltCompiler.VoltCompilerException)

Aggregations

HashMap (java.util.HashMap)1 TreeMap (java.util.TreeMap)1 VoltXMLElement (org.hsqldb_voltpatches.VoltXMLElement)1 VoltType (org.voltdb.VoltType)1 Column (org.voltdb.catalog.Column)1 Constraint (org.voltdb.catalog.Constraint)1 Index (org.voltdb.catalog.Index)1 Table (org.voltdb.catalog.Table)1 VoltCompilerException (org.voltdb.compiler.VoltCompiler.VoltCompilerException)1 DRTable (org.voltdb.compiler.statements.DRTable)1 ReplicateTable (org.voltdb.compiler.statements.ReplicateTable)1 TableAnnotation (org.voltdb.compilereport.TableAnnotation)1