I am currently just starting out and trying to implement my Data.java
access class. I am following the KISS principle: I'd rather have a
clear and simple, slower solution than a complex, fast solution that
is prone to bugs.
Thus, for simplication, there is no caching of the data, and no
NIO. There's also no fancy singleton
patterns and a single table is
assumed.
My implementation is as follows:
Data.java:
RandomAccessFile raf;
HashMap locks;
Data(..):
initialization (raf mode rwd)
validates magic cookie
synchronized read(int recNo):
reads the raf directly and gets the record
synchronized update(int recNo);
writes to the raf directly
lock:
synchronized on locks HashMap and does a lock
unlock:
synchronized on locks HashMap and unlocks the record
islocked:
synchronized on locks HashMap and returns whether it
is locked.
This is as a simple as I can get it. It ensures that only one client
may read, update, write, and delete, thus guaranteeing that only one
client can touch the raf at a time.
But the documentation states:
"Your server must be capable of handling multiple concurrent
requests."
My implementation prevents concurrent reads to the file, and thus two
people can't simultaneously pull two different records (one will have
to wait a short time).
I assume that the above quote simply means that multiple people should
be able to Book different records simultaneously, through the use of
lock, unlock, and isLocked. That is, by concurrent requests, they mean
at a higher, logical level (through locks), rather than at the file
access level.
Am I correct in this assumption? Is the implementation above okay?
[ May 15, 2005: Message edited by: Titus Barik ]