Any methods that throw RecordNotFoundException should do so if a specified record does not exist or is marked as deleted in the database file.
Should I need to check whether locking and unlocking an deleted/non-exists record??
If so, the locking code will be very complicated,
while some deleted records cannot be unlocked.
In your API documentation for the lock() method, do you have the instruction "Locks a record so that it can only be updated or deleted by this client."? If so, how are you ensuring that this is achieved?
Not necessarily: you could add a method to verify if a record is valid, and just call that common method from your lock() and unlock() methods. This will keep your code clean and easy to read, and is a good practice.
To use this, the client should only call "lock() ... delete()". The client should not call the unlock() method when doing a delete.
Even if the current thread checks again the validity of a record after it's wait() and before it's lock(), other thread can remove it in the mean time.
In data.delete(recNo), since I synchronized the RandomAccessFile, that means, no other I/O can perform, then it first check the validity of the record. If the record is valid, then it will be deleted, otherwise, RecordNotFoundException thrown. The data.unlock(recNo) removes the lock from the locker.
If we do not synchronize RAF, the case that, the record is valid during the check, but is removed by other thread while the current thread is waiting for the lock. Even if the current thread checks again the validity of a record after it's wait() and before it's lock(), other thread can remove it in the mean time.
By the way, should I provided create(), delete() functions to my wrapper class DataServer? At first, I do not have these functions becos the client will not perform such functions. But I feel it is very inconvenient to test the functions in Data without these (Since my test class actually need to implement those methods for testing purposes).
Should I provided them in my submission, or I should remove them after the testing?
In addition, I wanna ask, what should the unlock() checks?
1. Check whether the record is valid (deleted) in the data file? OR
2. Check whether the record is valid (deleted) in the dataLocker?