public class ObjectsDB
extends java.lang.Object
implements java.io.Serializable
| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
LONG_KEY_PREFIX
Suffix used for long keys.
|
static int |
TABLE_NAME_MAX_LENGTH
The maximal length of a table name.
|
static int |
VARCHAR_MAX_LENGTH
The maximal length of a varchar.
|
| Constructor and Description |
|---|
ObjectsDB(java.lang.String folder,
java.lang.String dbName,
boolean deleteOldDatabase,
ObjectsCache objectsCache)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addTable(java.lang.String tableName)
Adds the desired table in the database.
|
void |
close()
Closes the db connection.
|
java.lang.String |
correctKey(java.lang.String tableName,
java.lang.String key)
Indexes the long keys by a number.
|
java.lang.String |
correctTableName(java.lang.String tableName)
Surrounds the table name with quotation marks such that spaces etc are
allowed.
|
void |
deleteObject(java.lang.String tableName,
java.lang.String objectKey)
Deletes an object from the desired table.
|
void |
establishConnection(java.lang.String aDbFolder,
boolean deleteOldDatabase,
ObjectsCache objectsCache)
Establishes connection to the database.
|
java.lang.String |
getName()
Returns the database name.
|
ObjectsCache |
getObjectsCache()
Returns the cache used by this database.
|
java.lang.String |
getOriginalKey(java.lang.String tableName,
java.lang.String correctedKey)
Returns the original key of the corrected long key.
|
boolean |
inDB(java.lang.String tableName,
java.lang.String objectKey,
boolean cache)
Indicates whether an object is loaded in the given table.
|
void |
insertObject(java.lang.String tableName,
java.lang.String objectKey,
java.lang.Object object,
boolean inCache)
Stores an object in the desired table.
|
void |
insertObjects(java.lang.String tableName,
java.util.HashMap<java.lang.String,java.lang.Object> objects,
WaitingHandler waitingHandler)
Inserts a set of objects in the given table.
|
void |
insertObjects(java.lang.String tableName,
java.util.HashMap<java.lang.String,java.lang.Object> objects,
WaitingHandler waitingHandler,
boolean allNewObjects)
Inserts a set of objects in the given table.
|
void |
loadObjects(java.lang.String tableName,
java.util.ArrayList<java.lang.String> keys,
WaitingHandler waitingHandler)
Loads some objects from a table in the cache.
|
void |
loadObjects(java.lang.String tableName,
WaitingHandler waitingHandler)
Loads all objects from a table in the cache.
|
java.lang.Object |
retrieveObject(java.lang.String tableName,
java.lang.String objectKey,
boolean useDB)
Retrieves an object from the desired table.
|
java.lang.Object |
retrieveObject(java.lang.String tableName,
java.lang.String objectKey,
boolean useDB,
boolean useCache)
Retrieves an object from the desired table.
|
void |
setObjectCache(ObjectsCache objectCache)
Sets the object cache to be used by this database.
|
java.util.ArrayList<java.lang.String> |
tableContent(java.lang.String tableName)
Returns an arraylist with the content in the given table.
|
java.util.HashSet<java.lang.String> |
tableContentAsSet(java.lang.String tableName)
Returns a hashset with the content in the given table.
|
void |
updateObject(java.lang.String tableName,
java.lang.String objectKey,
java.lang.Object object)
Updates an object in the cache or in the tables if not in cache.
|
void |
updateObject(java.lang.String tableName,
java.lang.String objectKey,
java.lang.Object object,
boolean cache)
Updates an object in the cache or in the tables if not in cache or if
cache is wrong.
|
public static final int TABLE_NAME_MAX_LENGTH
public static final int VARCHAR_MAX_LENGTH
public static final java.lang.String LONG_KEY_PREFIX
public ObjectsDB(java.lang.String folder,
java.lang.String dbName,
boolean deleteOldDatabase,
ObjectsCache objectsCache)
throws java.sql.SQLException
folder - absolute path of the folder where to establish the databasedbName - name of the databasedeleteOldDatabase - if true, tries to delete the old databaseobjectsCache - java.sql.SQLExceptionpublic java.lang.String getName()
public ObjectsCache getObjectsCache()
public void setObjectCache(ObjectsCache objectCache)
objectCache - the object cache to be used by this databasepublic void addTable(java.lang.String tableName)
throws java.sql.SQLException
tableName - the name of the tablejava.sql.SQLException - exception thrown whenever a problem occurred while
working with the databasepublic void insertObject(java.lang.String tableName,
java.lang.String objectKey,
java.lang.Object object,
boolean inCache)
throws java.sql.SQLException,
java.io.IOException,
java.lang.InterruptedException
tableName - the name of the tableobjectKey - the key of the objectobject - the object to storeinCache - boolean indicating whether the method shall try to put the
object in cache or notjava.sql.SQLException - exception thrown whenever an error occurred while
storing the objectjava.io.IOException - exception thrown whenever an error occurred while
writing in the databasejava.lang.InterruptedExceptionpublic void insertObjects(java.lang.String tableName,
java.util.HashMap<java.lang.String,java.lang.Object> objects,
WaitingHandler waitingHandler)
throws java.sql.SQLException,
java.io.IOException
tableName - the name of the tableobjects - map of the objects (object key -> object)waitingHandler - a waiting handler displaying the progress (can be
null). The progress will be displayed on the secondary progress bar.java.sql.SQLExceptionjava.io.IOExceptionpublic void insertObjects(java.lang.String tableName,
java.util.HashMap<java.lang.String,java.lang.Object> objects,
WaitingHandler waitingHandler,
boolean allNewObjects)
throws java.sql.SQLException,
java.io.IOException
tableName - the name of the tableobjects - map of the objects (object key -> object)waitingHandler - a waiting handler displaying the progress (can be
null). The progress will be displayed on the secondary progress bar.allNewObjects - boolean indicating whether all objects are newjava.sql.SQLExceptionjava.io.IOExceptionpublic void loadObjects(java.lang.String tableName,
WaitingHandler waitingHandler)
throws java.sql.SQLException,
java.io.IOException,
java.lang.ClassNotFoundException,
java.lang.InterruptedException
tableName - the table namewaitingHandler - the waiting handlerjava.sql.SQLException - exception thrown whenever an error occurred while
interrogating the databasejava.io.IOException - exception thrown whenever an error occurred while
reading the databasejava.lang.ClassNotFoundException - exception thrown whenever the class of the
object is not found when deserializing it.java.lang.InterruptedExceptionpublic void loadObjects(java.lang.String tableName,
java.util.ArrayList<java.lang.String> keys,
WaitingHandler waitingHandler)
throws java.sql.SQLException,
java.io.IOException,
java.lang.ClassNotFoundException,
java.lang.InterruptedException
tableName - the table namekeys - the keys of the objects to loadwaitingHandler - the waiting handler, will only be increasedjava.sql.SQLException - exception thrown whenever an error occurred while
interrogating the databasejava.io.IOException - exception thrown whenever an error occurred while
reading the databasejava.lang.ClassNotFoundException - exception thrown whenever the class of the
object is not found when deserializing it.java.lang.InterruptedExceptionpublic java.lang.Object retrieveObject(java.lang.String tableName,
java.lang.String objectKey,
boolean useDB)
throws java.sql.SQLException,
java.io.IOException,
java.lang.ClassNotFoundException,
java.lang.InterruptedException
tableName - the name of the tableobjectKey - the object keyuseDB - if useDB is false, null will be returned if the object is
not in the cachejava.sql.SQLException - exception thrown whenever an error occurred while
interrogating the databasejava.io.IOException - exception thrown whenever an error occurred while
reading the databasejava.lang.ClassNotFoundException - exception thrown whenever the class of the
object is not found when deserializing it.java.lang.InterruptedExceptionpublic java.lang.Object retrieveObject(java.lang.String tableName,
java.lang.String objectKey,
boolean useDB,
boolean useCache)
throws java.sql.SQLException,
java.io.IOException,
java.lang.ClassNotFoundException,
java.lang.InterruptedException
tableName - the name of the tableobjectKey - the object keyuseDB - if useDB is false, null will be returned if the object is
not in the cacheuseCache - if true the retrieved object will be saved in cachejava.sql.SQLException - exception thrown whenever an error occurred while
interrogating the databasejava.io.IOException - exception thrown whenever an error occurred while
reading the databasejava.lang.ClassNotFoundException - exception thrown whenever the class of the
object is not found when deserializing it.java.lang.InterruptedExceptionpublic boolean inDB(java.lang.String tableName,
java.lang.String objectKey,
boolean cache)
throws java.sql.SQLException
tableName - the table nameobjectKey - the object keycache - a boolean indicating whether the cache should be searched as
welljava.sql.SQLException - exception thrown whenever an exception occurred
while interrogating the databasepublic java.util.ArrayList<java.lang.String> tableContent(java.lang.String tableName)
throws java.sql.SQLException
tableName - the table to get the content forjava.sql.SQLExceptionpublic java.util.HashSet<java.lang.String> tableContentAsSet(java.lang.String tableName)
throws java.sql.SQLException
tableName - the table to get the content forjava.sql.SQLExceptionpublic void deleteObject(java.lang.String tableName,
java.lang.String objectKey)
throws java.sql.SQLException,
java.io.IOException
tableName - the name of the tableobjectKey - the object keyjava.sql.SQLException - exception thrown whenever an error occurred while
interrogating the databasejava.io.IOException - exception thrown whenever an error occurred while
interrogating the databasepublic void updateObject(java.lang.String tableName,
java.lang.String objectKey,
java.lang.Object object)
throws java.sql.SQLException,
java.io.IOException
tableName - the name of the tableobjectKey - the key of the objectobject - the object to storejava.sql.SQLException - exception thrown whenever an error occurred while
storing the objectjava.io.IOException - exception thrown whenever an error occurred while
writing in the databasepublic void updateObject(java.lang.String tableName,
java.lang.String objectKey,
java.lang.Object object,
boolean cache)
throws java.sql.SQLException,
java.io.IOException
tableName - the name of the tableobjectKey - the key of the objectobject - the object to storecache - a boolean indicating whether the method should look in the
cachejava.sql.SQLException - exception thrown whenever an error occurred while
storing the objectjava.io.IOException - exception thrown whenever an error occurred while
writing in the databasepublic void close()
throws java.sql.SQLException
java.sql.SQLException - exception thrown whenever an error occurred while
closing the database connectionpublic void establishConnection(java.lang.String aDbFolder,
boolean deleteOldDatabase,
ObjectsCache objectsCache)
throws java.sql.SQLException
aDbFolder - the folder where the database is locateddeleteOldDatabase - if true, tries to delete the old databaseobjectsCache - java.sql.SQLException - exception thrown whenever an error occurred while
establishing the connectionpublic java.lang.String correctTableName(java.lang.String tableName)
tableName - the table namepublic java.lang.String correctKey(java.lang.String tableName,
java.lang.String key)
tableName - the table namekey - the key of the object to be storedpublic java.lang.String getOriginalKey(java.lang.String tableName,
java.lang.String correctedKey)
tableName - the tablecorrectedKey - the corrected key, should be prefix + numberCopyright © 2014. All Rights Reserved.