Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Return flag in read method ?

 
Alan W Morgan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

My read methods signature is as follows :
public String[] read(int recNo) throws RecordNotFoundException

The first byte of my record is a flag to say deleted or not.
Orinigally I wasn't returning this in the String[] but not I'm thinking that it does make sense.
However when I try to convert it to a String I get a period - '.'
Spec says 00 for valid and OxFF for deleted.
How have the rest of ye dealt with this ?

Thanks,
Alan.
 
Jared Chapman
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't return this with the String[]. If the flag specifies that the record is deleted/invalid, then just throw a RecordNotFoundException.
 
Alan W Morgan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jared Chapman:
I wouldn't return this with the String[]. If the flag specifies that the record is deleted/invalid, then just throw a RecordNotFoundException.


I was thinking the same way myself but then I looked at method signature
and comment on supplied DB file

// Reads a record from the file. Returns an array where each
// element is a record value.
public String[] read(int recNo) throws RecordNotFoundException;

It doesn't preclude returning invlaid records.
And then the question is how do I delete a record.
If read return the flag in the array then I simply have delete do a read and then update returned string array to change flag and call update passing string array.

If not then I have to go SIZEOFRECORDINBYTES * recno - 1 into byte array and change the relevant byte.

Just seemed cleaner to return the flag but I'm prepared to be talked out of it


Do you do delete similar to way set out above ?
 
Jared Chapman
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Alan,

The first think you should do in the read method is to check the flag. So to do this, you should navigate to (offset * recNo) in your data file. Then, you read the flag. If the record is marked as valid, then return the fields (i.e. name, location...etc) in a String[]. If the record is marked as invalid, then throw a RecordNotFoundException. At this point, the file pointer will be at the beginning of the first field (name). If the record is valid, read the fields of the record, populate your string array, and return it.

As far as deleting a record goes, you simply want to navigate to (offset * recNo), which will put you at the flag, and write OxFF. So according to my specs, you don't use read or update at all to delete a record, but rather use a delete(recNo) method.

Hope this helped.
 
Alan W Morgan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jared Chapman:
Hey Alan,

The first think you should do in the read method is to check the flag. So to do this, you should navigate to (offset * recNo) in your data file. Then, you read the flag. If the record is marked as valid, then return the fields (i.e. name, location...etc) in a String[]. If the record is marked as invalid, then throw a RecordNotFoundException. At this point, the file pointer will be at the beginning of the first field (name). If the record is valid, read the fields of the record, populate your string array, and return it.

As far as deleting a record goes, you simply want to navigate to (offset * recNo), which will put you at the flag, and write OxFF. So according to my specs, you don't use read or update at all to delete a record, but rather use a delete(recNo) method.

Hope this helped.


Hey Jared,

Your explanation is exactly the way I was thinking initially but for some reason I got to thinking that there was a better way.

Lastly 00 and OxFF - whats the best way to deal with these bytes ?...as ints ?
 
Jared Chapman
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I declared 2 private variables:

private final int VALID_FLAG = 00;
private final int INVALID_FLAG = 0x8000;

Then, when I'm reading a record, I do the following check:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic