Class DataTable

  • All Implemented Interfaces:
    RootTable, TableDataSource

    public final class DataTable
    extends DefaultDataTable
    DataTable is a wrapper for a MutableTableDataSource that fits into the query hierarchy level. A DataTable represents a table within a transaction. Adding, removing rows to a DataTable will change the contents only with the context of the transaction the table was created in.

    • Field Detail

      • connection

        private DatabaseConnection connection
        The DatabaseConnection object that is the parent of this DataTable.
      • data_source

        private MutableTableDataSource data_source
        A low level access to the underlying transactional data source.
      • LOCK_DEBUG

        static final boolean LOCK_DEBUG
        ------ NOTE: Following values are only kept for lock debugging reasons. These is no technical reason why they shouldn't be removed. They allow us to check that a data table is locked correctly when accesses are performed on it. ------
        See Also:
        Constant Field Values
      • debug_read_lock_count

        private int debug_read_lock_count
        The number of read locks we have on this table.
      • debug_write_lock_count

        private int debug_write_lock_count
        The number of write locks we have on this table (this should only ever be 0 or 1).
    • Method Detail

      • Debug

        public final DebugLogger Debug()
        Convenience - used to log debug messages.
        Overrides:
        Debug in class Table
      • blankSelectableSchemes

        protected void blankSelectableSchemes​(int type)
        Overwritten from DefaultDataTable to do nothing. All selectable schemes are handled within the DataTableManager now.
        Overrides:
        blankSelectableSchemes in class DefaultDataTable
      • getRootColumnScheme

        protected SelectableScheme getRootColumnScheme​(int column)
        Returns the SelectableScheme for the given column. (Overridden from DefaultDataTable). If the schemes are not in memory then they are loaded now. This will synchronize over the 'table_manager' which will effectively block this table at the lowest layer until the indices are loaded into memory.
        Overrides:
        getRootColumnScheme in class DefaultDataTable
      • declareAs

        public ReferenceTable declareAs​(TableName new_name)
        We can declare a DataTable as a new type. This means, instead of referencing a column as 'Customer.CustomerID' we can change the 'Customer' part to anything we wish such as 'C1'.
      • createRowDataObject

        public final RowData createRowDataObject​(QueryContext context)
        Generates an empty RowData object for 'addRow'ing into the Table. We must first call this method to retrieve a blank RowData object, fill it in with the required information, and then call 'addRow'
      • add

        public final void add​(RowData row_data)
                       throws DatabaseException
        Adds a given 'RowData' object to the table. This should be used for any rows added to the table. The order that rows are added into a table is not important.

        This method performs some checking of the cells in the table. It first checks that all columns declared as 'not null' have a value that is not null. It then checks that a the added row will not cause any duplicates in a column declared as unique.

        It then uses the low level io manager to store the data.

        SYNCHRONIZATION ISSUE: We are assuming this is running in a synchronized environment that is unable to add or alter rows in this object within the lifetime of this method.

        Throws:
        DatabaseException
      • add

        public final void add​(RowData[] row_data_arr)
                       throws DatabaseException
        Adds an array of 'RowData' objects to the table. This should be used for adding a group of rows to the table. The order that rows are added into a table is not important.

        This method performs some checking of the cells in the table. It first checks that all columns declared as 'not null' have a value that is not null. It then checks that a the added row will not cause any duplicates in a column declared as unique.

        It then uses the low level io manager to store the data.

        SYNCHRONIZATION ISSUE: We are assuming this is running in a synchronized environment that is unable to add or alter rows in this object within the lifetime of this method.

        Throws:
        DatabaseException
      • addRow

        private void addRow​(RowData row)
                     throws DatabaseException
        Adds a new row of data to the table. First of all, this tells the underlying database mechanism to add the data to this table. It then add the row information to each SelectableScheme.
        Throws:
        DatabaseException
      • removeRow

        private void removeRow​(int row_number)
                        throws DatabaseException
        Removes the given row from the table. This is called just before the row is actually deleted. The method is provided to allow for some maintenance of any search structures such as B-Trees. This is called from the 'delete' method in Table.
        Throws:
        DatabaseException
      • updateRow

        private void updateRow​(int row_number,
                               RowData row)
                        throws DatabaseException
        Updates the given row with the given data in this table. This method will likely add the modified data to a new row and delete the old version of the row.
        Throws:
        DatabaseException
      • delete

        public int delete​(Table table,
                          int limit)
                   throws DatabaseException
        This is the public method for removing a given result set from this table. Given a Table object, this will remove from this table any row that are in the given table. The given Table must have this object as its distant ancestor. If it does not then it will throw an exception. Examples: table.delete(table) -- delete the entire table. table.delete(table.select( < some condition > )); It returns the number of rows that were deleted.

        INTERNAL NOTE: The 'table' parameter may be the result of joins. This may cause the same row in this table to be referenced more than once. We must make sure that we delete any given row only once by using the 'distinct' function.

        'limit' dictates how many rows will be deleted. If 'limit' is less than 0 then this indicates there is no limit. Keep in mind that rows are picked out from top to bottom in the 'table' object. Normally the input table will be the result of an un-ordered 'where' clause so using a limit does not permit deletes in a deterministic manner.

        ASSUMPTION: There are no duplicate rows in the input set.

        Throws:
        DatabaseException
      • update

        public final int update​(QueryContext context,
                                Table table,
                                Assignment[] assign_list,
                                int limit)
                         throws DatabaseException
        Updates the table by applying the assignment operations over each row that is found in the input 'table' set. The input table must be a direct child of this DataTable.

        This operation assumes that there is a WRITE lock on this table. A WRITE lock means no other thread may access this table while the operation is being performed. (However, a result set may still be downloading from this table).

        'limit' dictates how many rows will be updated. If 'limit' is less than 0 then this indicates there is no limit. Keep in mind that rows are picked out from top to bottom in the 'table' object. Normally the input table will be the result of an un-ordered 'where' clause so using a limit does not permit updates in a deterministic manner.

        Returns the number of rows updated in this table.

        NOTE: We assume there are no duplicate rows to the root set from the given 'table'.

        Throws:
        DatabaseException
      • getDataTableDef

        public DataTableDef getDataTableDef()
        Returns the DataTableDef object for this table. This object describes how the table is made up.

        NOTE: Do not keep references to this object. The DataTableDef is invalidated when a table is closed.

        Specified by:
        getDataTableDef in interface TableDataSource
        Specified by:
        getDataTableDef in class Table
      • getSchema

        public java.lang.String getSchema()
        Returns the schema that this table is within.
      • addDataTableListener

        public void addDataTableListener​(DataTableListener listener)
        Adds a DataTableListener to the DataTable objects at the root of this table tree hierarchy. If this table represents the join of a number of tables then the DataTableListener is added to all the DataTable objects at the root.

        A DataTableListener is notified of all modifications to the raw entries of the table. This listener can be used for detecting changes in VIEWs, for triggers or for caching of common queries.

        Specified by:
        addDataTableListener in class Table
      • removeDataTableListener

        public void removeDataTableListener​(DataTableListener listener)
        Removes a DataTableListener from the DataTable objects at the root of this table tree hierarchy. If this table represents the join of a number of tables, then the DataTableListener is removed from all the DataTable objects at the root.
        Specified by:
        removeDataTableListener in class Table
      • setToRowTableDomain

        void setToRowTableDomain​(int column,
                                 IntegerVector row_set,
                                 TableDataSource ancestor)
        Given a set, this trickles down through the Table hierarchy resolving the given row_set to a form that the given ancestor understands. Say you give the set { 0, 1, 2, 3, 4, 5, 6 }, this function may check down three levels and return a new 7 element set with the rows fully resolved to the given ancestors domain.
        Overrides:
        setToRowTableDomain in class DefaultDataTable
      • getCellContents

        public TObject getCellContents​(int column,
                                       int row)
        Returns an object that represents the information in the given cell in the table. This can be used to obtain information about the given table cells.
        Specified by:
        getCellContents in interface TableDataSource
        Specified by:
        getCellContents in class Table
      • lockRoot

        public void lockRoot​(int lock_key)
        Locks the root table(s) of this table so that it is impossible to overwrite the underlying rows that may appear in this table. This is used when cells in the table need to be accessed 'outside' the lock. So we may have late access to cells in the table. 'lock_key' is a given key that will also unlock the root table(s).

        NOTE: This is nothing to do with the 'LockingMechanism' object.

        Specified by:
        lockRoot in class Table
      • unlockRoot

        public void unlockRoot​(int lock_key)
        Unlocks the root tables so that the underlying rows may once again be used if they are not locked and have been removed. This should be called some time after the rows have been locked.
        Specified by:
        unlockRoot in class Table
      • hasRootsLocked

        public boolean hasRootsLocked()
        Returns true if the table has its row roots locked (via the lockRoot(int) method.
        Specified by:
        hasRootsLocked in class Table
      • notifyAddRWLock

        final void notifyAddRWLock​(int lock_type)
        This is called by the 'Lock' class to notify this DataTable that a read/ write lock has been applied to this table. This is for lock debugging purposes only.
      • notifyReleaseRWLock

        final void notifyReleaseRWLock​(int lock_type)
        This is called by the 'Lock' class to notify this DataTable that a read/ write lock has been released from this table. This is for lock debugging purposes only.
      • isInExclusiveMode

        private boolean isInExclusiveMode()
        Returns true if the database is in exclusive mode.
      • checkInExclusiveMode

        private void checkInExclusiveMode()
        Checks the database is in exclusive mode.
      • checkReadLock

        private void checkReadLock()
        Check that we can safely read from this table.
      • checkReadWriteLock

        private void checkReadWriteLock()
        Check that we can safely read/write from this table. This should catch any synchronization concurrent issues.
      • checkSafeOperation

        private void checkSafeOperation()
        Check that we can run a safe operation.
      • getResolvedVariable

        public Variable getResolvedVariable​(int column)
        Description copied from class: DefaultDataTable
        Returns a fully qualified Variable object that represents the name of the column at the given index. For example, new Variable(new TableName("APP", "CUSTOMER"), "ID")
        Overrides:
        getResolvedVariable in class DefaultDataTable
      • findFieldName

        public int findFieldName​(Variable v)
        Description copied from class: DefaultDataTable
        Given a fully qualified variable field name, ie. 'APP.CUSTOMER.CUSTOMERID' this will return the column number the field is at. Returns -1 if the field does not exist in the table.
        Overrides:
        findFieldName in class DefaultDataTable
      • getSelectableSchemeFor

        SelectableScheme getSelectableSchemeFor​(int column,
                                                int original_column,
                                                Table table)
        Description copied from class: DefaultDataTable
        Returns a SelectableScheme object for the given column of the VirtualTable. The Table parameter specifies the domain in which the scheme should be given. If table != this, we can safely assume it is a VirtualTable.
        Overrides:
        getSelectableSchemeFor in class DefaultDataTable
      • resolveToRawTable

        RawTableInformation resolveToRawTable​(RawTableInformation info)
        Description copied from class: DefaultDataTable
        Return the list of DataTable and row sets that make up the raw information in this table. For a DataTable itselt, this is trivial. NOTE: Using this method is extremely inefficient, and should never be used. It is included only to complete feature set. IDEA: Put a warning to check if this method is ever used.
        Overrides:
        resolveToRawTable in class DefaultDataTable