Class SchemaWriter


  • public class SchemaWriter
    extends java.lang.Object

    This class contains a general purpose implementation for reading and writing a table schema into a data page. Specific file formats can customize this representation by subclassing this class, and overriding the various methods to modify the details they read and write.

    The schema is written out in the following format:

    Column Details:
    • Number of Columns (unsigned byte)
    • For each column:
      • Column's Type (signed byte, converted to SQLDataType enum)
      • If type requires a length: Column's Max Length (unsigned short)
      • Column's Name (a string up to 255 characters, stored as PageReader.readVarString255()
    Column names and types are checked to be valid at load time.
    NOT-NULL Constraints:
    • Number of NOT-NULL Columns (unsigned byte)
    • For each column:
      • Zero-based index of NOT-NULL Column (unsigned byte)
    Every index may only appear once. Additionally, indexes must reference valid columns within the table schema.
    Primary/Foreign/Candidate Key Constraints:
    • Total Number of Candidate + Foreign Keys (unsigned byte)
    • For each candidate key (which includes primary key, if any):
      • Constraint Type, and Name Flag (unsigned byte)
      • Constraint Name (if present) (a string up to 255 characters, stored as PageReader.readVarString255())
      • Number of Columns in the Key (unsigned byte)
      • For each column:
        • Zero-based index of Column in Key (unsigned byte)
    • For each foreign key:
      • Constraint Type, and Name Flag (unsigned byte)
      • Constraint Name (if present) (a string up to 255 characters, stored as PageReader.readVarString255())
      • Name of Referenced Table (a string up to 255 characters, stored as PageReader.readVarString255())
      • On-Delete Cascade Option (unsigned byte)
      • On-Update Cascade Option (unsigned byte)
      • Number of Columns in the Key (unsigned byte)
      • For each column:
        • Zero-based index of Column in Key (unsigned byte)
        • Corresponding Index of Column in Referenced Table (unsigned byte)
    Referencing Tables:
    • Total Number of Referencing Tables (unsigned byte)
    • For each referencing table:
    Indexes:
    • Number of Indexes (unsigned byte)
    • For each index:
      • Number of Columns in Index (unsigned byte)
      • For each column:
        • Zero-based index of Column in Index (unsigned byte)
      • Name of Index (a string up to 255 characters, stored as PageReader.readVarString255())
    • Field Detail

      • logger

        private static org.apache.logging.log4j.Logger logger
        A logging object for reporting anything interesting that happens.
    • Constructor Detail

      • SchemaWriter

        public SchemaWriter()
    • Method Detail

      • writeSchema

        public void writeSchema​(Schema schema,
                                PageWriter pgWriter)
        Parameters:
        schema - the schema to write to the page
        pgWriter - the PageWriter that can be used to write to the data page
      • writeColumnInfos

        protected void writeColumnInfos​(Schema schema,
                                        PageWriter pgWriter)
        This helper writes out the column details of the schema. For each column, the function writes: the column's type (1 byte), plus any
        Parameters:
        schema -
        pgWriter -
      • writeNotNullConstraints

        protected void writeNotNullConstraints​(Schema schema,
                                               PageWriter pgWriter)
        Parameters:
        schema -
        pgWriter -
      • writeKeyConstraints

        protected void writeKeyConstraints​(Schema schema,
                                           PageWriter pgWriter)
      • writeKey

        protected void writeKey​(PageWriter pgWriter,
                                TableConstraintType type,
                                KeyColumnRefs key)
        This helper function writes a primary key or candidate key to the schema representation.
        Parameters:
        pgWriter - the writer being used to write the table's schema to its header page
        type - the constraint type, either TableConstraintType.PRIMARY_KEY or TableConstraintType.FOREIGN_KEY.
        key - a specification of what columns appear in the key
        Throws:
        java.lang.IllegalArgumentException - if the type argument is null, or is not one of the accepted values
      • writeIndexes

        protected void writeIndexes​(Schema schema,
                                    PageWriter pgWriter)
      • writeReferencingTables

        protected void writeReferencingTables​(Schema schema,
                                              PageWriter pgWriter)
      • writeIndex

        protected void writeIndex​(PageWriter hpWriter,
                                  IndexColumnRefs idx)
        This helper function writes an index to the table's schema stored in the header page.
        Parameters:
        hpWriter - the writer being used to write the table's schema to its header page
        idx - a specification of what columns appear in the index
        Throws:
        java.lang.IllegalArgumentException - if the type argument is null, or is not one of the accepted values
      • readSchema

        public Schema readSchema​(PageReader pgReader)
        This method opens the data file corresponding to the specified table name and reads in the table's schema.
      • readColumnInfos

        protected void readColumnInfos​(PageReader pgReader,
                                       Schema schema)
      • readNotNullConstraints

        protected void readNotNullConstraints​(PageReader pgReader,
                                              Schema schema)
      • readKeyConstraints

        protected void readKeyConstraints​(PageReader pgReader,
                                          Schema schema)
      • readKey

        protected KeyColumnRefs readKey​(PageReader pgReader,
                                        int typeID,
                                        TableConstraintType type)
        This helper function writes a primary key or candidate key to the table's schema stored in the header page.
        Parameters:
        pgReader - the writer being used to write the table's schema to its header page
        typeID - the unsigned-byte value read from the table's header page, corresponding to this key's type. Although this value is already parsed before calling this method, it also contains flags that this method handles, so it must be passed in as well.
        type - the constraint type, either TableConstraintType.PRIMARY_KEY or TableConstraintType.FOREIGN_KEY.
        Returns:
        a specification of the key, including its name, what columns appear in the key, and what index is used to enforce the key
        Throws:
        java.lang.IllegalArgumentException - if the type argument is null, or is not one of the accepted values
      • readReferencingTables

        protected void readReferencingTables​(PageReader pgReader,
                                             Schema schema)
      • readIndexes

        protected void readIndexes​(PageReader pgReader,
                                   Schema schema)
      • readIndex

        protected IndexColumnRefs readIndex​(PageReader pgReader)
        This helper function reads an index to the table's schema stored in the header page.
        Parameters:
        pgReader - the reader being used to read the table's schema to its header page
        Returns:
        a specification of the index, including its name, what columns appear in the index
        Throws:
        java.lang.IllegalArgumentException - if the type argument is null, or is not one of the accepted values