Class PrecomputedTableManager


  • public class PrecomputedTableManager
    extends java.lang.Object
    Manages all the Precomputed tables in a given database.
    Author:
    Andrew Varley, Matthew Wakeling
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected PrecomputedTableManager​(java.sql.Connection conn)
      Create a PrecomputedTableManager for a given Connection.
      protected PrecomputedTableManager​(Database database)
      Create a PrecomputedTableManager for the given underlying database.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(PrecomputedTable pt)
      Add a precomputed table to the underlying database.
      void add​(PrecomputedTable pt, java.util.Collection<java.lang.String> indexes)
      Add a precomputed table to the underlying database.
      protected void addIndex​(java.lang.String table, java.lang.String field, java.sql.Connection con)
      Adds an index to the given table on the given field.
      void addTableToDatabase​(PrecomputedTable pt, java.util.Collection<java.lang.String> indexes, boolean record)
      Add a PrecomputedTable to the database.
      protected static java.util.Set<java.lang.String> canonicaliseIndexes​(java.util.Collection<java.lang.String> indexes)
      Takes a collection of index strings, and removes redundant entries.
      void delete​(PrecomputedTable pt)
      Delete a precomputed table from the underlying database.
      void deleteTableFromDatabase​(java.lang.String name)
      Delete a PrecomputedTable from the database.
      void dropAffected​(java.util.Set<java.lang.String> tablesAltered)
      Deletes all precomputed tables that would be affected by changes in any table in a given list of table names.
      void dropEverything()
      Deletes every single precomputed table.
      static PrecomputedTableManager getInstance​(java.sql.Connection conn)
      Gets a PrecomputedTableManager instance for the given underlying Connection.
      static PrecomputedTableManager getInstance​(Database database)
      Gets a PrecomputedTableManager instance for the given underlying database.
      java.util.Set<PrecomputedTable> getPrecomputedTables()
      Get all the precomputed tables in the underlying database.
      java.util.Map<java.lang.String,​PrecomputedTable> lookupCategory​(java.lang.String category)
      Returns a Map from original SQL to PrecomputedTable for a given category in the manager.
      PrecomputedTable lookupSql​(java.lang.String sql)
      Returns a PrecomputedTable object if one exists in the manager with the given original SQL string.
      PrecomputedTable lookupSql​(java.lang.String category, java.lang.String sql)
      Returns a PrecomputedTable object if one exists in the manager with the given category and original SQL string.
      protected void setupDatabase​(java.sql.Connection con)
      Sets up the database for storing precomputed tables
      protected void synchroniseWithDatabase​(java.sql.Connection con)
      Synchronise with the underlying database
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • precomputedTables

        protected java.util.TreeSet<PrecomputedTable> precomputedTables
      • types

        protected java.util.Map<java.lang.String,​java.util.Map<java.lang.String,​PrecomputedTable>> types
      • conn

        protected java.sql.Connection conn
    • Constructor Detail

      • PrecomputedTableManager

        protected PrecomputedTableManager​(java.sql.Connection conn)
                                   throws java.sql.SQLException
        Create a PrecomputedTableManager for a given Connection.
        Parameters:
        conn - the underlying database connection
        Throws:
        java.sql.SQLException - if an error occurs in the underlying database
      • PrecomputedTableManager

        protected PrecomputedTableManager​(Database database)
                                   throws java.sql.SQLException
        Create a PrecomputedTableManager for the given underlying database.
        Parameters:
        database - the underlying database
        Throws:
        java.sql.SQLException - if an error occurs in the underlying database
    • Method Detail

      • getInstance

        public static PrecomputedTableManager getInstance​(java.sql.Connection conn)
                                                   throws java.sql.SQLException
        Gets a PrecomputedTableManager instance for the given underlying Connection.
        Parameters:
        conn - the underlying database connection
        Returns:
        the PrecomputedTableManager for this database connection
        Throws:
        java.lang.IllegalArgumentException - if connection is invalid
        java.sql.SQLException - if an error occurs in the underlying database
      • getInstance

        public static PrecomputedTableManager getInstance​(Database database)
                                                   throws java.sql.SQLException
        Gets a PrecomputedTableManager instance for the given underlying database.
        Parameters:
        database - the underlying database
        Returns:
        the PrecomputedTableManager for this database
        Throws:
        java.lang.IllegalArgumentException - if database is invalid
        java.sql.SQLException - if an error occurs in the underlying database
      • add

        public void add​(PrecomputedTable pt)
                 throws java.sql.SQLException
        Add a precomputed table to the underlying database.
        Parameters:
        pt - the PrecomputedTable to add
        Throws:
        java.sql.SQLException - if an error occurs in the underlying database
        java.lang.NullPointerException - if pt is null
      • add

        public void add​(PrecomputedTable pt,
                        java.util.Collection<java.lang.String> indexes)
                 throws java.sql.SQLException
        Add a precomputed table to the underlying database.
        Parameters:
        pt - the PrecomputedTable to add
        indexes - the extra fields to index - a Collection of Strings. Each String can be a comma-separated list of fields that will be indexed as a multi-column index. The field names should be names of columns in the precomputed table - so they are the aliases specified in the PrecomputedTable
        Throws:
        java.sql.SQLException - if an error occurs in the underlying database
        java.lang.NullPointerException - if pt is null
        java.lang.IllegalArgumentException - if the precomputed table already exists
      • dropEverything

        public void dropEverything()
                            throws java.sql.SQLException
        Deletes every single precomputed table. Use this when you have made a change to the database.
        Throws:
        java.sql.SQLException - if something goes wrong
      • dropAffected

        public void dropAffected​(java.util.Set<java.lang.String> tablesAltered)
                          throws java.sql.SQLException
        Deletes all precomputed tables that would be affected by changes in any table in a given list of table names.
        Parameters:
        tablesAltered - a Set of table names that may have alterations
        Throws:
        java.sql.SQLException - if something goes wrong
      • delete

        public void delete​(PrecomputedTable pt)
                    throws java.sql.SQLException
        Delete a precomputed table from the underlying database.
        Parameters:
        pt - the PrecomputedTable to delete
        Throws:
        java.sql.SQLException - if an error occurs in the underlying database
        java.lang.NullPointerException - if pt is null
        java.lang.IllegalArgumentException - if pt is not a valid table
      • getPrecomputedTables

        public java.util.Set<PrecomputedTable> getPrecomputedTables()
        Get all the precomputed tables in the underlying database.
        Returns:
        a Set of PrecomputedTables present in the database
      • addTableToDatabase

        public void addTableToDatabase​(PrecomputedTable pt,
                                       java.util.Collection<java.lang.String> indexes,
                                       boolean record)
                                throws java.sql.SQLException
        Add a PrecomputedTable to the database.
        Parameters:
        pt - the PrecomputedTable to add
        indexes - a Collection of Strings that are indexes to create
        record - whether to record this table in the index
        Throws:
        java.sql.SQLException - if an error occurs in the underlying database
      • canonicaliseIndexes

        protected static java.util.Set<java.lang.String> canonicaliseIndexes​(java.util.Collection<java.lang.String> indexes)
        Takes a collection of index strings, and removes redundant entries. An index string is a list of SQL column names, separated by ", ", where the leftmost column is the most significant in the btree order.
        Parameters:
        indexes - the Collection of index strings
        Returns:
        a new Set of index strings
      • deleteTableFromDatabase

        public void deleteTableFromDatabase​(java.lang.String name)
                                     throws java.sql.SQLException
        Delete a PrecomputedTable from the database.
        Parameters:
        name - the name of the PrecomputedTable to delete
        Throws:
        java.sql.SQLException - if an error occurs in the underlying database
      • addIndex

        protected void addIndex​(java.lang.String table,
                                java.lang.String field,
                                java.sql.Connection con)
        Adds an index to the given table on the given field.
        Parameters:
        table - the name of the table
        field - the name of the field
        con - a Connection to use
      • synchroniseWithDatabase

        protected void synchroniseWithDatabase​(java.sql.Connection con)
                                        throws java.sql.SQLException
        Synchronise with the underlying database
        Parameters:
        con - a Connection to the database we are synchronising with
        Throws:
        java.sql.SQLException - if there is a problem in the underlying database
      • setupDatabase

        protected void setupDatabase​(java.sql.Connection con)
                              throws java.sql.SQLException
        Sets up the database for storing precomputed tables
        Parameters:
        con - the Connection to use
        Throws:
        java.sql.SQLException - if there is a problem in the underlying database
      • lookupSql

        public PrecomputedTable lookupSql​(java.lang.String category,
                                          java.lang.String sql)
        Returns a PrecomputedTable object if one exists in the manager with the given category and original SQL string.
        Parameters:
        category - a String
        sql - the original SQL string used to create the PrecomputedTable
        Returns:
        a PrecomputedTable or null
      • lookupSql

        public PrecomputedTable lookupSql​(java.lang.String sql)
        Returns a PrecomputedTable object if one exists in the manager with the given original SQL string.
        Parameters:
        sql - the original SQL string used to create the PrecomputedTable
        Returns:
        a PrecomputedTable or null
      • lookupCategory

        public java.util.Map<java.lang.String,​PrecomputedTable> lookupCategory​(java.lang.String category)
        Returns a Map from original SQL to PrecomputedTable for a given category in the manager.
        Parameters:
        category - a String
        Returns:
        a Map