aggtablemanager.java

来自「数据仓库展示程序」· Java 代码 · 共 496 行 · 第 1/2 页

JAVA
496
字号
/*
// $Id: //open/mondrian/src/main/mondrian/rolap/aggmatcher/AggTableManager.java#12 $
// This software is subject to the terms of the Common Public License
// Agreement, available at the following URL:
// http://www.opensource.org/licenses/cpl.html.
// Copyright (C) 2005-2005 Kana Software, Inc. and others.
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
*/

package mondrian.rolap.aggmatcher;

import mondrian.olap.*;
import mondrian.rolap.*;
import mondrian.recorder.*;
import mondrian.resource.MondrianResource;

import org.apache.log4j.Logger;
import org.eigenbase.util.property.*;
import org.eigenbase.util.property.Property;

import javax.sql.DataSource;
import java.util.*;
import java.sql.SQLException;

/**
 * Manages aggregate tables.
 *
 * <p>It is used as follows:<ul>
 * <li>A {@link mondrian.rolap.RolapSchema} creates an {@link AggTableManager},
 *     and stores it in a member variable to ensure that it is not
 *     garbage-collected.
 * <li>The {@link AggTableManager} creates and registers
 *     {@link org.eigenbase.util.property.Trigger} objects, so that it is notified
 *     when properties pertinent to aggregate tables change.
 * <li>The {@link mondrian.rolap.RolapSchema} calls {@link #initialize()},
 *     which scans the JDBC catalog and identifies aggregate tables.
 * <li>For each aggregate table, it creates an {@link AggStar} and calls
 *     {@link RolapStar#addAggStar(AggStar)}.
 *
 * @author <a>Richard M. Emberson</a>
 * @version
 */
public class AggTableManager {
    private static final Logger LOGGER =
            Logger.getLogger(AggTableManager.class);

    private final RolapSchema schema;

    private static final MondrianResource mres = MondrianResource.instance();

    /**
     * This is used to create forward references to triggers (so they do not
     * get reaped until the RolapSchema is reaped).
     */
    private Trigger[] triggers;

    public AggTableManager(final RolapSchema schema) {
        this.schema = schema;
    }

    /**
     * Get the Logger.
     *
     * @return
     */
    public Logger getLogger() {
        return LOGGER;
    }

    /**
     * Initializes this object, loading all aggregate tables and associating
     * them with {@link RolapStar}s.
     * This method should only be called once.
     */
    public void initialize() {
        if (MondrianProperties.instance().ReadAggregates.get()) {
            try {
                loadRolapStarAggregates();

            } catch (SQLException ex) {
                throw mres.AggLoadingError.ex(ex);
            }
        }
        registerTriggers();
        printResults();
    }
    private void printResults() {
/*
 *   This was too much information at the INFO level, compared to the
 *   rest of Mondrian
 *
 *         if (getLogger().isInfoEnabled()) {
            // print just Star table alias and AggStar table names
            StringBuffer buf = new StringBuffer(1024);
            buf.append(Util.nl);
            for (Iterator it = getStars(); it.hasNext(); ) {
                RolapStar star = (RolapStar) it.next();
                buf.append(star.getFactTable().getAlias());
                buf.append(Util.nl);
                for (Iterator ait = star.getAggStars(); ait.hasNext(); ) {
                    AggStar aggStar = (AggStar) ait.next();
                    buf.append("    ");
                    buf.append(aggStar.getFactTable().getName());
                    buf.append(Util.nl);
                }
            }
            getLogger().info(buf.toString());

        } else
*/
    	if (getLogger().isDebugEnabled()) {
            // print everything, Star, subTables, AggStar and subTables
            // could be a lot
            StringBuffer buf = new StringBuffer(4096);
            buf.append(Util.nl);
            for (Iterator it = getStars(); it.hasNext(); ) {
                RolapStar star = (RolapStar) it.next();
                buf.append(star.toString());
                buf.append(Util.nl);
            }
            getLogger().debug(buf.toString());
        }
    }
    private void reLoadRolapStarAggregates() {
        if (MondrianProperties.instance().ReadAggregates.get()) {
            try {
                clearJdbcSchema();
                loadRolapStarAggregates();

            } catch (SQLException ex) {
                throw mres.AggLoadingError.ex(ex);
            }
        }
    }

