public class SafeDBAccess extends Object
Modifier and Type | Field and Description |
---|---|
protected EntriesView |
anyView |
protected File |
directory |
boolean |
disableRestore
Set this true to temporarily disable restoring from a backup.
|
boolean |
isUpdating
If the Database is open for updating, this will be true.
|
Gate |
lock
All operations are synchronized on this lock.
|
DatabaseMaker |
maker
This is the DatabaseMaker used to open the Database.
|
protected String |
name |
Database |
opened
This is the currently open Database.
|
String |
openMode
If the Database is opened, this is the mode it was opened in.
|
protected String |
previousMode |
protected String[] |
sortNames |
protected int |
updateCount |
protected Hashtable |
views |
Constructor and Description |
---|
SafeDBAccess(File directory,
String dbName,
String[] sortNames)
Create a SafeDBAccess object.
|
Modifier and Type | Method and Description |
---|---|
boolean |
abortUpdate()
Abort the update process, close the db and do not rename the temp file to ".db".
|
boolean |
backupExists()
Check if a backup for the database exists.
|
boolean |
backupNow()
This causes the database to be closed (if it is not open for updating) and then
backed up.
|
boolean |
close()
Close the database if it is not open for updating. endUpdate() should be used for
when closing after an update.
|
void |
closeCreate()
Call this after initializing with openForCreate().
|
ByteArray |
copyToMemory(ByteArray dest)
This closes the database and copies the latest version to memory.
|
boolean |
create(Object initializer)
This calls openForCreate() and then calls closeCreate().
|
protected void |
databaseClosed()
This is called when the database has been closed.
|
boolean |
databaseExists()
Check if the database does exist.
|
protected void |
databaseOpened(Database db,
String mode,
boolean forUpdate)
This gets called when the DB has been successfully opened.
|
long |
dbSize()
This can only be called if the DB is closed.
|
boolean |
endUpdate()
End the update process.
|
EntriesView |
getAnyView() |
Time |
getBackupModifiedTime()
Return the time the backup was last made.
|
Time |
getDatabaseModifiedTime(Time dest)
Return the time the dabase was last modified.
|
EntriesView |
getView(String sortName)
This gets the EntriesView created when the Database was opened for the specified
sortName.
|
protected void |
makeBackup()
This makes a backup of the database.
|
void |
memoryToDB(byte[] src,
int offset,
int length)
Replace the DB with the provided bytes.
|
Database |
open(String mode)
Open the database in either "r" or "rw" mode.
|
Database |
openForCreate(Object initializer)
Open the database for creation only if it does not already exist.
|
protected void |
restore()
This restores the dabase from the backup file if one exists.
|
Database |
startUpdate() |
Database |
startUpdate(boolean appendMode)
Close the database if it is open, make a backup and then open it in "rw" or "a" mode
for updating.
|
long |
timeSinceBackup(Time since)
Return the time elapsed in milliseconds between the last backup time and
the provided Time.
|
void |
verifyBackup()
If no backup exists at all, make it now.
|
protected File directory
protected String name
protected String[] sortNames
protected String previousMode
protected int updateCount
protected Hashtable views
protected EntriesView anyView
public Database opened
public String openMode
public boolean isUpdating
public Gate lock
public DatabaseMaker maker
public boolean disableRestore
public SafeDBAccess(File directory, String dbName, String[] sortNames)
directory
- The directory which contains the database.dbName
- The root name (without the ".db" extension) of the database.sortNames
- an optional set of sortNames for which EntriesView objects
will be created.public EntriesView getView(String sortName)
sortName
- one of the sortNames provided in the constructor.public EntriesView getAnyView()
protected void databaseOpened(Database db, String mode, boolean forUpdate)
db
- the open database.mode
- the mode it was opened in.forUpdate
- true if has been opened for updating.protected void databaseClosed()
protected void restore() throws IOException
IOException
- if disableRestore is true.RestoreException
- if there is any problem restoring the database, including a missing
or invalid backup zip file.protected void makeBackup() throws BackupException
BackupException
- if an error occurs.public boolean close() throws IOException
IOException
- on an IOError.public Database open(String mode) throws IOException
mode
- either "r" or "rw" - any other value will generate an IllegalArgumentExceptionIOException
- on any IO error.IllegalArgumentException
- if the mode is invalid.public boolean create(Object initializer) throws IOException
initializer
- the initializer used to initialize the database, which must be
non-null.IOException
- if an IO error occurs during creation.public Database openForCreate(Object initializer) throws IOException
initializer
- an optional Object used to initialize the sorts and fields.IOException
- if an error occurs.public void closeCreate() throws IOException
IOException
public Database startUpdate(boolean appendMode) throws IOException
You can call startUpdate() multiple times but for each call to startUpdate() there must be a corresponding call to endUpdate(). Update mode is only exited when the last endUpdate() is called.
IOException
- if an IO error occurs.public Database startUpdate() throws IOException
IOException
public boolean abortUpdate() throws IOException
IOException
- if an IOException occurs.public boolean endUpdate() throws IOException
IOException
- if an IOException occurs.public boolean backupNow() throws IOException
IOException
- if an IO error occurs.public void verifyBackup() throws IOException
IOException
- if an IO error occurs.public boolean backupExists()
public Time getBackupModifiedTime() throws IOException
dest
- an optional destination Time.IOException
public long timeSinceBackup(Time since) throws IOException
since
- the Time to compare to the backup time, or null to use the current time.IOException
public boolean databaseExists() throws IOException
IOException
public Time getDatabaseModifiedTime(Time dest) throws IOException
dest
- an optional destination Time.IOException
public long dbSize()
public ByteArray copyToMemory(ByteArray dest) throws IOException
dest
- a destination ByteArray that the bytes will be appended to.IOException
public void memoryToDB(byte[] src, int offset, int length) throws IOException
src
- the bytes to write as the DB.offset
- the start of the bytes to write.length
- the number of bytes to write.IOException