Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Zhixiong Pan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jason Moors
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic