public class ObjectsDB extends Object implements Serializable
| Modifier and Type | Field and Description |
|---|---|
static String |
LONG_KEY_PREFIX
Suffix used for long keys.
|
static String |
LONG_KEY_TABLE
The table where to save the long keys.
|
static String |
LONG_TABLE_NAMES
Name for the long table names.
|
static int |
MAX_KEY_LENGTH
The maximum key length before using the key correction 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(String folder,
String dbName,
boolean deleteOldDatabase,
ObjectsCache objectsCache)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addTable(String tableName)
Adds the desired table in the database.
|
void |
close()
Closes the db connection.
|
String |
correctKey(String tableName,
String key)
Indexes the long keys by a number.
|
String |
correctTableName(String tableName)
Surrounds the table name with quotation marks such that spaces etc are
allowed.
|
void |
deleteObject(String tableName,
String objectKey)
Deletes an object from the desired table.
|
void |
establishConnection(String aDbFolder,
boolean deleteOldDatabase,
ObjectsCache objectsCache)
Establishes connection to the database.
|
String |
getName()
Returns the database name.
|
ObjectsCache |
getObjectsCache()
Returns the cache used by this database.
|
String |
getOriginalKey(String tableName,
String correctedKey)
Returns the original key of the corrected long key.
|
boolean |
hasTable(String tableName)
Indicates whether the database contains the given table.
|
boolean |
inDB(String tableName,
String objectKey,
boolean cache)
Indicates whether an object is loaded in the given table.
|
void |
insertObject(String tableName,
String objectKey,
Object object,
boolean inCache)
Stores an object in the desired table.
|
void |
insertObjects(String tableName,
HashMap<String,Object> objects,
WaitingHandler waitingHandler)
Inserts a set of objects in the given table.
|
void |
insertObjects(String tableName,
HashMap<String,Object> objects,
WaitingHandler waitingHandler,
boolean allNewObjects)
Inserts a set of objects in the given table.
|
void |
loadObjects(String tableName,
ArrayList<String> keys,
WaitingHandler waitingHandler)
Loads some objects from a table in the cache.
|
void |
loadObjects(String tableName,
WaitingHandler waitingHandler)
Loads all objects from a table in the cache.
|
Object |
retrieveObject(String tableName,
String objectKey,
boolean useDB)
Retrieves an object from the desired table.
|
Object |
retrieveObject(String tableName,
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.
|
ArrayList<String> |
tableContent(String tableName)
Returns an arraylist with the content in the given table.
|
HashSet<String> |
tableContentAsSet(String tableName)
Returns a hashset with the content in the given table.
|
void |
updateObject(String tableName,
String objectKey,
Object object)
Updates an object in the cache or in the tables if not in cache.
|
void |
updateObject(String tableName,
String objectKey,
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 int MAX_KEY_LENGTH
public static final String LONG_KEY_PREFIX
public static final String LONG_TABLE_NAMES
public static final String LONG_KEY_TABLE
public ObjectsDB(String folder, String dbName, boolean deleteOldDatabase, ObjectsCache objectsCache) throws SQLException
folder - absolute path of the folder where to establish the databasedbName - name of the databasedeleteOldDatabase - if true, tries to delete the old databaseobjectsCache - SQLExceptionpublic String getName()
public ObjectsCache getObjectsCache()
public void setObjectCache(ObjectsCache objectCache)
objectCache - the object cache to be used by this databasepublic void addTable(String tableName) throws SQLException
tableName - the name of the tableSQLException - exception thrown whenever a problem occurred while
working with the databasepublic boolean hasTable(String tableName) throws SQLException
tableName - the name of the table of interestSQLException - exception thrown whenever a problem occurred while
working with the databasepublic void insertObject(String tableName, String objectKey, Object object, boolean inCache) throws SQLException, IOException, 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 notSQLException - exception thrown whenever an error occurred while
storing the objectIOException - exception thrown whenever an error occurred while
writing in the databaseInterruptedExceptionpublic void insertObjects(String tableName, HashMap<String,Object> objects, WaitingHandler waitingHandler) throws SQLException, 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.SQLExceptionIOExceptionpublic void insertObjects(String tableName, HashMap<String,Object> objects, WaitingHandler waitingHandler, boolean allNewObjects) throws SQLException, 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 newSQLExceptionIOExceptionpublic void loadObjects(String tableName, WaitingHandler waitingHandler) throws SQLException, IOException, ClassNotFoundException, InterruptedException
tableName - the table namewaitingHandler - the waiting handlerSQLException - exception thrown whenever an error occurred while
interrogating the databaseIOException - exception thrown whenever an error occurred while
reading the databaseClassNotFoundException - exception thrown whenever the class of the
object is not found when deserializing it.InterruptedExceptionpublic void loadObjects(String tableName, ArrayList<String> keys, WaitingHandler waitingHandler) throws SQLException, IOException, ClassNotFoundException, InterruptedException
tableName - the table namekeys - the keys of the objects to loadwaitingHandler - the waiting handler, will only be increasedSQLException - exception thrown whenever an error occurred while
interrogating the databaseIOException - exception thrown whenever an error occurred while
reading the databaseClassNotFoundException - exception thrown whenever the class of the
object is not found when deserializing it.InterruptedExceptionpublic Object retrieveObject(String tableName, String objectKey, boolean useDB) throws SQLException, IOException, ClassNotFoundException, 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 cacheSQLException - exception thrown whenever an error occurred while
interrogating the databaseIOException - exception thrown whenever an error occurred while
reading the databaseClassNotFoundException - exception thrown whenever the class of the
object is not found when deserializing it.InterruptedExceptionpublic Object retrieveObject(String tableName, String objectKey, boolean useDB, boolean useCache) throws SQLException, IOException, ClassNotFoundException, 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 cacheSQLException - exception thrown whenever an error occurred while
interrogating the databaseIOException - exception thrown whenever an error occurred while
reading the databaseClassNotFoundException - exception thrown whenever the class of the
object is not found when deserializing it.InterruptedExceptionpublic boolean inDB(String tableName, String objectKey, boolean cache) throws SQLException
tableName - the table nameobjectKey - the object keycache - a boolean indicating whether the cache should be searched as
wellSQLException - exception thrown whenever an exception occurred
while interrogating the databasepublic ArrayList<String> tableContent(String tableName) throws SQLException
tableName - the table to get the content forSQLExceptionpublic HashSet<String> tableContentAsSet(String tableName) throws SQLException
tableName - the table to get the content forSQLExceptionpublic void deleteObject(String tableName, String objectKey) throws SQLException, IOException
tableName - the name of the tableobjectKey - the object keySQLException - exception thrown whenever an error occurred while
interrogating the databaseIOException - exception thrown whenever an error occurred while
interrogating the databasepublic void updateObject(String tableName, String objectKey, Object object) throws SQLException, IOException
tableName - the name of the tableobjectKey - the key of the objectobject - the object to storeSQLException - exception thrown whenever an error occurred while
storing the objectIOException - exception thrown whenever an error occurred while
writing in the databasepublic void updateObject(String tableName, String objectKey, Object object, boolean cache) throws SQLException, 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
cacheSQLException - exception thrown whenever an error occurred while
storing the objectIOException - exception thrown whenever an error occurred while
writing in the databasepublic void close()
throws SQLException
SQLException - exception thrown whenever an error occurred while
closing the database connectionpublic void establishConnection(String aDbFolder, boolean deleteOldDatabase, ObjectsCache objectsCache) throws SQLException
aDbFolder - the folder where the database is locateddeleteOldDatabase - if true, tries to delete the old databaseobjectsCache - SQLException - exception thrown whenever an error occurred while
establishing the connectionpublic String correctTableName(String tableName)
tableName - the table namepublic String correctKey(String tableName, String key)
tableName - the table namekey - the key of the object to be storedCopyright © 2014. All rights reserved.