suncertify.db
Class Data

java.lang.Object
  |
  +--suncertify.db.Data

public class Data
extends java.lang.Object

This class provides the basic database services. It uses two other support classes: DataInfo and FieldInfo.

Version:
1.0 07/31/1999

Constructor Summary
Data(java.lang.String dbname)
          This constructor opens an existing database given the name of the disk file containing it.
Data(java.lang.String dbname, FieldInfo[] fields)
          This constructor creates a new database file, using the name provided for the disk file and using the FieldInfo array to describe the field names and sizes that should be created.
 
Method Summary
 void add(java.lang.String[] newData)
          This method adds a new record to the database.
 void close()
          This method closes the database, flushing any outstanding writes at the same time.
 void delete(DataInfo toDelete)
          This method deletes the record referred to by the record number in the DataInfo argument.
protected  void finalize()
          The finalize method to close the underlying DataBase file when DataBase is garbage collected.
 DataInfo find(java.lang.String toMatch)
          This method searches the database for an entry which has a first field which exactly matches the string supplied.
 DataInfo[] findCriteria(java.lang.String criteria)
          The method to search in the database all records that matches a given criteria.
 FieldInfo[] getFieldInfo()
          This method returns a description of the database schema, as an array of FieldInfo objects.
 DataInfo getRecord(int recNum)
          Gets a requested record from the database based on record number.
 int getRecordCount()
          Gets the number of records stored in the database.
protected  void invariant()
          Ensures that the database structure is valid.
 void lock(int recNum, java.lang.String clientID)
          Lock the requested record.
 void modify(DataInfo newData)
          This method updates the record specified by the record number field in the DataInfo argument.
 void releaseLocks()
          This method is use to clear all locks at time of bringing down the database server and also when there are no client references to the Remote Server.
 void releaseLocks(java.lang.String clientID)
          The method to release all locks held by the ClientId which just got severed.
 void unLock(int recNum, java.lang.String clientID)
          Unlock the requested record.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Data

public Data(java.lang.String dbname)
     throws java.io.IOException
This constructor opens an existing database given the name of the disk file containing it.
Parameters:
String - dbname The name of the database file to open.
Throws:
java.io.IOException - - if Data in the specified file is not in the desired format.

Data

public Data(java.lang.String dbname,
            FieldInfo[] fields)
     throws java.io.IOException
This constructor creates a new database file, using the name provided for the disk file and using the FieldInfo array to describe the field names and sizes that should be created.
Parameters:
String - dbname The name of the database file to open.
FieldInfo[] - fields The list of fields for the schema of this database.
Throws:
java.io.IOException - Thrown if cannot create database file.
Method Detail

getFieldInfo

public FieldInfo[] getFieldInfo()
This method returns a description of the database schema, as an array of FieldInfo objects.
Returns:
FieldInfo[] The array of FieldInfo objects that comprise the schema to this database.

getRecordCount

public int getRecordCount()
Gets the number of records stored in the database.

getRecord

public DataInfo getRecord(int recNum)
                   throws DatabaseException
Gets a requested record from the database based on record number.
Parameters:
recNum - The number of the record to read (first record is 1).
Returns:
DataInfo for the record or null if the record has been marked for deletion.
Throws:
DatabaseException - Thrown if database file cannot be accessed.

find

public DataInfo find(java.lang.String toMatch)
              throws DatabaseException
This method searches the database for an entry which has a first field which exactly matches the string supplied. If the required record cannot be found, this method returns null. For this assignment, the key field is the record number field.
Parameters:
toMatch - The key field value to match upon for a successful find.
Returns:
DataInfo The matching record.
Throws:
DatabaseException - Thrown when database file could not be accessed.

add

public void add(java.lang.String[] newData)
         throws DatabaseException
This method adds a new record to the database. The array of strings must have exactly the same number of elements as the field count of the database schema, otherwise a RuntimeException is issued. The first field, the key, must be unique in the database or a RuntimeException is thrown.
Parameters:
newData - The elements of the record to add.
Throws:
DatabaseException - Attempted to add a duplicate key or database file could not be accessed.

modify

public void modify(DataInfo newData)
            throws DatabaseException
This method updates the record specified by the record number field in the DataInfo argument. The fields are all modified to reflect the values in that argument. If the key field specified in the argument matches any record other than the one indicated by the record number of the argument, then a RuntimeException is thrown.
Parameters:
newData - The updated record to modify.
Throws:
DatabaseException - Thrown if attempting to add a duplicate key.

delete

public void delete(DataInfo toDelete)
            throws DatabaseException
This method deletes the record referred to by the record number in the DataInfo argument.
Parameters:
DataInfo - newData The record to delete.
Throws:
DatabaseException - Thrown if database cannot be accessed.

close

public void close()
This method closes the database, flushing any outstanding writes at the same time. Any attempt to access the database after this results in a IOException.

finalize

protected void finalize()
The finalize method to close the underlying DataBase file when DataBase is garbage collected.
Overrides:
finalize in class java.lang.Object

invariant

protected final void invariant()
Ensures that the database structure is valid.
Throws:
java.lang.RuntimeException - If structure has become corrupted.

lock

public void lock(int recNum,
                 java.lang.String clientID)
          throws DatabaseException
Lock the requested record. If the argument is -1, lock the whole database. This method blocks until the lock succeeds. No timeouts are defined for this.
Parameters:
recNum: - The record number to lock.
Throws:
DatabaseException: - If the record position is invalid.

unLock

public void unLock(int recNum,
                   java.lang.String clientID)
Unlock the requested record. Ignored if the clientID does not have a current lock on the requested record.
Parameters:
recNum: - The record number to unLock.
Throws:
DatabaseException: - If the record position is invalid.

releaseLocks

public void releaseLocks(java.lang.String clientID)
The method to release all locks held by the ClientId which just got severed. This calls the corresponding method of the underlying 'DbLockTable" object of this DataBase.
Parameters:
clientID - : the id of the client whose connection just got severed.

releaseLocks

public void releaseLocks()
This method is use to clear all locks at time of bringing down the database server and also when there are no client references to the Remote Server. This calls the corresponding method of the underlying "DbLockTable" object of this DataBase.

findCriteria

public DataInfo[] findCriteria(java.lang.String criteria)
                        throws DatabaseException
The method to search in the database all records that matches a given criteria.
Parameters:
criteria - : A string of the form (fieldName1=value1,...fieldNamen=valuen)
Returns:
An array of DataInfo which represent the matched records.
Throws:
DatabaseException - is throwm if there was error in reading from the DataBase.