org.objectweb.telosys.common.tinydb
Class TinyTable

java.lang.Object
  extended byorg.objectweb.telosys.common.tinydb.TinyTable
All Implemented Interfaces:
java.io.Serializable

public class TinyTable
extends java.lang.Object
implements java.io.Serializable

This class represents one "table" of a "tiny database"
A "table" is based on a Hashtable instance, it contains 0..N "records" associated with a "key",
It provides standard entity persistance operations : insert, find, update, delete, insert or update
A table doesn't store the record object itself but a copy, in order to avoid unwanted changes

Since:
v 1.0.3
See Also:
Serialized Form

Constructor Summary
protected TinyTable(java.lang.String tableName, java.lang.Class keyClass, java.lang.Class recordClass, java.lang.String tableFile)
          Constructs a table
 
Method Summary
 boolean deleteRecord(java.lang.Object key)
          Deletes the record associated with the given key
If there's no record for this key nothing is deleted (it's not an error)
 java.lang.Object findRecord(java.lang.Object key)
          Finds the record associated with the given key
 java.lang.Class getKeyClass()
          Returns the class of the keys for this table
 java.lang.Class getRecordClass()
          Returns the class of the records stored in this table
 java.lang.String getTableFile()
          Returns the filename of this table (filesystem file path)
 java.lang.String getTableName()
          Returns the logical name of this table
 void insertOrUpdateRecord(java.lang.Object key, java.lang.Object record)
          Inserts or update the given record associated with the given key
If a record exists for the key it is updated, else it is inserted
 void insertRecord(java.lang.Object key, java.lang.Object record)
          Inserts the given record associated with the given key
If a record already exists for this key, a "duplicate key" exception if throwned
 boolean isLoaded()
          Return true if the table records have been loaded (at least on time) from the file
 java.util.Hashtable loadTable()
          Loads (or reloads) the table records from the file
 void saveTable()
          Saves the table records in the file
 boolean updateRecord(java.lang.Object key, java.lang.Object record)
          Updates the record associated with the given key
If there's no record for this key nothing is updated (it's not an error)
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TinyTable

protected TinyTable(java.lang.String tableName,
                    java.lang.Class keyClass,
                    java.lang.Class recordClass,
                    java.lang.String tableFile)
Constructs a table

Parameters:
tableName - the logical name of the table
keyClass - the class of the key used to identify a record
recordClass - the class of the records stored in the table
tableFile - the file where the table is physically stored
Throws:
java.lang.RuntimeException - if one of the parameters is null
Method Detail

getTableName

public java.lang.String getTableName()
Returns the logical name of this table

Returns:

getTableFile

public java.lang.String getTableFile()
Returns the filename of this table (filesystem file path)

Returns:

getKeyClass

public java.lang.Class getKeyClass()
Returns the class of the keys for this table

Returns:

getRecordClass

public java.lang.Class getRecordClass()
Returns the class of the records stored in this table

Returns:

isLoaded

public boolean isLoaded()
Return true if the table records have been loaded (at least on time) from the file

Returns:

loadTable

public java.util.Hashtable loadTable()
Loads (or reloads) the table records from the file

Returns:
the Hashtable loaded (never null)
Throws:
java.lang.RuntimeException - if FileSystem I/O error

saveTable

public void saveTable()
Saves the table records in the file

Throws:
java.lang.RuntimeException - if FileSystem I/O error

findRecord

public java.lang.Object findRecord(java.lang.Object key)
Finds the record associated with the given key

Parameters:
key - the key (must not be null )
Returns:
the record found, or null if there's no record for this key
Throws:
java.lang.RuntimeException - if an error occurs

insertOrUpdateRecord

public void insertOrUpdateRecord(java.lang.Object key,
                                 java.lang.Object record)
Inserts or update the given record associated with the given key
If a record exists for the key it is updated, else it is inserted

Parameters:
key - the key (must not be null )
record -
Throws:
java.lang.RuntimeException - if an error occurs

insertRecord

public void insertRecord(java.lang.Object key,
                         java.lang.Object record)
Inserts the given record associated with the given key
If a record already exists for this key, a "duplicate key" exception if throwned

Parameters:
key - the key (must not be null )
record -
Throws:
java.lang.RuntimeException - if an error occurs or if duplicate key

updateRecord

public boolean updateRecord(java.lang.Object key,
                            java.lang.Object record)
Updates the record associated with the given key
If there's no record for this key nothing is updated (it's not an error)

Parameters:
key - the key (must not be null )
record -
Returns:
true if an existing record was updated, false if there's no record for the given key
Throws:
java.lang.RuntimeException - if an error occurs

deleteRecord

public boolean deleteRecord(java.lang.Object key)
Deletes the record associated with the given key
If there's no record for this key nothing is deleted (it's not an error)

Parameters:
key - the key (must not be null )
Returns:
true if a record was found and deleted, false if there's no record for the given key
Throws:
java.lang.RuntimeException - if an error occurs