GeeCON Prague 2014*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Return flag in read method ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Return flag in read method ?" Watch "Return flag in read method ?" New topic
Author

Return flag in read method ?

Alan W Morgan
Greenhorn

Joined: Apr 18, 2005
Posts: 23
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

Joined: Feb 23, 2004
Posts: 81
I wouldn't return this with the String[]. If the flag specifies that the record is deleted/invalid, then just throw a RecordNotFoundException.


B.S. University of Wisconsin<br />SCJP 1.4 (85%)<br />SCJD 1.4 (92%) B&S Contractors
Alan W Morgan
Greenhorn

Joined: Apr 18, 2005
Posts: 23
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

Joined: Feb 23, 2004
Posts: 81
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

Joined: Apr 18, 2005
Posts: 23
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

Joined: Feb 23, 2004
Posts: 81
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:

 
Don't get me started about those stupid light bulbs.
 
subject: Return flag in read method ?