Package com.compomics.util.db.object
Class ObjectsDB
java.lang.Object
com.compomics.util.db.object.ObjectsDB
public class ObjectsDB extends Object
A database which can easily be used to store objects.
- Author:
- Marc Vaudel, Dominik Kopczynski, Harald Barsnes
-
Field Summary
Fields Modifier and Type Field Description com.esotericsoftware.kryo.Kryo
kryo
Configuration for fast serialization. -
Constructor Summary
-
Method Summary
Modifier and Type Method Description void
close(boolean saveCache)
Closes the db connection.void
commit()
Committing all changes into the database.void
dumpToDB()
Triggers a dump of all objects within the cache into the database.void
establishConnection()
Establishes connection to the database.HashSet<Long>
getClassObjectIDs(Class className)
Returns an iterator of all objects of a given class.HashSet<Long>
getClassObjectIDs(Class className, String filters)
Returns an iterator of all objects of a given class.int
getCurrentAdded()
Getter for the current number of added objects.Connection
getDB()
Getter for the persistence manager.File
getDbFile()
Getter for the database file.File
getDbFolder()
Getter for the database folder.HashSet<Long>
getKeysInBackend()
Returns the keys in backend set.String
getName()
Returns the database name.int
getNumber(Class className)
Returns the number of instances of a given class stored in the db.ObjectsCache
getObjectsCache()
Returns the cache used by this database.String
getPath()
Returns the path to the database.boolean
inBackend(long objectKey)
Indicates whether an object is loaded in the backend.boolean
inCache(long objectKey)
Indicates whether an object is loaded in cache.boolean
inDB(long objectKey)
Indicates whether an object is in the cache or the database.void
insertObject(long objectKey, Object object)
Stores an object in the desired table.void
insertObjects(HashMap<Long,Object> objects, WaitingHandler waitingHandler, boolean displayProgress)
Inserts a set of objects in the given table.static boolean
isConnectionActive()
Indicates whether the connection to the DB is active.void
loadObjects(Class className, WaitingHandler waitingHandler, boolean displayProgress)
Loads all objects from a given class.void
loadObjects(Collection<Long> keys, WaitingHandler waitingHandler, boolean displayProgress)
Loads objects from a table in the cache.void
lock(WaitingHandler waitingHandler)
Locking the db for storing.void
removeObject(long key)
Removing an object from the cache and database.void
removeObjects(Collection<Long> keys, WaitingHandler waitingHandler, boolean displayProgress)
Removing an object from the cache and database.Object
retrieveObject(long objectKey)
Retrieves an object from the database or cache.ArrayList<Object>
retrieveObjects(Class className, WaitingHandler waitingHandler, boolean displayProgress)
Retrieves all objects from a given class.ArrayList<Object>
retrieveObjects(Collection<Long> keys, WaitingHandler waitingHandler, boolean displayProgress)
Retrieves some objects from the database or cache.static void
setDebugInteractions(boolean debug)
Turn the debugging of interactions on or off.void
setObjectCache(ObjectsCache objectCache)
Sets the object cache to be used by this database.void
unlock()
Unlocking the db after storing.void
updateObject(long objectKey, Object object)
-
Field Details
-
kryo
public com.esotericsoftware.kryo.Kryo kryoConfiguration for fast serialization.
-
-
Constructor Details
-
ObjectsDB
public ObjectsDB()Empty default constructor. -
ObjectsDB
Constructor.- Parameters:
folder
- absolute path of the folder where to establish the databasedbName
- name of the database
-
ObjectsDB
Constructor.- Parameters:
path
- absolute path of the folder where to establish the databasedbName
- name of the databaseoverwrite
- overwriting old database
-
-
Method Details
-
commit
public void commit()Committing all changes into the database. -
getCurrentAdded
public int getCurrentAdded()Getter for the current number of added objects.- Returns:
- the current number of added objects
-
getDbFile
Getter for the database file.- Returns:
- the database file
-
getDbFolder
Getter for the database folder.- Returns:
- the database folder
-
getDB
Getter for the persistence manager.- Returns:
- the persistence manager
-
getName
Returns the database name.- Returns:
- the database name
-
getObjectsCache
Returns the cache used by this database.- Returns:
- the cache used by this database
-
setObjectCache
Sets the object cache to be used by this database.- Parameters:
objectCache
- the object cache to be used by this database
-
insertObject
Stores an object in the desired table. When multiple objects are to be inserted, use insertObjects instead.- Parameters:
objectKey
- the key of the objectobject
- the object to store
-
getClassObjectIDs
Returns an iterator of all objects of a given class.- Parameters:
className
- the class name- Returns:
- the iterator
-
getClassObjectIDs
Returns an iterator of all objects of a given class.- Parameters:
className
- the class namefilters
- string with filters;- Returns:
- the iterator
-
insertObjects
public void insertObjects(HashMap<Long,Object> objects, WaitingHandler waitingHandler, boolean displayProgress)Inserts a set of objects in the given table.- Parameters:
objects
- 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.displayProgress
- boolean indicating whether the progress of this method should be displayed on the waiting handler
-
loadObjects
public void loadObjects(Collection<Long> keys, WaitingHandler waitingHandler, boolean displayProgress)Loads objects from a table in the cache.- Parameters:
keys
- the keys of the objects to loadwaitingHandler
- the waiting handler allowing displaying progress and canceling the processdisplayProgress
- boolean indicating whether the progress of this method should be displayed on the waiting handler
-
loadObjects
Loads all objects from a given class.- Parameters:
className
- the class name of the objects to be retrievedwaitingHandler
- the waiting handler allowing displaying progress and canceling the processdisplayProgress
- boolean indicating whether the progress of this method should be displayed on the waiting handler
-
retrieveObject
Retrieves an object from the database or cache.- Parameters:
objectKey
- the key of the object to load- Returns:
- the retrieved object
-
getNumber
Returns the number of instances of a given class stored in the db.- Parameters:
className
- the class name of the objects to be load- Returns:
- the number of objects
-
dumpToDB
public void dumpToDB()Triggers a dump of all objects within the cache into the database. -
retrieveObjects
public ArrayList<Object> retrieveObjects(Collection<Long> keys, WaitingHandler waitingHandler, boolean displayProgress)Retrieves some objects from the database or cache.- Parameters:
keys
- the keys of the objects to loadwaitingHandler
- the waiting handler allowing displaying progress and canceling the processdisplayProgress
- boolean indicating whether the progress of this method should be displayed on the waiting handler- Returns:
- a list of objects
-
updateObject
-
retrieveObjects
public ArrayList<Object> retrieveObjects(Class className, WaitingHandler waitingHandler, boolean displayProgress)Retrieves all objects from a given class.- Parameters:
className
- the class name of the objects to be retrievedwaitingHandler
- the waiting handler allowing displaying progress and canceling the processdisplayProgress
- boolean indicating whether the progress of this method should be displayed on the waiting handler- Returns:
- the list of objects
-
removeObjects
public void removeObjects(Collection<Long> keys, WaitingHandler waitingHandler, boolean displayProgress)Removing an object from the cache and database.- Parameters:
keys
- the object keywaitingHandler
- the waiting handler allowing displaying progress and canceling the processdisplayProgress
- boolean indicating whether the progress of this method should be displayed on the waiting handler
-
removeObject
public void removeObject(long key)Removing an object from the cache and database.- Parameters:
key
- the object key
-
inCache
public boolean inCache(long objectKey)Indicates whether an object is loaded in cache.- Parameters:
objectKey
- the object key- Returns:
- a boolean indicating whether an object is loaded
-
inDB
public boolean inDB(long objectKey)Indicates whether an object is in the cache or the database.- Parameters:
objectKey
- the object key- Returns:
- a boolean indicating whether an object is loaded
-
inBackend
public boolean inBackend(long objectKey)Indicates whether an object is loaded in the backend.- Parameters:
objectKey
- the object key- Returns:
- a boolean indicating whether an object is loaded
-
getKeysInBackend
Returns the keys in backend set.- Returns:
- the keys in backend set
-
isConnectionActive
public static boolean isConnectionActive()Indicates whether the connection to the DB is active.- Returns:
- true if the connection to the DB is active
-
lock
Locking the db for storing.- Parameters:
waitingHandler
- the waiting handler
-
unlock
public void unlock()Unlocking the db after storing. -
close
public void close(boolean saveCache)Closes the db connection.- Parameters:
saveCache
- clearing all database structures
-
establishConnection
public void establishConnection()Establishes connection to the database. -
getPath
Returns the path to the database.- Returns:
- the path to the database
-
setDebugInteractions
public static void setDebugInteractions(boolean debug)Turn the debugging of interactions on or off.- Parameters:
debug
- if true, the debugging is turned on
-