int create(String[] data) throws DuplicateKeyException {
// look for a deleted record
// when you find a candidate deleted record then
synchronized (raf)
{
// if record is deleted then write the new record here
// if record is not deleted then you'll have to keep looking
// for a deleted record
}
// if you have overwritten a deleted record then you're done
// if you haven't overwritten a deleted record then you have to
// append at the end of the file
synchronized (raf)
{
// add the new record to the end of the file
}
}
I think this takes care of the simultaneous overwrite problem. Anytime the database file is accessed the code is placed in a block synchronized on the raf.
From your mention(2),i think about that you are right.I don't know whether
i understank your suggestion.My opinion is : In my design,i create new Data
instance for each network client,so in my create() method, i must synchronize all of actual database accessing on raf static variabe.so do i can guaranteer that has not corrupt state of database file.
In your design,because you has only one Data instance for all clients(also
singleton Data is shared by all of remote object),so you use synchronize method instead of synchronize block on raf.Am i right?
If i am right. then go to your codes of bold fonts,this process also has
actual datafile accessing,Whehter it also should be put into synchronized block?
Plese you give me some comments about this?
A clear design, such as will be readily understood by junior programmers, will be preferred to a complex one, even if the complex one is a little more efficient.
Now i am thinking about your design(use Data singleton),and i think this scheme is more simple than me.
Regards, George
SCJP, SCJD, SCWCD, SCBCD
You might want to put the whole thing in a block synchronized on the raf.
Locking
Your server must be capable of handling multiple concurrent requests, and as part of this capability, must provide locking functionality as specified in the interface provided above. You may assume that at any moment, at most one program is accessing the database file; therefore your locking system only needs to be concerned with multiple concurrent clients of your server. Any attempt to lock a resource that is already locked should cause the current thread to give up the CPU, consuming no CPU cycles until the desired resource becomes available.
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Another question is whether when at the end of file,any write() or skipByte() will not move the file pointer?because in API doc,i could not find any discussion about this point.Could you give me some detail about the file pointer?
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
// Returns an array of record numbers that match the specified
// criteria. Field n in the database file is described by
// criteria[n]. A null value in criteria[n] matches any field
// value. A non-null value in criteria[n] matches any field
// value that begins with criteria[n]. (For example, "Fred"
// matches "Fred" or "Freddy".)
public int[] find(String[] criteria);
It must allow the user to search the data for all records, or for records where the name and/or location fields exactly match values specified by the user.
It must present search results in a JTable.
Your understanding of the logic of requirement is technically correct. 1 and 2 are relatively easy to implement. Implementing
3 is difficult to do in a single search. Many people (maybe most people) are not supporting 3 in a single search. Instead
they are supporting 3 as two independent searches.
3a: records where the name field exactly matches values:
(criteria[] name null null null null null), and
3b: records where the location field exactly matches values:
(criteria[] null, location, null, null, null, null)
Hi Andrew,i will modify my code again from original location.pelease you see if i understand what is your suggestion?
For bold fonts above please you give me some comments about it?
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Hi George and Andrew:
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Originally posted by liqun chang:
My purpose is only put actual file accessing code in synchronized block on
raf static variable.Certainly,some codes that don't relate to actual file
accessing should not be placed in synchronized block. Am i right?
Regards, George
SCJP, SCJD, SCWCD, SCBCD
Originally posted by liqun chang:
The primary purpose of file lock is:at the same time ,only one program is
accessing the data file(as the mention of bold fonts).so do can guarantee
maintaining the valid datafile state.Am i right?
Yes.
The purpose of record lock is:if a alive client(also a thread) is locking a record(possible at the same time many threads lock many different records),
other client(thread) cann't lock this locked record at the same time.This situation guarantee each client is dealing with itself record.
So for read(),delete() and update(),we can use record lock on these methods
for exclusive reading/writting,For create() and find(),we should not use record lock on these methods for concurrent reading/writting.Am i right?Yes
Regards, George
SCJP, SCJD, SCWCD, SCBCD
Originally posted by liqun chang:
1: In create method,i don't declare DuplicateKeyException,because the specification is not clear,i want document it to doc.
I will wrap IOException and other exception into DataAccessException(a subclass of RuntimeException).Am i right?
Yes, given your assignment (URLyBird) that seems to be the best way to handle the DuplicateKeyException. Yes.
2: Because the codes of actual accessing data file are too much,i decide to put most of codes in synchronized block on raf.
perhaps this has lower performance.Please you help me? whether has other skill?
Seems reasonable to me.
3: The most important is: whether my logic algorithm is right? Sorry for so material question,because you are a good teacher. I like discuss with you and very trust you.
Your algorithm generally looks OK to me. But you shouldn't rely just on reading the code. I hope you're planning on unit testing the Data class. Especially you should test the boundary conditions. What happens: when you create a record in a data file that contains no other records, when you create a record in a data file that only contains deleted records, when you create a record in a data file that doesn't contain any deleted records? Try creating a record in a file without any deleted records. Then create another record in that file. Try a data file without any deleted records, delete a record, then create a record. Does the created record end up where you would expect? The point is I'm not going to be as good at catching errors reading the code as unit testing will be.
Regards, George
SCJP, SCJD, SCWCD, SCBCD
The only remaining problem is that section which I had a comment in bold for before - where you are writing to the disk, but your comment next to the write statement says that you are doing a read.
Sorry, it doesnt work like that. You need to develop the submission yourself. There is no value in me providing potential solutions to you - you will not have gone through the learning exercise yourself.
If you would like to suggest some ideas for any topic and get feedback on whether it is workable or not, that is OK.
Also it is a good idea not to post questions directly to individuals like this - other people may decide not to answer since the question is addressed to George and myself.
Regards, George
SCJP, SCJD, SCWCD, SCBCD
Roses are red, violets are blue. Some poems rhyme and some don't. And some poems are a tiny ad.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|