Package org.voltdb.catalog

Examples of org.voltdb.catalog.Table


            //
            List<Table> candidates = new ArrayList<Table>();
            candidates.addAll(replication_check.getReplicationCandidates());
            boolean forced_dependency = false;
            for (DesignerVertex conflict_vertex : replication_check.getConflictVertices()) {
                Table conflict_tbl = conflict_vertex.getCatalogItem();
                //
                // For each edge in the replication tree that is coming into
                // this conflict vertex,
                // see whether there is an overlapping ancestor
                //
                Map<Column, Integer> ancestors = new HashMap<Column, Integer>();
                int max_count = 0;
                Column max_column = null;
                Column max_conflict_column = null;
                for (DesignerEdge conflict_edge : rtree.getInEdges(conflict_vertex)) {
                    PredicatePairs cset = (PredicatePairs) conflict_edge.getAttribute(AccessGraph.EdgeAttributes.COLUMNSET.name());
                    for (Column conflict_column : cset.findAllForParent(Column.class, conflict_tbl)) {
                        Column ancestor_column = CollectionUtil.last(info.dependencies.getAncestors(conflict_column));
                        Integer count = ancestors.get(ancestor_column);
                        count = (count == null ? 1 : count + 1);
                        ancestors.put(ancestor_column, count);
                        if (count > max_count) {
                            max_count = count;
                            max_column = ancestor_column;
                            max_conflict_column = conflict_column;
                        }
                    } // FOR
                } // FOR
                assert (max_column != null);
                //
                // If we have a column that is used in both trees, then that's
                // the one we'll want to partition
                // our buddy on...
                //
                boolean valid = true;
                for (Column column : ancestors.keySet()) {
                    if (!max_column.equals(column) && max_count == ancestors.get(column)) {
                        valid = false;
                        break;
                    }
                } // FOR
                LOG.debug("CONFLICT - " + conflict_vertex + ": " + ancestors);
                if (valid) {
                    String child_key = CatalogKey.createKey(max_conflict_column.getParent());
                    String parent_key = CatalogKey.createKey(max_column.getParent());
                    String orig_parent_key = proc_hints.force_dependency.put(child_key, parent_key);
                    if (!parent_key.equals(orig_parent_key)) {
                        LOG.debug("Forcing dependency " + child_key + "->" + parent_key);
                        forced_dependency = true;
                    }
                }
            } // FOR
            if (forced_dependency)
                continue;

            //
            // Now for each candidate we need to check whether replicating would
            // actually improve
            // the performance of this procedure. So first we need to get a
            // baseline cost
            // of the procedure on the workload
            // TODO: Support set sizes greater than 2!!
            //
            Set<Set<Table>> candidate_sets = new LinkedHashSet<Set<Table>>();
            assert (candidates.size() <= 2);
            for (int ctr0 = 0, cnt = candidates.size(); ctr0 < cnt; ctr0++) {
                Set<Table> set = new HashSet<Table>();
                set.add(candidates.get(ctr0));
                candidate_sets.add(set);
                for (int ctr1 = ctr0 + 1; ctr1 < cnt; ctr1++) {
                    set = new HashSet<Table>();
                    set.add(candidates.get(ctr0));
                    set.add(candidates.get(ctr1));
                    candidate_sets.add(set);
                } // FOR
            } // FOR

            //
            // Calculate the cost of each replication candidate set
            // If this is the first time we are doing this for this procedure,
            // then add in a blank
            // replication set so that we can get the baseline cost
            //
            Set<Table> best_set = null;
            double best_cost = Double.MAX_VALUE;
            if (best_cost == overall_best_cost)
                candidate_sets.add(new HashSet<Table>());
            for (Set<Table> replication_set : candidate_sets) {
                cost_model.invalidateCache(candidates);

                Catalog new_catalog = CatalogCloner.cloneBaseCatalog(info.catalogContext.database.getCatalog());
                for (Table catalog_tbl : proc_tables) {
                    DesignerVertex vertex = ptree.getVertex(catalog_tbl);
                    assert (vertex != null) : "PartitionTree is missing a vertex for " + catalog_tbl + " " + ptree.getVertices();
                    Table new_catalog_tbl = CatalogCloner.clone(catalog_tbl, new_catalog);

                    //
                    // Mark the table as replicated if it's in the current set
                    //
                    if (replication_set.contains(catalog_tbl) || ptree.isReplicated(vertex)) {
                        new_catalog_tbl.setIsreplicated(true);
                        new_catalog_tbl.setPartitioncolumn(ReplicatedColumn.get(new_catalog_tbl));
                        //
                        // Otherwise partition it according to the current
                        // partition tree
                        //
                    } else {
                        Column catalog_col = (Column) vertex.getAttribute(ptree, PartitionTree.VertexAttributes.ATTRIBUTE.name());
                        assert (catalog_col != null) : "Failed to retrieve partition column for " + catalog_tbl + " " + vertex.getAttributeValues(ptree);
                        Column new_catalog_col = new_catalog_tbl.getColumns().get(catalog_col.getName());
                        assert (new_catalog_col != null);
                        new_catalog_tbl.setIsreplicated(false);
                        new_catalog_tbl.setPartitioncolumn(new_catalog_col);
                    }
                } // FOR
                Database new_catalog_db = CatalogUtil.getDatabase(new_catalog);
                CatalogCloner.cloneConstraints(info.catalogContext.database, new_catalog_db);
                CatalogContext newCatalogContext = new CatalogContext(new_catalog);
View Full Code Here


        // Starting at the current vertex, get all vertices at the next depth
        // level
        SortedSet<DesignerVertex> next = new TreeSet<DesignerVertex>(new VertexComparator(agraph, parent));
        int parent_depth = ptree.getDepth(parent);
        Table parent_table = parent.getCatalogItem();

        LOG.debug("===============================================");
        LOG.debug("Current Parent: " + parent + " " + parent.getAttributes(ptree));
        LOG.debug("Current Depth:  " + parent_depth);
        LOG.debug("Successors:     " + agraph.getSuccessors(parent));

        // Look through all the vertices that our parent is adjacent to in the
        // AccessGraph
        // and come up with a list of the next vertices to visit
        DesignerVertex parent_root = ptree.getRoot(parent);
        for (DesignerVertex vertex : agraph.getSuccessors(parent)) {
            boolean force = false;
            Table vertex_tbl = vertex.getCatalogItem();

            // Skip any self-references
            if (vertex == parent)
                continue;
            // Skip any vertices that are marked as replicated
            if (ptree.isReplicated(vertex)) {
                LOG.debug("Skipping " + vertex + " because it is already marked for replication");
                continue;
            }

            // Force dependencies
            if (hints.force_dependency.containsKey(CatalogKey.createKey(vertex_tbl))) {
                String force_dependency = hints.force_dependency.get(CatalogKey.createKey(vertex_tbl));
                // We'll force this We're allowed to break this forced
                // dependency if the current parent
                // is a descendant of the table that the child should be forced
                // to
                if (force_dependency != null) {
                    Table force_tbl = CatalogKey.getFromKey(info.catalogContext.database, force_dependency, Table.class);
                    if (parent_table.equals(force_tbl)) {
                        force = true;
                        LOG.debug("Forcing dependency: " + parent_table + "->" + vertex_tbl);
                    } else if (info.dependencies.getDescendants(force_tbl).contains(parent_table) && (ptree.containsVertex(vertex) || hints.force_replication.contains(force_dependency))) {
                        LOG.debug("Allowing take over of dependency: " + parent_table + "->" + vertex_tbl);
                    } else {
                        LOG.debug("Not allowing: " + parent_table + "->" + vertex_tbl);
                        LOG.debug(info.dependencies.getDescendants(force_tbl));
                        LOG.debug("ptree.containsVertex: " + ptree.containsVertex(vertex));
                        continue;
                    }
                }
            }

            // We then need to check whether the current parent table is a
            // descendant of
            // the other vertex in the DependencyGraph. This to check that you
            // don't violate
            // a foreign key dependency that may be several vertices removed
            List<DesignerEdge> path = info.dgraph.getPath(vertex, parent);
            if (!path.isEmpty()) {
                LOG.debug("Skipping " + vertex + " because it is an ancestor of " + parent + " in the DependencyGraph");
                continue;
            }

            // If this vertex is already in the PartitionTree, we need to check
            // whether our
            // current parent has an edge with a greater weight to the vertex
            // than the one it
            // currently has in the tree.
            //
            // What if the other vertex is a direct ascendant of the current
            // parent? That means
            // if we break the edge then the whole path will get messed up and
            // all the vertices
            // will be orphans again. Therefore, I think it should only move the
            // vertex if
            // the edge is greater *AND* it's not an ascendant of the current
            // parent.
            if (ptree.containsVertex(vertex)) {
                if (ptree.getPath(parent).contains(vertex)) {
                    LOG.debug("Skipping " + vertex + " because it is an ancestor of " + parent + " in the PartitionTree");
                    continue;
                }

                // Now look to whether there is a new Edge in the AccessGraph
                // with a greater
                // weight than the one that is currently being used in the
                // PartitionTree
                // We will only move the vertex if it's in the same tree
                DesignerVertex vertex_orig_parent = ptree.getParent(vertex);
                if (vertex_orig_parent != null) {
                    // Check whether these guys have the same root
                    // If they don't, then we won't move the child.
                    DesignerVertex child_root = ptree.getRoot(vertex);
                    if (!child_root.equals(parent_root)) {
                        LOG.debug("Skipping " + vertex + " because it's in a different partition tree (" + child_root + "<->" + parent_root + ")");
                        continue;
                    }

                    DesignerEdge orig_edge = null;
                    Double max_weight = null;
                    try {
                        orig_edge = ptree.findEdge(vertex_orig_parent, vertex);
                        // TODO: Need to think about whether it makes sense to
                        // take the total weight
                        // or whether we need to consider
                        max_weight = orig_edge.getTotalWeight();
                    } catch (Exception ex) {
                        LOG.error(vertex + " => " + vertex_orig_parent);
                        if (orig_edge != null) {
                            LOG.error(orig_edge.debug());
                        } else {
                            LOG.error("ORIG EDGE: null");
                        }
                        ex.printStackTrace();
                        System.exit(1);
                    }
                    DesignerEdge max_edge = orig_edge;
                    for (DesignerEdge candidate_edge : agraph.findEdgeSet(parent, vertex)) {
                        Double candidate_weight = (Double) candidate_edge.getAttribute(PartitionTree.EdgeAttributes.WEIGHT.name());
                        if (candidate_weight > max_weight) {
                            max_edge = candidate_edge;
                            max_weight = candidate_weight;
                        }
                    } // FOR
                      //
                      // If the edge has changed, then we need to add it to our
                      // list of next vertices to visit.
                      // What if there isn't an AttributeSet that matches up
                      // with the parent? Then
                      // we just broke up the graph for no good reason
                      //
                    if (!force && max_edge.equals(orig_edge)) {
                        LOG.debug("Skipping " + vertex + " because there was not an edge with a greater weight than what it currently has in the PartitionTree");
                        continue;
                    } else {
                        //
                        // Check whether this child was our parent before, then
                        // we need to make sure that
                        // we switch ourselves to the HASH partition method
                        // instead of MAP
                        //
                        if (ptree.getParent(parent) != null && ptree.getParent(parent).equals(vertex)) {
                            parent.setAttribute(ptree, PartitionTree.VertexAttributes.METHOD.name(), PartitionMethodType.HASH);
                        }
                        LOG.debug("Remove existing child " + vertex + " from PartitionTree");
                        ptree.removeChild(vertex);
                    }
                }
            }
            LOG.debug("Adding " + vertex + " to the list of the next nodes to visit");
            next.add(vertex);
        } // FOR

        // --------------------------------------------------------
        // STEP #2: Selecting Partitioning Attribute
        // --------------------------------------------------------

        if (!next.isEmpty()) {

            //
            // If we're the root, then we need to select our partitioning
            // attribute
            //
            if (is_root) {
                TablePartitionSets attributes = new TablePartitionSets((Table) parent.getCatalogItem());
                boolean debug = parent.getCatalogItem().getName().equals("STOCK");
                for (DesignerVertex child : next) {
                    //
                    // We now need to pick what edge from the AccessGraph to use
                    // as the dependency edge
                    // in our partition mapping
                    //
                    Table catalog_child_tbl = child.getCatalogItem();
                    LOG.debug("Looking for edge between " + parent + " and " + child + ": " + agraph.findEdgeSet(parent, child));
                    for (DesignerEdge edge : agraph.findEdgeSet(parent, child)) {
                        LOG.debug("Creating AttributeSet entry for " + edge);
                        //
                        // We only want to use edges that are used in joins.
View Full Code Here

                        LOG.debug("SimpleCountingMapper.CALLBACK -> " + element.getCatalogItem());
                        DesignerVertex parent = this.getPrevious();

                        PartitionMethodType method = (PartitionMethodType) (element.getAttribute(ptree, PartitionTree.VertexAttributes.METHOD.name()));
                        Column attribute = null;
                        Table parent_table = null;
                        Column parent_attribute = null;

                        if (method == PartitionMethodType.REPLICATION) {
                            // Anything???
                        } else {
View Full Code Here

        Database clone_db = CatalogUtil.getDatabase(clone_catalog);
        assert (!catalog_db.equals(clone_db));

        // Need to also clone the MultiColumn guys too!
        for (Table catalog_tbl : catalog_db.getTables()) {
            Table clone_tbl = clone_db.getTables().get(catalog_tbl.getName());
            for (Column catalog_col : catalog_tbl.getColumns()) {
                if (catalog_col instanceof MultiColumn) {
                    MultiColumn mc = (MultiColumn) catalog_col;
                    Column clone_cols[] = new Column[mc.size()];
                    for (int i = 0; i < clone_cols.length; i++) {
                        clone_cols[i] = clone_tbl.getColumns().get(mc.get(i).getName());
                    } // FOR

                    MultiColumn clone_mc = MultiColumn.get(clone_cols);
                    assert (clone_mc != null);
                }
            }
            assert (catalog_tbl.getColumns().size() == clone_tbl.getColumns().size()) : catalog_tbl.getColumns() + " != " + clone_tbl.getColumns();
        } // FOR

        // And don't forget MultiProcParameter!
        for (Procedure catalog_proc : catalog_db.getProcedures()) {
            Procedure clone_proc = clone_db.getProcedures().get(catalog_proc.getName());
View Full Code Here

        // SPECIAL HANDLING

        // Table
        if (src_item instanceof Table) {
            Table src_tbl = (Table) src_item;
            Table dest_tbl = (Table) clone;

            // Columns
            for (Column src_col : src_tbl.getColumns()) {
                if (!(src_col instanceof MultiColumn))
                    CatalogCloner.clone(src_col, dest_catalog);
            } // FOR
            // Indexes
            for (Index src_idx : src_tbl.getIndexes()) {
                CatalogCloner.clone(src_idx, dest_catalog);
            } // FOR
            // MaterializedViews
            for (MaterializedViewInfo src_view : src_tbl.getViews()) {
                CatalogCloner.clone(src_view, dest_catalog);
            } // FOR

            // // Constraints
            // for (Constraint src_cons : ((Table)src).getConstraints()) {
            // CatalogUtil.clone(src_cons, dest_catalog);
            // } // FOR

            // Partitioning Column
            if (src_tbl.getPartitioncolumn() != null) {
                Column src_part_col = src_tbl.getPartitioncolumn();
                Column dest_part_col = null;

                // Special Case: Replicated Column Marker
                if (src_part_col instanceof ReplicatedColumn) {
                    dest_part_col = ReplicatedColumn.get(dest_tbl);
                // Special Case: MultiColumn
                } else if (src_part_col instanceof MultiColumn) {
                    MultiColumn mc = (MultiColumn) src_part_col;
                    Column dest_cols[] = new Column[mc.size()];
                    for (int i = 0; i < dest_cols.length; i++) {
                        dest_cols[i] = dest_tbl.getColumns().get(mc.get(i).getName());
                    } // FOR
                    dest_part_col = MultiColumn.get(dest_cols);

                } else {
                    dest_part_col = dest_tbl.getColumns().get(src_part_col.getName());
                }
                assert (dest_part_col != null) : "Missing partitioning column " + CatalogUtil.getDisplayName(src_part_col);
                dest_tbl.setPartitioncolumn(dest_part_col);
            }
        // MaterializedViewInfo
        } else if (src_item instanceof MaterializedViewInfo) {
            // ColumnRefs
            MaterializedViewInfo src_view = (MaterializedViewInfo) src_item;
            MaterializedViewInfo dest_view = (MaterializedViewInfo) clone;
            updateColumnsRefs((Table) src_view.getParent(), src_view.getGroupbycols(), (Table) dest_view.getParent(), dest_view.getGroupbycols());

        // Index
        } else if (src_item instanceof Index) {
            // ColumnRefs
            Index src_idx = (Index) src_item;
            Index dest_idx = (Index) clone;
            updateColumnsRefs((Table) src_idx.getParent(), src_idx.getColumns(), (Table) dest_idx.getParent(), dest_idx.getColumns());

        // Constraint
        } else if (src_item instanceof Constraint) {
            // ColumnRefs
            Constraint src_cons = (Constraint) src_item;
            Constraint dest_cons = (Constraint) clone;

            Table src_fkey_tbl = src_cons.getForeignkeytable();
            if (src_fkey_tbl != null) {
                Database dest_db = (Database) dest_cons.getParent().getParent();
                Table dest_fkey_tbl = dest_db.getTables().get(src_fkey_tbl.getName());
                if (dest_fkey_tbl != null) {
                    dest_cons.setForeignkeytable(dest_fkey_tbl);
                    for (ColumnRef src_cref : ((Constraint) src_item).getForeignkeycols()) {
                        CatalogCloner.clone(src_cref, dest_catalog);

                        // Correct what it's pointing to
                        ColumnRef dest_colref = dest_cons.getForeignkeycols().get(src_cref.getName());
                        assert (dest_colref != null);
                        dest_colref.setColumn(dest_fkey_tbl.getColumns().get(src_cref.getColumn().getName()));
                    } // FOR
                }
            }

            // Important: We have to add ConstraintRefs to Columns *after* we add the columns
            Table src_tbl = (Table) src_cons.getParent();
            Table dest_tbl = (Table) dest_cons.getParent();
            for (Column src_col : src_tbl.getColumns()) {
                Column dest_col = dest_tbl.getColumns().get(src_col.getName());
                assert (dest_col != null);
                for (ConstraintRef src_conref : src_col.getConstraints()) {
                    if (!src_conref.getConstraint().equals(src_cons))
                        continue;
                    CatalogCloner.clone(src_conref, dest_catalog);

                    // Correct what it's pointing to
                    ConstraintRef dest_conref = dest_col.getConstraints().get(src_conref.getName());
                    assert (dest_conref != null);
                    // System.out.println("dest_tbl: " + dest_tbl);
                    // System.out.println("dest_tbl.getConstraints(): " +
                    // CatalogUtil.debug(dest_tbl.getConstraints()));
                    // System.out.println("src_confref: " + src_conref);
                    // System.out.println("src_confref.getConstraint(): " +
                    // src_conref.getConstraint());
                    dest_conref.setConstraint(dest_tbl.getConstraints().get(src_conref.getConstraint().getName()));
                } // FOR
            } // FOR

            Index src_index = src_cons.getIndex();
            if (src_index != null) {
                Index dest_index = dest_tbl.getIndexes().get(src_index.getName());
                dest_cons.setIndex(dest_index);
            }

        // StmtParameter
        } else if (src_item instanceof StmtParameter) {
View Full Code Here

     * @param dest_db
     */
    public static void cloneConstraints(Database src_db, Database dest_db) {
        Catalog dest_catalog = dest_db.getCatalog();
        for (Table src_tbl : src_db.getTables()) {
            Table dest_tbl = dest_db.getTables().get(src_tbl.getName());
            if (dest_tbl != null) {
                for (Constraint src_cons : src_tbl.getConstraints()) {
                    // Only clone FKEY constraint if the other table is in the catalog
                    ConstraintType cons_type = ConstraintType.get(src_cons.getType());
                    if (cons_type != ConstraintType.FOREIGN_KEY || (cons_type == ConstraintType.FOREIGN_KEY && dest_db.getTables().get(src_cons.getForeignkeytable().getName()) != null)) {
View Full Code Here

            public int getColumnCount() { return (this.columns.length); }
            public int getRowCount() { return (tables.size()); }
            public Object getValueAt(int row, int col) {
                String ret = null;
               
                Table catalog_tbl = tables.get(row);
                TableEntry entry = plan.getTableEntries().get(catalog_tbl);
                switch (col) {
                    case 0:
                        ret = catalog_tbl.getName();
                        break;
                    case 1:
                        ret = entry.getMethod().toString();
                        break;
                    case 2:
                        ret = (entry.getAttribute() != null ? entry.getAttribute().getName() : "-");
                        break;
                    case 3:
                        ret = (entry.getParent() != null ? entry.getParent().getName() : "-");
                        break;
                    case 4:
                        ret = (entry.getParentAttribute() != null ? entry.getParentAttribute().getName() : "-");
                        break;
                } // SWITCH
                return (ret);
            }
            public boolean isCellEditable(int row, int col) {
                return (false);
            }
            public Class<?> getColumnClass(int c) {
                return getValueAt(0, c).getClass();
            }
        });
       
        partitonTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        partitonTable.setFillsViewportHeight(false);
        partitonTable.setDragEnabled(false);
        partitonTable.setColumnSelectionAllowed(false);
        partitonTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
       
        //
        // Select the vertex in the graph when they select its corresponding row
        //
        partitonTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                Table catalog_tbl = tables.get(partitonTable.getSelectedRow());
                GraphVisualizationPanel<DesignerVertex, DesignerEdge> visualizer = PartitionPlanPanel.this.parent.getCurrentVisualizer();
                IGraph<DesignerVertex, DesignerEdge> graph = (IGraph<DesignerVertex, DesignerEdge>)visualizer.getGraph();
                DesignerVertex vertex = graph.getVertex(catalog_tbl);
                visualizer.selectVertex(vertex);
                return;
View Full Code Here

        new ExpressionTreeWalker() {
            @Override
            protected void callback(AbstractExpression element) {
                if (element instanceof TupleValueExpression) {
                    String table_name = ((TupleValueExpression) element).getTableName();
                    Table catalog_tbl = catalog_db.getTables().get(table_name);
                    if (catalog_tbl == null) {
                        // If it's a temp then we just ignore it. Otherwise
                        // throw an error!
                        if (table_name.contains(PlanAssembler.AGGREGATE_TEMP_TABLE) == false) {
                            this.stop();
                            throw new RuntimeException(String.format("Unknown table '%s' referenced in Expression node %s", table_name, element));
                        } else if (debug.val) {
                            LOG.debug("Ignoring temporary table '" + table_name + "'");
                        }
                        return;
                    }

                    String column_name = ((TupleValueExpression) element).getColumnName();
                    Column catalog_col = catalog_tbl.getColumns().get(column_name);
                    if (catalog_col == null) {
                        this.stop();
                        throw new RuntimeException(String.format("Unknown column '%s.%s' referenced in Expression node %s", table_name, column_name, element));
                    }
                    found_columns.add(catalog_col);
View Full Code Here

     * @param exp
     */
    public static Collection<Table> getReferencedTables(final Database catalog_db, AbstractExpression exp) {
        Set<Table> found_tables = new HashSet<Table>();
        for (Column catalog_col : ExpressionUtil.getReferencedColumns(catalog_db, exp)) {
            Table catalog_tbl = catalog_col.getParent();
            found_tables.add(catalog_tbl);
        } // FOR
        return (found_tables);
    }
View Full Code Here

                                    LOG.trace("entry.getFirst().getParent(): " + (pair.getFirst().getParent() != null ?
                                                    pair.getFirst().getParent().hashCode() :
                                                    pair.getFirst() + " parent is null?"));
                           
                                    if (pair.getFirst().getParent() instanceof Table) {
                                        Table parent = pair.getFirst().getParent();
                                        if (parent.getName().equals(catalog_tbl.getName())) {
                                            assert(parent.equals(catalog_tbl)) :
                                                "Mismatch on " + parent.getName() + "???";
                                        }
                                    }
                                } else {
                                    LOG.trace("entry.getFirst():             " + null);
                                }
View Full Code Here

TOP

Related Classes of org.voltdb.catalog.Table

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.