public class ObjectsDB extends Object implements Serializable
Modifier and Type | Field and Description |
---|---|
static String |
CONNECTION_LOG_TABLE
The name of the table to use to log connections.
|
static String |
derbyConnectionID
The identifier used to register the derby connection in the DerbyUtil
class.
|
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.
|
String |
getPath()
Returns the path to the database.
|
ArrayList<String> |
getTables()
Returns a list of tables present in the database.
|
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.
|
boolean |
isConnectionActive()
Indicates whether the connection to the DB is active.
|
void |
loadObjects(String tableName,
ArrayList<String> keys,
WaitingHandler waitingHandler,
boolean displayProgress)
Loads some objects from a table in the cache.
|
void |
loadObjects(String tableName,
WaitingHandler waitingHandler,
boolean displayProgress)
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 static final String CONNECTION_LOG_TABLE
public static final String derbyConnectionID
public ObjectsDB(String folder, String dbName, boolean deleteOldDatabase, ObjectsCache objectsCache) throws SQLException, IOException, ClassNotFoundException, InterruptedException
folder
- absolute path of the folder where to establish the databasedbName
- name of the databasedeleteOldDatabase
- if true, tries to delete the old databaseobjectsCache
- a cache to store objects without interacting with the
databaseSQLException
- exception thrown whenever a problem occurred when
establishing the connection to the databaseIOException
- exception thrown whenever an error occurred
while reading or writing a fileClassNotFoundException
- exception thrown whenever an
error occurred while deserializing a fileInterruptedException
- exception thrown whenever a
threading error occurred while establishing the connectionpublic 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
interacting with the databasepublic boolean hasTable(String tableName) throws SQLException
tableName
- the name of the table of interestSQLException
- exception thrown whenever a problem occurred while
interacting with the databasepublic ArrayList<String> getTables() throws SQLException
SQLException
- exception thrown whenever a problem occurred while
interacting 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 databaseInterruptedException
- exception thrown whenever a threading error
occurred while interacting with the databasepublic 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.SQLException
- exception thrown whenever an error occurs while
interacting with the databaseIOException
- exception thrown whenever an error occurs while
reading or writing a filepublic 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 newSQLException
- exception thrown whenever an error occurs while
interacting with the databaseIOException
- exception thrown whenever an error occurs while
reading or writing a filepublic void loadObjects(String tableName, WaitingHandler waitingHandler, boolean displayProgress) throws SQLException, IOException, ClassNotFoundException, InterruptedException
tableName
- the table namewaitingHandler
- the waiting handler allowing displaying progress
and cancelling the processdisplayProgress
- boolean indicating whether the progress of this
method should be displayed on the waiting handlerSQLException
- exception thrown whenever an error occurs while
interacting with the databaseIOException
- exception thrown whenever an error occurs while
reading or writing a fileClassNotFoundException
- exception thrown whenever an error
occurred while deserializing a file from the databaseInterruptedException
- exception thrown if a threading error occurs
while interacting with the databasepublic void loadObjects(String tableName, ArrayList<String> keys, WaitingHandler waitingHandler, boolean displayProgress) throws SQLException, IOException, ClassNotFoundException, InterruptedException
tableName
- the table namekeys
- the keys of the objects to loadwaitingHandler
- the waiting handler allowing displaying progress
and cancelling the processdisplayProgress
- boolean indicating whether the progress of this
method should be displayed on the waiting handlerSQLException
- exception thrown whenever an error occurs while
interacting with the databaseIOException
- exception thrown whenever an error occurs while
reading or writing a fileClassNotFoundException
- exception thrown whenever an error
occurred while deserializing a file from the databaseInterruptedException
- exception thrown if a threading error occurs
while interacting with the databasepublic 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 isSQLException
- exception thrown whenever an error occurs while
interacting with the databaseIOException
- exception thrown whenever an error occurs while
reading or writing a fileClassNotFoundException
- exception thrown whenever an error
occurred while deserializing a file from the databaseInterruptedException
- exception thrown if a threading error occurs
while interacting with the databasepublic 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 occurs while
interacting with the databaseIOException
- exception thrown whenever an error occurs while
reading or writing a fileClassNotFoundException
- exception thrown whenever an error
occurred while deserializing a file from the databaseInterruptedException
- exception thrown if a threading error occurs
while interacting with the databasepublic 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 forSQLException
- exception thrown whenever an error occurs while
interacting with the databasepublic HashSet<String> tableContentAsSet(String tableName) throws SQLException
tableName
- the table to get the content forSQLException
- exception thrown whenever an error occurs while
interacting with the databasepublic 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 boolean isConnectionActive()
public 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, IOException, ClassNotFoundException, InterruptedException
aDbFolder
- the folder where the database is locateddeleteOldDatabase
- if true, tries to delete the old databaseobjectsCache
- the objects cacheSQLException
- exception thrown whenever an error occurred while
establishing the connection to the databaseIOException
- exception thrown whenever an error occurred
while reading or writing a fileClassNotFoundException
- exception thrown whenever an
error occurred while deserializing a fileInterruptedException
- exception thrown whenever a
threading 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 storedpublic String getOriginalKey(String tableName, String correctedKey)
tableName
- the tablecorrectedKey
- the corrected key, should be prefix + numberpublic String getPath()
Copyright © 2016. All rights reserved.