|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--suncertify.db.Data
Data class is a simple implementation of database access class. Implementation is based on RandomAccessFile class. It does not cover the most significat problems of file access eg.:
- reallive implementation should consider the problem of inconsistent data file
- effective alghoritms for searching like indexing
- effective alghoritms for inserting data (pointer to the first free entry)
- data caching should be consider
All above problems were consider not crucial for thie solution of SUN assignement and the following solution was proposed: - implmentation works on the one copy of db file - and system crash during writting may destory its consistenty - implemntation does not use indexes for speedup
Locking - general concept while implmenting the lock machnism was to put all lock information in the map indexed by the record id. This makes finding lock information for the specified record quite simple. Another approach was to keep in the map the information about the client locking the row - the clientId string. The problem was that we may not do wait on this objects as it use to change - if more then two clients wait for the unlock the whole idea has to be refine. To overcome the problem the supported class was created - LockObject. This class know the current id of the client owning the row and has one object (that does not chage when client id changes) to wait on for all the clients.
Field Summary | |
static int |
MAGIC_COOKIE
The value of magic cookie that maut be at the begginig of a data file |
Constructor Summary | |
Data()
Creates new instance of Data class using default filename as DBfile |
|
Data(java.lang.String fileName)
Creates new instance of Data class using the specified fielName as DBfile |
Method Summary | |
void |
closeDB()
The method closes the db file - created for secure server shutdown you should never use the Data object after calling this method!! |
int |
create(java.lang.String[] data)
Creates a new record in the database (possibly reusing a deleted entry). |
void |
delete(int recNo)
Deletes a record, making the record number and associated disk storage available for reuse. |
int[] |
find(java.lang.String[] criteria)
Returns an array of record numbers that match the specified criteria. |
boolean |
isLocked(int recNo)
Determines if a record is currenly locked. |
void |
lock(int recNo)
Locks a record so that it can only be updated or deleted by this client. |
java.lang.String[] |
read(int recNo)
Reads a record from the file. |
void |
unlock(int recNo)
Releases the lock on a record. |
void |
update(int recNo,
java.lang.String[] data)
Modifies the fields of a record. |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final int MAGIC_COOKIE
Constructor Detail |
public Data() throws IncorrectDataFile, java.io.IOException
java.io.IOException
- - is thrown if file does not exist or cannot be opened
IncorrectDataFile
- - is thrown when magic cookie value is incorrectPropertiesWrapper.getDbFileName()
,
MAGIC_COOKIE
public Data(java.lang.String fileName) throws java.io.IOException, IncorrectDataFile
fileName
- - the name of the file with data to be opened
java.io.IOException
- - is thrown if file does not exist or cannot be opened
IncorrectDataFile
- - is thrown when magic cookie value is incorrectMAGIC_COOKIE
Method Detail |
public java.lang.String[] read(int recNo) throws RecordNotFoundException
read
in interface DBMain
recNo
- the index of the requested record
RecordNotFoundException
- is thrown if a specified record does not exist or is
marked as deleted in the database file.DBMain.read(int)
public void update(int recNo, java.lang.String[] data) throws RecordNotFoundException
update
in interface DBMain
recNo
- the index of the record to operate ondata
- the new set of data for the requested record
RecordNotFoundException
- is thrown if a specified record does not exist or is
marked as deleted in the database file.# update(int, java.lang.String[])
public void delete(int recNo) throws RecordNotFoundException
delete
in interface DBMain
recNo
- the index of the record to delete
RecordNotFoundException
- is thrown if a specified record does not exist or is
marked as deleted in the database file.lock(int)
,
DBMain.delete(int)
public int[] find(java.lang.String[] criteria) throws RecordNotFoundException
find
in interface DBMain
criteria
- String array representing the criteria for each record field
RecordNotFoundException
- is thrown if a specified record does not exist or is
marked as deleted in the database file.DBMain.find(java.lang.String[])
public int create(java.lang.String[] data) throws DuplicateKeyException
create
in interface DBMain
data
- the array string representig values of the fields of the new record
DuplicateKeyException
DBMain.create(java.lang.String[])
public void lock(int recNo) throws RecordNotFoundException
If a row is already locked by the current thread nothing happen - if we make thread owing the lock waiting for unlock it would wait forever.
lock
in interface DBMain
recNo
- the index of the record to lock
RecordNotFoundException
- is thrown if a specified record does not exist or is
marked as deleted in the database file.DBMain.lock(int)
public void unlock(int recNo) throws RecordNotFoundException
unlock
in interface DBMain
recNo
- the index of the record to unlock
RecordNotFoundException
- is thrown if a specified record does not exist or is
marked as deleted in the database file.DBMain.unlock(int)
public boolean isLocked(int recNo) throws RecordNotFoundException
isLocked
in interface DBMain
recNo
- the index of the record to check
RecordNotFoundException
- is thrown if a specified record does not exist or is
marked as deleted in the database file or in case of problem with file access.DBMain.isLocked(int)
public void closeDB()
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |