Tai Hu

Greenhorn
+ Follow
since Apr 27, 2007
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Tai Hu

Actually the second operator should be recNo > validRecNo.last() I stored all the valid recNo into a SortedSet. So the validRecNo.last() represent the maximum recNo. If a given recNo is larger than the maximum recNo, it is out of range.
Good answer Mark! I think I should reconsider my approach. Initially I used single RAF and synchronized on this single RAF for all methods (read, update, delete and find). However, the problem with this is we lock the whole data file and only allow one thread at a time. So now I switched to use ReetrantReadWriteLock and synchronized delete and update methods on write lock and read, find methods on read lock. To do it this way, we could allow multiple read at the same time. But I have to open RAF in each method calls. I will do a performance test to see if there is a big difference between those 2 approach.
Thanks so much for your answers. I think I will just display everything initially. Then when user do a search, I will filter out the invalid ones.
My recNo is an sequence starting from 0 (same as position in the db file). Then I stored all valid and deleted recNos into 2 separated sortedSet. When
recNo < 0 || recNo > validRecNos.last() + 1 || deletedRecNos.contains(recNo), I through a RecordNotFoundException
I am working on URLyBird(1.1.1) which indicates that CSR only take reservation within 48 hours of start of room occupancy. I am a little confused about this. All the date value in my db file is at least 2 years ago. So if I implemented my logic based on date available field, no room will be available in my GUI. Should I just ignore this date available field and just assume customer could only book the room within 48 hours starting from today? That means I will provide a date selector in my booking GUI. If user select any date, out of the 48 hours range, I will report an error to user. Can I implement it this way? I am open for any suggestions. Thanks!
Yes, I knew. If I synchronized on a single RAF, I locked all the record. But I didn't see how a record based locking could be achieved. For update, delete, with lockCookie, it is ok to allow multiple threads to change data file at the same time. However, how do you handle create() method? Let's same there is one thread is creating a new record at the end of data file. Before it writes out all the bytes. There is another thread coming in for update() method. At that moment, your new RAF in update() method will open a partially completed file. I am not sure if RAF could handle this thing. Also as I mentioned before, you may need to keep a counter for all the opening RAF to limit the total number of file handler.
I think as long as you synchronized your RandomAccessFile object each time you use it. It should be fine to have one RandomAccessFile object for whole Data class. Opening RAF for each method (update, create, and delete) could create too much open file handlers (Linux only allow 256 physical file handlers, Windows also has limitation but it is bigger). By the way, finalize() method is not guaranteed to be called.
I think for each place you used wait(), you need to call notifyAll() at the end. Otherwise there is possible that waiting thread will not be waken up forever. For instance, you have 2 threads are waiting for a record becoming unlock. However, all the active threads (threads called lock method but not called unlock methods yet) are crashed before calling unlock() methods. Then your 2 waiting threads will be waiting forever.
Hi Markus, I really agree with your approach and indeed I implemented my URLyBird (v1.1.1) using the similar strategy. However, I have one question about this approach. If SCJD grader used an automatic tool to test DBMain directly, can we assume that their testing method will call lock method before calling update, delete method and call unlock method afterwards? If not, since we put locking strategy into an higher level class, direct calling update, delete method will mess up the data.