*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes B&S: How do I get the 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 "B&S: How do I get the "new" recNo?" Watch "B&S: How do I get the "new" recNo?" New topic
Author

B&S: How do I get the "new" recNo?

Allan Smith
Greenhorn

Joined: Aug 18, 2005
Posts: 16
Hi,

Well I'm into Andrews book a lot, and have a solution where I map recNo's to the location in the file, in a HashMap. The recNo is just an incrementing long in a loop which is executed at startup, where I map it to the location in the file. So far so good and everything works fine.

However, in the interface provided by Sun, it is stated in the createRecord-method, that it should return the recordnumber of the new number. Now I've managed to write to the file, but how do I know which is my next record number? In RDBMS you would probably to something like "SELECT MAX(id)" or something like that, but here my recordnumbers are stored in a HashMap and there is no way to find the "last inserted" in a HashMap. Or is there?

In Andrews book, he already has the record number ready since it is something he creates as a part of a DVD(UPC-number!?) whereas here it is just an incremental long which I saved during startup.

Any help would be appreciated.

BR

Allan
Musab Al-Rawi
Ranch Hand

Joined: Aug 06, 2007
Posts: 231
hi,
in the create method where you decide where to store the record (I am guessing that you save the new record at the end without reusing deleted record) you get the writing position on the file before writing to the file using the RandomAccessFile's method length().
by doing simple calculations (length/(record length-header length)) you can get the record number.

hope that helps


SCBCD - SCWCD - SCJD - SCJP - OCA
Allan Smith
Greenhorn

Joined: Aug 18, 2005
Posts: 16
Hi Musab,

thanks for the input. Never really thought about that, that I could calculate the new number with the help of the length etc. You wrote:

"(length/(record length-header length))"

Was that only a quick assumption or a definitive solution? I know record length is my total record length(size of all fields together) and the length of the file is obvious, but what is the header length? Do you mean the size from the start of the file to where the actualy records begin?

BR

Allan
Musab Al-Rawi
Ranch Hand

Joined: Aug 06, 2007
Posts: 231
Yep.
Jonathan Wolter
Greenhorn

Joined: Nov 26, 2007
Posts: 12
I didn't get to calculating the formula yet, but you may also have to add one for the 'deletion' flag, depending if you keep that in your record length number.

As for the header, that would include all of the schema: magic number, number of fields, field names, data length per field, etc.
Allan Smith
Greenhorn

Joined: Aug 18, 2005
Posts: 16
Musab,

Thanks, I will try that.

Jonathan,

Good point. I have actually included it, but at first I didn't. So it's a good point for anyone else who might stumble into the same problem I did.

Thanks for the input guys!

BR

Allan
Musab Al-Rawi
Ranch Hand

Joined: Aug 06, 2007
Posts: 231
sorry correction for the formula :

( length - header ) / record length
sorry for the confusion
Allan Smith
Greenhorn

Joined: Aug 18, 2005
Posts: 16
Musab,

No problem. I'm still at work, so I can't really try it yet. Have to wait til I get home.

BR

Allan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: B&S: How do I get the "new" recNo?