com.zolltov.zdb.file
Class FileDataBase

java.lang.Object
  extended by com.zolltov.zdb.DataBase
      extended by com.zolltov.zdb.file.FileDataBase

public final class FileDataBase
extends DataBase

A database implementation that stores the database as a series of files. It uses objects that implement the interface ColumnLoader to perform the actual reading and writing.


Constructor Summary
FileDataBase(ColumnLoader loader)
          Create a FileDataBase that uses the given ColumnLoader.
 
Method Summary
 Table createPartitionedTable(java.lang.String name, ColumnInfo... columns)
          Creates a new date-partitioned database table and add it to this database.
 Table createTable(java.lang.String name, ColumnInfo... columns)
          Creates a new database table and add it to this database.
 TableAppender directAppend(java.lang.String table)
          Create a TableAppender instance for the named table in this database.
 long getSchemaID()
          Return the schema ID for this database.
 Table getTable(java.lang.String name)
          Return all columns for a given table in this database.
 Table getTable(java.lang.String name, ColumnInfo... columns)
          Return a subset of all columns for a given table in this database.
 Table getTable(java.lang.String name, Date from, Date to)
          Return all columns between two dates for a given table in this database.
 Table getTable(java.lang.String name, Date from, Date to, ColumnInfo... columns)
          Return a subset of all columns for a date partitioned table in this database.
 void putTable(java.lang.String name, Table table)
          Change the contents of a table in this database.
 void putTable(java.lang.String name, Table table, Date from, Date to)
          Change the contents of a date partitioned table in this database.
 void setSchemaID(long id)
          Store a new schema id in this database.
protected  void updateMetaData(java.lang.String table, ColumnInfo[] columns, ColumnInfo keyColumn)
          Updates or sets the metadata for a table.
 
Methods inherited from class com.zolltov.zdb.DataBase
getMetaData, getTableNames
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileDataBase

public FileDataBase(ColumnLoader loader)
Create a FileDataBase that uses the given ColumnLoader.

Parameters:
loader - A ColumnLoader that will do all reading and writing.
Throws:
FileException - If there is any error preparing this database.
Method Detail

createTable

public Table createTable(java.lang.String name,
                         ColumnInfo... columns)
Creates a new database table and add it to this database. The new database will have the given columns in the order given and a length of 0.

Mutations done to the returned table are not reflected in this database. In order to record any mutations, pass the table to putTable.

Specified by:
createTable in class DataBase
Parameters:
name - A string identifying this table
columns - An array containing the column names and types.
Returns:
The newly created table.
See Also:
DataBase.putTable(String, Table), DataBase.getTable(String)

createPartitionedTable

public Table createPartitionedTable(java.lang.String name,
                                    ColumnInfo... columns)
Description copied from class: DataBase
Creates a new date-partitioned database table and add it to this database. The new database will have the given columns in the order given and a length of 0.

Mutations done to the returned table are not reflected in this database. In order to record any mutations, pass the table to putTable.

Specified by:
createPartitionedTable in class DataBase
Parameters:
name - A string identifying this table
columns - An array containing the column names and types.
Returns:
The created table.
See Also:
DataBase.putTable(String, Table, Date, Date), DataBase.getTable(String, Date, Date)

getTable

public Table getTable(java.lang.String name,
                      ColumnInfo... columns)
Return a subset of all columns for a given table in this database. The columns in the returned table will have the same order as the one given by columns, regardless of the order of the underlying table in this database.

The type of each column must match the metadata types as returned by DataBase.getMetaData(String) or a TypeMismatchException will be thrown.

If there is no table with the given name in this database, null will be returned.

Specified by:
getTable in class DataBase
Parameters:
name - A string identifying the table to fetch data from.
columns - An array of columns to fetch from this database.
Returns:
The requested columns collected into a table.
Throws:
FileException - if there is an error reading data from file.

getTable

public Table getTable(java.lang.String name)
Description copied from class: DataBase
Return all columns for a given table in this database. If there is no table with the given name in this database, null will be returned.

Specified by:
getTable in class DataBase
Parameters:
name - A string identifying the table to return.
Returns:
The requested table as it existed in this database at the time of this call.
See Also:
DataBase.putTable(String, Table)

getTable

public Table getTable(java.lang.String name,
                      Date from,
                      Date to)
Return all columns between two dates for a given table in this database. The given date limits are inclusive. One or both date limits may be null meaning an open-ended interval. Both dates can be null and then the entire table will be returned.

If there is no table with the given name in this database, null will be returned.

Specified by:
getTable in class DataBase
Parameters:
name - A string identifying a date partitioned table in this database.
from - A date giving the first date (inclusive) or null meaning no start date.
to - A date giving the last date (inclusive) or null meaning no end date.
Returns:
The date partitioned table.
Throws:
FileException - if there is an error reading data from file.

getTable

public Table getTable(java.lang.String name,
                      Date from,
                      Date to,
                      ColumnInfo... columns)
Return a subset of all columns for a date partitioned table in this database. The columns in the returned table will have the same order as the one given by columns, regardless of the order of the underlying table in this database. The given date limits are inclusive. One or both date limits may be null meaning an open-ended interval. Both dates can be null and then the entire table will be returned.

The type of each column must match the metadata types as returned by DataBase.getMetaData(String) or a TypeMismatchException will be thrown.

If there is no table with the given name in this database, null will be returned.

Specified by:
getTable in class DataBase
Parameters:
name - A string identifying a date partitioned table in this database.
from - A date giving the first date (inclusive) or null meaning no start date.
to - A date giving the last date (inclusive) or null meaning no end date.
columns - The requested columns that will be included in the returned table.
Returns:
The requested table
Throws:
FileException - if there is an error reading data from file.

putTable

public void putTable(java.lang.String name,
                     Table table)
Change the contents of a table in this database. The name must have been previously created by a call to createTable.

It is not necessary for the table to be of any specific origin or any specific column layout. The metadata is change to reflect the passed table and then the table contents is recorded in this database.

It is undefined if further mutations to the passed table is reflected in the database or not, but it is strongly advised against any such mutations taking place.

Specified by:
putTable in class DataBase
Parameters:
name - A string giving the case sensitive table name to change.
table - A table object giving the new table contents.
Throws:
FileException - if there is an error writing data to file.

putTable

public void putTable(java.lang.String name,
                     Table table,
                     Date from,
                     Date to)
Change the contents of a date partitioned table in this database. The name must have been previously created by a call to creatPartitionedeTable.

It is not necessary for the table to be of any specific origin. The from and to dates give the date range that will be overwritten with data from table. Both dates must be null if the column names or types are different in this database and the passed table or a TypeMismatchException will be thrown. The behavior if table contains a date outside from and to is unspecified.

It is undefined if further mutations to the passed table is reflected in the database or not, but it is strongly advised against any such mutations taking place.

Specified by:
putTable in class DataBase
Parameters:
name - A string giving the table name to update in this database.
table - A table containing the new data for the given time period.
from - A date giving the start (inclusive) of the date range to change.
to - A date giving the end (inclusive) of the date range to change.
Throws:
FileException - if there is an error writing data to file.

directAppend

public TableAppender directAppend(java.lang.String table)
Create a TableAppender instance for the named table in this database. Appending data using the returned TableAppender is the same as getTable-append-putTable but as done as one atomic operation. If the method ColumnLoader.append is implemented properly, it will also be faster since it avoids reading and writing the already existing data.

Parameters:
table - A string giving the name of the table to create an appender for.
Returns:
A ColumnAppender that appends data directly to the given table.

updateMetaData

protected void updateMetaData(java.lang.String table,
                              ColumnInfo[] columns,
                              ColumnInfo keyColumn)
Updates or sets the metadata for a table. The passed array is stored as-is in the metadata so further mutations to it should not be done.

Overrides:
updateMetaData in class DataBase
Parameters:
table - A string identifying a table in this database
columns - An array containing the column information and their default order.
keyColumn - ColumnInfo for the key column, or null if there is none.
Throws:
FileException

getSchemaID

public long getSchemaID()
Return the schema ID for this database. The schema ID is a user defined long integer intended to convey compact information about the tables and columns in this database. If there is no id stored earlier this method returns 0.

Returns:
The previously stored schema id or 0.

setSchemaID

public void setSchemaID(long id)
Store a new schema id in this database.

Parameters:
id - A long integer containing the new schema id.
Throws:
FileException - if there is an error writing the new id to file.