Package edu.caltech.nanodb.storage
Class SchemaWriter
- java.lang.Object
-
- edu.caltech.nanodb.storage.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's Type (signed byte, converted to
- NOT-NULL Constraints:
-
- Number of NOT-NULL Columns (unsigned byte)
- For each column:
- Zero-based index of NOT-NULL Column (unsigned byte)
- 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:
- Table Name (a string up to 255 characters, stored as
PageReader.readVarString255()
)
- Table Name (a string up to 255 characters, stored as
- 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 Summary
Fields Modifier and Type Field Description private static org.apache.logging.log4j.Logger
logger
A logging object for reporting anything interesting that happens.
-
Constructor Summary
Constructors Constructor Description SchemaWriter()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
readColumnInfos(PageReader pgReader, Schema schema)
protected ForeignKeyColumnRefs
readForeignKey(PageReader pgReader, int typeID)
protected IndexColumnRefs
readIndex(PageReader pgReader)
This helper function reads an index to the table's schema stored in the header page.protected void
readIndexes(PageReader pgReader, Schema schema)
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.protected void
readKeyConstraints(PageReader pgReader, Schema schema)
protected void
readNotNullConstraints(PageReader pgReader, Schema schema)
protected void
readReferencingTables(PageReader pgReader, Schema schema)
Schema
readSchema(PageReader pgReader)
This method opens the data file corresponding to the specified table name and reads in the table's schema.protected void
writeColumnInfos(Schema schema, PageWriter pgWriter)
This helper writes out the column details of the schema.protected void
writeForeignKey(PageWriter pgWriter, ForeignKeyColumnRefs key)
protected void
writeIndex(PageWriter hpWriter, IndexColumnRefs idx)
This helper function writes an index to the table's schema stored in the header page.protected void
writeIndexes(Schema schema, PageWriter pgWriter)
protected void
writeKey(PageWriter pgWriter, TableConstraintType type, KeyColumnRefs key)
This helper function writes a primary key or candidate key to the schema representation.protected void
writeKeyConstraints(Schema schema, PageWriter pgWriter)
protected void
writeNotNullConstraints(Schema schema, PageWriter pgWriter)
protected void
writeReferencingTables(Schema schema, PageWriter pgWriter)
void
writeSchema(Schema schema, PageWriter pgWriter)
-
-
-
Method Detail
-
writeSchema
public void writeSchema(Schema schema, PageWriter pgWriter)
- Parameters:
schema
- the schema to write to the pagepgWriter
- thePageWriter
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 pagetype
- the constraint type, eitherTableConstraintType.PRIMARY_KEY
orTableConstraintType.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
-
writeForeignKey
protected void writeForeignKey(PageWriter pgWriter, ForeignKeyColumnRefs key)
-
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 pageidx
- 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 pagetypeID
- 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, eitherTableConstraintType.PRIMARY_KEY
orTableConstraintType.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
-
readForeignKey
protected ForeignKeyColumnRefs readForeignKey(PageReader pgReader, int typeID)
-
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
-
-