This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
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

Read record indicator flag

 
Maria Lepschy
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the contractor assignment and I started to read the db file.
I have two questions
1.I don't understand how to read the record indicator flag.
My instruction file contains:
"Repeat to end of file:
1 byte flag. 00 implies valid record, 0xFF implies deleted record ".
Because the 0xFF value extends the range of byte type, I think I have to use:
RandomAccessFile raf= new RandomAccessFile("db");
short recordIndicator=raf.readShort();

The problem is, that for the first line I got 2048 and for the others 8192.
This is not 00 and not 0xFF. I must cast the value to byte:
byte recordInd=(byte)recordIndicator ??
2. Where I have to use the magic cookie value?
Thanks,
Maria
 
Ken Krebs
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
0xFF is a valid byte so can use readByte to get it. Java interprets a byte (0x00 to 0xFF) as a 2's complement signed integer in the range -128 through +127. 0x00 through 0x7F is interpreted as 0 through 127. 0x80 through 0xFF is interpreted as -128 through -1.
If you use readShort, you will read the next 2 bytes, so this is an incorrect approach.
[ February 08, 2004: Message edited by: Ken Krebs ]
 
Maria Lepschy
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks, Ken. I have tried again and now I got for all record the
value -1. What can I do now? This means that all the lines are deleted.
I have to create new lines reusing the deleted lines?
 
Maria Lepschy
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A guestion again. How can I reuse a deleted line?
Read it and write the record indicator with the valid value(0)? But I read the record only if they are valid. I am confused now. I hope you know an answer. Maybe I should read it using the create method? But how I know if I want to reuse a deleted record or to create a new record? My create method has the following signature: int create(String[] data).
Regards,
Maria
 
George Marinkovich
Ranch Hand
Posts: 619
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Maria,
My recommendation is not to try to fix your current database file. I would make another copy of your original database file (you did make a copy of the original file you downloaded from Sun, didn't you) and start working from that. If by some chance you did destroy your original database file, I think you may be able to download the assignment again. It is very important to maintain a copy of your original database file, not only because your application may corrupt the file while you are testing, but also because you must submit an (unaltered) copy of your original database file with your project submission.
Concentrate on reading the file in (the read method) before moving on to the implementation of the other database methods. I would focus only on reading the file in at first to get that part working before moving on to writing the file. Take it one step at a time, it's easier that way in my opinion, and you get to see some progress at an earlier time.
Hope this helps,
George
[ February 08, 2004: Message edited by: George Marinkovich ]
 
Maria Lepschy
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi George,
I have not understand your answer.
I have already read all records and displayed in a JTable. My problem is that I have read all records witch are deleted, I have not validate the records again the record indicator flag.
If I do this-if I read the records with a valid record indicator- then my table will be empty.I think, I must first create valid records an then do the read.
Regards,
Maria
 
George Marinkovich
Ranch Hand
Posts: 619
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Maria,
Well, I'm confused now too.
Originally posted by Maria Lepschy:

I have already read all records and displayed in a JTable. My problem is that I have read all records witch are deleted, I have not validate the records again the record indicator flag.
If I do this-if I read the records with a valid record indicator- then my table will be empty.I think, I must first create valid records an then do the read.

I recommend you start with a new copy of your original database file. To my knowledge this file contains only valid (that is, non-deleted records). So were you to read the database file, I think you would find that the validity flag for each of the records should have the value 0x00, indicating that the record is valid.
It seems to me that your read method should first determine whether the record in question is valid or not. That is, it should read the validity flag and if it's 0x00 then read the record. If the validity flag is 0xFF then you should throw the RecordNotFoundException because the record in question has been marked for deletion.
If still believe that all the record in your database file are marked for deletion, then I would suggest that you may not be reading the validity flag correctly. Check the database file layout section of the assignment instructions again and make sure that you are reading each element specified in the correct way: the proper number of bytes and the in the proper order. If you had a one-off error in your reading you could very well be seeing 0xFF as the validity flag of every database record. It's just not reasonable (possible yes, reasonable no) that all the records in the Sun-supplied database file would be marked for deletion.
Hope this helps,
George
 
Maria Lepschy
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi George,
Thank you for your advise. I am now again at home after a full work day and I was very happy to see your answer.
I will check now if I have read the validity flag correctly. It is seems to be correctly because in the table each column is properly filled- but maybe there are some mistakes.
Regards,
Maria.
 
Maria Lepschy
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
George, I have found out my mistake- now I can read correctly.
Many thanks,
Maria
 
George Marinkovich
Ranch Hand
Posts: 619
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maria,
Congratulations on finding your problem.
George
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic