com.zolltov.zdb.file
Class SimpleColumnLoader

java.lang.Object
  extended by com.zolltov.zdb.file.SimpleColumnLoader
All Implemented Interfaces:
ColumnLoader

public class SimpleColumnLoader
extends java.lang.Object
implements ColumnLoader

Store a file database in a single directory tree.

Each column is stored as its own file in a three rooted at a specified BasePath


Nested Class Summary
static class SimpleColumnLoader.UnsupportedFileVersionException
          Thrown when trying to read a file that is in an unknown format.
 
Field Summary
protected  java.io.File meta
          This file points out the metadata file for this database.
protected  java.io.File root
          This file is the root directory of the entire database.
protected static int typeid
          This is the magic number in the beginning of each column file, indicating that it is a known format used by this column loader.
 
Constructor Summary
SimpleColumnLoader(java.io.File root)
          Create a new SimpleColumnLoader with the given directory as the BasePath.
 
Method Summary
 void append(java.lang.String tableName, ColumnInfo info, Date date, Column column)
          Append data to a column file.
 void createDataBase()
          Create a new database in the directory pointed to by this ColumnLoader.
protected  java.io.File createFileName(java.lang.String tableName, ColumnInfo info, Date date)
           
 void dropColumn(java.lang.String tableName, ColumnInfo info)
          Delete a column previously saved to file.
 boolean isDataBase()
          Returns true of this loader points to an existing database.
 java.util.List<Date> listDates()
          Returns a list of all dates known to this ColumnLoader.
 Column loadColumn(java.lang.String tableName, ColumnInfo info, Date date)
          Load a column from file.
 DataBaseInfo loadMetaData()
          Load all metadata for this database.
 void saveColumn(java.lang.String tableName, ColumnInfo info, Date date, Column column)
          Save a column to file.
 void saveMetaData(DataBaseInfo meta)
          Save all metadata for this database.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

root

protected final java.io.File root
This file is the root directory of the entire database.


meta

protected final java.io.File meta
This file points out the metadata file for this database.


typeid

protected static final int typeid
This is the magic number in the beginning of each column file, indicating that it is a known format used by this column loader.

See Also:
Constant Field Values
Constructor Detail

SimpleColumnLoader

public SimpleColumnLoader(java.io.File root)
Create a new SimpleColumnLoader with the given directory as the BasePath.

Parameters:
root - A file giving the path to the root directory.
Method Detail

isDataBase

public boolean isDataBase()
Returns true of this loader points to an existing database.

Returns:
true if this points to an existing database.

createDataBase

public void createDataBase()
Create a new database in the directory pointed to by this ColumnLoader. If the database already exists (isDataBase() returns true) all information about it will be lost, and existing column will NOT be dropped/deleted.


loadColumn

public Column loadColumn(java.lang.String tableName,
                         ColumnInfo info,
                         Date date)
                  throws java.io.IOException
Load a column from file. If there is no data for the given date, an empty column with correct type is returned.

Specified by:
loadColumn in interface ColumnLoader
Parameters:
tableName - A string giving the table to load a column for.
info - A ColumnInfo describing the type of column to load.
date - A date to load for, or null if the table is not partitioned.
Returns:
The loaded column.
Throws:
java.io.IOException - if there is an error when reading data from file.

saveColumn

public void saveColumn(java.lang.String tableName,
                       ColumnInfo info,
                       Date date,
                       Column column)
                throws java.io.IOException
Save a column to file.

Specified by:
saveColumn in interface ColumnLoader
Parameters:
tableName - A string giving the table to load a column for.
info - A ColumnInfo describing the type of column to load.
date - A date to load for, or null if the table is not partitioned.
column - The Column object containing data to be written.
Throws:
java.io.IOException - if there is an error when writing data to file.

createFileName

protected java.io.File createFileName(java.lang.String tableName,
                                      ColumnInfo info,
                                      Date date)

listDates

public java.util.List<Date> listDates()
Returns a list of all dates known to this ColumnLoader. The order of elements are unspecified.

Specified by:
listDates in interface ColumnLoader
Returns:
A list of all dates known to this ColumnLoader.

loadMetaData

public DataBaseInfo loadMetaData()
                          throws java.io.IOException
Load all metadata for this database.

Specified by:
loadMetaData in interface ColumnLoader
Returns:
All metadata for this database.
Throws:
java.io.IOException - if there is an error when loading meta data from file.

saveMetaData

public void saveMetaData(DataBaseInfo meta)
                  throws java.io.IOException
Save all metadata for this database.

Specified by:
saveMetaData in interface ColumnLoader
Parameters:
meta - The new metadata contents
Throws:
java.io.IOException - if there is an error when saving meta data from file.

append

public void append(java.lang.String tableName,
                   ColumnInfo info,
                   Date date,
                   Column column)
            throws java.io.IOException
Append data to a column file. This method should be implemented in a way that avoids reading and writing the already existing column data.

Specified by:
append in interface ColumnLoader
Parameters:
tableName - A string giving the table to append data for.
info - A ColumnInfo describing the column to append.
date - A date to append for, or null if the table is not partitioned.
column - A column containing all new data to append to the end of the file.
Throws:
java.io.IOException - if there is an error when appending data.

dropColumn

public void dropColumn(java.lang.String tableName,
                       ColumnInfo info)
                throws java.io.IOException
Delete a column previously saved to file.

Specified by:
dropColumn in interface ColumnLoader
Parameters:
tableName - A string giving the table to delete a column from.
info - A ColumnInfo describing the no longer existing column.
Throws:
java.io.IOException - if there is an error when deleting the file.