wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes (URLyBird)How to mark a record to deleted one? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "(URLyBird)How to mark a record to deleted one?" Watch "(URLyBird)How to mark a record to deleted one?" New topic
Author

(URLyBird)How to mark a record to deleted one?

Zhixiong Pan
Ranch Hand

Joined: Jan 25, 2006
Posts: 239
Hi ranchers,
Thank you for caring my quesion.I really need your help.Now see my trouble first.
So far as I know, to delete a record we should not really remove it from disk, instead we just mark it. Comments in assignment say
Data section.
Repeat to end of file:
1 byte flag. 00 implies valid record, 0xFF implies deleted record

Can any kind of you give out advices in details.


SCJP 1.4 SCJD
Jason Moors
Ranch Hand

Joined: Dec 04, 2001
Posts: 188
Okay, you understand that the first byte of each record is used to determine if a record is marked as deleted.

You now need to think about what impact that flag will have on the create, delete, read, update, find and other methods in your Data class.

For example the documentation for the create method states that
Creates a record in the database (Possibly reusing a deleted entry)

So for this method you would need to loop through the records in the file until you find a record which is marked for deletion i.e has a value of 0xFF. Then all you have to do is update the information for the record.

You also want to think why a RecordNotFoundException might be thrown.

Jason.
Zhixiong Pan
Ranch Hand

Joined: Jan 25, 2006
Posts: 239
Hi Jason,
Thanks for your reply. If i want to delete a record, my RAF seek() to the top of every record and writeByte(255). Any comprehension errors here?
One more quesion here. When read(recNo) received a deleted record's recNO, then how to deal with?
Jason Moors
Ranch Hand

Joined: Dec 04, 2001
Posts: 188
If i want to delete a record, my RAF seek() to the top of every record and writeByte(255). Any comprehension errors here?


You've got it! You will also need to consider your locking mechanism when deleting records.

When read(recNo) received a deleted record's recNO, then how to deal with?


This is were the RecordNotFoundException should be thrown.

Jason
Long Ng
Greenhorn

Joined: Feb 17, 2006
Posts: 28
Hi,

There is still a confusion, the "recNo" value.

For example: at the first time, there're 3 records in the data file.
0x00 : row0
0x00 : row1
0x00 : row2

If the "read(2)" method were call it will return the row2 value.

Then the row0 was deleted, the data file is :
0xFF : row0
0x00 : row1
0x00 : row2

If the "read(2)" method were call, again, it will return the row2 value or RecordNotFoundException?

If the row2 was return, then the total rows is 3 (0-2) it's incorrect because the row0 was deleted.

If the RecordNotFoundException was return, then there has a confusion because the row2 is still there.

So is there any solution for this issue?
Thanks,
Long
Zhixiong Pan
Ranch Hand

Joined: Jan 25, 2006
Posts: 239
Hi Long,
In your case
If the "read(2)" method were call, again, it will return the row2 value or RecordNotFoundException?
row2 value will be returned.
If the row2 was return, then the total rows is 3 (0-2) it's incorrect because the row0 was deleted.
I think the total rows 3 means nothing, caues in your client you only display the records validable. That is to say, you should do some filtering in your Business layer.(Am I right?)
Long Ng
Greenhorn

Joined: Feb 17, 2006
Posts: 28
Hi,
I see, I will use the "fixed recNo number" solution for the simple.
For ex:
Start:
0x00 : row0
0x00 : row1
0x00 : row2

Delete recNo = 0:
0xFF : row0
0x00 : row1
0x00 : row2

Delete recNo = 0, one more time:
throws RecordNotFoundException.

Create a new row:
0x00 : row0
0x00 : row1
0x00 : row2
then return the recNo = 0 (reuse the deleted row).

Get the total (active) row = total_rows - deleted_rows.

Is it the good choice?
Thanks,
Long
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: (URLyBird)How to mark a record to deleted one?