Package com.flora.db
Class FloraDB
java.lang.Object
com.flora.db.FloraDB
This is the main class of the database. This class is a utility class that provides static methods to perform
database operations. You have to initialize this by
init(File)
method before calling any of the database method.
Otherwise, a NullPointerException
will be thrown. You also need to commit (commit()
) to the database to
save the changes. Not all methods requires committing. Methods that requires committing are clearly stated in the javadoc of
that method.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
Callback forcommitAsync()
method.static interface
An interface that is used to update a list or a object in the database. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
commit()
Saves and commits all the changes to database.static void
commitAsync
(FloraDB.Callback callback) Similar ascommit()
but runs asynchronously.static <T> void
createList
(String id, T[] items) Creates and inserts a new list into the database.static <T extends Syncable>
voidcreateObject
(String id, T object) Creates and inserts a new object into the database.static <T> List<T>
Creates a new syncable mutable list or gets if already exist.static <T extends Syncable>
TcreateOrGetObject
(String id, Class<T> objClass) Creates a new syncable mutable object (POJO) or gets if already exist.static void
Does as the name suggest.static Object
Gets the simple data from the database.static <T> List<T>
Gets a non-syncable immutable list from the database.static <T extends Syncable>
TGets a syncable object (POJO) from the database.static void
Initialize the database with the directory provided.static void
Inserts a simple datatype into the database.static Query
query()
static <T> void
updateList
(String id, FloraDB.SyncedDataManager<List<T>> manager) Updates the list asynchronously from the interface deification.static <T> void
updateObject
(String id, FloraDB.SyncedDataManager<T> manager) Updates the object asynchronously from the interface deification.
-
Constructor Details
-
FloraDB
public FloraDB()
-
-
Method Details
-
init
Initialize the database with the directory provided. This may took longer time depending on your data size. This method runs synchronously (and it should be) and blocks thread until completed.- Parameters:
databaseDir
- The directory where your database is located or should be created. Usually file dir of your application- Throws:
RuntimeException
- If file is not a directory or is not writable
-
put
Inserts a simple datatype into the database. Updates if id already exist. Requirescommit()
call.- Parameters:
id
- ID of the data (like primary key)data
- A String,Number or boolean value to insert into database
-
get
Gets the simple data from the database. (data inserted usingput(String, Object)
method- Parameters:
id
- The unique id of the data.defaultValue
- Value in case it does not exist in the database- Returns:
- Value or defaultValue if none found.
-
createList
Creates and inserts a new list into the database. Updates if it already exist. You need to callcommit()
for this.- Parameters:
id
- The id of the listitems
- The default items in the list
-
updateList
Updates the list asynchronously from the interface deification. TheFloraDB.SyncedDataManager.update(Object)
method contains a list object. That list is the the list which is saved in the database. You can modify it and it will be reflected in the database. You don't need to callcommit()
for this.- Parameters:
id
- The id of the listmanager
- Interface defining the list- Throws:
IllegalArgumentException
- if there is no list with the id
-
createOrGetList
Creates a new syncable mutable list or gets if already exist. The returned list will be syncable and mutable, it means that any modification made to it will also be modified in the database. You need to however callcommit()
after calling this if you had modified it, Otherwise not.- Parameters:
id
- The id of the list- Returns:
- A syncable and mutable list
-
getList
Gets a non-syncable immutable list from the database. null if not exist- Parameters:
id
- Id of the list- Returns:
List
or null if not exist in the database- Throws:
ClassCastException
- If the id is not of a list
-
createObject
Creates and inserts a new object into the database. Updates if already exist Requirescommit()
call.- Parameters:
id
- The id of the listobject
- Your POJO
-
createOrGetObject
Creates a new syncable mutable object (POJO) or gets if already exist. The returned object will be syncable and mutable, it means that any modification made to it will also be modified in the database. You need to however callcommit()
after calling this if you had modified it, Otherwise not.- Parameters:
id
- The id of the objectobjClass
- The class type of your object.- Returns:
- A syncable and mutable Object of generic type
- Throws:
ClassCastException
- If id is not of a valid syncable object
-
getObject
Gets a syncable object (POJO) from the database. Mutable means that the object returned by this method can be further modified. You need to however callcommit()
after calling this if you had modified it, Otherwise not.- Type Parameters:
T
- Type class of your POJO- Parameters:
id
- Id of the object- Returns:
- a POJO or null if not exist in the database
- Throws:
IllegalArgumentException
- If the id is not of a 'type' object or if the object cannot be casted to 'type' class.
-
updateObject
Updates the object asynchronously from the interface deification. TheFloraDB.SyncedDataManager.update(Object)
method contains a provided class object. That object is the the POJO which is saved in the database. You can modify it and it will be reflected in the database. You don't need to callcommit()
for this.- Type Parameters:
T
- Type class of your POJO- Parameters:
id
- The id of the objectmanager
- Interface defining the object- Throws:
IllegalArgumentException
- if there is no object with the id
-
delete
Does as the name suggest. It requirecommit()
call- Parameters:
id
- Id of the data to delete
-
query
-
commit
public static void commit()Saves and commits all the changes to database. This must be the last call on database as calling this will nullify and close all the streams related to database.- Throws:
RuntimeException
- A checked-runtime exception when an IOException occurs. Runtime because there are extremely low chances of occurring most common types if IOException like FileNotFoundException (already handled while initializing). This may only occur when the underlying filesystem throws it. It is recommended not to handle it as it would indicate serious bug.
-
commitAsync
Similar ascommit()
but runs asynchronously. This will not block the thread.- Parameters:
callback
- A optional callback that indicates success and failure of the commit.
-