Best Regards<br /> <br />SCJP1.4 SCJD1.4 SCEA
// Reads a record from the file. Returns an array where each
// element is a record value.
public String[] read(int recNo) throws RecordNotFoundException
public String[] read(int recNo) throws RecordNotFoundException
{
Sting[] data;
//check whether recNo is validity
isValidated(recNo);
//if not then throw RecordNotFoundException
//else go on
synchronized(raf)
{
raf.seek(position);
raf.read();
}
//convert the byte[] to the String[] data
return data;
}
isValidated(recNo)
{
synchronized(raf)
{
raf.seek();
raf.read();
}
//check recNo validity,can throw RecordNotFoundException if recNo is invalid.
}
Originally posted by liqun chang:
I will create static RandomAccessFile variable(be shared by all Data instances) as mutex,in DB interface there are really two kind of actions concerns data file. read(involves read and find) and write(involves update and delete and create).All of actual file access of each method(read,find,update,delete and create) are put into synchronized block on raf.
So do,i can guarantee the valid data(although some time is dated)without
corrupted state.If i want get latest value of record,i can use lock() and
read() and unlock() to accomplish task.
Am i right?
Regards, George
SCJP, SCJD, SCWCD, SCBCD
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.
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.
Originally posted by liqun chang:
1: Also in read method,there are twice raf.seek calls,how could i avoid this situation.You can see,although read method has not potential deadlock,but it is low efficiency. Could you give me some suggestion about this situation and provide yourself design?
Regards, George
SCJP, SCJD, SCWCD, SCBCD
Originally posted by liqun chang:
From instruction,I find that read(),update(),delete()method can implements easily.but on create method,i am confusion,because
i must do below:
1: get length of datafile
2: length minus head
3: seek start of record data
4: sequence search deleted flags,if find write new record to datafile
5: if not find deleted flags,then write new record to the end of datafile
Could you give me some suggestions about this method,or you have other implementation?
Regards, George
SCJP, SCJD, SCWCD, SCBCD
Originally posted by liqun chang:
Is the mean three situation:
1: all records:also criteria[] null null null null null null (because have six fields except owner)
2: records where the name and location fields exactly match values:
(criteria[] name null null null null null)&&(criteria[] null location null null null null)
3: records where the name or location fields exactly match values:
(criteria[] name null null null null null)||(criteria[] null location null null null null)
Another question is whether exactly match means that case sensitive.please you give me comments?
Regards, George
SCJP, SCJD, SCWCD, SCBCD
Originally posted by liqun chang:
public String[] read(int recNo) throws RecordNotFoundException
{
Sting[] data;
//check whether recNo is validity
isValidated(recNo);
//if not then throw RecordNotFoundException
//else go on
synchronized(raf)
{
raf.seek(position);
raf.read();
}
//convert the byte[] to the String[] data
return data;
}
isValidated(recNo)
{
synchronized(raf)
{
raf.seek();
raf.read();
}
//check recNo validity,can throw RecordNotFoundException if recNo is invalid.
}
As you can see,if i implements read as above,then can occur the follow questions:
1: Also in read method,there are twice raf.seek calls,how could i avoid this situation.You can see,although read method has not potential deadlock,but it is low efficiency. Could you give me some suggestion about this situation and provide yourself design?
Originally posted by liqun chang:
Hi Andrew and George,According to your suggestion.I will sum up my design for read/write lock.However if i have mistakes,please you give me some comments on it?
I will create static RandomAccessFile variable(be shared by all Data instances) as mutex,in DB interface there are really two kind of actions concerns data file. read(involves read and find) and write(involves update and delete and create).All of actual file access of each method(read,find,update,delete and create) are put into synchronized block on raf.
So do,i can guarantee the valid data(although some time is dated)without
corrupted state.If i want get latest value of record,i can use lock() and
read() and unlock() to accomplish task.
Am i right?
From today,i will ask you questions that is not over 4.because you are busy
on yourself job.
Originally posted by George Marinkovich:
Here are some suggestions about the create method:
0) You should calculate the start location of the record data when you finish reading in the header and database schema. This doesn't change once the database file has been opened.
1) I don't think you need to be concerned with locking in the create method. You're only interested in deleted records which shouldn't be locked.
2) Check to see if new record will cause a duplicate key exception, and if so throw the exception.
3) Get the first record in the database that has been deleted, and if you find one, mark the record as valid, and write the new record to this location.
4) If you don't find a deleted record to reuse, then append the new record to the end of the database file.
Satish, about DuplicateKeyException:
It seems there are couple of solutions to this. One is to ignore it as stated by Phil once and document it.I also remember that Mark is the only one to stick on to the idea that create method will throw DuplicateKeyException. Can you throw some light on how to check for the DuplicateKeyException?
Originally posted by Philippe Maquet:
Hi Liqun, Satish and George,
Mmh... let's optimize a bit that first sentence: Hi everybody,
- In createRecord(), the recNo to be chosen is either the first entry in deletedRecord() is there is one, or recCount + 1. Simple and efficient either, because you don't need to access the file.
Regards,
Phil.
And Phil, one more thing. In the createRecord(), I do not understand the purpose of deleted flag. If we have a deleted flag for each record to tell that the record is deleted, then there should be some purpose of keeping the record for future use right. I mean, I don't know, probably in the future we need to use that record and just need to invert the deleted flag. I mean is it essential or say in Sun terms "must" to delete the record physically though we have a method of specifying that the record is deleted(using deleted flag)?
Originally posted by Satish Avadhanam:
Can you please explain me what the term "mutex" refers to?
Regards, George
SCJP, SCJD, SCWCD, SCBCD
Originally posted by Satish Avadhanam:
In the 2) point, you said to check if new record will cause a duplicate
key exception, and if so throw the exception. Mine is URLYBird assignment.
I forgot you guys have the URLYBird (I had B&S Contractors and I would argue there that you can discern a primary key - namely, name and location), and from what I understand there really isn't a primary key, so the duplicate key exception is sort of moot. I agree with Phil that you can simply document that it's impossible for the DuplicateKeyException to be thrown and then ignore the issue.
The signature of the create method provided by Sun is like this
In the 3) point you were saying that "Get the first record in the database that has been deleted, and if you find one, mark the record as valid, and write the new record to this location". Each record in the database file starts with a deleted flag. So what you meant from above is to check if any of the deleted flag is 1(implies deleted), and if so, just insert the record that is passed to the create method there and return that recNo? What I was thinking was as there is deleted flag, we have to keep all the records in the database file like that and any records to be added should be appended at the bottom of the file. If we invoke the deleteRecord method, all we need to do is to set the deleted flag to 1. I think I'm missing something big time. Please correct me if I'm wrong.
I was simply trying to follow the method comment that says: "Creates a new record in the database (possibly reusing a deleted entry)." I took that to mean that we should check to see if the database file contained any records that had been deleted (marked for deletion) and use that space before deciding we had to append the new record at the end of the database file.
I think what you say about the delete method is correct: all we need to do is set the deleted flag to 1. From that time on a record so marked is considered deleted and is not used by any of the database methods with one exception. That exception is the create method which says it can "possibly reuse a deleted entry".
Regards, George
SCJP, SCJD, SCWCD, SCBCD
1) I don't think you need to be concerned with locking in the create method. You're only interested in deleted records which shouldn't be locked.
Originally posted by Don Wood:
Don't you still need some mechanism to prevent two threads running in the create method from trying to overwrite the same deleted record? The raf lock is only for seeking/reading/writing so it does not prevent this conflict. It seems that the other options are a database lock (sounds too heavyweight to me) or a lock on the deleted record to prevent "simultaneous" overwrites.
Perhaps I just don't understand enough about the assignments yet.
[ February 17, 2004: Message edited by: Don Wood ]
1) I don't think you need to be concerned with locking in the create method. You're only interested in deleted records which shouldn't be locked.
Regards, George
SCJP, SCJD, SCWCD, SCBCD
Originally posted by liqun chang:
Hi George,Philippe,and guys,This topic is very big.and i will open another
thread nx:All of URLy Bird 1.1.3 read/write lock and create() method
[ February 18, 2004: Message edited by: liqun chang ]
Originally posted by Don Wood:
Satish and George,
I understand what you are describing. It seems to me that the disadvantage is that it is effectively a database lock. All reading, updating and deleting will have to wait for the create to complete before they can continue.
Yes, we have to effectively lock the database, i.e. getting lock on "raf" in order to perfom the createRecord() operation. If we get a lock on "raf", all reading, updating and deleting have to wait. That's true.
Read, update and delete are concurrent operations. That is multiple threads can do these operations simultaneously on different threads as long as the records are different. It seems to me that create can be concurrent too. My thinking when I asked the question was that we could lock the deleted record.
The advantage to this approach is that concurrent operations with other records can proceed.
[ February 17, 2004: Message edited by: Don Wood ]
Modified the pseudocode because I realized that adding a record at the end of the file had to be done in a loop. Another thread may have added one so it is necessary to check after locking and retry if required.
[ February 18, 2004: Message edited by: Don Wood ]
Regards, George
SCJP, SCJD, SCWCD, SCBCD
He baked a muffin that stole my car! And this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|