    /**
     * Clear the possibly already loaded snapshot of what is in the database.
     */
    private void clearJdbcSchema() {
        DataSource dataSource = schema.getInternalConnection().getDataSource();
        JdbcSchema.clearDB(dataSource);
    }
    private JdbcSchema getJdbcSchema() {
        DataSource dataSource = schema.getInternalConnection().getDataSource();

        // This actually just does a lookup or simple constructor invocation,
        // its not expected to fail
        JdbcSchema db = JdbcSchema.makeDB(dataSource);

        return db;
    }

    /**
     * This method loads and/or reloads the aggregate tables.
     * <p>
     * NOTE: At this point all RolapStars have been made for this
     * schema (except for dynamically added cubes which I am going
     * to ignore for right now). So, All stars have their columns
     * and their BitKeys can be generated.
     *
     * @throws SQLException
     */
    private void loadRolapStarAggregates() throws SQLException {
        ListRecorder msgRecorder = new ListRecorder();
        try {

        DefaultRules rules = DefaultRules.getInstance();
        JdbcSchema db = getJdbcSchema();
        // loads tables, not their columns
        db.load();

        loop:
        for (Iterator it = getStars(); it.hasNext(); ) {
            RolapStar star = (RolapStar) it.next();
            // This removes any AggStars from any previous invocation of this
            // method (if any)
            star.prepareToLoadAggregates();

            List aggGroups = getAggGroups(star);
            for (Iterator git = aggGroups.iterator(); git.hasNext(); ) {
                ExplicitRules.Group group = (ExplicitRules.Group) git.next();
                group.validate(msgRecorder);
            }


            String factTableName = star.getFactTable().getAlias();

            JdbcSchema.Table dbFactTable = db.getTable(factTableName);
            if (dbFactTable == null) {
                msgRecorder.reportWarning("No Table found for fact name="
                    +factTableName);

                continue loop;
            }

            // For each column in the dbFactTable, figure out it they are
            // measure or foreign key columns
            bindToStar(dbFactTable, star, msgRecorder);
            String schema = dbFactTable.table.schema;

            // Now look at all tables in the database and per table, first see
            // if it is a match for an aggregate table for this fact table and
            // second see if its columns match foreign key and level columns.
            for (Iterator tit = db.getTables(); tit.hasNext(); ) {
                JdbcSchema.Table dbTable = (JdbcSchema.Table) tit.next();
                String name = dbTable.getName();

                // Do the catalog schema aggregate excludes, exclude this
                // table name.
                if (ExplicitRules.excludeTable(name, aggGroups)) {
                    continue;
                }

                //
                // First see if there is an ExplicitRules match. If so, then if all
                // of the columns match up, then make an AggStar.
                // On the other hand, if there is no ExplicitRules match, see if
                // there is a Default match. If so and if all the columns
                // match up, then also make an AggStar.
                //
                ExplicitRules.TableDef tableDef =
                    ExplicitRules.getIncludeByTableDef(name, aggGroups);

                boolean makeAggStar = false;
                // Is it handled by the ExplicitRules
                if (tableDef != null) {
                    // load columns
                    dbTable.load();
                    makeAggStar = tableDef.columnsOK(star,
                                    dbFactTable,
                                    dbTable,
                                    msgRecorder);
                }
                if (! makeAggStar) {
                    // Is it handled by the DefaultRules
                    if (rules.matchesTableName(factTableName, name)) {
                        // load columns
                        dbTable.load();
                        makeAggStar = rules.columnsOK(star,
                                            dbFactTable,
                                            dbTable,
                                            msgRecorder);
                    }
                }


                if (makeAggStar) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?